/**
* QRCode
*
* A quick example of generating a QR code.
*
* This prints the QR code to the serial monitor as solid blocks. Each module
* is two characters wide, since the monospace font used in the serial monitor
* is approximately twice as tall as wide.
*
*/
#include <stdio.h>
#include "qrcode.h" // https://github.com/ricmoo/QRCode
QRCode qrcode;
uint8_t bitmap[178] = {0};
//uint8_t bitmapBase64[240] = {0};
uint8_t qrcodeData[106];
const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String base64_encode(const uint8_t* data, size_t length) {
String encoded;
for (size_t i = 0; i < length; i += 3) {
uint32_t triplet = (data[i] << 16) | (i + 1 < length ? data[i + 1] << 8 : 0) | (i + 2 < length ? data[i + 2] : 0);
encoded += base64_chars[(triplet >> 18) & 0x3F];
encoded += base64_chars[(triplet >> 12) & 0x3F];
encoded += i + 1 < length ? base64_chars[(triplet >> 6) & 0x3F] : '=';
encoded += i + 2 < length ? base64_chars[triplet & 0x3F] : '=';
}
return encoded;
}
void pack29BitsTo4Bytes(const uint8_t input, uint8_t *output, const uint8_t position) {
if(input == 1) {
*output |= (1 << (7-position));
}
}
//void createBitmap(const uint8_t* data, int width, int height, const char* filename) {
void createBitmap(void) {
uint32_t width, height = qrcode.size;
// Header
bitmap[0] = 0x42; // File Type (BM)
bitmap[1] = 0x4D; // File Type (BM)
bitmap[2] = 178; // File size
bitmap[3] = 0; // File size
bitmap[4] = 0; // File size
bitmap[5] = 0; // File size
bitmap[6] = 0; // Reserved
bitmap[7] = 0; // Reserved
bitmap[8] = 0; // Reserved
bitmap[9] = 0; // Reserved
bitmap[10] = 62; // Data image offset
bitmap[11] = 0; // Data image offset
bitmap[12] = 0; // Data image offset
bitmap[13] = 0; // Data image offset
bitmap[14] = 40; // Header size
bitmap[15] = 0; // Header size
bitmap[16] = 0; // Header size
bitmap[17] = 0; // Header size
bitmap[18] = 29; // Width
bitmap[19] = 0; // Width
bitmap[20] = 0; // Width
bitmap[21] = 0; // Width
bitmap[22] = 29; // Height
bitmap[23] = 0; // Height
bitmap[24] = 0; // Height
bitmap[25] = 0; // Height
bitmap[26] = 1; // Numbers of color planes
bitmap[27] = 0; // Numbers of color planes
bitmap[28] = 1; // Bits per color
bitmap[29] = 0; // Bits per color
bitmap[30] = 0; // Compression
bitmap[31] = 0; // Compression
bitmap[32] = 0; // Compression
bitmap[33] = 0; // Compression
bitmap[34] = 116; // Image size
bitmap[35] = 0; // Image size
bitmap[36] = 0; // Image size
bitmap[37] = 0; // Image size
bitmap[38] = 0x13;// Horizontal resolution
bitmap[39] = 0x0B;// Horizontal resolution
bitmap[40] = 0; // Horizontal resolution
bitmap[41] = 0; // Horizontal resolution
bitmap[42] = 0x13;// Vertical resolution
bitmap[43] = 0x0B;// Vertical resolution
bitmap[44] = 0; // Vertical resolution
bitmap[45] = 0; // Vertical resolution
bitmap[46] = 2; // Number of colors
bitmap[47] = 0; // Number of colors
bitmap[48] = 0; // Number of colors
bitmap[49] = 0; // Number of colors
bitmap[50] = 2; // Number of important colors
bitmap[51] = 0; // Number of important colors
bitmap[52] = 0; // Number of important colors
bitmap[53] = 0; // Number of important colors
bitmap[54] = 0; // Black
bitmap[55] = 0; // Black
bitmap[56] = 0; // Black
bitmap[57] = 0; // Black
bitmap[58] = 0xff;// White
bitmap[59] = 0xff;// White
bitmap[60] = 0xff;// White
bitmap[61] = 0x00;// White
// Image data
for (uint8_t y = 0; y < qrcode.size; y++) {
// Each horizontal module
for (uint8_t x = 0; x < 4; x++) {
for(uint8_t z = 0; z < 8; z++)
{
pack29BitsTo4Bytes(qrcode_getModule(&qrcode, z + x*8, y), &bitmap[62 + (qrcode.size-1)*4 + x - y*4], z);
}
}
}
for (int i = 0; i < sizeof(bitmap) / sizeof(bitmap[0]); i++) {
if (bitmap[i] < 0x10) {
Serial.print("0");
}
Serial.print(bitmap[i], HEX);
}
Serial.println();
}
void printQRCode(void)
{
// Top quiet zone
Serial.println();
Serial.println();
Serial.println();
Serial.println();
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.println();
}
// Bottom quiet zone
Serial.println();
Serial.println();
Serial.println();
Serial.println();
}
void generateQRCode(const char* QRCodeString)
{
// Create the QR code
//uint8_t qrcodeData[qrcode_getBufferSize(3)];
qrcode_initText(&qrcode, qrcodeData, 3, 0, QRCodeString);
}
void convertToBase64(void) {
size_t bitmapSize = sizeof(bitmap);
String bitmapBase64 = base64_encode(bitmap, bitmapSize);
Serial.println(bitmapBase64);
}
void setup() {
Serial.begin(115200);
generateQRCode("192.168.123.125");
printQRCode();
createBitmap();
convertToBase64();
}
void loop() {
delay(100);
}