refactor(display): improve touch handling and code formatting
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
// boards/esp32-s3-lcd-43/DisplayDriverGFX.cpp
|
||||
#include <Arduino.h>
|
||||
#include "DisplayDriverGFX.h"
|
||||
|
||||
#include "LovyanPins.h"
|
||||
#include "board_config.h"
|
||||
#include "DisplayDriverGFX.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// ── Globals ──
|
||||
static LGFX* _gfx = nullptr;
|
||||
@@ -18,9 +20,12 @@ static constexpr int DISP_H = 480;
|
||||
static void initDisplay() {
|
||||
Serial.println("LovyanGFX init...");
|
||||
|
||||
// Note: LovyanGFX handles I2C internally (port 1 for touch, port 0 for CH422G)
|
||||
// No need to call Wire.begin() or Wire1.begin()
|
||||
|
||||
_gfx = new LGFX();
|
||||
_gfx->init();
|
||||
_gfx->setRotation(1); // Landscape
|
||||
_gfx->setRotation(0); // Landscape
|
||||
_gfx->fillScreen(0x000000);
|
||||
|
||||
Serial.println("Display ready");
|
||||
@@ -36,6 +41,8 @@ DisplayDriverGFX& DisplayDriverGFX::instance() {
|
||||
|
||||
void DisplayDriverGFX::begin() {
|
||||
initDisplay();
|
||||
// Turn on backlight immediately
|
||||
setBacklight(true);
|
||||
}
|
||||
|
||||
void DisplayDriverGFX::setBacklight(bool on) {
|
||||
@@ -45,35 +52,30 @@ void DisplayDriverGFX::setBacklight(bool on) {
|
||||
}
|
||||
}
|
||||
|
||||
int DisplayDriverGFX::width() {
|
||||
return DISP_W;
|
||||
}
|
||||
int DisplayDriverGFX::width() { return DISP_W; }
|
||||
|
||||
int DisplayDriverGFX::height() {
|
||||
return DISP_H;
|
||||
}
|
||||
int DisplayDriverGFX::height() { return DISP_H; }
|
||||
|
||||
// ── Touch handling ──
|
||||
|
||||
TouchEvent DisplayDriverGFX::readTouch() {
|
||||
TouchEvent evt;
|
||||
|
||||
if (!_gfx) return evt;
|
||||
if(!_gfx)
|
||||
return evt;
|
||||
|
||||
int32_t x, y;
|
||||
if (_gfx->getTouch(&x, &y)) {
|
||||
evt.pressed = true;
|
||||
bool pressed = _gfx->getTouch(&x, &y);
|
||||
|
||||
// Only report NEW touches (debounce - ignore held touches)
|
||||
evt.pressed = pressed && !_lastTouch.pressed;
|
||||
|
||||
if(pressed) {
|
||||
evt.x = static_cast<int>(x);
|
||||
evt.y = static_cast<int>(y);
|
||||
|
||||
// Track press start for hold detection
|
||||
if (!_lastTouch.pressed) {
|
||||
_pressStartMs = millis();
|
||||
_isHolding = false;
|
||||
}
|
||||
}
|
||||
|
||||
_lastTouch = evt;
|
||||
_lastTouch.pressed = pressed;
|
||||
return evt;
|
||||
}
|
||||
|
||||
@@ -122,7 +124,8 @@ HoldState DisplayDriverGFX::updateHold(unsigned long holdMs) {
|
||||
// ── Rendering ──
|
||||
|
||||
void DisplayDriverGFX::render(const ScreenState& state) {
|
||||
if (!_gfx) return;
|
||||
if(!_gfx)
|
||||
return;
|
||||
|
||||
// Check if we need full redraw
|
||||
if(state.screen != _lastScreen) {
|
||||
@@ -187,7 +190,8 @@ void DisplayDriverGFX::drawDashboard(const ScreenState& state) {
|
||||
_gfx->fillScreen(0x001030); // Dark blue
|
||||
|
||||
// Header
|
||||
_gfx->fillRect(0, 0, DISP_W, 30, 0x0220);
|
||||
_gfx->fillRect(0, 0, DISP_W, 30, 0x1A1A); // Dark gray
|
||||
_gfx->setFont(&fonts::Font0); // Built-in minimal font
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(1);
|
||||
_gfx->setCursor(5, 10);
|
||||
@@ -197,7 +201,6 @@ void DisplayDriverGFX::drawDashboard(const ScreenState& state) {
|
||||
_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;
|
||||
@@ -232,11 +235,14 @@ void DisplayDriverGFX::drawDashboard(const ScreenState& state) {
|
||||
}
|
||||
|
||||
void DisplayDriverGFX::updateHint() {
|
||||
if (!_gfx) return;
|
||||
if(!_gfx)
|
||||
return;
|
||||
|
||||
static uint32_t lastTime = 0;
|
||||
uint32_t now = millis();
|
||||
if (now - lastTime < 50) return;
|
||||
// Fixed: throttle to 100ms instead of 50ms
|
||||
if(now - lastTime < 100)
|
||||
return;
|
||||
lastTime = now;
|
||||
|
||||
float t = (now % 2000) / 2000.0f;
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
// Klubhaus Doorbell — ESP32-S3-Touch-LCD-4.3 target
|
||||
//
|
||||
|
||||
#include <KlubhausCore.h>
|
||||
#include "DisplayDriverGFX.h"
|
||||
#include "board_config.h"
|
||||
#include "secrets.h"
|
||||
#include "DisplayDriverGFX.h"
|
||||
|
||||
#include <KlubhausCore.h>
|
||||
|
||||
DisplayDriverGFX gfxDriver;
|
||||
DisplayManager display(&gfxDriver);
|
||||
@@ -19,14 +20,12 @@ void setup() {
|
||||
logic.finishBoot();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// ── State machine tick ──
|
||||
logic.update();
|
||||
// void loop() {
|
||||
TouchEvent evt = display.readTouch();
|
||||
|
||||
// ── Render current screen ──
|
||||
logic.update();
|
||||
display.render(logic.getScreenState());
|
||||
|
||||
// ── Touch handling ──
|
||||
const ScreenState& st = logic.getScreenState();
|
||||
|
||||
if(st.deviceState == DeviceState::ALERTING) {
|
||||
@@ -37,18 +36,36 @@ void loop() {
|
||||
if(!h.active) {
|
||||
display.updateHint();
|
||||
}
|
||||
} else {
|
||||
TouchEvent evt = display.readTouch();
|
||||
if (evt.pressed && st.screen == ScreenID::DASHBOARD) {
|
||||
} else if(evt.pressed) {
|
||||
if(st.screen == ScreenID::OFF) {
|
||||
// Tap in OFF mode → wake to DASHBOARD
|
||||
Serial.println("[TOUCH] OFF → DASHBOARD");
|
||||
logic.setScreen(ScreenID::DASHBOARD);
|
||||
display.setBacklight(true);
|
||||
} else if(st.screen == ScreenID::DASHBOARD) {
|
||||
int tile = display.dashboardTouch(evt.x, evt.y);
|
||||
if (tile >= 0) Serial.printf("[DASH] Tile %d tapped\n", tile);
|
||||
if(tile >= 0) {
|
||||
Serial.printf("[DASH] Tile %d tapped\n", tile);
|
||||
// TODO: Handle tile actions
|
||||
}
|
||||
} else if(st.screen == ScreenID::ALERT) {
|
||||
// Tap in ALERT mode → could dismiss or show more info
|
||||
Serial.println("[TOUCH] ALERT tap");
|
||||
// For now, let's make tap do nothing (hold to silence only)
|
||||
// Or: logic.dismissAlert();
|
||||
}
|
||||
}
|
||||
|
||||
// Serial console (unchanged)
|
||||
if(Serial.available()) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
// ── Serial console ──
|
||||
if(Serial.available()) {
|
||||
String cmd = Serial.readStringUntil('\n');
|
||||
cmd.trim();
|
||||
if (cmd.length() > 0) logic.onSerialCommand(cmd);
|
||||
if(cmd.length() > 0)
|
||||
logic.onSerialCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,3 +258,13 @@ void DoorbellLogic::onSerialCommand(const String& cmd) {
|
||||
}
|
||||
else Serial.println(F("[CMD] alert|silence|reboot|dashboard|off|status"));
|
||||
}
|
||||
|
||||
void DoorbellLogic::setScreen(ScreenID s) {
|
||||
Serial.printf("[SCREEN] Set to %s\n", screenIdStr(s));
|
||||
_state.screen = s;
|
||||
|
||||
// Auto-manage backlight based on screen
|
||||
bool needsBacklight = (s != ScreenID::OFF);
|
||||
_display->setBacklight(needsBacklight);
|
||||
_state.backlightOn = needsBacklight;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ private:
|
||||
void flushStatus(const String& message);
|
||||
void heartbeat();
|
||||
void transition(DeviceState s);
|
||||
void setScreen(ScreenID s);
|
||||
String topicUrl(const char* base);
|
||||
|
||||
DisplayManager* _display;
|
||||
|
||||
2
vendor/.gitignore
vendored
Normal file
2
vendor/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user