Improving the API

This commit is contained in:
2025-11-11 23:31:43 +00:00
parent 69868c1b31
commit 12209f9789
15 changed files with 92 additions and 44 deletions
+3 -3
View File
@@ -1,7 +1,7 @@
#include "UIBar.h"
#include "UIDrawing.h"
UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor,
char *title)
: UIElement(d, dims, decor, title) {}
@@ -95,10 +95,10 @@ void UIBar::Box() {
char legend[5];
sprintf(legend, "%d", k);
this->display->setCursor(x - (CHR_WIDTH(this->decor->textSize) / 2),
this->display->setCursor(x - (CHR_WIDTH(this->decor.textSize) / 2),
this->dims.y + this->dims.height + 7 + 2);
this->display->setTextColor(WHITE);
this->display->setTextSize(this->decor->textSize);
this->display->setTextSize(this->decor.textSize);
this->display->print(legend);
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ struct UIBar : UIElement {
uint32_t value = 0; // current tach value
uint8_t range = 0; // define the tach range, ie: 8 for 8000rpm
UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, char *title);
UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title);
void Update(char *val);
void DrawBar(uint32_t *oldValue, uint32_t *newValue);
+25 -16
View File
@@ -11,19 +11,16 @@
#define DEBUG 1
UIElement::UIElement()
: display(nullptr), dims(UIDimensions(0, 0, 0, 0)), decor(nullptr) {}
: display(nullptr), dims(UIDimensions(0, 0, 0, 0)), decor(UIDecorations()) {}
UIElement::UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
UIElement::UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations decor,
char *title)
: display(d), dims(dims), decor(decor) {
strncpy(this->title, title, MAX_TITLE_LEN);
}
UIDimensions::UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
: x(x), y(y), width(w), height(h) {}
int16_t UIElement::getContentAreaX0() {
if (this->decor->hasBorder) {
if (this->decor.hasBorder) {
return this->dims.x + DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN;
}
@@ -31,7 +28,7 @@ int16_t UIElement::getContentAreaX0() {
}
int16_t UIElement::getContentAreaY0() {
if (this->decor->hasBorder) {
if (this->decor.hasBorder) {
return this->dims.y + this->getTitleAreaHeight() +
DEFAULT_TITLE_CONTENT_SPACING;
}
@@ -58,7 +55,7 @@ uint16_t UIElement::getTitleAreaHeight() {
int16_t x = 0, y = 0;
uint16_t w = 0, h = 0;
this->display->setTextSize(this->decor->titleSize);
this->display->setTextSize(this->decor.titleSize);
this->display->getTextBounds((const char *)this->title, 0, 0, &x, &y, &w, &h);
return h;
@@ -68,7 +65,7 @@ uint16_t UIElement::getTitleAreaWidth() {
int16_t x = 0, y = 0;
uint16_t w = 0, h = 0;
this->display->setTextSize(this->decor->titleSize);
this->display->setTextSize(this->decor.titleSize);
this->display->getTextBounds((const char *)this->title, 0, 0, &x, &y, &w, &h);
return w;
@@ -81,6 +78,18 @@ void UIElement::SetTitle(char* format, ...) {
va_end(args);
}
void UIElement::SetUIDecorations(UIDecorations decor) {
this->decor = decor;
}
void UIElement::SetUIDimensions(UIDimensions dims) {
this->dims = dims;
}
void UIElement::SetDisplay(Arduino_GFX *d) {
this->display = d;
}
void UIElement::replaceString(char *oldVal, char *newVal) {
size_t newValLen = strlen(newVal);
size_t oldValLen = strlen(oldVal);
@@ -90,16 +99,16 @@ void UIElement::replaceString(char *oldVal, char *newVal) {
int16_t y0 = this->getContentAreaY0(); // <- Crashes here
// Before here
this->display->setTextSize(this->decor->textSize);
this->display->setTextSize(this->decor.textSize);
// If the new value is shorter, delete the extra in advance
if (oldValLen > newValLen) {
// If the old value is longer than the new value, we have to delete the
// extra of the old value, which is whatever starts at the new value len
for (std::size_t start = newValLen; start < oldValLen; start++) {
this->display->setCursor(x0 + start * (CHR_WIDTH(this->decor->textSize)),
this->display->setCursor(x0 + start * (CHR_WIDTH(this->decor.textSize)),
y0);
this->display->setTextColor(this->decor->bgColor);
this->display->setTextColor(this->decor.bgColor);
this->display->print(oldVal[start]);
}
}
@@ -107,16 +116,16 @@ void UIElement::replaceString(char *oldVal, char *newVal) {
for (std::size_t k = 0; k < newValLen; k++) {
if (k <= oldValLen) {
if (oldVal[k] != newVal[k]) {
this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor->textSize)),
this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor.textSize)),
y0 + 0);
this->display->setTextColor(this->decor->bgColor);
this->display->setTextColor(this->decor.bgColor);
this->display->print(oldVal[k]);
}
}
this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor->textSize)),
this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor.textSize)),
y0 + 0);
this->display->setTextColor(this->decor->fgColor);
this->display->setTextColor(this->decor.fgColor);
this->display->print(newVal[k]);
}
}
+5 -2
View File
@@ -24,14 +24,14 @@ private:
public:
Arduino_GFX *display;
UIDimensions dims;
UIDecorations *decor;
UIDecorations decor;
char title[MAX_TITLE_LEN];
uint64_t lastUpdate = 0;
uint16_t refreshRate = 0;
ComponentType type;
UIElement();
UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations decor,
char *Title);
// Children
@@ -54,6 +54,9 @@ public:
// Setters
void SetTitle(char* format, ...);
void SetUIDecorations(UIDecorations decor);
void SetUIDimensions(UIDimensions dims);
void SetDisplay(Arduino_GFX *d);
// Positioning
void horizontalCenter(UIElement *reference);
+12
View File
@@ -7,3 +7,15 @@ UIDecorations::UIDecorations() {}
UIDecorations::UIDecorations(uint16_t bg, uint16_t fg, uint16_t title,
uint16_t border, uint8_t titleSize,
uint8_t textSize) {}
UIDecorations& UIDecorations::operator=(const UIDecorations& other) {
this->textSize = other.textSize;
this->titleSize = other.titleSize;
this->titleColor = other.titleColor;
this->borderColor = other.borderColor;
this->bgColor = other.bgColor;
this->fgColor = other.fgColor;
this->hasBorder = other.hasBorder;
return *this;
}
+1
View File
@@ -13,6 +13,7 @@ struct UIDecorations {
UIDecorations();
UIDecorations(uint16_t bg, uint16_t fg, uint16_t title, uint16_t border,
uint8_t titleSize, uint8_t textSize);
UIDecorations& operator=(const UIDecorations& other);
};
#endif
+13
View File
@@ -0,0 +1,13 @@
#include "UIDimensions.h"
UIDimensions::UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
: x(x), y(y), width(w), height(h) {}
UIDimensions& UIDimensions::operator=(const UIDimensions& other) {
this->x = other.x;
this->y = other.y;
this->width = other.width;
this->height = other.height;
return *this;
}
+1
View File
@@ -8,6 +8,7 @@ struct UIDimensions {
uint16_t width = 0, height = 0;
UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
UIDimensions& operator=(const UIDimensions& other);
};
#endif
+3 -3
View File
@@ -51,11 +51,11 @@ void UIElement::placeLeft(UIElement *ref) {
void UIElement::drawBox() {
// This is the border rectangle
if (this->decor->hasBorder) {
if (this->decor.hasBorder) {
for (int k = 0; k < DEFAULT_BORDER_THICKNESS; k++) {
this->display->drawRect(
this->dims.x + k, this->dims.y + k, this->dims.width - (k * 2),
this->dims.height - (k * 2), this->decor->borderColor);
this->dims.height - (k * 2), this->decor.borderColor);
}
}
@@ -88,7 +88,7 @@ void UIElement::drawBox() {
this->display->drawRect(x, y, w, h, BLUE);
#endif
if (this->decor->hasBorder) {
if (this->decor.hasBorder) {
// Remove the border line behind the title
this->display->fillRect(titleTopLeftX - DEFAULT_MARGIN, this->dims.y,
titleAreaWidth + DEFAULT_MARGIN,
+11 -3
View File
@@ -3,10 +3,12 @@
#include "UIDecorations.h"
#include "UIDimensions.h"
Curses::Screen::Screen(Arduino_GFX *d, UIDimensions dims, UIDecorations decor)
: display(d), dims(dims), decor(decor) {}
Curses::Screen::Screen(Arduino_GFX *d, UIDimensions dims,
UIDecorations decor)
: display(d), dims(dims), decor(decor) {
}
void Curses::Screen::Setup() {
void Curses::Screen::Setup(const char* title) {
// This should always return the first available component
this->mainWindowID = WindowPool::Allocate();
if (this->mainWindowID != 0) {
@@ -18,4 +20,10 @@ void Curses::Screen::Setup() {
Serial2.println("Program interrupted due to setup failure");
}
}
this->mainWindowHandle = WindowPool::GetHandle(this->mainWindowID);
this->mainWindowHandle->SetDisplay(this->display);
this->mainWindowHandle->SetTitle((char*)"%s [%2d]", title, this->mainWindowID);
this->mainWindowHandle->SetUIDecorations(this->decor);
this->mainWindowHandle->SetUIDimensions(this->dims);
}
+2 -1
View File
@@ -15,8 +15,9 @@ public:
Arduino_GFX *display;
int16_t mainWindowID;
UIElement* mainWindowHandle;
Screen(Arduino_GFX *d, UIDimensions dims, UIDecorations decor);
void Setup();
void Setup(const char* title);
};
}; // namespace Curses
+2 -2
View File
@@ -3,12 +3,12 @@
#include "UIComponent.h"
#include <Arduino_GFX.h>
UIString::UIString() : UIElement(nullptr, {0, 0, 0, 0}, nullptr, (char *)"") {
UIString::UIString() : UIElement(nullptr, {0, 0, 0, 0}, UIDecorations(), (char *)"") {
this->type = STRING;
memset(this->value, 0, this->bufferSize);
}
UIString::UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
UIString::UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations decor,
char *title)
: UIElement(d, dims, decor, title) {
this->type = STRING;
+1 -1
View File
@@ -9,7 +9,7 @@ struct UIString : UIElement {
char value[bufferSize];
UIString();
UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations decor,
char *title);
void Update(const char *value);
};
+11 -11
View File
@@ -6,7 +6,7 @@
#define DEBUG
UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor,
UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations decor,
int nRows, int nCols, int *colWidths, char *title)
: UIElement(gfx, dims, decor, title) {
this->type = TABLE;
@@ -16,18 +16,18 @@ UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor,
this->colWidths = colWidths;
// Default decoration for every cell
this->decor->textSize = 2;
this->decor.textSize = 2;
for (int k = 0; k < ROWS * COLUMNS; k++) {
UIDecorations *cellDecor = new UIDecorations();
cellDecor->textSize = this->decor->textSize;
UIDecorations cellDecor = UIDecorations();
cellDecor.textSize = this->decor.textSize;
UIDimensions dims = {0, 0, 0, 0};
this->tableData[k] = new UIString(display, dims, cellDecor, (char *)"");
}
int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN;
int cellH = CHR_HEIGHT(this->decor.textSize) + CELL_MARGIN;
// Initialize the table dimensions
//// Calculate the height
@@ -37,7 +37,7 @@ UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor,
//// Calculate the width
for (int c = 0; c < this->nColumns; c++) {
this->dims.width +=
(CHR_WIDTH(this->decor->textSize) + CELL_MARGIN) * this->colWidths[c];
(CHR_WIDTH(this->decor.textSize) + CELL_MARGIN) * this->colWidths[c];
}
this->dims.width += (DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN) * 2;
}
@@ -52,7 +52,7 @@ uint16_t UITable::getContentAreaHeight() {
* I recon its dynamic enough for now
*/
void UITable::setup() {
int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN;
int cellH = CHR_HEIGHT(this->decor.textSize) + CELL_MARGIN;
// Initialize the cells
uint16_t yOffset = this->getContentAreaY0();
@@ -60,18 +60,18 @@ void UITable::setup() {
int16_t xOffset = this->getContentAreaX0();
for (int c = 0; c < COLUMNS; c++) {
this->tableData[r * COLUMNS + c]->refreshRate = this->refreshRate;
this->tableData[r * COLUMNS + c]->decor->textSize = this->decor->textSize;
this->tableData[r * COLUMNS + c]->decor->hasBorder = false;
this->tableData[r * COLUMNS + c]->decor.textSize = this->decor.textSize;
this->tableData[r * COLUMNS + c]->decor.hasBorder = false;
this->tableData[r * COLUMNS + c]->dims.x = xOffset + CELL_MARGIN;
this->tableData[r * COLUMNS + c]->dims.y = (cellH * r) + yOffset;
this->tableData[r * COLUMNS + c]->dims.height = cellH;
this->tableData[r * COLUMNS + c]->dims.width =
this->colWidths[c] * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN);
this->colWidths[c] * (CHR_WIDTH(this->decor.textSize) + CELL_MARGIN);
memset(this->tableData[r * COLUMNS + c]->value, 0,
this->tableData[r * COLUMNS + c]->bufferSize);
xOffset +=
this->colWidths[c] * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN);
this->colWidths[c] * (CHR_WIDTH(this->decor.textSize) + CELL_MARGIN);
#ifdef DEBUG
this->tableData[r * COLUMNS + c]->drawBox();
+1 -1
View File
@@ -18,7 +18,7 @@ struct UITable : UIElement {
int *colWidths;
UIString **tableData;
UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, int nR,
UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations decor, int nR,
int nC, int *cWidth, char *t);
// void Update(StandingLine standings[5]);