From d0ab4a8da720fa5d2987b0411876dc0f526b32ff Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Mon, 16 Feb 2026 12:38:14 -0800 Subject: [PATCH] refactor: parameterize Dashboard for multi-target - TFT_eSPI / TFT_eSprite -> Gfx / GfxSprite typedefs - Hard-coded 480x320 -> SCREEN_WIDTH / SCREEN_HEIGHT from BoardConfig - Top bar, WiFi bars, time position all scale to any resolution - Zero behavioral change on E32R35T (Gfx = TFT_eSPI typedef) --- .../doorbell-touch-esp32-32e/Dashboard.cpp | 83 ++++++++++--------- sketches/doorbell-touch-esp32-32e/Dashboard.h | 41 ++++----- 2 files changed, 63 insertions(+), 61 deletions(-) diff --git a/sketches/doorbell-touch-esp32-32e/Dashboard.cpp b/sketches/doorbell-touch-esp32-32e/Dashboard.cpp index 13554f0..bae23a9 100644 --- a/sketches/doorbell-touch-esp32-32e/Dashboard.cpp +++ b/sketches/doorbell-touch-esp32-32e/Dashboard.cpp @@ -1,15 +1,15 @@ #include "Dashboard.h" -#define COL_BG 0x1082 -#define COL_BAR 0x2104 -#define COL_RED 0xF800 -#define COL_ORANGE 0xFBE0 -#define COL_GREEN 0x07E0 -#define COL_CYAN 0x07FF -#define COL_PURPLE 0x780F -#define COL_WHITE 0xFFFF -#define COL_GRAY 0x8410 -#define COL_DARK_TILE 0x18E3 +#define COL_BG 0x1082 +#define COL_BAR 0x2104 +#define COL_RED 0xF800 +#define COL_ORANGE 0xFBE0 +#define COL_GREEN 0x07E0 +#define COL_CYAN 0x07FF +#define COL_PURPLE 0x780F +#define COL_WHITE 0xFFFF +#define COL_GRAY 0x8410 +#define COL_DARK_TILE 0x18E3 static const uint16_t tileBG[] = { COL_RED, COL_ORANGE, COL_CYAN, COL_PURPLE, COL_DARK_TILE, COL_DARK_TILE @@ -18,15 +18,15 @@ static const uint16_t tileFG[] = { COL_WHITE, COL_WHITE, 0x0000, COL_WHITE, COL_WHITE, COL_WHITE }; -Dashboard::Dashboard(TFT_eSPI& tft) +Dashboard::Dashboard(Gfx& tft) : _tft(tft), _sprite(&tft) { - _tiles[TILE_LAST_ALERT] = { "!", "LAST ALERT", "none", "", 0, 0, true }; - _tiles[TILE_STATS] = { "#", "TODAY", "0 alerts", "", 0, 0, true }; - _tiles[TILE_NETWORK] = { "~", "NETWORK", "---", "", 0, 0, true }; - _tiles[TILE_MUTE] = { "M", "MUTE", "OFF", "", 0, 0, true }; - _tiles[TILE_HISTORY] = { ">", "HISTORY", "tap to view", "", 0, 0, true }; - _tiles[TILE_SYSTEM] = { "*", "SYSTEM", "---", "", 0, 0, true }; + _tiles[TILE_LAST_ALERT] = { "!", "LAST ALERT", "none", "", 0, 0, true }; + _tiles[TILE_STATS] = { "#", "TODAY", "0 alerts", "", 0, 0, true }; + _tiles[TILE_NETWORK] = { "~", "NETWORK", "---", "", 0, 0, true }; + _tiles[TILE_MUTE] = { "M", "MUTE", "OFF", "", 0, 0, true }; + _tiles[TILE_HISTORY] = { ">", "HISTORY", "tap to view", "", 0, 0, true }; + _tiles[TILE_SYSTEM] = { "*", "SYSTEM", "---", "", 0, 0, true }; for (int i = 0; i < TILE_COUNT; i++) { _tiles[i].bgColor = tileBG[i]; @@ -59,9 +59,9 @@ void Dashboard::drawTile(TileID id) { int tx, ty; tilePosition(id, tx, ty); - _sprite.fillSprite(COL_BG); // screen bg in corners - _sprite.fillRoundRect(0, 0, TILE_W, TILE_H, 8, t.bgColor); // rounded tile fill - _sprite.drawRoundRect(0, 0, TILE_W, TILE_H, 8, COL_GRAY); // border + _sprite.fillSprite(COL_BG); + _sprite.fillRoundRect(0, 0, TILE_W, TILE_H, 8, t.bgColor); + _sprite.drawRoundRect(0, 0, TILE_W, TILE_H, 8, COL_GRAY); _sprite.setTextColor(t.fgColor, t.bgColor); _sprite.setTextFont(4); @@ -89,7 +89,7 @@ void Dashboard::drawTile(TileID id) { } void Dashboard::drawTopBar(const char* time, int rssi, bool wifiOk) { - _tft.fillRect(0, 0, 480, DASH_TOP_BAR, COL_BAR); + _tft.fillRect(0, 0, SCREEN_WIDTH, DASH_TOP_BAR, COL_BAR); _tft.setTextColor(COL_WHITE, COL_BAR); _tft.setTextFont(4); _tft.setTextSize(1); @@ -98,16 +98,16 @@ void Dashboard::drawTopBar(const char* time, int rssi, bool wifiOk) { _tft.drawString("KLUBHAUS ALERT", DASH_MARGIN, DASH_TOP_BAR / 2); _tft.setTextDatum(MR_DATUM); - _tft.drawString(time, 470, DASH_TOP_BAR / 2); + _tft.drawString(time, SCREEN_WIDTH - 10, DASH_TOP_BAR / 2); int bars = 0; if (wifiOk) { - if (rssi > -50) bars = 4; + if (rssi > -50) bars = 4; else if (rssi > -60) bars = 3; else if (rssi > -70) bars = 2; - else bars = 1; + else bars = 1; } - int barX = 370, barW = 6, barGap = 3; + int barX = SCREEN_WIDTH - 110, barW = 6, barGap = 3; for (int i = 0; i < 4; i++) { int barH = 6 + i * 5; int barY = DASH_TOP_BAR - 8 - barH; @@ -126,8 +126,12 @@ void Dashboard::updateTile(TileID id, const char* value, const char* sub) { if (sub && strcmp(t.sub, sub) != 0) changed = true; if (!changed && !t.dirty) return; - strncpy(t.value, value, 31); t.value[31] = '\0'; - if (sub) { strncpy(t.sub, sub, 31); t.sub[31] = '\0'; } + strncpy(t.value, value, 31); + t.value[31] = '\0'; + if (sub) { + strncpy(t.sub, sub, 31); + t.sub[31] = '\0'; + } t.dirty = true; drawTile(id); } @@ -136,32 +140,30 @@ int Dashboard::handleTouch(int x, int y) { for (int i = 0; i < TILE_COUNT; i++) { int tx, ty; tilePosition((TileID)i, tx, ty); - if (x >= tx && x < tx + TILE_W && y >= ty && y < ty + TILE_H) - return i; + if (x >= tx && x < tx + TILE_W && y >= ty && y < ty + TILE_H) return i; } return -1; } void Dashboard::refreshFromState(const ScreenState& state) { - bool barChanged = (strcmp(_barTime, state.timeString) != 0) || - (_barRSSI != state.wifiRSSI) || - (_barWifiOk != state.wifiConnected); + bool barChanged = (strcmp(_barTime, state.timeString) != 0) + || (_barRSSI != state.wifiRSSI) + || (_barWifiOk != state.wifiConnected); if (barChanged) { drawTopBar(state.timeString, state.wifiRSSI, state.wifiConnected); } if (state.alertHistoryCount > 0) { - updateTile(TILE_LAST_ALERT, - state.alertHistory[0].message, - state.alertHistory[0].timestamp); + updateTile(TILE_LAST_ALERT, state.alertHistory[0].message, + state.alertHistory[0].timestamp); } else { updateTile(TILE_LAST_ALERT, "none", ""); } char statsBuf[32]; snprintf(statsBuf, sizeof(statsBuf), "%d alert%s", - state.alertHistoryCount, - state.alertHistoryCount == 1 ? "" : "s"); + state.alertHistoryCount, + state.alertHistoryCount == 1 ? "" : "s"); updateTile(TILE_STATS, statsBuf, "this session"); if (state.wifiConnected) { @@ -177,10 +179,10 @@ void Dashboard::refreshFromState(const ScreenState& state) { if (state.alertHistoryCount > 1) { char histBuf[48]; snprintf(histBuf, sizeof(histBuf), "%s %.20s", - state.alertHistory[1].timestamp, - state.alertHistory[1].message); + state.alertHistory[1].timestamp, + state.alertHistory[1].message); const char* sub = (state.alertHistoryCount > 2) - ? state.alertHistory[2].message : ""; + ? state.alertHistory[2].message : ""; updateTile(TILE_HISTORY, histBuf, sub); } else { updateTile(TILE_HISTORY, "no history", ""); @@ -192,4 +194,3 @@ void Dashboard::refreshFromState(const ScreenState& state) { snprintf(uptimeBuf, sizeof(uptimeBuf), "up %lum", state.uptimeMinutes); updateTile(TILE_SYSTEM, heapBuf, uptimeBuf); } - diff --git a/sketches/doorbell-touch-esp32-32e/Dashboard.h b/sketches/doorbell-touch-esp32-32e/Dashboard.h index 9bf1332..133ca26 100644 --- a/sketches/doorbell-touch-esp32-32e/Dashboard.h +++ b/sketches/doorbell-touch-esp32-32e/Dashboard.h @@ -1,13 +1,15 @@ #pragma once -#include + +#include "DisplayDriver.h" #include "ScreenData.h" -#define DASH_COLS 3 -#define DASH_ROWS 2 -#define DASH_MARGIN 8 -#define DASH_TOP_BAR 40 -#define TILE_W ((480 - (DASH_COLS + 1) * DASH_MARGIN) / DASH_COLS) -#define TILE_H ((320 - DASH_TOP_BAR - (DASH_ROWS + 1) * DASH_MARGIN) / DASH_ROWS) +#define DASH_COLS 3 +#define DASH_ROWS 2 +#define DASH_MARGIN 8 +#define DASH_TOP_BAR 40 + +#define TILE_W ((SCREEN_WIDTH - (DASH_COLS + 1) * DASH_MARGIN) / DASH_COLS) +#define TILE_H ((SCREEN_HEIGHT - DASH_TOP_BAR - (DASH_ROWS + 1) * DASH_MARGIN) / DASH_ROWS) enum TileID : uint8_t { TILE_LAST_ALERT = 0, @@ -22,16 +24,16 @@ enum TileID : uint8_t { struct TileData { const char* icon; const char* label; - char value[32]; - char sub[32]; - uint16_t bgColor; - uint16_t fgColor; - bool dirty; + char value[32]; + char sub[32]; + uint16_t bgColor; + uint16_t fgColor; + bool dirty; }; class Dashboard { public: - Dashboard(TFT_eSPI& tft); + Dashboard(Gfx& tft); void begin(); void drawAll(); @@ -41,15 +43,14 @@ public: void refreshFromState(const ScreenState& state); private: - TFT_eSPI& _tft; - TFT_eSprite _sprite; - TileData _tiles[TILE_COUNT]; + Gfx& _tft; + GfxSprite _sprite; + TileData _tiles[TILE_COUNT]; - char _barTime[12] = ""; - int _barRSSI = 0; - bool _barWifiOk = false; + char _barTime[12] = ""; + int _barRSSI = 0; + bool _barWifiOk = false; void drawTile(TileID id); void tilePosition(TileID id, int& x, int& y); }; -