refactor(touch): centralize touch handling in DoorbellLogic

This commit is contained in:
2026-02-17 23:35:59 -08:00
parent 4f389ac0fe
commit 46289b9d40
6 changed files with 91 additions and 53 deletions

View File

@@ -21,18 +21,12 @@ void setup() {
}
void loop() {
// ── Touch debug: poll continuously ──
// ── Read touch ──
TouchEvent evt = display.readTouch();
// ── Touch debug ──
if(evt.pressed) {
Serial.printf("[TOUCH] pressed: x=%d, y=%d\n", evt.x, evt.y);
} else {
// Debug: print raw Z even when not touched, to see controller state
static uint32_t lastDebug = 0;
if(millis() - lastDebug > 2000) {
lastDebug = millis();
uint16_t z = tftDriver.getRawTouchZ();
Serial.printf("[TOUCH] Raw Z=%d\n", z);
}
}
// ── State machine tick ──
@@ -41,8 +35,11 @@ void loop() {
// ── Render current screen ──
display.render(logic.getScreenState());
// ── Touch handling ──
// ── Touch handling (tap gestures) ──
const ScreenState& st = logic.getScreenState();
int tile = logic.handleTouch(evt);
// ── Hold gesture (for silencing alerts) ──
static int holdStartX = -1;
static int holdStartY = -1;
@@ -54,9 +51,8 @@ void loop() {
holdStartY = -1;
}
if(h.started) {
TouchEvent t = display.readTouch();
holdStartX = t.x;
holdStartY = t.y;
holdStartX = evt.x;
holdStartY = evt.y;
}
if(holdStartX >= 0) {
display.updateHint(holdStartX, holdStartY, h.active);
@@ -66,15 +62,6 @@ void loop() {
holdStartY = -1;
}
if(st.screen == ScreenID::DASHBOARD) {
TouchEvent evt = display.readTouch();
if(evt.pressed) {
int tile = display.dashboardTouch(evt.x, evt.y);
if(tile >= 0)
Serial.printf("[DASH] Tile %d tapped\n", tile);
}
}
// ── Serial console ──
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');

View File

@@ -21,14 +21,20 @@ void setup() {
}
void loop() {
// ── Read touch ──
TouchEvent evt = display.readTouch();
// ── State machine tick ──
logic.update();
// ── Render current screen ──
display.render(logic.getScreenState());
// ── Touch handling ──
// ── Touch handling (tap gestures) ──
const ScreenState& st = logic.getScreenState();
int tile = logic.handleTouch(evt);
// ── Hold gesture (for silencing alerts) ──
static int holdStartX = -1;
static int holdStartY = -1;
@@ -40,11 +46,9 @@ void loop() {
holdStartY = -1;
}
if(h.started) {
TouchEvent t = display.readTouch();
holdStartX = t.x;
holdStartY = t.y;
holdStartX = evt.x;
holdStartY = evt.y;
}
// Fix for esp32-32e.ino
if(holdStartX >= 0) {
display.updateHint(holdStartX, holdStartY, h.active);
}
@@ -53,15 +57,6 @@ void loop() {
holdStartY = -1;
}
if(st.screen == ScreenID::DASHBOARD) {
TouchEvent evt = display.readTouch();
if(evt.pressed) {
int tile = display.dashboardTouch(evt.x, evt.y);
if(tile >= 0)
Serial.printf("[DASH] Tile %d tapped\n", tile);
}
}
// ── Serial console ──
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');

View File

@@ -21,14 +21,18 @@ void setup() {
}
void loop() {
// ── Read touch ──
TouchEvent evt = display.readTouch();
// ── State machine tick ──
logic.update();
display.render(logic.getScreenState());
// ── Touch handling (tap gestures) ──
const ScreenState& st = logic.getScreenState();
int tile = logic.handleTouch(evt);
// Track initial hold position for hint
// ── Hold gesture (for silencing alerts) ──
static int holdStartX = -1;
static int holdStartY = -1;
@@ -43,7 +47,6 @@ void loop() {
holdStartX = evt.x;
holdStartY = evt.y;
}
// Draw hint during hold (ACTIVE) or idle (IDLE)
if(holdStartX >= 0) {
if(h.active) {
display.updateHint(holdStartX, holdStartY, true);
@@ -55,21 +58,6 @@ void loop() {
holdStartX = -1;
holdStartY = -1;
}
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);
}
} else if(st.screen == ScreenID::ALERT) {
Serial.println("[TOUCH] ALERT tap");
}
}
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');