From 36cdd67f23c6dbe7fad6dbc4f2ee86507f8641ce Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 20 Feb 2026 21:53:09 +0000 Subject: [PATCH] using polymorphism now but still have to decide on a better pattern --- src/UIBar.cpp | 6 ++++-- src/UIBar.h | 3 ++- src/UIBase.cpp | 19 +++++++++++++++++++ src/UIBase.h | 17 +++++++++++++++++ src/UIComponent.h | 4 ++++ src/UIString.cpp | 8 ++++++-- src/UIString.h | 3 ++- src/UITable.cpp | 5 ++++- src/UITable.h | 4 ++++ src/windowPool.cpp | 3 ++- src/windowPool.h | 4 +++- 11 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 src/UIBase.cpp create mode 100644 src/UIBase.h diff --git a/src/UIBar.cpp b/src/UIBar.cpp index b2dd6d9..4e634b4 100644 --- a/src/UIBar.cpp +++ b/src/UIBar.cpp @@ -7,7 +7,7 @@ UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title) : UIElement(d, dims, decor, title) {} -void UIBar::Update(char *v) { +void UIBar::Update(const char *v, bool forceRedraw) { unsigned long time = millis(); if ((time - this->lastUpdate) <= this->refreshRate) { return; @@ -16,7 +16,7 @@ void UIBar::Update(char *v) { // Conver the string to an integer uint32_t newVal = atoi(v); - if (this->value == newVal) { + if ((this->value == newVal) && !forceRedraw) { // If the value hasn't changed we do not need to re-render return; } @@ -104,3 +104,5 @@ void UIBar::Box() { this->display->print(legend); } } + +void UIBar::Redraw() {} diff --git a/src/UIBar.h b/src/UIBar.h index 9bec301..d054b0e 100644 --- a/src/UIBar.h +++ b/src/UIBar.h @@ -12,7 +12,8 @@ struct UIBar : UIElement { UIBar(); UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title); - void Update(char *val); + void Update(const char *val, bool forceRedraw) override; + void Redraw() override; void DrawBar(uint32_t *oldValue, uint32_t *newValue); void renderBlank(); void Box(); diff --git a/src/UIBase.cpp b/src/UIBase.cpp new file mode 100644 index 0000000..62044ce --- /dev/null +++ b/src/UIBase.cpp @@ -0,0 +1,19 @@ +#include "UIBase.h" +#include "HardwareSerial.h" +#include "UIComponent.h" +#include + +UIBase::UIBase() : UIElement(nullptr, {0, 0, 0, 0}, UIDecorations(), (char *)"") { + this->type = STRING; + memset(this->value, 0, this->bufferSize); +} + +UIBase::UIBase(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, + char *title) + : UIElement(d, dims, decor, title) { + this->type = BASE; + memset(this->value, 0, this->bufferSize); +} + +void UIBase::Update(const char* data, bool forceRedraw) {} +void UIBase::Redraw() {} diff --git a/src/UIBase.h b/src/UIBase.h new file mode 100644 index 0000000..9024cdd --- /dev/null +++ b/src/UIBase.h @@ -0,0 +1,17 @@ +#ifndef __UI_BASE__ +#define __UI_BASE__ + +#include "UIComponent.h" + +// Representation of single line strings +struct UIBase : UIElement { + static const std::size_t bufferSize = 64; + char value[bufferSize]; + + UIBase(); + UIBase(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title); + void Update(const char *value, bool forceRedraw) override; + void Redraw() override; +}; + +#endif diff --git a/src/UIComponent.h b/src/UIComponent.h index 379246a..0f7a9ad 100644 --- a/src/UIComponent.h +++ b/src/UIComponent.h @@ -63,6 +63,10 @@ public: void drawBox(); void noBox(); void replaceString(char *oldVal, char *newVal); + + // Viewing - more like actual rendering data + virtual void Update(const char *data, bool forceRedraw) = 0; + virtual void Redraw() = 0; }; #endif diff --git a/src/UIString.cpp b/src/UIString.cpp index da76821..b28ac2b 100644 --- a/src/UIString.cpp +++ b/src/UIString.cpp @@ -15,7 +15,7 @@ UIString::UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, memset(this->value, 0, this->bufferSize); } -void UIString::Update(const char *v) { +void UIString::Update(const char *v, bool forceRedraw) { uint64_t time = millis(); if ((time - this->lastUpdate) <= this->refreshRate) { return; @@ -28,7 +28,7 @@ void UIString::Update(const char *v) { } // If the value hasn't changed we do not need to re-render - if (strcmp(this->value, v) == 0) { + if ((strcmp(this->value, v) == 0) && !forceRedraw) { return; } @@ -46,3 +46,7 @@ void UIString::Update(const char *v) { replaceString(oldVal, newVal); strncpy(this->value, v, this->bufferSize); } + +void UIString::Redraw() { + this->Update(this->value, true); +} diff --git a/src/UIString.h b/src/UIString.h index 344602a..de045ea 100644 --- a/src/UIString.h +++ b/src/UIString.h @@ -11,7 +11,8 @@ struct UIString : UIElement { UIString(); UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title); - void Update(const char *value); + void Update(const char *value, bool forceRedraw) override; + void Redraw() override; }; #endif diff --git a/src/UITable.cpp b/src/UITable.cpp index 07747b3..1305350 100644 --- a/src/UITable.cpp +++ b/src/UITable.cpp @@ -76,8 +76,11 @@ void UITable::setup() { #ifdef DEBUG this->tableData[r * COLUMNS + c]->drawBox(); - this->tableData[r * COLUMNS + c]->Update("---"); + this->tableData[r * COLUMNS + c]->Update("---", false); #endif } } } + +void UITable::Update(const char* data, bool forceRedraw) {} +void UITable::Redraw() {} diff --git a/src/UITable.h b/src/UITable.h index 0a7e05b..19e23b7 100644 --- a/src/UITable.h +++ b/src/UITable.h @@ -27,6 +27,10 @@ struct UITable : UIElement { // Getters uint16_t getContentAreaHeight(); + + // Rendering + void Update(const char* data, bool forceRedraw) override; + void Redraw() override; }; #endif diff --git a/src/windowPool.cpp b/src/windowPool.cpp index 32140e1..0ecf812 100644 --- a/src/windowPool.cpp +++ b/src/windowPool.cpp @@ -2,6 +2,7 @@ #include "UIComponent.h" #include "UIBar.h" #include "UIString.h" +#include "UIBase.h" #include "UITable.h" #include "values.h" #include "logger.h" @@ -43,7 +44,7 @@ namespace WindowPool { pool[nextIndex].table = new UITable(); break; case BASE: - pool[nextIndex].base = new UIElement(); + pool[nextIndex].base = new UIBase(); break; } diff --git a/src/windowPool.h b/src/windowPool.h index 68edb92..ebafa3d 100644 --- a/src/windowPool.h +++ b/src/windowPool.h @@ -9,12 +9,14 @@ struct UIElement; struct UIString; struct UIBar; struct UITable; +struct UIBase; namespace WindowPool { struct WindowEntry { ComponentType type; + // Well I did this like this but I recon we can use polymorphism instead union { - UIElement* base; + UIBase* base; UIString* str; UIBar* bar; UITable* table;