payload processing

This commit is contained in:
Siwat Sirichai 2023-12-29 11:55:59 +07:00
parent 7b63256835
commit 009ac8d961
2 changed files with 91 additions and 8 deletions

View File

@ -1,6 +1,7 @@
#include <ESPMegaDisplay.hpp>
void ESPMegaDisplay::recieveSerialCommand(){
bool ESPMegaDisplay::recieveSerialCommand(bool process){
bool dataRecieved = false;
// Read the serial buffer if available
while(displayAdapter->available()) {
rx_buffer[rx_buffer_index] = displayAdapter->read();
@ -9,17 +10,19 @@ void ESPMegaDisplay::recieveSerialCommand(){
if(rx_buffer_index>=256){
rx_buffer_index = 0;
}
this-> processSerialCommand();
if(process) this-> processSerialCommand();
dataRecieved = true;
}
return dataRecieved;
}
bool ESPMegaDisplay::recieveSerialCommand(){
return recieveSerialCommand(true);
}
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;
if(!payloadIsValid()) 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
@ -53,4 +56,78 @@ void ESPMegaDisplay::processPageReportPayload() {
pageChangeCallback(this->currentPage);
}
rx_buffer_index = 0;
}
void ESPMegaDisplay::sendStopBytes() {
displayAdapter->write(0xFF);
displayAdapter->write(0xFF);
displayAdapter->write(0xFF);
}
void ESPMegaDisplay::sendCommand(char* command) {
displayAdapter->print(command);
sendStopBytes();
}
void ESPMegaDisplay::refreshPage() {
if(this->currentPage==0){
refreshInputPage();
}
else if(this->currentPage==1){
refreshOutputPage();
}
}
void ESPMegaDisplay::jumpToPage(int page) {
this->displayAdapter->print("page ");
this->displayAdapter->print(page);
sendStopBytes();
}
void ESPMegaDisplay::setNumber(char* component, int value) {
this->displayAdapter->print(component);
this->displayAdapter->print("=");
this->displayAdapter->print(value);
sendStopBytes();
}
void ESPMegaDisplay::setString(char* component, char* value) {
this->displayAdapter->print(component);
this->displayAdapter->print("=\"");
this->displayAdapter->print(value);
this->displayAdapter->print("\"");
sendStopBytes();
}
int ESPMegaDisplay::getNumber(char* component) {
uint32_t start = millis();
// Send the get command
this->displayAdapter->print("get ");
this->displayAdapter->print(component);
sendStopBytes();
// Wait for the response
if(!waitForValidPayload(FETCH_TIMEOUT)) return 0;
// The rx buffer is valid
}
bool ESPMegaDisplay::waitForValidPayload(uint32_t timeout) {
uint32_t start = millis();
// If the payload is not valid, keep reading the serial buffer until timeout
while(!this->payloadIsValid()){
if(millis()-start>timeout){
return false;
}
recieveSerialCommand(false);
}
return true;
}
bool ESPMegaDisplay::payloadIsValid() {
// Check if the rx buffer ended with stop bytes (0xFF 0xFF 0xFF)
if(rx_buffer_index<3) return false;
if(rx_buffer[rx_buffer_index-1]!=0xFF) return false;
if(rx_buffer[rx_buffer_index-2]!=0xFF) return false;
if(rx_buffer[rx_buffer_index-3]!=0xFF) return false;
return true;
}

View File

@ -1,5 +1,8 @@
#include <Arduino.h>
#include <ESPMegaPRO_OOP.hpp>
#define FETCH_TIMEOUT 100
class ESPMegaDisplay
{
public:
@ -30,7 +33,8 @@ class ESPMegaDisplay
uint8_t rx_buffer_index;
char rx_buffer[256];
char tx_buffer[256];
void recieveSerialCommand();
bool recieveSerialCommand();
bool recieveSerialCommand(bool process);
void processSerialCommand();
void processTouchPayload();
void processPageReportPayload();
@ -39,6 +43,8 @@ class ESPMegaDisplay
void refreshPage();
void refreshInputPage();
void refreshOutputPage();
bool payloadIsValid();
bool waitForValidPayload(uint32_t timeout);
HardwareSerial *displayAdapter;
ESPMegaPRO *espmega;
std::function<void(uint8_t, uint8_t)> pushCallback;