refactor(display): split render into screen-specific draw methods
This commit is contained in:
@@ -1,20 +1,3 @@
|
||||
#include "LovyanPins.h"
|
||||
#include "board_config.h"
|
||||
#include <Arduino.h>
|
||||
#include <ESP_IOExpander_Library.h>
|
||||
|
||||
// Global display instance
|
||||
static LGFX* _gfx = nullptr;
|
||||
static ESP_IOExpander* _expander = nullptr;
|
||||
|
||||
// Forward declarations
|
||||
void initExpander();
|
||||
void initDisplay();
|
||||
|
||||
// ── Expander initialization (from Westcott) ──
|
||||
void initExpander()
|
||||
{
|
||||
Serial.println("IO expander init...");
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <ESP_IOExpander_Library.h>
|
||||
@@ -34,11 +17,11 @@ static void initDisplay();
|
||||
static constexpr int DISP_W = 800;
|
||||
static constexpr int DISP_H = 480;
|
||||
|
||||
// ── Expander initialization ──
|
||||
// ── Expander initialization (from Westcott) ──
|
||||
static void initExpander() {
|
||||
Serial.println("IO expander init...");
|
||||
|
||||
// Initialize I2C for expander
|
||||
// Initialize I2C for expander (port 0)
|
||||
Wire.begin(TOUCH_SDA, TOUCH_SCL);
|
||||
|
||||
_expander = new ESP_IOExpander_CH422G(
|
||||
@@ -51,7 +34,7 @@ static void initExpander() {
|
||||
// Set all pins to output
|
||||
_expander->multiPinMode(TP_RST | LCD_BL | LCD_RST | SD_CS | USB_SEL, OUTPUT);
|
||||
|
||||
// Reset sequence
|
||||
// Reset sequence for LCD
|
||||
_expander->digitalWrite(LCD_RST, LOW);
|
||||
delay(50);
|
||||
_expander->digitalWrite(LCD_RST, HIGH);
|
||||
@@ -115,7 +98,7 @@ TouchEvent DisplayDriverGFX::readTouch() {
|
||||
evt.x = static_cast<int>(x);
|
||||
evt.y = static_cast<int>(y);
|
||||
|
||||
// Track press start
|
||||
// Track press start for hold detection
|
||||
if (!_lastTouch.pressed) {
|
||||
_pressStartMs = millis();
|
||||
_isHolding = false;
|
||||
@@ -154,7 +137,6 @@ HoldState DisplayDriverGFX::updateHold(unsigned long holdMs) {
|
||||
unsigned long elapsed = millis() - _pressStartMs;
|
||||
|
||||
if (!_isHolding) {
|
||||
// Start tracking hold
|
||||
_isHolding = true;
|
||||
}
|
||||
|
||||
@@ -174,49 +156,111 @@ HoldState DisplayDriverGFX::updateHold(unsigned long holdMs) {
|
||||
void DisplayDriverGFX::render(const ScreenState& state) {
|
||||
if (!_gfx) return;
|
||||
|
||||
// Clear with background color
|
||||
_gfx->fillScreen(0x001030); // Dark blue
|
||||
|
||||
if (!state.showDashboard) {
|
||||
// Show alert/message screen
|
||||
renderAlert(state);
|
||||
return;
|
||||
// Check if we need full redraw
|
||||
if (state.screen != _lastScreen) {
|
||||
_needsRedraw = true;
|
||||
_lastScreen = state.screen;
|
||||
}
|
||||
|
||||
// Draw dashboard tiles
|
||||
switch (state.screen) {
|
||||
case ScreenID::BOOT:
|
||||
if (_needsRedraw) {
|
||||
_gfx->fillScreen(0x000000);
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(2);
|
||||
_gfx->setCursor(10, 10);
|
||||
_gfx->print("KLUBHAUS BOOT");
|
||||
_needsRedraw = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case ScreenID::OFF:
|
||||
if (_needsRedraw) {
|
||||
_gfx->fillScreen(0x000000);
|
||||
_needsRedraw = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case ScreenID::ALERT:
|
||||
drawAlert(state);
|
||||
break;
|
||||
|
||||
case ScreenID::DASHBOARD:
|
||||
if (_needsRedraw) {
|
||||
drawDashboard(state);
|
||||
_needsRedraw = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void DisplayDriverGFX::drawAlert(const ScreenState& state) {
|
||||
uint32_t elapsed = millis() - state.alertStartMs;
|
||||
uint8_t pulse = static_cast<uint8_t>(180.0f + 75.0f * sinf(elapsed / 300.0f));
|
||||
uint16_t bg = _gfx->color565(pulse, 0, 0);
|
||||
|
||||
_gfx->fillScreen(bg);
|
||||
_gfx->setTextColor(0xFFFF, bg);
|
||||
|
||||
_gfx->setTextSize(3);
|
||||
_gfx->setCursor(10, 20);
|
||||
_gfx->print(state.alertTitle.length() > 0 ? state.alertTitle.c_str() : "ALERT");
|
||||
|
||||
_gfx->setTextSize(2);
|
||||
_gfx->setCursor(10, 80);
|
||||
_gfx->print(state.alertBody);
|
||||
|
||||
_gfx->setTextSize(1);
|
||||
_gfx->setCursor(10, DISP_H - 20);
|
||||
_gfx->print("Hold to silence...");
|
||||
}
|
||||
|
||||
void DisplayDriverGFX::drawDashboard(const ScreenState& state) {
|
||||
_gfx->fillScreen(0x001030); // Dark blue
|
||||
|
||||
// Header
|
||||
_gfx->fillRect(0, 0, DISP_W, 30, 0x0220);
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(1);
|
||||
_gfx->setCursor(5, 10);
|
||||
_gfx->printf("KLUBHAUS");
|
||||
|
||||
// WiFi status
|
||||
_gfx->setCursor(DISP_W - 100, 10);
|
||||
_gfx->printf("WiFi:%s", state.wifiSsid.length() > 0 ? "ON" : "OFF");
|
||||
|
||||
|
||||
// Tiles: 2 rows × 4 columns
|
||||
constexpr int cols = 4;
|
||||
constexpr int rows = 2;
|
||||
constexpr int tileW = DISP_W / cols;
|
||||
constexpr int tileH = DISP_H / rows;
|
||||
constexpr int tileH = (DISP_H - 30) / rows;
|
||||
constexpr int margin = 8;
|
||||
|
||||
for (int i = 0; i < state.dashTiles.size(); i++) {
|
||||
// Draw placeholder tiles (8 total for 2x4 grid)
|
||||
const char* tileLabels[] = {"1", "2", "3", "4", "5", "6", "7", "8"};
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int col = i % cols;
|
||||
int row = i / cols;
|
||||
|
||||
int x = col * tileW + margin;
|
||||
int y = row * tileH + margin;
|
||||
int y = 30 + row * tileH + margin;
|
||||
int w = tileW - 2 * margin;
|
||||
int h = tileH - 2 * margin;
|
||||
|
||||
// Tile background
|
||||
uint16_t tileColor = state.dashTiles[i].active ? 0x04A0 : 0x0220;
|
||||
_gfx->fillRoundRect(x, y, w, h, 8, tileColor);
|
||||
_gfx->fillRoundRect(x, y, w, h, 8, 0x0220);
|
||||
|
||||
// Tile border
|
||||
_gfx->drawRoundRect(x, y, w, h, 8, 0xFFFF);
|
||||
|
||||
// Tile label
|
||||
// Tile number
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(2);
|
||||
_gfx->setCursor(x + 10, y + 10);
|
||||
_gfx->print(state.dashTiles[i].label);
|
||||
_gfx->setCursor(x + w/2 - 10, y + h/2 - 10);
|
||||
_gfx->print(tileLabels[i]);
|
||||
}
|
||||
|
||||
// Draw WiFi status
|
||||
_gfx->setTextSize(1);
|
||||
_gfx->setCursor(DISP_W - 100, 10);
|
||||
_gfx->printf("WiFi: %s", state.wifiConnected ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
void DisplayDriverGFX::updateHint() {
|
||||
@@ -233,12 +277,3 @@ void DisplayDriverGFX::updateHint() {
|
||||
|
||||
_gfx->drawCircle(DISP_W / 2, DISP_H / 2, 50, col);
|
||||
}
|
||||
|
||||
// Helper for alert rendering (implement as needed)
|
||||
void DisplayDriverGFX::renderAlert(const ScreenState& state) {
|
||||
// Placeholder - implement based on your ScreenState fields
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(3);
|
||||
_gfx->setCursor(200, 200);
|
||||
_gfx->print("Alert Mode");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user