Skip to content

some suggestions #11

@ben-mkiv

Description

@ben-mkiv

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

  • added buffering of QR String to compare and skip encoding when unnecessary
  • added direct calls to display->setPixelColor, instead of having an extra method for that
  • removed display->display() call to avoid rendering twice
  • removed the white background and replaced it by a 2px wide border around the QR

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions