ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.cpp

56 lines
1.7 KiB
C++

#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;
}