display prototype

This commit is contained in:
Siwat Sirichai 2023-12-29 02:36:18 +07:00
parent f63f498b42
commit 7b63256835
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,56 @@
#include <ESPMegaDisplay.hpp>
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;
}

View File

@ -0,0 +1,47 @@
#include <Arduino.h>
#include <ESPMegaPRO_OOP.hpp>
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<void(uint8_t, uint8_t)> callback);
void registerPopCallback(std::function<void(uint8_t, uint8_t)> callback);
void registerPageChangeCallback(std::function<void(uint8_t)> 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<void(uint8_t, uint8_t)> pushCallback;
std::function<void(uint8_t, uint8_t)> popCallback;
std::function<void(uint8_t)> pageChangeCallback;
};