anunpanya / ESP8266_QRcode

102 stars 53 forks source link

some suggestions #11

Open ben-mkiv opened 4 years ago

ben-mkiv commented 4 years ago

first of all, thanks for sharing your lib :)

I've noticed that encoding takes ~30ms on my ESP32 which lead to low FPS when using QRs with the SSD1306-lib UI methods

the QR isn't centered in my code as i want to display some text next to it, just wanted to share some ideas so you can take what you like of it.

my QRCode.h

#include "OLEDDisplay.h"

class QRcode
{
    private:
        String QRBuffer = "";

    public:
        void create(String message);
        void render(OLEDDisplay *display, int x, int y);

        void render(OLEDDisplay *display, String message, int x, int y){
            create(message);
            render(display, x, y);
        }
};

my QRCode.cpp

#include <Arduino.h>
#include "qrcode.h"
#include "qrencode.h"

void QRcode::create(String message) {
    // encoding takes ~30ms so we rather buffer the message on encoding and return if nothing changed
    if(message.equals(QRBuffer))
        return;

    QRBuffer = message;
    message.toCharArray((char *) strinbuf, 260);
    qrencode();
}

void QRcode::render(OLEDDisplay *display, int x, int y) {

    int offsetX = x + (display->height() - WD) / 2;
    int offsetY = y + (display->height() - WD) / 2;

    display->fillRect(offsetX - 2, offsetY - 2, WD + 4, WD + 4);

    // print QR Code
    for (byte x = 0; x < WD; x += 2) {
        for (byte y = 0; y < WD; y++) {
            if (QRBIT(x, y) && QRBIT((x + 1), y)) {
                // black square on top of black square
                display->setPixelColor(offsetX + x, offsetY + y, BLACK);
                display->setPixelColor(offsetX + (x + 1), offsetY + y, BLACK);
            }
            if (!QRBIT(x, y) && QRBIT((x + 1), y)) {
                // white square on top of black square
                display->setPixelColor(offsetX + x, offsetY + y, WHITE);
                display->setPixelColor(offsetX + (x + 1), offsetY + y, BLACK);
            }
            if (QRBIT(x, y) && !QRBIT((x + 1), y)) {
                // black square on top of white square
                display->setPixelColor(offsetX + x, offsetY + y, BLACK);
                display->setPixelColor(offsetX + (x + 1), offsetY + y, WHITE);
            }
            if (!QRBIT(x, y) && !QRBIT((x + 1), y)) {
                // white square on top of white square
                display->setPixelColor(offsetX + x, offsetY + y, WHITE);
                display->setPixelColor(offsetX + (x + 1), offsetY + y, WHITE);
            }
        }
    }

    //display->display(); //disabled that call within the class as it would double render frames with the UI
}