Created
December 2, 2024 12:36
-
-
Save hieptuanle/3c0189d5d06156a3900b2af98033e6d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "device_qr.h" | |
| #include "qrcode.h" | |
| #include "screen.h" | |
| #define TOP_MARGIN 10 | |
| DeviceQR::DeviceQR() | |
| { | |
| } | |
| DeviceQR::~DeviceQR() | |
| { | |
| freeMemory(); | |
| } | |
| void DeviceQR::setup() | |
| { | |
| int bufferSize = qrcode_getBufferSize(QR_VERSION); | |
| Serial.print("Buffer size: "); | |
| Serial.println(bufferSize); | |
| qrcodeBytes = new uint8_t[bufferSize]; | |
| qrcode = new QRCode(); | |
| } | |
| void DeviceQR::freeMemory() | |
| { | |
| delete[] qrcodeBytes; | |
| delete qrcode; | |
| } | |
| void DeviceQR::displayQR(BoardScreen *screen, char *content) | |
| { | |
| qrcode_initText(qrcode, qrcodeBytes, QR_VERSION, ECC_LOW, content); | |
| uint8_t offsetX = (screen->width() - qrcode->size * QR_SCALE) / 2; | |
| uint8_t offsetY = (screen->height() - TOP_MARGIN - qrcode->size * QR_SCALE) / 2 + TOP_MARGIN; | |
| for (uint8_t y = 0; y < qrcode->size; y++) | |
| { | |
| for (uint8_t x = 0; x < qrcode->size; x++) | |
| { | |
| if (qrcode_getModule(qrcode, x, y)) | |
| { | |
| screen->fillRect(x * QR_SCALE + offsetX, y * QR_SCALE + offsetY, QR_SCALE, QR_SCALE, BLACK); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef DEVICE_QR_H | |
| #define DEVICE_QR_H | |
| #include <Arduino.h> | |
| #include "qrcode.h" | |
| #include "screen.h" | |
| #define QR_VERSION 6 | |
| #define QR_SCALE 3 | |
| class DeviceQR | |
| { | |
| public: | |
| DeviceQR(); | |
| ~DeviceQR(); | |
| void setup(); | |
| void freeMemory(); | |
| void displayQR(BoardScreen *screen, char *content); | |
| private: | |
| QRCode *qrcode; | |
| uint8_t *qrcodeBytes; | |
| }; | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screen.h" | |
| #include "MCUFRIEND_kbv.h" | |
| #define ORIENTATION 2 // PORTRAIT | |
| uint16_t logY = 0; | |
| BoardScreen::BoardScreen() : tft(), currentBackgroundColor(BEIGE) | |
| { | |
| screenActive = true; | |
| } | |
| void BoardScreen::setupScreen() | |
| { | |
| tft.reset(); | |
| uint16_t ID = tft.readID(); | |
| tft.begin(ID); | |
| tft.setRotation(ORIENTATION); | |
| } | |
| void BoardScreen::fillScreen(uint16_t color) | |
| { | |
| currentBackgroundColor = color; | |
| tft.fillScreen(color); | |
| } | |
| void BoardScreen::drawText(int x, int y, String msg, uint16_t color) | |
| { | |
| tft.setCursor(x, y); | |
| tft.setTextColor(color, currentBackgroundColor); | |
| tft.println(msg); | |
| } | |
| void BoardScreen::log(String text) | |
| { | |
| drawText(0, logY, text, BLACK); | |
| // logY += 10; | |
| // if (logY > height() - 10) | |
| // { | |
| // logY = 0; | |
| // } | |
| } | |
| void BoardScreen::drawTextCentered(String text, uint16_t y, uint16_t color) | |
| { | |
| int16_t x1, y1; | |
| uint16_t w, h; | |
| tft.getTextBounds(text, 0, y, &x1, &y1, &w, &h); | |
| uint16_t x = (tft.width() - w) / 2; | |
| drawText(x, y, text, color); | |
| } | |
| int16_t BoardScreen::height() | |
| { | |
| return tft.height(); | |
| } | |
| int16_t BoardScreen::width() | |
| { | |
| return tft.width(); | |
| } | |
| void BoardScreen::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) | |
| { | |
| tft.fillRect(x, y, w, h, color); | |
| } | |
| void BoardScreen::setScreenState(boolean active) | |
| { | |
| // tft.setDisplay(active); | |
| // tft.setBacklight(active); | |
| screenActive = active; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef screen_h | |
| #define screen_h | |
| #include <Arduino.h> | |
| #include "MCUFRIEND_kbv.h" | |
| // Assign human-readable names to some common 16-bit color values: | |
| #define BLACK 0x0000 | |
| #define BLUE 0x001F | |
| #define RED 0xF800 | |
| #define GREEN 0x07E0 | |
| #define CYAN 0x07FF | |
| #define MAGENTA 0xF81F | |
| #define YELLOW 0xFFE0 | |
| #define WHITE 0xFFFF | |
| #define BEIGE 0xFFD7 // Warmer beige with more yellow tone | |
| #define LIGHTGREEN 0x97EF | |
| #define ORANGE 0xFD20 | |
| #define DARKRED 0x8400 | |
| class BoardScreen | |
| { | |
| public: | |
| BoardScreen(); | |
| void drawText(int x, int y, String msg, uint16_t color); | |
| void drawTextCentered(String text, uint16_t y, uint16_t color); | |
| void log(String text); | |
| void setupScreen(); | |
| void fillScreen(uint16_t color); | |
| void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); | |
| void setScreenState(boolean active); | |
| int16_t height(); | |
| int16_t width(); | |
| boolean screenActive; | |
| MCUFRIEND_kbv tft; | |
| private: | |
| uint16_t currentBackgroundColor; | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment