#include "wifi_qr.h" #include "vendor/qrcodegen.h" #include #include // Nayuki QR Code generator v1.8.0, vendored unchanged under its MIT license. // https://github.com/nayuki/QR-Code-generator/tree/v1.8.0/c #define MAX_VERSION 5 static bool append_field(char **dst, const char *end, const char *text) { for (; *text; text++) { // ZXing/iOS Wi-Fi join syntax escapes special field characters. if (strchr("\\;,:\"", *text)) { if (*dst == end) return false; *(*dst)++ = '\\'; } if (*dst == end) return false; *(*dst)++ = *text; } return true; } bool wifi_qr_bitmap(const char *ssid, const char *password, uint8_t out[WIFI_QR_BYTES]) { char payload[96] = "WIFI:T:WPA;S:"; char *dst = payload + strlen(payload); const char *end = payload + sizeof(payload) - 1; if (!append_field(&dst, end, ssid) || end - dst < 3) return false; memcpy(dst, ";P:", 3); dst += 3; if (!append_field(&dst, end, password) || end - dst < 2) return false; *dst++ = ';'; *dst++ = ';'; *dst = 0; uint8_t work[qrcodegen_BUFFER_LEN_FOR_VERSION(MAX_VERSION)]; uint8_t qr[qrcodegen_BUFFER_LEN_FOR_VERSION(MAX_VERSION)]; if (!qrcodegen_encodeText(payload, work, qr, qrcodegen_Ecc_LOW, 1, MAX_VERSION, qrcodegen_Mask_AUTO, true)) return false; const int size = qrcodegen_getSize(qr); const int scale = WIFI_QR_SIDE / (size + 8); if (scale < 1) return false; const int origin = (WIFI_QR_SIDE - size * scale) / 2; // OLED lit pixels are the light modules. Do not invert the QR or truncate // its quiet zone just to fit a 2x scale on this 64-pixel-high panel. memset(out, 0xff, WIFI_QR_BYTES); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (!qrcodegen_getModule(qr, x, y)) continue; for (int dy = 0; dy < scale; dy++) { int py = origin + y * scale + dy; for (int dx = 0; dx < scale; dx++) { int px = origin + x * scale + dx; out[(py / 8) * WIFI_QR_SIDE + px] &= (uint8_t)~(1u << (py % 8)); } } } } return true; }