From 7d69ffe85cea4d25c408627e7a63a1cb20b49bb6 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 13 Apr 2025 23:34:33 +0100 Subject: [PATCH] Dynamic Tables --- lib/cursesAtHome/src/UITable.cpp | 37 +++++++++++++---------- lib/cursesAtHome/src/UITable.h | 8 +++-- src/data.h | 11 +++++-- src/main.cpp | 51 +++++++++++++++++++++----------- src/ui.cpp | 4 +-- src/ui.h | 1 + 6 files changed, 71 insertions(+), 41 deletions(-) diff --git a/lib/cursesAtHome/src/UITable.cpp b/lib/cursesAtHome/src/UITable.cpp index 1b8a82e..a9bd6f2 100644 --- a/lib/cursesAtHome/src/UITable.cpp +++ b/lib/cursesAtHome/src/UITable.cpp @@ -7,11 +7,14 @@ #define DEBUG UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, - char *title) + int nRows, int nCols, int *colWidths, char *title) : UIElement(gfx, dims, decor, title) { this->type = TABLE; - this->tableData = new UIString*[ROWS * COLUMNS]; - + this->nRows = nRows; + this->nColumns = nCols; + this->tableData = new UIString *[this->nRows * this->nColumns]; + this->colWidths = colWidths; + // Default decoration for every cell this->decor->textSize = 2; @@ -27,11 +30,16 @@ UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN; // Initialize the table dimensions + //// Calculate the height this->dims.height = ROWS * cellH + this->getTitleAreaHeight() + DEFAULT_MARGIN + DEFAULT_BORDER_THICKNESS; - this->dims.width = - (DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN) * 2 + - (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN) * (3 + 18 + 12); + + //// Calculate the width + for (int c = 0; c < this->nColumns; c++) { + this->dims.width += + (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN) * this->colWidths[c]; + } + this->dims.width += (DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN) * 2; } uint16_t UITable::getContentAreaHeight() { @@ -39,16 +47,11 @@ uint16_t UITable::getContentAreaHeight() { this->getTitleAreaHeight(); } -/* Very shitty hardcoded function to get this going, only works for th - * relative - * POS NAME DELTA - * 3 | 24 | 5 +/* + * This was hardcoded for the relative and had a warning for that but + * I recon its dynamic enough for now */ void UITable::setup() { - // Define the cell dimensions - int tableCellW[] = {3 * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN), - 18 * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN), - 12 * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN)}; int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN; // Initialize the cells @@ -61,11 +64,13 @@ void UITable::setup() { 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 = tableCellW[c]; + this->tableData[r * COLUMNS + c]->dims.width = + 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 += tableCellW[c]; + xOffset += + this->colWidths[c] * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN); #ifdef DEBUG this->tableData[r * COLUMNS + c]->drawBox(); diff --git a/lib/cursesAtHome/src/UITable.h b/lib/cursesAtHome/src/UITable.h index c43dcc2..8098358 100644 --- a/lib/cursesAtHome/src/UITable.h +++ b/lib/cursesAtHome/src/UITable.h @@ -13,11 +13,13 @@ */ struct UITable : UIElement { // will hold rows * columns UIStrings to fill the table - int rows = ROWS; - int columns = COLUMNS; + int nRows = 0; + int nColumns = 0; + int *colWidths; UIString **tableData; - UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, char *t); + UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, int nR, + int nC, int *cWidth, char *t); // void Update(StandingLine standings[5]); void setup(); diff --git a/src/data.h b/src/data.h index 11b505c..a24c8b2 100644 --- a/src/data.h +++ b/src/data.h @@ -11,14 +11,17 @@ const int speedLen = 5; const int gearLen = 3; const int rpmLen = 6; const int lapNumberLen = 5; +const int DeltaToBestLapLen = 6; +const int bestLapTimeLen = 10; const int currLapTimeLen = 10; const int lastLapTimeLen = 10; -const int DriverNameLen = 24; -const int TimeBehindStringLen = 16; -const int LapStringLen = 4; 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]; @@ -32,6 +35,8 @@ struct DataPacket { char gear[gearLen]; char rpm[rpmLen]; char LapNumber[lapNumberLen]; + char DeltaToBestLap[DeltaToBestLapLen]; + char bestLapTime[bestLapTimeLen]; char currLapTime[currLapTimeLen]; char lastLapTime[lastLapTimeLen]; char FuelEst[FuelEstLen]; diff --git a/src/main.cpp b/src/main.cpp index e997173..e8c624b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,6 +48,7 @@ DashUI *ui = new DashUI(); UIDecorations *rpmTextDecor = new UIDecorations(); UIDecorations *speedTextDecor = new UIDecorations(); UIDecorations *gearTextDecor = new UIDecorations(); +UIDecorations *lapDeltaDecor = new UIDecorations(); UIDecorations *bestLapDecor = new UIDecorations(); UIDecorations *lastLapDecor = new UIDecorations(); UIDecorations *curLapDecor = new UIDecorations(); @@ -60,6 +61,8 @@ UIString speedText(gfx, UIDimensions(0, 0, 0, 0), speedTextDecor, (char *)"Speed"); UIString gearText(gfx, UIDimensions(0, 0, 0, 0), rpmTextDecor, (char *)"Gear"); +UIString lapDelta(gfx, UIDimensions(0, 0, 0, 0), lapDeltaDecor, + (char *)"Delta"); UIString bestLap(gfx, UIDimensions(0, 0, 0, 0), bestLapDecor, (char *)"Best Lap"); UIString lastLap(gfx, UIDimensions(0, 0, 0, 0), lastLapDecor, @@ -69,8 +72,6 @@ UIString fuelTank(gfx, UIDimensions(0, 0, 0, 0), fuelTankDecor, (char *)"Fuel Tank"); UIString fuelConsumption(gfx, UIDimensions(0, 0, 0, 0), fuelConsumptionDecor, (char *)"Fuel Consumption"); -UITable relative(gfx, UIDimensions(250, 250, 0, 0), relativeDecor, - (char *)"Relative"); void setup() { psramInit(); @@ -86,14 +87,20 @@ void setup() { initialDisplaySetup(gfx); // Setup the relative + // Define the width of the columns in number of chars + int *colW = (int *)malloc(sizeof(int) * 3); + colW[0] = 3; + colW[1] = 18; + colW[2] = 9; + UITable *relative = + new UITable(gfx, UIDimensions(250, 250, 0, 0), relativeDecor, 5, 3, colW, + (char *)"Relative"); relativeDecor->textSize = 2; - relative.dims.x = gfx->width() - relative.dims.width; - relative.dims.y = gfx->height() - relative.dims.height; - // relative.dims.x = 250; - // relative.dims.y = 250; - relative.setup(); - relative.drawBox(); - ui->relative = &relative; + relative->dims.x = gfx->width() - relative->dims.width; + relative->dims.y = gfx->height() - relative->dims.height; + relative->setup(); + relative->drawBox(); + ui->relative = relative; // Setup the rpmText box rpmTextDecor->textSize = 7; @@ -103,7 +110,7 @@ void setup() { rpmText.horizontalCenter(); ui->digiTacho = &rpmText; ui->digiTacho->drawBox(); - ui->digiTacho->Update("12345"); + ui->digiTacho->Update("-----"); // Setup the speedText box speedTextDecor->textSize = 4; @@ -113,7 +120,7 @@ void setup() { speedText.placeRight(&rpmText); ui->digiSpeedo = &speedText; ui->digiSpeedo->drawBox(); - ui->digiSpeedo->Update("253"); + ui->digiSpeedo->Update("---"); // Setup the gear text box gearTextDecor->textSize = 7; @@ -124,7 +131,17 @@ void setup() { gearText.dims.y = rpmText.dims.height + 5; ui->digiGear = &gearText; ui->digiGear->drawBox(); - ui->digiGear->Update("3"); + ui->digiGear->Update("-"); + + // Setup the lap delta box + lapDeltaDecor->textSize = 3; + lapDelta.dims.height = + calculateHeight(lapDeltaDecor->titleSize, lapDeltaDecor->textSize, 1); + lapDelta.dims.width = calculateWidth(lapDeltaDecor->textSize, 6); + lapDelta.placeBelow(&gearText); + ui->lapDelta = &lapDelta; + ui->lapDelta->drawBox(); + ui->lapDelta->Update("--.-"); // Setup the best lap box bestLapDecor->textSize = 3; @@ -133,7 +150,7 @@ void setup() { bestLap.dims.width = calculateWidth(bestLapDecor->textSize, 8); ui->bestLap = &bestLap; ui->bestLap->drawBox(); - ui->bestLap->Update("01:24.39"); + ui->bestLap->Update("--:--.--"); // Setup the last lap box lastLapDecor->textSize = 3; @@ -143,7 +160,7 @@ void setup() { lastLap.placeBelow(&bestLap); ui->lastLap = &lastLap; ui->lastLap->drawBox(); - ui->lastLap->Update("01:24.87"); + ui->lastLap->Update("--:--.--"); // Setup the cur lap box curLapDecor->textSize = 3; @@ -153,7 +170,7 @@ void setup() { curLap.placeBelow(&lastLap); ui->curLap = &curLap; ui->curLap->drawBox(); - ui->curLap->Update("00:54.21"); + ui->curLap->Update("--:--.--"); // Fuel tank fuelTankDecor->textSize = 3; @@ -163,7 +180,7 @@ void setup() { fuelTank.placeBelow(&curLap); ui->fuelTank = &fuelTank; ui->fuelTank->drawBox(); - ui->fuelTank->Update("123 / 320"); + ui->fuelTank->Update("--- / ---"); // Fuel consumption fuelConsumptionDecor->textSize = 3; @@ -174,7 +191,7 @@ void setup() { fuelConsumption.placeBelow(&fuelTank); ui->fuelConsumption = &fuelConsumption; ui->fuelConsumption->drawBox(); - ui->fuelConsumption->Update("10.4 | 11.2"); + ui->fuelConsumption->Update("--.- | --.-"); // const char *words[] = {"hel", "wor", "why", "is", "thi", "hap", "to", // "me"}; diff --git a/src/ui.cpp b/src/ui.cpp index ff4bde3..2278180 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1,6 +1,4 @@ #include "ui.h" - -#include "UIDecorations.h" #include "data.h" DashUI::DashUI() {} @@ -9,6 +7,8 @@ void DashUI::Update(DataPacket *p) { this->digiSpeedo->Update(p->speed); this->digiTacho->Update(p->rpm); this->digiGear->Update(p->gear); + this->lapDelta->Update(p->DeltaToBestLap); + this->bestLap->Update(p->bestLapTime); this->curLap->Update(p->currLapTime); this->lastLap->Update(p->lastLapTime); this->fuelConsumption->Update(p->FuelEst); diff --git a/src/ui.h b/src/ui.h index ae431b6..56b00e2 100644 --- a/src/ui.h +++ b/src/ui.h @@ -10,6 +10,7 @@ struct DashUI { UIString *digiTacho; UIString *digiSpeedo; UIString *digiGear; + UIString *lapDelta; UIString *bestLap; UIString *lastLap; UIString *curLap;