Search code examples
unicodearduinoblocksymbols

Usage of `? :` in Arduino's QR code generator


I'm trying to create a QR code generator using Arduino. but there is a line I cannot understand. any one can help me.My code is down below.

#include "qrcode.h"

void setup() {
Serial.begin(115200);

// Start time
uint32_t dt = millis();
const char* number = "NUMBER";
// Create the QR code
QRCode qrcode;
uint8_t qrcodeData[qrcode_getBufferSize(3)];
qrcode_initText(&qrcode, qrcodeData, 3, 3, number);

// Delta time
dt = millis() - dt;
Serial.print("QR Code Generation Time: ");
Serial.print(dt);
Serial.print("\n");

// Top quiet zone
Serial.print("\n\n\n\n");

for (uint8_t y = 0; y < qrcode.size; y++) {

    // Left quiet zone
    Serial.print("        ");

    // Each horizontal module
    for (uint8_t x = 0; x < qrcode.size; x++) {

        // Print each module (UTF-8 \u2588 is a solid block)
        Serial.print(qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": "  ");

    }

    Serial.print("\n");
}

// Bottom quiet zone
Serial.print("\n\n\n\n");
   }

    void loop() {

   }

I cant understand Serial.print(qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": " "); this line. what is the meaning of this part. "\u2588\u2588": " ". I know \u2588 is a block symbol is in unicode. but what is the use of two double commas after the block symbols???


Solution

  • I assume you don't understand the ternary operator ?:.

    The construction qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": " " means:

    • if qrcode_getModule(&qrcode, x, y) can evaluate to the boolean "true" (here a non-null byte) then use the part before the colon : "\u2588\u2588" (██) (plain block)
    • else (qrcode_getModule(&qrcode, x, y) == 0) then use the part after the colon : " " (empty block)