Files
klubhaus-doorbell/sketches/doorbell-touch-esp32-32e/Dashboard.cpp
2026-02-16 19:05:13 -08:00

170 lines
4.6 KiB
C++

#include "Dashboard.h"
// 16-bit color helpers
#define COL_BG 0x1082 // dark charcoal
#define COL_BAR 0x2104 // slightly lighter
#define COL_RED 0xF800
#define COL_ORANGE 0xFBE0
#define COL_GREEN 0x07E0
#define COL_CYAN 0x07FF
#define COL_BLUE 0x001F
#define COL_PURPLE 0x780F
#define COL_YELLOW 0xFFE0
#define COL_WHITE 0xFFFF
#define COL_GRAY 0x8410
#define COL_DARK_TILE 0x18E3
// Tile color themes
static const uint16_t tileBG[] = {
COL_RED, // LAST_ALERT — red
COL_ORANGE, // STATS — orange
COL_CYAN, // NETWORK — cyan
COL_PURPLE, // MUTE — purple
COL_DARK_TILE, // HISTORY — dark
COL_DARK_TILE // SYSTEM — dark
};
static const uint16_t tileFG[] = {
COL_WHITE, COL_WHITE, 0x0000, COL_WHITE, COL_WHITE, COL_WHITE
};
Dashboard::Dashboard(TFT_eSPI& tft)
: _tft(tft), _sprite(&tft)
{
// Initialize tile metadata
_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];
_tiles[i].fgColor = tileFG[i];
}
}
void Dashboard::begin() {
// Create sprite sized to one tile (reused for each)
_sprite.createSprite(TILE_W, TILE_H);
_sprite.setTextDatum(MC_DATUM);
_tft.fillScreen(COL_BG);
}
void Dashboard::tilePosition(TileID id, int& x, int& y) {
int col = id % DASH_COLS;
int row = id / DASH_COLS;
x = DASH_MARGIN + col * (TILE_W + DASH_MARGIN);
y = DASH_TOP_BAR + DASH_MARGIN + row * (TILE_H + DASH_MARGIN);
}
void Dashboard::drawTile(TileID id) {
TileData& t = _tiles[id];
int tx, ty;
tilePosition(id, tx, ty);
// Draw into sprite (off-screen)
_sprite.fillSprite(t.bgColor);
// Rounded corner effect — draw border pixels
_sprite.drawRoundRect(0, 0, TILE_W, TILE_H, 8, COL_GRAY);
// Icon — large character at top
_sprite.setTextColor(t.fgColor, t.bgColor);
_sprite.setTextFont(4);
_sprite.setTextSize(2);
_sprite.setTextDatum(TC_DATUM);
_sprite.drawString(t.icon, TILE_W / 2, 8);
// Label — below icon
_sprite.setTextSize(1);
_sprite.setTextFont(2);
_sprite.setTextDatum(MC_DATUM);
_sprite.drawString(t.label, TILE_W / 2, TILE_H / 2 + 5);
// Value — bottom area
_sprite.setTextFont(2);
_sprite.setTextDatum(BC_DATUM);
_sprite.drawString(t.value, TILE_W / 2, TILE_H - 25);
// Sub text — very bottom
if (strlen(t.sub) > 0) {
_sprite.setTextFont(1);
_sprite.setTextDatum(BC_DATUM);
_sprite.drawString(t.sub, TILE_W / 2, TILE_H - 8);
}
// Push sprite to screen in one operation — no flicker
_sprite.pushSprite(tx, ty);
t.dirty = false;
}
void Dashboard::drawTopBar(const char* time, int rssi, bool wifiOk) {
_tft.fillRect(0, 0, 480, DASH_TOP_BAR, COL_BAR);
_tft.setTextColor(COL_WHITE, COL_BAR);
_tft.setTextFont(4);
_tft.setTextSize(1);
// Title — left
_tft.setTextDatum(ML_DATUM);
_tft.drawString("KLUBHAUS ALERT", DASH_MARGIN, DASH_TOP_BAR / 2);
// Time — right
_tft.setTextDatum(MR_DATUM);
_tft.drawString(time, 470, DASH_TOP_BAR / 2);
// WiFi indicator — signal bars
int bars = 0;
if (wifiOk) {
if (rssi > -50) bars = 4;
else if (rssi > -60) bars = 3;
else if (rssi > -70) bars = 2;
else bars = 1;
}
int barX = 370;
int barW = 6;
int barGap = 3;
for (int i = 0; i < 4; i++) {
int barH = 6 + i * 5;
int barY = DASH_TOP_BAR - 8 - barH;
uint16_t col = (i < bars) ? COL_GREEN : COL_GRAY;
_tft.fillRect(barX + i * (barW + barGap), barY, barW, barH, col);
}
}
void Dashboard::drawAll() {
drawTopBar("--:--", 0, false);
for (int i = 0; i < TILE_COUNT; i++) {
drawTile((TileID)i);
}
}
void Dashboard::updateTile(TileID id, const char* value, const char* sub) {
TileData& t = _tiles[id];
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); // immediate redraw of just this tile
}
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;
}
}
return -1;
}