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
+107 -92
View File
@@ -77,7 +77,7 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
// Initialize the debug serial object // 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("=== ESP32 SimRacing DashDisplay ===\r\n"));
LOG_INFO(F("* Initiating display\r\n")); LOG_INFO(F("* Initiating display\r\n"));
@@ -90,13 +90,16 @@ void setup() {
mainWindow->drawBox(); mainWindow->drawBox();
delay(500); 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; const uint16_t PayloadMax = 2056;
uint8_t payload[PayloadMax] = {0}; uint8_t payload[PayloadMax] = {0};
char lineBuffer[64]; // Enough for "XX XX XX XX XX XX XX XX " char lineBuffer[64]; // Enough for "XX XX XX XX XX XX XX XX "
// //
bool connected = false;
void print_memory_stats() { void print_memory_stats() {
multi_heap_info_t info; multi_heap_info_t info;
@@ -135,103 +138,115 @@ void loop(void) {
// print_memory_stats(); // print_memory_stats();
switch(cmd) { // If its not connected the only command we can receive is the CmdRequestID
case CmdRequestID: if (!connected) {
IdentificationPacket papers; if (cmd != CmdRequestID) {
initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID); return;
WalkieTalkie::SendData(&papers); }
break;
case CmdCreateWindow: { // Handle the ID request, it should connect us. TODO: Improve this
int8_t newWindowID = Window::Create(payload, &mainScreen); IdentificationPacket papers;
if (newWindowID < 0) { initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID);
LOG_WARN(F("Failed to add new window!\n")); 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);
break; break;
} }
case CmdUpdateWinDims: {
LOG_INFO(F("Successfully added a new window: %d\n"), newWindowID); LOG_INFO(F("Updating Win DIMS\n"));
bool res = Window::UpdateDims(payload, &mainScreen);
// Now we return the ID of this new window - esdi should expect it if (!res) {
WindowIDReply wID; LOG_WARN(F("Failed to update window dims\n"));
initWindowIDReply(&wID, newWindowID); }
WalkieTalkie::SendData(&wID); break;
};
break; case CmdUpdateWin: {
} LOG_INFO(F("UPDATEING WINDOW\n"));
case CmdUpdateWinDims: { bool res = Window::UpdateWindow(payload, &mainScreen);
LOG_INFO(F("Updating Win DIMS\n")); if (!res) {
bool res = Window::UpdateDims(payload, &mainScreen); LOG_WARN(F("Failed to update window\n"));
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; 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("Succesfully destroyed window: %d\n"), destroyedWinID); LOG_INFO(F("Succesfully destroyed window: %d\n"), destroyedWinID);
break; 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));
}
} }
LOG_WARN(F("\r\n")); case CMDData: {
break; 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));
}
}
LOG_WARN(F("\r\n"));
break;
}
} }
} }
} }