From c4288d45421626ac1b89e0943be2160ccc3206d5 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 1 Jan 2026 23:46:00 +0000 Subject: [PATCH] Should I manually copy bytes? --- src/communication/walkieTalkie.cpp | 2 +- src/main.cpp | 70 +----------------------------- src/methods/methods.cpp | 42 ++++++++++++------ src/methods/methods.h | 27 +++++++++--- 4 files changed, 52 insertions(+), 89 deletions(-) diff --git a/src/communication/walkieTalkie.cpp b/src/communication/walkieTalkie.cpp index d20668d..886812f 100644 --- a/src/communication/walkieTalkie.cpp +++ b/src/communication/walkieTalkie.cpp @@ -40,7 +40,7 @@ namespace WalkieTalkie { } static inline void RecvPayloadRoutine(uint8_t *payload, byte *b) { - LOG_TRACE(F("Reading payload: %d %s %d\n"), bufferIndex, " - ", *b); + LOG_TRACE(F("Reading payload: %-.3d %s 0x%02x\n"), bufferIndex, " - ", *b); buffer[bufferIndex++] = *b; if (bufferIndex == len) { diff --git a/src/main.cpp b/src/main.cpp index e9fb657..445e07a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,7 +55,6 @@ Arduino_ESP32RGBPanel *panel = new Arduino_ESP32RGBPanel( // Arduino_GFX *gfx = new Arduino_RGB_Display(TFT_HOR_RES, TFT_VER_RES, panel, 16); -// HardwareSerial debugSerial(1); Curses::Screen mainScreen(gfx, UIDimensions(0, 0, TFT_HOR_RES, TFT_VER_RES), UIDecorations()); @@ -85,78 +84,13 @@ void setup() { // Only setup the main screen after initializing the Serial2 mainScreen.Setup("Main Window"); - - // Grab a handle for the main window of the screen UIElement *mainWindow = mainScreen.mainWindowHandle; mainWindow->drawBox(); - // int16_t childID = mainWindow->AddChild(STRING); - // if (childID < 0) { - // Serial2.println("Failed to add new window!"); - // } else { - // Serial2.print("Successfully added a new window: "); - // Serial2.println(childID); - // - // UIElement *childComponent = mainWindow->GetChild(childID); - // - // childComponent->SetTitle((char *)"%s [%2d]", (const char *)"Child Window", - // childID); - // childComponent->SetUIDecorations(UIDecorations()); - // childComponent->SetUIDimensions(UIDimensions(20, 20, 300, 300)); - // childComponent->SetDisplay(mainScreen.display); - // - // childComponent->drawBox(); - // } - // - // childID = mainWindow->AddChild(STRING); - // if (childID < 0) { - // Serial2.println("Failed to add new window!"); - // } else { - // Serial2.print("Successfully added a new window: "); - // Serial2.println(childID); - // - // UIElement *childComponent = mainWindow->GetChild(childID); - // - // childComponent->SetTitle((char *)"%s [%2d]", (const char *)"Second Child", - // childID); - // childComponent->SetUIDecorations(UIDecorations()); - // childComponent->SetUIDimensions(UIDimensions(330, 20, 300, 300)); - // childComponent->SetDisplay(mainScreen.display); - // - // childComponent->drawBox(); - // } - // - // mainWindow->RemoveChild(1); - // - // childID = mainWindow->AddChild(STRING); - // if (childID < 0) { - // Serial2.println("Failed to add new window!"); - // } else { - // Serial2.print("Successfully added a new window: "); - // Serial2.println(childID); - // - // UIElement *childComponent = mainWindow->GetChild(childID); - // - // childComponent->SetTitle((char *)"%s [%2d]", (const char *)"Third Child", - // childID); - // childComponent->SetUIDecorations(UIDecorations()); - // childComponent->SetUIDimensions(UIDimensions(20, 20, 300, 300)); - // childComponent->SetDisplay(mainScreen.display); - // - // childComponent->drawBox(); - // } - - WindowPool::PrintInUse(); - delay(500); LOG_INFO(F("* Ready for loop")); } -uint64_t lastDataRead = 0; - -bool gotAck = false; - -uint64_t lastSent = 0; void loop(void) { uint64_t cur = millis(); @@ -183,7 +117,7 @@ void loop(void) { WalkieTalkie::SendData(&papers); break; case CmdCreateWindow: { - uint8_t newWindowID = CreateWindow::Create(payload, &mainScreen); + uint8_t newWindowID = Window::Create(payload, &mainScreen); if (newWindowID < 0) { LOG_WARN(F("Failed to add new window!\n")); break; @@ -196,7 +130,7 @@ void loop(void) { break; } case CmdDestroyWindow: { - uint8_t destroyedWinID = DestroyWindow::Destroy(payload, &mainScreen); + uint8_t destroyedWinID = Window::Destroy(payload, &mainScreen); if (destroyedWinID < 0) { LOG_WARN(F("Failed to destroy window\n")); break; diff --git a/src/methods/methods.cpp b/src/methods/methods.cpp index 7e9c7a2..1b2dac9 100644 --- a/src/methods/methods.cpp +++ b/src/methods/methods.cpp @@ -3,22 +3,38 @@ #include "logger.h" #include "UIComponent.h" #include "UIScreen.h" +#include #include -namespace CreateWindow { +namespace Window { + // Window creation 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); + LOG_INFO(F(" Dimensions:\n")); + LOG_INFO(F(" X0 : %d\n"), win->dims.x); + LOG_INFO(F(" Y0 : %d\n"), win->dims.y); + LOG_INFO(F(" Width : %d\n"), win->dims.width); + LOG_INFO(F(" Height: %d\n"), win->dims.height); + LOG_INFO(F(" Decorations:\n")); + LOG_INFO(F(" HasBorder : %d\n"), win->decor.hasBorder); + LOG_INFO(F(" BGColour : %d\n"), win->decor.bgColor); + LOG_INFO(F(" FGColour : %d\n"), win->decor.fgColor); + LOG_INFO(F(" TitleColour : %d\n"), win->decor.titleColor); + LOG_INFO(F(" BorderColour: %d\n"), win->decor.borderColor); + LOG_INFO(F(" TitleSize : %d\n"), win->decor.titleSize); + LOG_INFO(F(" TextSize : %d\n"), win->decor.textSize); + LOG_INFO(F(" Title: %s\n"), win->title); } + UICreateWindowPacket::UICreateWindowPacket() {} + int8_t Create(uint8_t *payload, Curses::Screen *mainScreen) { // Load the payload into a struct - CreateWindow::UICreateWindowPacket win; - memcpy(&win, payload, sizeof(CreateWindow::UICreateWindowPacket)); + // UICreateWindowPacket* win; + UICreateWindowPacket win; + // Maybe find a better way to copy this - if we had metadata to the payload + // for example + memcpy((void*)&win, payload, sizeof(UICreateWindowPacket)); printUIWindowPacket(&win); UIElement *mainWindow = mainScreen->mainWindowHandle; @@ -31,24 +47,24 @@ namespace CreateWindow { childComponent->SetTitle((char *)"%s [%2d]", (const char *)win.title, childID); childComponent->SetUIDecorations(UIDecorations()); - childComponent->SetUIDimensions(UIDimensions(win.x0, win.y0, win.width, win.height)); + childComponent->SetUIDimensions(UIDimensions(win.dims.x, win.dims.y, + win.dims.width, win.dims.height)); childComponent->SetDisplay(mainScreen->display); childComponent->drawBox(); return childID; } -}; -namespace DestroyWindow { + // Window destruction void printUIDestroyWindowPacket(UIDestroyWindowPacket *win) { LOG_INFO(F("Destroy window:\n")); LOG_INFO(F(" WinID: %d\n"), win->WinID); } int8_t Destroy(uint8_t *payload, Curses::Screen *mainScreen) { - DestroyWindow::UIDestroyWindowPacket win; - memcpy(&win, payload, sizeof(DestroyWindow::UIDestroyWindowPacket)); + UIDestroyWindowPacket win; + memcpy(&win, payload, sizeof(UIDestroyWindowPacket)); printUIDestroyWindowPacket(&win); UIElement *mainWindow = mainScreen->mainWindowHandle; diff --git a/src/methods/methods.h b/src/methods/methods.h index b7e152d..dcac6c4 100644 --- a/src/methods/methods.h +++ b/src/methods/methods.h @@ -4,22 +4,24 @@ #include #include #include "UIScreen.h" +#include "UIDimensions.h" +#include "UIDecorations.h" -namespace CreateWindow { +namespace Window { + // Window creation struct UICreateWindowPacket { - uint16_t x0; - uint16_t y0; - uint16_t width; - uint16_t height; + UIDimensions dims; + UIDecorations decor; char title[32]; // Note: we optimize this type of data transfer if necessary // we are sending 32 - len(title) extra bytes everytime + + UICreateWindowPacket(); }; void printUIWindowPacket(UICreateWindowPacket *win); int8_t Create(uint8_t *payload, Curses::Screen *mainScreen); -}; -namespace DestroyWindow { + // Window destruction struct UIDestroyWindowPacket { int16_t WinID; }; @@ -28,4 +30,15 @@ namespace DestroyWindow { int8_t Destroy(uint8_t *payload, Curses::Screen *mainScreen); }; +namespace Screen { + struct UICreateScreenPacket{ + uint16_t x0; + uint16_t y0; + uint16_t width; + uint16_t height; + char title[32]; // Note: we optimize this type of data transfer if necessary + // we are sending 32 - len(title) extra bytes everytime + }; +} + #endif