snapshot
This commit is contained in:
202
sketches/doorbell-touch-esp32-32e/DisplayManager.cpp
Normal file
202
sketches/doorbell-touch-esp32-32e/DisplayManager.cpp
Normal file
@@ -0,0 +1,202 @@
|
||||
#include "DisplayManager.h"
|
||||
#include "Config.h"
|
||||
|
||||
void DisplayManager::begin() {
|
||||
pinMode(PIN_LCD_BL, OUTPUT);
|
||||
setBacklight(true);
|
||||
|
||||
_tft.init();
|
||||
_tft.setRotation(1); // landscape: 480x320
|
||||
_tft.fillScreen(COL_BLACK);
|
||||
}
|
||||
|
||||
void DisplayManager::setBacklight(bool on) {
|
||||
digitalWrite(PIN_LCD_BL, on ? HIGH : LOW);
|
||||
}
|
||||
|
||||
TouchEvent DisplayManager::readTouch() {
|
||||
TouchEvent evt;
|
||||
uint16_t x, y;
|
||||
if (_tft.getTouch(&x, &y)) {
|
||||
evt.pressed = true;
|
||||
evt.x = x;
|
||||
evt.y = y;
|
||||
}
|
||||
return evt;
|
||||
}
|
||||
|
||||
void DisplayManager::render(const ScreenState& state) {
|
||||
// Detect screen change → force full redraw
|
||||
if (state.screen != _lastScreen) {
|
||||
_needsFullRedraw = true;
|
||||
_lastScreen = state.screen;
|
||||
}
|
||||
|
||||
switch (state.screen) {
|
||||
case ScreenID::BOOT_SPLASH:
|
||||
if (_needsFullRedraw) drawBootSplash(state);
|
||||
break;
|
||||
case ScreenID::WIFI_CONNECTING:
|
||||
if (_needsFullRedraw) drawWifiConnecting();
|
||||
break;
|
||||
case ScreenID::WIFI_CONNECTED:
|
||||
if (_needsFullRedraw) drawWifiConnected(state);
|
||||
break;
|
||||
case ScreenID::WIFI_FAILED:
|
||||
if (_needsFullRedraw) drawWifiFailed();
|
||||
break;
|
||||
case ScreenID::ALERT:
|
||||
// Alert redraws every blink cycle
|
||||
if (_needsFullRedraw || state.blinkPhase != _lastBlink) {
|
||||
drawAlertScreen(state);
|
||||
_lastBlink = state.blinkPhase;
|
||||
}
|
||||
break;
|
||||
case ScreenID::STATUS:
|
||||
if (_needsFullRedraw) drawStatusScreen(state);
|
||||
break;
|
||||
case ScreenID::OFF:
|
||||
break;
|
||||
}
|
||||
|
||||
_needsFullRedraw = false;
|
||||
}
|
||||
|
||||
// ----- Helpers -----
|
||||
|
||||
void DisplayManager::drawCentered(const char* txt, int y, int sz, uint16_t col) {
|
||||
_tft.setTextSize(sz);
|
||||
_tft.setTextColor(col);
|
||||
int w = strlen(txt) * 6 * sz;
|
||||
_tft.setCursor(max(0, (SCREEN_WIDTH - w) / 2), y);
|
||||
_tft.print(txt);
|
||||
}
|
||||
|
||||
void DisplayManager::drawInfoLine(int x, int y, uint16_t col, const char* text) {
|
||||
_tft.setTextSize(1);
|
||||
_tft.setTextColor(col);
|
||||
_tft.setCursor(x, y);
|
||||
_tft.print(text);
|
||||
}
|
||||
|
||||
void DisplayManager::drawHeaderBar(uint16_t col, const char* label, const char* timeStr) {
|
||||
_tft.setTextSize(2);
|
||||
_tft.setTextColor(col);
|
||||
_tft.setCursor(8, 8);
|
||||
_tft.print(label);
|
||||
|
||||
int tw = strlen(timeStr) * 12;
|
||||
_tft.setCursor(SCREEN_WIDTH - tw - 8, 8);
|
||||
_tft.print(timeStr);
|
||||
}
|
||||
|
||||
// ----- Screens -----
|
||||
|
||||
void DisplayManager::drawBootSplash(const ScreenState& s) {
|
||||
_tft.fillScreen(COL_BLACK);
|
||||
drawCentered("KLUBHAUS", 60, 4, COL_NEON_TEAL);
|
||||
drawCentered("ALERT", 110, 4, COL_HOT_FUCHSIA);
|
||||
drawCentered("v5.0 E32R35T", 180, 2, COL_DARK_GRAY);
|
||||
if (s.debugMode) {
|
||||
drawCentered("DEBUG MODE", 210, 2, COL_YELLOW);
|
||||
}
|
||||
}
|
||||
|
||||
void DisplayManager::drawWifiConnecting() {
|
||||
_tft.fillScreen(COL_BLACK);
|
||||
drawCentered("Connecting", 130, 3, COL_NEON_TEAL);
|
||||
drawCentered("to WiFi...", 170, 3, COL_NEON_TEAL);
|
||||
}
|
||||
|
||||
void DisplayManager::drawWifiConnected(const ScreenState& s) {
|
||||
_tft.fillScreen(COL_BLACK);
|
||||
drawCentered("Connected!", 100, 3, COL_GREEN);
|
||||
drawCentered(s.wifiSSID, 150, 2, COL_WHITE);
|
||||
drawCentered(s.wifiIP, 180, 2, COL_WHITE);
|
||||
}
|
||||
|
||||
void DisplayManager::drawWifiFailed() {
|
||||
_tft.fillScreen(COL_BLACK);
|
||||
drawCentered("WiFi FAILED", 140, 3, COL_RED);
|
||||
}
|
||||
|
||||
void DisplayManager::drawAlertScreen(const ScreenState& s) {
|
||||
uint16_t bg = s.blinkPhase ? COL_NEON_TEAL : COL_HOT_FUCHSIA;
|
||||
uint16_t fg = s.blinkPhase ? COL_BLACK : COL_WHITE;
|
||||
|
||||
_tft.fillScreen(bg);
|
||||
drawHeaderBar(fg, "ALERT", s.timeString);
|
||||
|
||||
// Scale text to fit 480px wide screen
|
||||
int sz = 5;
|
||||
int len = strlen(s.alertMessage);
|
||||
if (len > 10) sz = 4;
|
||||
if (len > 18) sz = 3;
|
||||
if (len > 30) sz = 2;
|
||||
|
||||
if (len > 12) {
|
||||
// Two-line split
|
||||
String msg(s.alertMessage);
|
||||
int mid = len / 2;
|
||||
int sp = msg.lastIndexOf(' ', mid);
|
||||
if (sp < 0) sp = mid;
|
||||
String l1 = msg.substring(0, sp);
|
||||
String l2 = msg.substring(sp + 1);
|
||||
int lh = 8 * sz + 8;
|
||||
int y1 = (SCREEN_HEIGHT - lh * 2) / 2;
|
||||
drawCentered(l1.c_str(), y1, sz, fg);
|
||||
drawCentered(l2.c_str(), y1 + lh, sz, fg);
|
||||
} else {
|
||||
drawCentered(s.alertMessage, (SCREEN_HEIGHT - 8 * sz) / 2, sz, fg);
|
||||
}
|
||||
|
||||
drawCentered("TAP TO SILENCE", SCREEN_HEIGHT - 25, 2, fg);
|
||||
}
|
||||
|
||||
void DisplayManager::drawStatusScreen(const ScreenState& s) {
|
||||
_tft.fillScreen(COL_BLACK);
|
||||
drawHeaderBar(COL_MINT, "KLUBHAUS", s.timeString);
|
||||
|
||||
drawCentered("MONITORING", 60, 3, COL_WHITE);
|
||||
|
||||
char buf[80];
|
||||
int y = 110;
|
||||
int sp = 22;
|
||||
int x = 20;
|
||||
|
||||
snprintf(buf, sizeof(buf), "WiFi: %s (%ddBm)",
|
||||
s.wifiConnected ? s.wifiSSID : "DOWN", s.wifiRSSI);
|
||||
drawInfoLine(x, y, COL_WHITE, buf); y += sp;
|
||||
|
||||
snprintf(buf, sizeof(buf), "IP: %s",
|
||||
s.wifiConnected ? s.wifiIP : "---");
|
||||
drawInfoLine(x, y, COL_WHITE, buf); y += sp;
|
||||
|
||||
snprintf(buf, sizeof(buf), "Up: %lu min Heap: %lu KB",
|
||||
s.uptimeMinutes, s.freeHeapKB);
|
||||
drawInfoLine(x, y, COL_WHITE, buf); y += sp;
|
||||
|
||||
snprintf(buf, sizeof(buf), "NTP: %s UTC",
|
||||
s.ntpSynced ? s.timeString : "not synced");
|
||||
drawInfoLine(x, y, COL_WHITE, buf); y += sp;
|
||||
|
||||
const char* stName = s.deviceState == DeviceState::SILENT ? "SILENT" :
|
||||
s.deviceState == DeviceState::ALERTING ? "ALERTING" : "WAKE";
|
||||
snprintf(buf, sizeof(buf), "State: %s Net: %s",
|
||||
stName, s.networkOK ? "OK" : "FAIL");
|
||||
uint16_t stCol = s.deviceState == DeviceState::ALERTING ? COL_RED :
|
||||
s.deviceState == DeviceState::SILENT ? COL_GREEN : COL_NEON_TEAL;
|
||||
drawInfoLine(x, y, stCol, buf); y += sp;
|
||||
|
||||
if (strlen(s.alertMessage) > 0) {
|
||||
snprintf(buf, sizeof(buf), "Last: %.40s", s.alertMessage);
|
||||
drawInfoLine(x, y, COL_DARK_GRAY, buf); y += sp;
|
||||
}
|
||||
|
||||
if (s.debugMode) {
|
||||
drawInfoLine(x, y, COL_YELLOW, "DEBUG MODE - _test topics");
|
||||
}
|
||||
|
||||
drawCentered("tap to dismiss", SCREEN_HEIGHT - 18, 1, COL_DARK_GRAY);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user