From 7a4d5435618a8271d2f6a677f639f9f7b58c6069 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 31 Dec 2025 15:56:29 +0000 Subject: [PATCH] Added the create window command --- src/communication/commands.cpp | 4 +- src/data.cpp | 84 ---------------------------------- src/data.h | 59 ------------------------ src/main.cpp | 43 +++++++++++++++-- 4 files changed, 40 insertions(+), 150 deletions(-) delete mode 100644 src/data.cpp delete mode 100644 src/data.h diff --git a/src/communication/commands.cpp b/src/communication/commands.cpp index 71c8c44..a1fca05 100644 --- a/src/communication/commands.cpp +++ b/src/communication/commands.cpp @@ -10,7 +10,7 @@ char* CommandToStr(Command id) { return (char*)"CmdCreateWindow"; case CmdDestroyWindow: return (char*)"CmdDestroyWindow"; + default: + return (char*)"CmdUnknown"; } - - return (char*)"CmdInvalid"; } diff --git a/src/data.cpp b/src/data.cpp deleted file mode 100644 index deb5341..0000000 --- a/src/data.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "data.h" -#include -#include - -uint8_t packetBuffer[sizeof(DataPacket)] = {0}; -int bufferIndex = 0; -bool isReceiving = false; - -void SendDataRequest() { - DataReq req = {5}; - Serial.write((uint8_t *)&req, sizeof(req)); - Serial.flush(); -} - -int RecvDataPacket(DataPacket *p) { - while (Serial.available() > 0) { - uint8_t byte = Serial.read(); - - if (!isReceiving) { - if (byte == 0x02) { - isReceiving = true; - bufferIndex = 0; - } - } - packetBuffer[bufferIndex++] = byte; - - if (bufferIndex >= sizeof(DataPacket)) { - Serial.flush(); - bufferIndex = 0; - isReceiving = false; - - memcpy(p, packetBuffer, sizeof(DataPacket)); - - if (p->EndMarker != 0x03) { - // Serial2.println(F("\r////////////// DATA IS MALFORMED - // //////////////")); - return 1; - } - - return 0; - // for(int k = 0; k < sizeof(DataPacket); k++) { - // if (k % 8 == 0) { - // Serial2.printf("\r\n"); - // } - // Serial2.printf(" 0x%02x", packetBuffer[k]); - // } - // Serial2.printf("\r\n"); - - // Serial2.printf("%d / %d [%2d] || %d / %d [%2d]\n\n\r", - // ESP.getFreeHeap(), - // ESP.getHeapSize(), - // (int)(((float)(ESP.getHeapSize() - ESP.getFreeHeap()) / - // ESP.getHeapSize()) * - // 100), - // ESP.getFreePsram(), ESP.getPsramSize(), - // (int)(((float)(ESP.getPsramSize() - ESP.getFreePsram()) - // / - // ESP.getPsramSize()) * - // 100)); - - // debugDataPacket(&packet); - } - } - return 1; -} - -void DebugDataPacket(DataPacket *p) { - Serial2.printf("\rSize: %zu\n", sizeof(struct DataPacket)); - Serial2.printf("\rStartMarker: 0x%02x\n", p->StartMarker); - Serial2.printf("\rSpeed: %s\n", p->speed); - Serial2.printf("\rGear: %s\n", p->gear); - Serial2.printf("\rRPM: %s\n", p->rpm); - Serial2.printf("\rLapNumber: %s\n", p->LapNumber); - Serial2.printf("\rcurrLapTime: %s\n", p->currLapTime); - Serial2.printf("\rlastLapTime: %s\n", p->lastLapTime); - Serial2.printf("\rfuelEst: %s\n", p->FuelEst); - Serial2.printf("\rStandings:\n"); - for (int k = 0; k < 5; k++) { - Serial2.printf("\r Lap: %s\n", p->standings[k].Lap); - Serial2.printf("\r DriverName: %s\n", p->standings[k].DriverName); - Serial2.printf("\r TimeBehind: %s\n", p->standings[k].TimeBehindString); - } - Serial2.printf("\rEndMarker: 0x%02x\n\n", p->EndMarker); -} diff --git a/src/data.h b/src/data.h deleted file mode 100644 index e89cdcb..0000000 --- a/src/data.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __DASH_DISPLAY_DATA -#define __DASH_DISPLAY_DATA - -#include - -struct DataReq { - uint8_t type; -}; - -// Car data -const int speedLen = 5; -const int gearLen = 3; -const int rpmLen = 6; -const int brakeBiasLen = 6; - -// Lap data -const int lapNumberLen = 5; -const int DeltaToBestLapLen = 6; -const int bestLapTimeLen = 10; -const int currLapTimeLen = 10; -const int lastLapTimeLen = 10; - -// Fuel data -const int FuelTankLen = 15; -const int FuelEstLen = 15; - -const int LapStringLen = 4; -const int DriverNameLen = 24; -const int TimeBehindStringLen = 8; - -#pragma pack(push, 1) -struct StandingLine { - char Lap[LapStringLen]; - char DriverName[DriverNameLen]; - char TimeBehindString[TimeBehindStringLen]; -}; - -struct DataPacket { - uint8_t StartMarker; - char speed[speedLen]; - char gear[gearLen]; - char rpm[rpmLen]; - char brakeBias[brakeBiasLen]; - char LapNumber[lapNumberLen]; - char DeltaToBestLap[DeltaToBestLapLen]; - char bestLapTime[bestLapTimeLen]; - char currLapTime[currLapTimeLen]; - char lastLapTime[lastLapTimeLen]; - char FuelEst[FuelEstLen]; - StandingLine standings[5]; - uint8_t EndMarker; -}; -#pragma pack(pop) - -void DebugDataPacket(DataPacket *p); -int RecvDataPacket(DataPacket *p); -void SendDataRequest(); - -#endif diff --git a/src/main.cpp b/src/main.cpp index d554a5d..ce1606b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -156,21 +156,28 @@ uint64_t lastDataRead = 0; bool gotAck = false; -struct UIWindowPacket { - uint8_t StartMarker; +struct UICreateWindowPacket { uint16_t x0; uint16_t y0; uint16_t width; uint16_t height; - uint8_t EndMarker; + char title[32]; // Note: we optimize this type of data transfer if necessary + // we are sending 32 - len(title) extra bytes everytime }; +void printUIWindowPacket(UICreateWindowPacket *win) { + LOG_INFO(F("New window:\n")); + LOG_INFO(F(" X0 : %d\n"), win->x0); + LOG_INFO(F(" Y0 : %d\n"), win->y0); + LOG_INFO(F(" Width : %d\n"), win->width); + LOG_INFO(F(" Height: %d\n"), win->height); + LOG_INFO(F(" Title : %s\n"), win->title); +} + uint64_t lastSent = 0; void loop(void) { uint64_t cur = millis(); - // with this new way of reading data, we are just gonna read a payload - // and get a command Command cmd = CmdUnknown; uint8_t payload[256] = {0}; @@ -193,6 +200,32 @@ void loop(void) { initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID); WalkieTalkie::SendData(&papers); break; + case CmdCreateWindow: { + // Here we parse the payload a a UIWindowPacket + UICreateWindowPacket win; + memcpy(&win, payload, sizeof(UICreateWindowPacket)); + printUIWindowPacket(&win); + + UIElement *mainWindow = mainScreen.mainWindowHandle; + + int16_t childID = mainWindow->AddChild(STRING); + if (childID < 0) { + LOG_WARN(F("Failed to add new window!\n")); + } else { + LOG_INFO(F("Successfully added a new window: %d\n"), childID); + + UIElement *childComponent = mainWindow->GetChild(childID); + + childComponent->SetTitle((char *)"%s [%2d]", (const char *)win.title, childID); + childComponent->SetUIDecorations(UIDecorations()); + childComponent->SetUIDimensions(UIDimensions(20, 20, 300, 300)); + childComponent->SetDisplay(mainScreen.display); + + childComponent->drawBox(); + } + + break; + } default: LOG_WARN(F("Command: `%s` has not been implemented yet."), CommandToStr(cmd)); }