From 7b63256835064e7705a20445c695f9271a76f9d6 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 29 Dec 2023 02:36:18 +0700 Subject: [PATCH] display prototype --- .../lib/ESPMegaPRO/ESPMegaDisplay.cpp | 56 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaDisplay.hpp | 47 ++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Template Project/lib/ESPMegaPRO/ESPMegaDisplay.cpp create mode 100644 Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.cpp new file mode 100644 index 0000000..b3e35a5 --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.cpp @@ -0,0 +1,56 @@ +#include + +void ESPMegaDisplay::recieveSerialCommand(){ + // Read the serial buffer if available + while(displayAdapter->available()) { + rx_buffer[rx_buffer_index] = displayAdapter->read(); + rx_buffer_index++; + // Check for overflow + if(rx_buffer_index>=256){ + rx_buffer_index = 0; + } + this-> processSerialCommand(); + } + +} + +void ESPMegaDisplay::processSerialCommand(){ + // Check if the rx buffer ended with stop bytes (0xFF 0xFF 0xFF) + if(rx_buffer_index<3) return; + if(rx_buffer[rx_buffer_index-1]!=0xFF) return; + if(rx_buffer[rx_buffer_index-2]!=0xFF) return; + if(rx_buffer[rx_buffer_index-3]!=0xFF) return; + // The rx buffer ended with stop bytes + // Check if the rx buffer is a push payload + // The payload type is the first byte of the payload + // 0x00 is invalid instruction + // 0x01 is success + // 0x65 is a touch event + // 0x66 is a page report event + if(rx_buffer[0]==0x65){ + processTouchPayload(); + } + else if(rx_buffer[0]==0x66){ + processPageReportPayload(); + } +} + +void ESPMegaDisplay::processTouchPayload() { + if (rx_buffer_index != 4) return; + // The second byte of the payload is the page number + uint8_t page = rx_buffer[1]; + // The third byte of the payload is the component id + uint8_t component = rx_buffer[2]; + // The fourth byte of the payload is the event + uint8_t event = rx_buffer[3]; + // 0x01 is press, 0x00 is release +} + +void ESPMegaDisplay::processPageReportPayload() { + // The second byte of the payload is the page number + this->currentPage = rx_buffer[1]; + if(pageChangeCallback!=NULL){ + pageChangeCallback(this->currentPage); + } + rx_buffer_index = 0; +} \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp new file mode 100644 index 0000000..760219c --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp @@ -0,0 +1,47 @@ +#include +#include +class ESPMegaDisplay +{ + public: + ESPMegaDisplay(HardwareSerial *displayAdapter, ESPMegaPRO *espmega); + void begin(); + void loop(); + void reset(); + void setBrightness(int value); + void jumpToPage(int page); + void setString(char* component, char* value); + void setNumber(char* component, int value); + char* getString(char* component); + int getNumber(char* component); + void bindInputPage(uint8_t card_id); + void bindOutputPage(uint8_t card_id); + void handlePwmStateChange(uint8_t pin, uint16_t value); + void handleInputStateChange(uint8_t pin, bool state); + void registerPushCallback(std::function callback); + void registerPopCallback(std::function callback); + void registerPageChangeCallback(std::function callback); + void unregisterPushCallback(); + void unregisterPopCallback(); + void unregisterPageChangeCallback(); + private: + uint8_t inputCardId; + uint8_t outputCardId; + uint8_t currentPage; + uint8_t rx_buffer_index; + char rx_buffer[256]; + char tx_buffer[256]; + void recieveSerialCommand(); + void processSerialCommand(); + void processTouchPayload(); + void processPageReportPayload(); + void sendStopBytes(); + void sendCommand(char* command); + void refreshPage(); + void refreshInputPage(); + void refreshOutputPage(); + HardwareSerial *displayAdapter; + ESPMegaPRO *espmega; + std::function pushCallback; + std::function popCallback; + std::function pageChangeCallback; +}; \ No newline at end of file