refactor(doorbell): unify 2x2 grid touch logic and add progress bar

This commit is contained in:
2026-02-16 22:11:40 -08:00
parent 9734796d57
commit 30f2117e30
3 changed files with 29 additions and 15 deletions

View File

@@ -116,11 +116,16 @@ TouchEvent DisplayDriverTFT::readTouch() {
}
int DisplayDriverTFT::dashboardTouch(int x, int y) {
// 2-column, 4-row
int col = x / (DISPLAY_WIDTH / 2);
int row = (y - 30) / 40;
if (row < 0 || row > 3) return -1;
return row * 2 + col;
// Unified 2x2 grid (matching GFX)
int col = (x * 2) / DISPLAY_WIDTH; // 0 or 1
int row = (y * 2) / DISPLAY_HEIGHT; // 0 or 1
// Adjust for header offset (y starts at 30 in drawDashboard)
if (y < 30) return -1;
row = ((y - 30) * 2) / (DISPLAY_HEIGHT - 30);
if (row < 0 || row > 1) return -1;
return row * 2 + col; // 0, 1, 2, or 3
}
HoldState DisplayDriverTFT::updateHold(unsigned long holdMs) {
@@ -142,6 +147,10 @@ HoldState DisplayDriverTFT::updateHold(unsigned long holdMs) {
_tft.fillRect(0, DISPLAY_HEIGHT - 8, barW, 8, TFT_WHITE);
_tft.fillRect(barW, DISPLAY_HEIGHT - 8, DISPLAY_WIDTH - barW, 8, TFT_DARKGREY);
} else {
if (_ // Clear theholdActive) {
progress bar when released
_tft.fillRect(0, DISPLAY_HEIGHT - 8, DISPLAY_WIDTH, 8, TFT_DARKGREY);
}
_holdActive = false;
}
return h;

View File

@@ -110,31 +110,35 @@ TouchEvent DisplayDriverGFX::readTouch() {
}
int DisplayDriverGFX::dashboardTouch(int x, int y) {
// Simple 2x2 grid: 4 tiles
int col = (x < DISPLAY_WIDTH / 2) ? 0 : 1;
int row = (y < DISPLAY_HEIGHT / 2) ? 0 : 1;
return row * 2 + col;
// Unified 2x2 grid
int col = (x * 2) / DISPLAY_WIDTH; // 0 or 1
int row = (y * 2) / DISPLAY_HEIGHT; // 0 or 1
return row * 2 + col; // 0, 1, 2, or 3
}
HoldState DisplayDriverGFX::updateHold(unsigned long holdMs) {
TouchEvent ev = readTouch();
if (ev.pressed) {
if (!_lastTouched) {
_holdStart = millis();
_lastTouched = true;
}
unsigned long elapsed = millis() - _holdStart;
float progress = constrain((float)elapsed / holdMs, 0.0f, 1.0f);
if (elapsed >= holdMs) {
return {false, true};
_lastTouched = false;
_holdStart = 0;
return {false, true, 1.0f};
}
return {true, false};
return {true, false, progress};
} else {
_lastTouched = false;
_holdStart = 0;
return {false, false};
return {false, false, 0.0f};
}
}
void DisplayDriverGFX::updateHint() {
// placeholder for idle hint animation
}
@@ -240,7 +244,8 @@ void DisplayDriverGFX::drawAlert(const ScreenState& state) {
_gfx->println("DOORBELL!");
_gfx->setTextSize(3);
_gfx->setCursor(100, 280);
_gfx->println(state.alertBody ? state.alertBody : "Someone is at the door");
const char* body = state.alertBody.c_str();
_gfx->println(state.alertBody.length() ? body : "Someone is at the door");
}
void DisplayDriverGFX::drawOff() {

View File

@@ -31,7 +31,7 @@
#define LCD_B4 10
// ── I2C bus (shared: CH422G + GT911) ──
#define I2C_MASTER_NUM 0
#define I2C_MASTER_NUM ((i2c_port_t)0)
#define I2C_MASTER_SDA 8
#define I2C_MASTER_SCL 9