need to rework how commands are received and the main system state

This commit is contained in:
2026-09-21 17:46:04 +01:00
parent 3437ea473e
commit cceafe027c
+105 -90
View File
@@ -77,7 +77,7 @@ void setup() {
Serial.begin(115200);
// Initialize the debug serial object
Logger::Initialize(UART1_RX, UART1_TX);
Logger::Initialize(UART1_RX, UART1_TX, 115200);
LOG_INFO(F("=== ESP32 SimRacing DashDisplay ===\r\n"));
LOG_INFO(F("* Initiating display\r\n"));
@@ -90,13 +90,16 @@ void setup() {
mainWindow->drawBox();
delay(500);
LOG_INFO(F("* Ready for loop"));
LOG_INFO(F("* Ready for loop\r\n"));
}
// I have to organize this better, but for now it will do
const uint16_t PayloadMax = 2056;
uint8_t payload[PayloadMax] = {0};
char lineBuffer[64]; // Enough for "XX XX XX XX XX XX XX XX "
//
bool connected = false;
void print_memory_stats() {
multi_heap_info_t info;
@@ -134,104 +137,116 @@ void loop(void) {
LOG_DEBUG(F("Response Payload Len: %d\n"), resp);
// print_memory_stats();
// If its not connected the only command we can receive is the CmdRequestID
if (!connected) {
if (cmd != CmdRequestID) {
return;
}
// Handle the ID request, it should connect us. TODO: Improve this
IdentificationPacket papers;
initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID);
WalkieTalkie::SendData(&papers);
LOG_INFO(F("Connection request came in"));
connected = true;
} else {
switch(cmd) {
case CmdCreateWindow: {
int8_t newWindowID = Window::Create(payload, &mainScreen);
if (newWindowID < 0) {
LOG_WARN(F("Failed to add new window!\n"));
break;
}
LOG_INFO(F("Successfully added a new window: %d\n"), newWindowID);
// Now we return the ID of this new window - esdi should expect it
WindowIDReply wID;
initWindowIDReply(&wID, newWindowID);
WalkieTalkie::SendData(&wID);
switch(cmd) {
case CmdRequestID:
IdentificationPacket papers;
initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID);
WalkieTalkie::SendData(&papers);
break;
case CmdCreateWindow: {
int8_t newWindowID = Window::Create(payload, &mainScreen);
if (newWindowID < 0) {
LOG_WARN(F("Failed to add new window!\n"));
break;
}
case CmdUpdateWinDims: {
LOG_INFO(F("Updating Win DIMS\n"));
bool res = Window::UpdateDims(payload, &mainScreen);
if (!res) {
LOG_WARN(F("Failed to update window dims\n"));
}
break;
};
case CmdUpdateWin: {
LOG_INFO(F("UPDATEING WINDOW\n"));
bool res = Window::UpdateWindow(payload, &mainScreen);
if (!res) {
LOG_WARN(F("Failed to update window\n"));
}
break;
}
case CmdDestroyWindow: {
uint8_t destroyedWinID = Window::Destroy(payload, &mainScreen);
if (destroyedWinID < 0) {
LOG_WARN(F("Failed to destroy window\n"));
break;
}
LOG_INFO(F("Successfully added a new window: %d\n"), newWindowID);
LOG_INFO(F("Succesfully destroyed window: %d\n"), destroyedWinID);
// Now we return the ID of this new window - esdi should expect it
WindowIDReply wID;
initWindowIDReply(&wID, newWindowID);
WalkieTalkie::SendData(&wID);
break;
}
case CmdUpdateWinDims: {
LOG_INFO(F("Updating Win DIMS\n"));
bool res = Window::UpdateDims(payload, &mainScreen);
if (!res) {
LOG_WARN(F("Failed to update window dims\n"));
}
break;
};
case CmdUpdateWin: {
LOG_INFO(F("UPDATEING WINDOW\n"));
bool res = Window::UpdateWindow(payload, &mainScreen);
if (!res) {
LOG_WARN(F("Failed to update window\n"));
}
break;
}
case CmdDestroyWindow: {
uint8_t destroyedWinID = Window::Destroy(payload, &mainScreen);
if (destroyedWinID < 0) {
LOG_WARN(F("Failed to destroy window\n"));
break;
}
case CMDData: {
LOG_DEBUG(F("Data Received: %d bytes\r\n"), resp);
// int pos = 0;
// for (int k = 0; k < resp; k++) {
// // Write 2 hex digits and a space to our local buffer
// // %02X ensures leading zeros (e.g., 0A instead of A)
// pos += sprintf(lineBuffer + pos, "%02X ", payload[k]);
//
// // Every 8 bytes OR if it's the very last byte in the payload
// if ((k + 1) % 8 == 0 || k == resp - 1) {
// // Send the completed line to the logger
// // We use LOG_DEBUG or similar so we don't spam [WARN] on every line
// LOG_DEBUG(F("%s\r\n"), lineBuffer);
//
// // Reset buffer position for the next line
// pos = 0;
// memset(lineBuffer, 0, sizeof(lineBuffer));
// }
// }
// LOG_DEBUG(F("\r\n"));
LOG_INFO(F("Succesfully destroyed window: %d\n"), destroyedWinID);
Data::Parse(payload, resp, &mainScreen);
break;
}
case CMDData: {
LOG_DEBUG(F("Data Received: %d bytes\r\n"), resp);
// int pos = 0;
// for (int k = 0; k < resp; k++) {
// // Write 2 hex digits and a space to our local buffer
// // %02X ensures leading zeros (e.g., 0A instead of A)
// pos += sprintf(lineBuffer + pos, "%02X ", payload[k]);
//
// // Every 8 bytes OR if it's the very last byte in the payload
// if ((k + 1) % 8 == 0 || k == resp - 1) {
// // Send the completed line to the logger
// // We use LOG_DEBUG or similar so we don't spam [WARN] on every line
// LOG_DEBUG(F("%s\r\n"), lineBuffer);
//
// // Reset buffer position for the next line
// pos = 0;
// memset(lineBuffer, 0, sizeof(lineBuffer));
// }
// }
// LOG_DEBUG(F("\r\n"));
Data::Parse(payload, resp, &mainScreen);
break;
}
default:
LOG_ERROR(F("Command: %s\n"), CommandToStr(cmd));
LOG_ERROR(F("Unknown Command: `%02X` has not been implemented yet.\n"), cmd);
int pos = 0;
for (int k = 0; k < resp; k++) {
// Write 2 hex digits and a space to our local buffer
// %02X ensures leading zeros (e.g., 0A instead of A)
pos += sprintf(lineBuffer + pos, "%02X ", payload[k]);
// Every 8 bytes OR if it's the very last byte in the payload
if ((k + 1) % 8 == 0 || k == resp - 1) {
// Send the completed line to the logger
// We use LOG_DEBUG or similar so we don't spam [WARN] on every line
LOG_WARN(F("%s\r\n"), lineBuffer);
// Reset buffer position for the next line
pos = 0;
memset(lineBuffer, 0, sizeof(lineBuffer));
}
break;
}
LOG_WARN(F("\r\n"));
break;
default:
LOG_ERROR(F("Command: %s\n"), CommandToStr(cmd));
LOG_ERROR(F("Unknown Command: `%02X` has not been implemented yet.\n"), cmd);
int pos = 0;
for (int k = 0; k < resp; k++) {
// Write 2 hex digits and a space to our local buffer
// %02X ensures leading zeros (e.g., 0A instead of A)
pos += sprintf(lineBuffer + pos, "%02X ", payload[k]);
// Every 8 bytes OR if it's the very last byte in the payload
if ((k + 1) % 8 == 0 || k == resp - 1) {
// Send the completed line to the logger
// We use LOG_DEBUG or similar so we don't spam [WARN] on every line
LOG_WARN(F("%s\r\n"), lineBuffer);
// Reset buffer position for the next line
pos = 0;
memset(lineBuffer, 0, sizeof(lineBuffer));
}
}
LOG_WARN(F("\r\n"));
break;
}
}
}
}