payload processing
This commit is contained in:
parent
7b63256835
commit
009ac8d961
|
@ -1,6 +1,7 @@
|
||||||
#include <ESPMegaDisplay.hpp>
|
#include <ESPMegaDisplay.hpp>
|
||||||
|
|
||||||
void ESPMegaDisplay::recieveSerialCommand(){
|
bool ESPMegaDisplay::recieveSerialCommand(bool process){
|
||||||
|
bool dataRecieved = false;
|
||||||
// Read the serial buffer if available
|
// Read the serial buffer if available
|
||||||
while(displayAdapter->available()) {
|
while(displayAdapter->available()) {
|
||||||
rx_buffer[rx_buffer_index] = displayAdapter->read();
|
rx_buffer[rx_buffer_index] = displayAdapter->read();
|
||||||
|
@ -9,17 +10,19 @@ void ESPMegaDisplay::recieveSerialCommand(){
|
||||||
if(rx_buffer_index>=256){
|
if(rx_buffer_index>=256){
|
||||||
rx_buffer_index = 0;
|
rx_buffer_index = 0;
|
||||||
}
|
}
|
||||||
this-> processSerialCommand();
|
if(process) this-> processSerialCommand();
|
||||||
|
dataRecieved = true;
|
||||||
}
|
}
|
||||||
|
return dataRecieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ESPMegaDisplay::recieveSerialCommand(){
|
||||||
|
return recieveSerialCommand(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPMegaDisplay::processSerialCommand(){
|
void ESPMegaDisplay::processSerialCommand(){
|
||||||
// Check if the rx buffer ended with stop bytes (0xFF 0xFF 0xFF)
|
// Check if the rx buffer ended with stop bytes (0xFF 0xFF 0xFF)
|
||||||
if(rx_buffer_index<3) return;
|
if(!payloadIsValid()) 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
|
// The rx buffer ended with stop bytes
|
||||||
// Check if the rx buffer is a push payload
|
// Check if the rx buffer is a push payload
|
||||||
// The payload type is the first byte of the payload
|
// The payload type is the first byte of the payload
|
||||||
|
@ -53,4 +56,78 @@ void ESPMegaDisplay::processPageReportPayload() {
|
||||||
pageChangeCallback(this->currentPage);
|
pageChangeCallback(this->currentPage);
|
||||||
}
|
}
|
||||||
rx_buffer_index = 0;
|
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;
|
||||||
}
|
}
|
|
@ -1,5 +1,8 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ESPMegaPRO_OOP.hpp>
|
#include <ESPMegaPRO_OOP.hpp>
|
||||||
|
|
||||||
|
#define FETCH_TIMEOUT 100
|
||||||
|
|
||||||
class ESPMegaDisplay
|
class ESPMegaDisplay
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -30,7 +33,8 @@ class ESPMegaDisplay
|
||||||
uint8_t rx_buffer_index;
|
uint8_t rx_buffer_index;
|
||||||
char rx_buffer[256];
|
char rx_buffer[256];
|
||||||
char tx_buffer[256];
|
char tx_buffer[256];
|
||||||
void recieveSerialCommand();
|
bool recieveSerialCommand();
|
||||||
|
bool recieveSerialCommand(bool process);
|
||||||
void processSerialCommand();
|
void processSerialCommand();
|
||||||
void processTouchPayload();
|
void processTouchPayload();
|
||||||
void processPageReportPayload();
|
void processPageReportPayload();
|
||||||
|
@ -39,6 +43,8 @@ class ESPMegaDisplay
|
||||||
void refreshPage();
|
void refreshPage();
|
||||||
void refreshInputPage();
|
void refreshInputPage();
|
||||||
void refreshOutputPage();
|
void refreshOutputPage();
|
||||||
|
bool payloadIsValid();
|
||||||
|
bool waitForValidPayload(uint32_t timeout);
|
||||||
HardwareSerial *displayAdapter;
|
HardwareSerial *displayAdapter;
|
||||||
ESPMegaPRO *espmega;
|
ESPMegaPRO *espmega;
|
||||||
std::function<void(uint8_t, uint8_t)> pushCallback;
|
std::function<void(uint8_t, uint8_t)> pushCallback;
|
||||||
|
|
Loading…
Reference in New Issue