Compare commits

...

13 Commits

Author SHA1 Message Date
8c92487a47 refactor: add active parameter to updateHint method signature
1. **Method Signature Update**: Added `bool active` parameter to `updateHint()` method across the display driver hierarchy:
   - `DisplayManager::updateHint(x, y, active)` - delegates to driver
   - `DisplayDriverTFT::updateHint(x, y, active)` - override implementation
   - `DisplayDriverGFX::updateHint(x, y, active)` - override implementation

2. **Code Formatting**: `DisplayManager.h` reformatted (whitespace/comment style changes only)

- **Breaking Change**: All existing `updateHint(x, y)` calls will fail to compile until updated to include the `active` parameter
- **Enhanced Control**: Callers can now explicitly show/hide touch hints rather than just updating position, enabling better touch feedback UX (e.g., hide hint on touch release)
- **API Consistency**: All implementations in the driver hierarchy now enforce the same signature
2026-02-17 05:23:43 -08:00
4414185891 feat(display): add active state parameter to hint animation
1. **Added `active` parameter to hint animation**
   - `updateHint()` now accepts a boolean `active` parameter across both display drivers (TFT and GFX)
   - When `active=true`: faster pulse animation (500ms period) during active hold
   - When `active=false`: slower pulse animation (2000ms period) during idle state

2. **Improved animation calculations**
   - Replaced modulo operator with `fmodf()` for cleaner float calculations
   - Standardized to `static_cast<uint8_t>()` for type conversions
   - Fixed GFX driver to use `color565()` method instead of manual bit shifting

3. **Updated hint display logic**
   - Now differentiates between "holding" state (fast pulse) and "idle" state (slow pulse)
   - Hint draws at both states when `holdStartX >= 0` (touch position captured)

4. **Added code formatter task**
   - New `mise.toml` task for running clang-format across all source files

- Users get **visual feedback differentiation**: fast pulsing during active hold vs. slow pulsing when idle
- More intuitive UI that clearly indicates whether a long-press is in progress or just waiting
- Cleaner, more maintainable code with standardized calculations and type conversions
2026-02-17 05:11:02 -08:00
db668f27dd fix(display): correct type cast syntax in pulse calculation 2026-02-17 04:21:05 -08:00
3b0c9c98b7 style: apply consistent code formatting and spacing 2026-02-17 04:15:48 -08:00
4d66bded22 style(esp32-32e): reformat code and fix display dimensions 2026-02-17 04:10:03 -08:00
853bb38c46 feat(display): draw hint animation at touch position instead of center 2026-02-17 03:49:34 -08:00
5ebbf0177a style: fix indentation and formatting in doorbell code 2026-02-17 03:19:12 -08:00
4876fb29b7 refactor(display): improve touch handling and code formatting 2026-02-17 03:10:52 -08:00
40c0a0a97e refactor(esp32-s3-lcd-43): remove IO expander and use LovyanGFX backlight 2026-02-17 02:13:30 -08:00
c348de9e38 fix(doorbell-touch): correct LovyanGFX library integration and build 2026-02-17 01:13:28 -08:00
f658b95a2b refactor(doorbell-touch): simplify LovyanGFX library setup 2026-02-17 01:00:57 -08:00
dcc710e9b3 refactor(display): split render into screen-specific draw methods 2026-02-17 00:06:30 -08:00
0e867a196c chore: remove TFT_eSPI submodule 2026-02-16 23:14:43 -08:00
20 changed files with 701 additions and 559 deletions

3
.gitmodules vendored
View File

@@ -1,6 +1,3 @@
[submodule "TFT_eSPI"]
path = TFT_eSPI
url = https://github.com/Cincinnatu/TFT_eSPI
[submodule "examples/Waveshare-ESP32-S3-Touch-LCD-4.3-and-Arduino"]
path = examples/Waveshare-ESP32-S3-Touch-LCD-4.3-and-Arduino
url = https://github.com/Westcott1/Waveshare-ESP32-S3-Touch-LCD-4.3-and-Arduino.git

View File

@@ -17,31 +17,39 @@ void DisplayDriverTFT::begin() {
Serial.println("[GFX] Backlight ON");
}
void DisplayDriverTFT::setBacklight(bool on) {
digitalWrite(PIN_LCD_BL, on ? HIGH : LOW);
}
void DisplayDriverTFT::setBacklight(bool on) { digitalWrite(PIN_LCD_BL, on ? HIGH : LOW); }
// ── Rendering ───────────────────────────────────────────────
void DisplayDriverTFT::render(const ScreenState& st) {
if (st.screen != _lastScreen) {
if(st.screen != _lastScreen) {
_needsRedraw = true;
_lastScreen = st.screen;
}
switch (st.screen) {
case ScreenID::BOOT:
if (_needsRedraw) { drawBoot(); _needsRedraw = false; }
break;
case ScreenID::ALERT:
drawAlert(st);
break;
case ScreenID::DASHBOARD:
if (_needsRedraw) { drawDashboard(st); _needsRedraw = false; }
break;
case ScreenID::OFF:
if (_needsRedraw) { _tft.fillScreen(TFT_BLACK); _needsRedraw = false; }
break;
switch(st.screen) {
case ScreenID::BOOT:
if(_needsRedraw) {
drawBoot();
_needsRedraw = false;
}
break;
case ScreenID::ALERT:
drawAlert(st);
break;
case ScreenID::DASHBOARD:
if(_needsRedraw) {
drawDashboard(st);
_needsRedraw = false;
}
break;
case ScreenID::OFF:
if(_needsRedraw) {
_tft.fillScreen(TFT_BLACK);
_needsRedraw = false;
}
break;
}
}
@@ -88,18 +96,21 @@ void DisplayDriverTFT::drawDashboard(const ScreenState& st) {
_tft.printf("KLUBHAUS — %s", deviceStateStr(st.deviceState));
int y = 30;
_tft.setCursor(5, y); y += 18;
_tft.setCursor(5, y);
y += 18;
_tft.printf("WiFi: %s %ddBm", st.wifiSsid.c_str(), st.wifiRssi);
_tft.setCursor(5, y); y += 18;
_tft.setCursor(5, y);
y += 18;
_tft.printf("IP: %s", st.ipAddr.c_str());
_tft.setCursor(5, y); y += 18;
_tft.setCursor(5, y);
y += 18;
_tft.printf("Up: %lus Heap: %d", st.uptimeMs / 1000, ESP.getFreeHeap());
_tft.setCursor(5, y); y += 18;
_tft.printf("Last poll: %lus ago",
st.lastPollMs > 0 ? (millis() - st.lastPollMs) / 1000 : 0);
_tft.setCursor(5, y);
y += 18;
_tft.printf("Last poll: %lus ago", st.lastPollMs > 0 ? (millis() - st.lastPollMs) / 1000 : 0);
}
// ── Touch ───────────────────────────────────────────────────
@@ -107,7 +118,7 @@ void DisplayDriverTFT::drawDashboard(const ScreenState& st) {
TouchEvent DisplayDriverTFT::readTouch() {
TouchEvent evt;
uint16_t tx, ty;
if (_tft.getTouch(&tx, &ty)) {
if(_tft.getTouch(&tx, &ty)) {
evt.pressed = true;
evt.x = tx;
evt.y = ty;
@@ -116,29 +127,31 @@ TouchEvent DisplayDriverTFT::readTouch() {
}
int DisplayDriverTFT::dashboardTouch(int x, int y) {
// 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
// 2x2 grid, accounting for 30px header
if(y < 30)
return -1;
int col = (x * 2) / DISPLAY_WIDTH; // 0 or 1
int row = ((y - 30) * 2) / (DISPLAY_HEIGHT - 30); // 0 or 1
if(col < 0 || col > 1 || row < 0 || row > 1)
return -1;
return row * 2 + col; // 0, 1, 2, or 3
}
HoldState DisplayDriverTFT::updateHold(unsigned long holdMs) {
HoldState h;
TouchEvent t = readTouch();
if (t.pressed) {
if (!_holdActive) {
_holdActive = true;
if(t.pressed) {
if(!_holdActive) {
_holdActive = true;
_holdStartMs = millis();
h.started = true;
}
uint32_t held = millis() - _holdStartMs;
h.active = true;
h.active = true;
h.progress = constrain((float)held / (float)holdMs, 0.0f, 1.0f);
h.completed = (held >= holdMs);
@@ -147,8 +160,8 @@ 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
if(_holdActive) {
// Clear the progress bar when released
_tft.fillRect(0, DISPLAY_HEIGHT - 8, DISPLAY_WIDTH, 8, TFT_DARKGREY);
}
_holdActive = false;
@@ -156,9 +169,10 @@ HoldState DisplayDriverTFT::updateHold(unsigned long holdMs) {
return h;
}
void DisplayDriverTFT::updateHint() {
float t = (millis() % 2000) / 2000.0f;
uint8_t v = (uint8_t)(30.0f + 30.0f * sinf(t * 2 * PI));
void DisplayDriverTFT::updateHint(int x, int y, bool active) {
float period = active ? 500.0f : 2000.0f;
float t = fmodf(millis(), period) / period;
uint8_t v = static_cast<uint8_t>(30.0f + 30.0f * sinf(t * 2.0f * PI));
uint16_t col = _tft.color565(v, v, v);
_tft.drawRect(DISPLAY_WIDTH / 2 - 40, DISPLAY_HEIGHT / 2 + 20, 80, 40, col);
_tft.drawRect(x - 40, y - 20, 80, 40, col);
}

View File

@@ -1,8 +1,9 @@
#pragma once
#include "board_config.h"
#include <KlubhausCore.h>
#include <TFT_eSPI.h>
#include "board_config.h"
class DisplayDriverTFT : public IDisplayDriver {
public:
@@ -10,11 +11,11 @@ public:
void setBacklight(bool on) override;
void render(const ScreenState& state) override;
TouchEvent readTouch() override;
int dashboardTouch(int x, int y) override;
int dashboardTouch(int x, int y) override;
HoldState updateHold(unsigned long holdMs) override;
void updateHint() override;
int width() override { return DISPLAY_WIDTH; }
int height() override { return DISPLAY_HEIGHT; }
void updateHint(int x, int y, bool active) override;
int width() override { return DISPLAY_WIDTH; }
int height() override { return DISPLAY_HEIGHT; }
private:
void drawBoot();
@@ -23,8 +24,8 @@ private:
TFT_eSPI _tft;
bool _holdActive = false;
bool _holdActive = false;
uint32_t _holdStartMs = 0;
ScreenID _lastScreen = ScreenID::BOOT;
bool _needsRedraw = true;
ScreenID _lastScreen = ScreenID::BOOT;
bool _needsRedraw = true;
};

View File

@@ -1,6 +1,6 @@
#pragma once
#define BOARD_NAME "WS_32E"
#define BOARD_NAME "WS_32E"
// ══════════════════════════════════════════════════════════
// TODO: Set these to match YOUR display + wiring.
@@ -9,12 +9,12 @@
// (which gets copied into the vendored TFT_eSPI library).
// ══════════════════════════════════════════════════════════
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 240
#define DISPLAY_ROTATION 1 // landscape
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 240
#define DISPLAY_ROTATION 1 // landscape
// Backlight GPIO (directly wired)
#define PIN_LCD_BL 22
#define PIN_LCD_BL 22
// Touch — if using XPT2046 via TFT_eSPI, set TOUCH_CS in tft_user_setup.h
// If using capacitive touch (e.g. FT6236), configure I2C pins here:

View File

@@ -2,14 +2,15 @@
// Klubhaus Doorbell — ESP32-32E target
//
#include <KlubhausCore.h>
#include "DisplayDriverTFT.h"
#include "board_config.h"
#include "secrets.h"
#include "DisplayDriverTFT.h"
#include <KlubhausCore.h>
DisplayDriverTFT tftDriver;
DisplayManager display(&tftDriver);
DoorbellLogic logic(&display);
DisplayManager display(&tftDriver);
DoorbellLogic logic(&display);
void setup() {
Serial.begin(115200);
@@ -28,27 +29,44 @@ void loop() {
// ── Touch handling ──
const ScreenState& st = logic.getScreenState();
static int holdStartX = -1;
static int holdStartY = -1;
if (st.deviceState == DeviceState::ALERTING) {
if(st.deviceState == DeviceState::ALERTING) {
HoldState h = display.updateHold(HOLD_TO_SILENCE_MS);
if (h.completed) {
if(h.completed) {
logic.silenceAlert();
holdStartX = -1;
holdStartY = -1;
}
if (!h.active) {
display.updateHint();
if(h.started) {
TouchEvent t = display.readTouch();
holdStartX = t.x;
holdStartY = t.y;
}
// Fix for esp32-32e.ino
if(holdStartX >= 0) {
display.updateHint(holdStartX, holdStartY, h.active);
}
} else {
holdStartX = -1;
holdStartY = -1;
}
if(st.screen == ScreenID::DASHBOARD) {
TouchEvent evt = display.readTouch();
if (evt.pressed && st.screen == ScreenID::DASHBOARD) {
if(evt.pressed) {
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);
}
}
// ── Serial console ──
if (Serial.available()) {
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd.length() > 0) logic.onSerialCommand(cmd);
if(cmd.length() > 0)
logic.onSerialCommand(cmd);
}
}

View File

@@ -2,39 +2,36 @@
// TFT_eSPI User_Setup for ESP32-32E target
// This file is copied over vendor/esp32-32e/TFT_eSPI/User_Setup.h
// by the install-libs-32e task.
//
// TODO: Change the driver, pins, and dimensions to match your display.
// ═══════════════════════════════════════════════════════════
#define USER_SETUP_ID 200
// ── Driver ──
#define ILI9341_DRIVER
// #define ST7789_DRIVER
// #define ILI9488_DRIVER
// ── Resolution ──
#define TFT_WIDTH 240
#define TFT_HEIGHT 320
// FIXED: Match board_config.h (320x240 landscape)
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
// ── SPI Pins ──
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS 5
#define TFT_DC 27
#define TFT_RST 33
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS 5
#define TFT_DC 27
#define TFT_RST 33
// ── Backlight (optional, can also use GPIO directly) ──
// #define TFT_BL 22
// #define TFT_BACKLIGHT_ON HIGH
// ── Touch (XPT2046 resistive) ──
#define TOUCH_CS 14
#define TOUCH_CS 14
// ── SPI speed ──
#define SPI_FREQUENCY 40000000
#define SPI_READ_FREQUENCY 20000000
#define SPI_TOUCH_FREQUENCY 2500000
#define SPI_FREQUENCY 40000000
#define SPI_READ_FREQUENCY 20000000
#define SPI_TOUCH_FREQUENCY 2500000
// ── Misc ──
#define LOAD_GLCD

View File

@@ -1,77 +1,33 @@
#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>
#include "LovyanPins.h"
#include "board_config.h"
// boards/esp32-s3-lcd-43/DisplayDriverGFX.cpp
#include "DisplayDriverGFX.h"
#include "LovyanPins.h"
#include "board_config.h"
#include <Arduino.h>
// ── Globals ──
static LGFX* _gfx = nullptr;
static ESP_IOExpander* _expander = nullptr;
// ── Forward declarations ──
static void initExpander();
static void initDisplay();
// ── Dimensions ──
static constexpr int DISP_W = 800;
static constexpr int DISP_H = 480;
// ── Expander initialization ──
static void initExpander() {
Serial.println("IO expander init...");
// Initialize I2C for expander
Wire.begin(TOUCH_SDA, TOUCH_SCL);
_expander = new ESP_IOExpander_CH422G(
(i2c_port_t)I2C_NUM_0,
ESP_IO_EXPANDER_I2C_CH422G_ADDRESS
);
_expander->init();
_expander->begin();
// Set all pins to output
_expander->multiPinMode(TP_RST | LCD_BL | LCD_RST | SD_CS | USB_SEL, OUTPUT);
// Reset sequence
_expander->digitalWrite(LCD_RST, LOW);
delay(50);
_expander->digitalWrite(LCD_RST, HIGH);
delay(150);
// Turn on backlight
_expander->digitalWrite(LCD_BL, HIGH);
Serial.println("IO expander ready");
}
// ── Display initialization ──
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");
}
@@ -84,45 +40,42 @@ DisplayDriverGFX& DisplayDriverGFX::instance() {
// ── IDisplayDriver implementation ──
void DisplayDriverGFX::begin() {
initExpander();
initDisplay();
// Turn on backlight immediately
setBacklight(true);
}
void DisplayDriverGFX::setBacklight(bool on) {
if (_expander) {
_expander->digitalWrite(LCD_BL, on ? HIGH : LOW);
if(_gfx) {
// LovyanGFX handles backlight via setBrightness
_gfx->setBrightness(on ? 255 : 0);
}
}
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
if (!_lastTouch.pressed) {
_pressStartMs = millis();
_isHolding = false;
}
_pressStartMs = millis();
}
_lastTouch = evt;
_lastTouch.pressed = pressed;
return evt;
}
@@ -132,113 +85,175 @@ int DisplayDriverGFX::dashboardTouch(int x, int y) {
constexpr int rows = 2;
constexpr int tileW = DISP_W / cols;
constexpr int tileH = DISP_H / rows;
if (x < 0 || x >= DISP_W || y < 0 || y >= DISP_H) {
if(x < 0 || x >= DISP_W || y < 0 || y >= DISP_H) {
return -1;
}
int col = x / tileW;
int row = y / tileH;
return row * cols + col;
}
HoldState DisplayDriverGFX::updateHold(unsigned long holdMs) {
HoldState state;
if (!_lastTouch.pressed) {
if(!_lastTouch.pressed) {
_isHolding = false;
return state;
}
unsigned long elapsed = millis() - _pressStartMs;
if (!_isHolding) {
// Start tracking hold
if(!_isHolding) {
_isHolding = true;
state.started = true;
}
state.active = true;
state.progress = static_cast<float>(elapsed) / static_cast<float>(holdMs);
if (state.progress >= 1.0f) {
if(state.progress >= 1.0f) {
state.progress = 1.0f;
state.completed = true;
}
return state;
}
// ── Rendering ──
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);
if(!_gfx)
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:
// Only redraw on first entry or screen change
if(_needsRedraw) {
drawAlert(state);
_needsRedraw = false;
}
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, 0x1A1A); // Dark gray
_gfx->setFont(&fonts::Font0); // Built-in minimal font
_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() {
if (!_gfx) return;
void DisplayDriverGFX::updateHint(int x, int y, bool active) {
if(!_gfx)
return;
static uint32_t lastTime = 0;
uint32_t now = millis();
if (now - lastTime < 50) return;
if(now - lastTime < 100)
return;
lastTime = now;
float t = (now % 2000) / 2000.0f;
uint8_t v = static_cast<uint8_t>(30.0f + 30.0f * sinf(t * 2 * 3.14159f));
uint16_t col = ((v >> 3) << 11) | ((v >> 2) << 5) | (v >> 3);
_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");
// active=true: faster pulse (500ms), active=false: slower pulse (2000ms)
float period = active ? 500.0f : 2000.0f;
float t = fmodf(now, period) / period;
uint8_t v = static_cast<uint8_t>(30.0f + 30.0f * sinf(t * 2.0f * 3.14159f));
uint16_t col = _gfx->color565(v, v, v);
_gfx->drawCircle(x, y, 50, col);
}

View File

@@ -1,20 +1,8 @@
#pragma once
#include <Arduino.h>
#include "ScreenState.h"
#include "IDisplayDriver.h"
struct TouchEvent {
bool pressed = false;
int x = 0;
int y = 0;
};
struct HoldState {
bool active = false;
bool completed = false;
float progress = 0.0f; // 0.0 1.0
};
#include <Arduino.h>
class DisplayDriverGFX : public IDisplayDriver {
public:
@@ -26,7 +14,7 @@ public:
TouchEvent readTouch() override;
int dashboardTouch(int x, int y) override;
HoldState updateHold(unsigned long holdMs) override;
void updateHint() override;
void updateHint(int x, int y, bool active) override;
int width() override;
int height() override;
@@ -35,8 +23,16 @@ public:
static DisplayDriverGFX& instance();
private:
// Helper rendering functions
void drawAlert(const ScreenState& state);
void drawDashboard(const ScreenState& state);
// Touch handling
TouchEvent _lastTouch = {false, 0, 0};
TouchEvent _lastTouch = { false, 0, 0 };
unsigned long _pressStartMs = 0;
bool _isHolding = false;
// Screen tracking
ScreenID _lastScreen = ScreenID::BOOT;
bool _needsRedraw = true;
};

View File

@@ -2,117 +2,117 @@
#define LGFX_USE_V1
#include <LovyanGFX.hpp>
#include <lgfx/v1/platforms/esp32s3/Panel_RGB.hpp>
#include <lgfx/v1/platforms/esp32s3/Bus_RGB.hpp>
#include <lgfx/v1/platforms/esp32s3/Panel_RGB.hpp>
// ── Display dimensions ──
#define TFT_HOR_RES 800
#define TFT_VER_RES 480
#define TFT_HOR_RES 800
#define TFT_VER_RES 480
// ── Touch I2C (from Westcott example) ──
#define TOUCH_SDA 8
#define TOUCH_SCL 9
#define TOUCH_INT 4
#define TOUCH_RST -1
#define TOUCH_SDA 8
#define TOUCH_SCL 9
#define TOUCH_INT 4
#define TOUCH_RST -1
// ── CH422G Expander pins ──
#define TP_RST 1
#define LCD_BL 2
#define LCD_RST 3
#define SD_CS 4
#define USB_SEL 5
#define TP_RST 1
#define LCD_BL 2
#define LCD_RST 3
#define SD_CS 4
#define USB_SEL 5
class LGFX : public lgfx::LGFX_Device {
public:
lgfx::Bus_RGB _bus_instance;
lgfx::Panel_RGB _panel_instance;
lgfx::Touch_GT911 _touch_instance;
lgfx::Bus_RGB _bus_instance;
lgfx::Panel_RGB _panel_instance;
lgfx::Touch_GT911 _touch_instance;
LGFX(void) {
// Panel config
{
auto cfg = _panel_instance.config();
cfg.memory_width = TFT_HOR_RES;
cfg.memory_height = TFT_VER_RES;
cfg.panel_width = TFT_HOR_RES;
cfg.panel_height = TFT_VER_RES;
cfg.offset_x = 0;
cfg.offset_y = 0;
_panel_instance.config(cfg);
LGFX(void) {
// Panel config
{
auto cfg = _panel_instance.config();
cfg.memory_width = TFT_HOR_RES;
cfg.memory_height = TFT_VER_RES;
cfg.panel_width = TFT_HOR_RES;
cfg.panel_height = TFT_VER_RES;
cfg.offset_x = 0;
cfg.offset_y = 0;
_panel_instance.config(cfg);
}
// RGB parallel bus config (from Westcott)
{
auto cfg = _bus_instance.config();
cfg.panel = &_panel_instance;
// Blue channel
cfg.pin_d0 = 14;
cfg.pin_d1 = 38;
cfg.pin_d2 = 18;
cfg.pin_d3 = 17;
cfg.pin_d4 = 10;
// Green channel
cfg.pin_d5 = 39;
cfg.pin_d6 = 0;
cfg.pin_d7 = 45;
cfg.pin_d8 = 48;
cfg.pin_d9 = 47;
cfg.pin_d10 = 21;
// Red channel
cfg.pin_d11 = 1;
cfg.pin_d12 = 2;
cfg.pin_d13 = 42;
cfg.pin_d14 = 41;
cfg.pin_d15 = 40;
// Timing
cfg.pin_henable = 5;
cfg.pin_vsync = 3;
cfg.pin_hsync = 46;
cfg.pin_pclk = 7;
cfg.freq_write = 14000000;
cfg.hsync_polarity = 0;
cfg.hsync_front_porch = 20;
cfg.hsync_pulse_width = 10;
cfg.hsync_back_porch = 10;
cfg.vsync_polarity = 0;
cfg.vsync_front_porch = 10;
cfg.vsync_pulse_width = 10;
cfg.vsync_back_porch = 10;
cfg.pclk_active_neg = 0;
cfg.de_idle_high = 0;
cfg.pclk_idle_high = 0;
_bus_instance.config(cfg);
}
_panel_instance.setBus(&_bus_instance);
// Touch config (I2C port 1, address 0x14 - from Westcott!)
{
auto cfg = _touch_instance.config();
cfg.x_min = 0;
cfg.x_max = TFT_HOR_RES - 1;
cfg.y_min = 0;
cfg.y_max = TFT_VER_RES - 1;
cfg.pin_int = TOUCH_INT;
cfg.pin_rst = TOUCH_RST;
cfg.bus_shared = false;
cfg.offset_rotation = 0;
cfg.i2c_port = I2C_NUM_1; // IMPORTANT: Port 1, not 0!
cfg.pin_sda = TOUCH_SDA;
cfg.pin_scl = TOUCH_SCL;
cfg.freq = 400000;
cfg.i2c_addr = 0x14; // IMPORTANT: Address 0x14, not 0x5D!
_touch_instance.config(cfg);
_panel_instance.setTouch(&_touch_instance);
}
setPanel(&_panel_instance);
}
// RGB parallel bus config (from Westcott)
{
auto cfg = _bus_instance.config();
cfg.panel = &_panel_instance;
// Blue channel
cfg.pin_d0 = 14;
cfg.pin_d1 = 38;
cfg.pin_d2 = 18;
cfg.pin_d3 = 17;
cfg.pin_d4 = 10;
// Green channel
cfg.pin_d5 = 39;
cfg.pin_d6 = 0;
cfg.pin_d7 = 45;
cfg.pin_d8 = 48;
cfg.pin_d9 = 47;
cfg.pin_d10 = 21;
// Red channel
cfg.pin_d11 = 1;
cfg.pin_d12 = 2;
cfg.pin_d13 = 42;
cfg.pin_d14 = 41;
cfg.pin_d15 = 40;
// Timing
cfg.pin_henable = 5;
cfg.pin_vsync = 3;
cfg.pin_hsync = 46;
cfg.pin_pclk = 7;
cfg.freq_write = 14000000;
cfg.hsync_polarity = 0;
cfg.hsync_front_porch = 20;
cfg.hsync_pulse_width = 10;
cfg.hsync_back_porch = 10;
cfg.vsync_polarity = 0;
cfg.vsync_front_porch = 10;
cfg.vsync_pulse_width = 10;
cfg.vsync_back_porch = 10;
cfg.pclk_active_neg = 0;
cfg.de_idle_high = 0;
cfg.pclk_idle_high = 0;
_bus_instance.config(cfg);
}
_panel_instance.setBus(&_bus_instance);
// Touch config (I2C port 1, address 0x14 - from Westcott!)
{
auto cfg = _touch_instance.config();
cfg.x_min = 0;
cfg.x_max = TFT_HOR_RES - 1;
cfg.y_min = 0;
cfg.y_max = TFT_VER_RES - 1;
cfg.pin_int = TOUCH_INT;
cfg.pin_rst = TOUCH_RST;
cfg.bus_shared = false;
cfg.offset_rotation = 0;
cfg.i2c_port = I2C_NUM_1; // IMPORTANT: Port 1, not 0!
cfg.pin_sda = TOUCH_SDA;
cfg.pin_scl = TOUCH_SCL;
cfg.freq = 400000;
cfg.i2c_addr = 0x14; // IMPORTANT: Address 0x14, not 0x5D!
_touch_instance.config(cfg);
_panel_instance.setTouch(&_touch_instance);
}
setPanel(&_panel_instance);
}
};

View File

@@ -2,14 +2,15 @@
// 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);
DoorbellLogic logic(&display);
DisplayManager display(&gfxDriver);
DoorbellLogic logic(&display);
void setup() {
Serial.begin(115200);
@@ -20,35 +21,60 @@ void setup() {
}
void loop() {
// ── State machine tick ──
logic.update();
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) {
// Track initial hold position for hint
static int holdStartX = -1;
static int holdStartY = -1;
if(st.deviceState == DeviceState::ALERTING) {
HoldState h = display.updateHold(HOLD_TO_SILENCE_MS);
if (h.completed) {
if(h.completed) {
logic.silenceAlert();
holdStartX = -1;
holdStartY = -1;
}
if (!h.active) {
display.updateHint();
if(h.started) {
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);
} else {
display.updateHint(holdStartX, holdStartY, false);
}
}
} else {
TouchEvent evt = display.readTouch();
if (evt.pressed && st.screen == ScreenID::DASHBOARD) {
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);
if(tile >= 0) {
Serial.printf("[DASH] Tile %d tapped\n", tile);
}
} else if(st.screen == ScreenID::ALERT) {
Serial.println("[TOUCH] ALERT tap");
}
}
// ── Serial console ──
if (Serial.available()) {
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd.length() > 0) logic.onSerialCommand(cmd);
if(cmd.length() > 0)
logic.onSerialCommand(cmd);
}
}

View File

@@ -1,23 +1,41 @@
#pragma once
#include "IDisplayDriver.h"
/// Owns a pointer to the concrete driver; all calls delegate.
/// Board sketch creates the concrete driver and passes it in.
class DisplayManager {
public:
DisplayManager() : _drv(nullptr) {}
explicit DisplayManager(IDisplayDriver* drv) : _drv(drv) {}
void setDriver(IDisplayDriver* drv) { _drv = drv; }
DisplayManager(IDisplayDriver* drv)
: _drv(drv) { }
void begin() { if (_drv) _drv->begin(); }
void setBacklight(bool on) { if (_drv) _drv->setBacklight(on); }
void render(const ScreenState& st) { if (_drv) _drv->render(st); }
TouchEvent readTouch() { return _drv ? _drv->readTouch() : TouchEvent{}; }
int dashboardTouch(int x, int y) { return _drv ? _drv->dashboardTouch(x, y) : -1; }
HoldState updateHold(unsigned long ms) { return _drv ? _drv->updateHold(ms) : HoldState{}; }
void updateHint() { if (_drv) _drv->updateHint(); }
int width() { return _drv ? _drv->width() : 0; }
int height() { return _drv ? _drv->height() : 0; }
void begin() {
if(_drv)
_drv->begin();
}
void setBacklight(bool on) {
if(_drv)
_drv->setBacklight(on);
}
void render(const ScreenState& st) {
if(_drv)
_drv->render(st);
}
TouchEvent readTouch() { return _drv ? _drv->readTouch() : TouchEvent {}; }
int dashboardTouch(int x, int y) { return _drv ? _drv->dashboardTouch(x, y) : -1; }
HoldState updateHold(unsigned long ms) { return _drv ? _drv->updateHold(ms) : HoldState {}; }
void updateHint(int x, int y, bool active) {
if(_drv)
_drv->updateHint(x, y, active);
}
int width() { return _drv ? _drv->width() : 0; }
int height() { return _drv ? _drv->height() : 0; }
private:
IDisplayDriver* _drv;

View File

@@ -1,29 +1,29 @@
#include "DoorbellLogic.h"
DoorbellLogic::DoorbellLogic(DisplayManager* display)
: _display(display) {}
: _display(display) { }
// ── URL builder ─────────────────────────────────────────────
String DoorbellLogic::topicUrl(const char* base) {
String suffix = _debug ? "_test" : "";
return String("https://") + NTFY_SERVER + "/" + base + suffix
+ "/json?since=20s&poll=1";
return String("https://") + NTFY_SERVER + "/" + base + suffix + "/json?since=20s&poll=1";
}
// ── Lifecycle ───────────────────────────────────────────────
void DoorbellLogic::begin(const char* version, const char* boardName,
const WiFiCred* creds, int credCount) {
void DoorbellLogic::begin(
const char* version, const char* boardName, const WiFiCred* creds, int credCount) {
_version = version;
_board = boardName;
_board = boardName;
#ifdef DEBUG_MODE
_debug = true;
#endif
Serial.println(F("========================================"));
Serial.printf( " KLUBHAUS ALERT v%s — %s\n", _version, _board);
if (_debug) Serial.println(F(" *** DEBUG MODE — _test topics ***"));
Serial.printf(" KLUBHAUS ALERT v%s — %s\n", _version, _board);
if(_debug)
Serial.println(F(" *** DEBUG MODE — _test topics ***"));
Serial.println(F("========================================\n"));
// Display
@@ -32,29 +32,28 @@ void DoorbellLogic::begin(const char* version, const char* boardName,
// Network
_net.begin(creds, credCount);
if (_net.isConnected()) {
if(_net.isConnected()) {
_net.syncNTP();
Serial.printf("[NET] WiFi:%s RSSI:%d IP:%s\n",
_net.getSSID().c_str(), _net.getRSSI(),
_net.getIP().c_str());
Serial.printf("[NET] WiFi:%s RSSI:%d IP:%s\n", _net.getSSID().c_str(), _net.getRSSI(),
_net.getIP().c_str());
_net.dnsCheck(NTFY_SERVER);
_net.tlsCheck(NTFY_SERVER);
}
// Topic URLs
_alertUrl = topicUrl(ALERT_TOPIC);
_alertUrl = topicUrl(ALERT_TOPIC);
_silenceUrl = topicUrl(SILENCE_TOPIC);
_adminUrl = topicUrl(ADMIN_TOPIC);
String sfx = _debug ? "_test" : "";
_statusUrl = String("https://") + NTFY_SERVER + "/" + STATUS_TOPIC + sfx;
_adminUrl = topicUrl(ADMIN_TOPIC);
String sfx = _debug ? "_test" : "";
_statusUrl = String("https://") + NTFY_SERVER + "/" + STATUS_TOPIC + sfx;
Serial.printf("[CONFIG] ALERT_URL: %s\n", _alertUrl.c_str());
Serial.printf("[CONFIG] SILENCE_URL: %s\n", _silenceUrl.c_str());
Serial.printf("[CONFIG] ADMIN_URL: %s\n", _adminUrl.c_str());
Serial.printf("[CONFIG] ALERT_URL: %s\n", _alertUrl.c_str());
Serial.printf("[CONFIG] SILENCE_URL: %s\n", _silenceUrl.c_str());
Serial.printf("[CONFIG] ADMIN_URL: %s\n", _adminUrl.c_str());
// Boot status
flushStatus(String("BOOTED — ") + _net.getSSID() + " "
+ _net.getIP() + " RSSI:" + String(_net.getRSSI()));
flushStatus(String("BOOTED — ") + _net.getSSID() + " " + _net.getIP()
+ " RSSI:" + String(_net.getRSSI()));
}
void DoorbellLogic::finishBoot() {
@@ -73,114 +72,124 @@ void DoorbellLogic::update() {
uint32_t now = millis();
_state.uptimeMs = now;
if (_net.isConnected()) {
if(_net.isConnected()) {
_state.wifiRssi = _net.getRSSI();
_state.wifiSsid = _net.getSSID();
_state.ipAddr = _net.getIP();
_state.ipAddr = _net.getIP();
}
if (!_net.checkConnection()) return;
if(!_net.checkConnection())
return;
// Poll
if (now - _lastPollMs >= POLL_INTERVAL_MS) {
if(now - _lastPollMs >= POLL_INTERVAL_MS) {
_lastPollMs = now;
pollTopics();
}
// Heartbeat
if (now - _lastHeartbeatMs >= HEARTBEAT_INTERVAL_MS) {
if(now - _lastHeartbeatMs >= HEARTBEAT_INTERVAL_MS) {
_lastHeartbeatMs = now;
heartbeat();
}
// Auto-transitions
switch (_state.deviceState) {
case DeviceState::ALERTING:
if (now - _state.alertStartMs > ALERT_TIMEOUT_MS) {
Serial.println("[STATE] Alert timed out → SILENT");
transition(DeviceState::SILENT);
_state.screen = ScreenID::OFF;
_display->setBacklight(false);
_state.backlightOn = false;
}
break;
case DeviceState::SILENCED:
if (now - _state.silenceStartMs > SILENCE_DISPLAY_MS) {
Serial.println("[STATE] Silence display done → SILENT");
transition(DeviceState::SILENT);
_state.screen = ScreenID::OFF;
_display->setBacklight(false);
_state.backlightOn = false;
}
break;
default: break;
switch(_state.deviceState) {
case DeviceState::ALERTING:
if(now - _state.alertStartMs > ALERT_TIMEOUT_MS) {
Serial.println("[STATE] Alert timed out → SILENT");
transition(DeviceState::SILENT);
_state.screen = ScreenID::OFF;
_display->setBacklight(false);
_state.backlightOn = false;
}
break;
case DeviceState::SILENCED:
if(now - _state.silenceStartMs > SILENCE_DISPLAY_MS) {
Serial.println("[STATE] Silence display done → SILENT");
transition(DeviceState::SILENT);
_state.screen = ScreenID::OFF;
_display->setBacklight(false);
_state.backlightOn = false;
}
break;
default:
break;
}
}
// ── Polling ─────────────────────────────────────────────────
void DoorbellLogic::pollTopics() {
pollTopic(_alertUrl, "ALERT");
pollTopic(_alertUrl, "ALERT");
pollTopic(_silenceUrl, "SILENCE");
pollTopic(_adminUrl, "ADMIN");
pollTopic(_adminUrl, "ADMIN");
_state.lastPollMs = millis();
}
void DoorbellLogic::pollTopic(const String& url, const char* label) {
String body;
int code = _net.httpGet(url.c_str(), body);
if (code != 200 || body.length() == 0) return;
if(code != 200 || body.length() == 0)
return;
// ntfy returns newline-delimited JSON
int pos = 0;
while (pos < (int)body.length()) {
while(pos < (int)body.length()) {
int nl = body.indexOf('\n', pos);
if (nl < 0) nl = body.length();
if(nl < 0)
nl = body.length();
String line = body.substring(pos, nl);
line.trim();
pos = nl + 1;
if (line.length() == 0) continue;
if(line.length() == 0)
continue;
JsonDocument doc;
if (deserializeJson(doc, line)) continue;
if(deserializeJson(doc, line))
continue;
const char* evt = doc["event"] | "";
if (strcmp(evt, "message") != 0) continue;
if(strcmp(evt, "message") != 0)
continue;
const char* title = doc["title"] | "";
const char* title = doc["title"] | "";
const char* message = doc["message"] | "";
Serial.printf("[%s] title=\"%s\" message=\"%s\"\n",
label, title, message);
Serial.printf("[%s] title=\"%s\" message=\"%s\"\n", label, title, message);
if (strcmp(label, "ALERT") == 0) onAlert(String(title), String(message));
else if (strcmp(label, "SILENCE") == 0) onSilence();
else if (strcmp(label, "ADMIN") == 0) onAdmin(String(message));
if(strcmp(label, "ALERT") == 0)
onAlert(String(title), String(message));
else if(strcmp(label, "SILENCE") == 0)
onSilence();
else if(strcmp(label, "ADMIN") == 0)
onAdmin(String(message));
}
}
// ── Event handlers ──────────────────────────────────────────
void DoorbellLogic::onAlert(const String& title, const String& body) {
if (millis() < _bootGraceEnd) {
if(millis() < _bootGraceEnd) {
Serial.println("[ALERT] Ignored (boot grace)");
return;
}
Serial.printf("[ALERT] %s: %s\n", title.c_str(), body.c_str());
_state.alertTitle = title;
_state.alertBody = body;
_state.alertTitle = title;
_state.alertBody = body;
_state.alertStartMs = millis();
transition(DeviceState::ALERTING);
_state.screen = ScreenID::ALERT;
_state.screen = ScreenID::ALERT;
_display->setBacklight(true);
_state.backlightOn = true;
flushStatus("ALERT: " + title);
}
void DoorbellLogic::onSilence() {
if (_state.deviceState != DeviceState::ALERTING) return;
if(_state.deviceState != DeviceState::ALERTING)
return;
Serial.println("[SILENCE] Alert silenced");
_state.silenceStartMs = millis();
transition(DeviceState::SILENCED);
_state.screen = ScreenID::OFF;
_state.screen = ScreenID::OFF;
_display->setBacklight(false);
_state.backlightOn = false;
flushStatus("SILENCED");
@@ -190,20 +199,20 @@ void DoorbellLogic::silenceAlert() { onSilence(); }
void DoorbellLogic::onAdmin(const String& cmd) {
Serial.printf("[ADMIN] %s\n", cmd.c_str());
if (cmd == "reboot") {
if(cmd == "reboot") {
flushStatus("REBOOTING (admin)");
delay(500);
ESP.restart();
} else if (cmd == "dashboard") {
_state.screen = ScreenID::DASHBOARD;
} else if(cmd == "dashboard") {
_state.screen = ScreenID::DASHBOARD;
_display->setBacklight(true);
_state.backlightOn = true;
} else if (cmd == "off") {
_state.screen = ScreenID::OFF;
} else if(cmd == "off") {
_state.screen = ScreenID::OFF;
_display->setBacklight(false);
_state.backlightOn = false;
} else if (cmd == "status") {
heartbeat(); // re-uses heartbeat message format
} else if(cmd == "status") {
heartbeat(); // re-uses heartbeat message format
}
}
@@ -219,9 +228,8 @@ void DoorbellLogic::flushStatus(const String& message) {
void DoorbellLogic::heartbeat() {
String m = String("HEARTBEAT ") + deviceStateStr(_state.deviceState)
+ " up:" + String(millis() / 1000) + "s"
+ " RSSI:" + String(_net.getRSSI())
+ " heap:" + String(ESP.getFreeHeap());
+ " up:" + String(millis() / 1000) + "s" + " RSSI:" + String(_net.getRSSI())
+ " heap:" + String(ESP.getFreeHeap());
#ifdef BOARD_HAS_PSRAM
m += " psram:" + String(ESP.getFreePsram());
#endif
@@ -237,24 +245,36 @@ void DoorbellLogic::transition(DeviceState s) {
void DoorbellLogic::onSerialCommand(const String& cmd) {
Serial.printf("[CMD] %s\n", cmd.c_str());
if (cmd == "alert") onAlert("Test Alert", "Serial test");
else if (cmd == "silence") onSilence();
else if (cmd == "reboot") ESP.restart();
else if (cmd == "dashboard") onAdmin("dashboard");
else if (cmd == "off") onAdmin("off");
else if (cmd == "status") {
Serial.printf("[STATE] %s screen:%s bl:%s\n",
deviceStateStr(_state.deviceState),
screenIdStr(_state.screen),
_state.backlightOn ? "ON" : "OFF");
if(cmd == "alert")
onAlert("Test Alert", "Serial test");
else if(cmd == "silence")
onSilence();
else if(cmd == "reboot")
ESP.restart();
else if(cmd == "dashboard")
onAdmin("dashboard");
else if(cmd == "off")
onAdmin("off");
else if(cmd == "status") {
Serial.printf("[STATE] %s screen:%s bl:%s\n", deviceStateStr(_state.deviceState),
screenIdStr(_state.screen), _state.backlightOn ? "ON" : "OFF");
Serial.printf("[MEM] heap:%d", ESP.getFreeHeap());
#ifdef BOARD_HAS_PSRAM
Serial.printf(" psram:%d", ESP.getFreePsram());
#endif
Serial.println();
Serial.printf("[NET] %s RSSI:%d IP:%s\n",
_state.wifiSsid.c_str(), _state.wifiRssi,
_state.ipAddr.c_str());
}
else Serial.println(F("[CMD] alert|silence|reboot|dashboard|off|status"));
Serial.printf("[NET] %s RSSI:%d IP:%s\n", _state.wifiSsid.c_str(), _state.wifiRssi,
_state.ipAddr.c_str());
} 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;
}

View File

@@ -1,18 +1,18 @@
#pragma once
#include <Arduino.h>
#include <ArduinoJson.h>
#include "Config.h"
#include "ScreenState.h"
#include "DisplayManager.h"
#include "NetManager.h"
#include "ScreenState.h"
#include <Arduino.h>
#include <ArduinoJson.h>
class DoorbellLogic {
public:
explicit DoorbellLogic(DisplayManager* display);
/// Call from setup(). Pass board-specific WiFi creds.
void begin(const char* version, const char* boardName,
const WiFiCred* creds, int credCount);
void begin(const char* version, const char* boardName, const WiFiCred* creds, int credCount);
/// Call from loop() — polls topics, runs timers, transitions state.
void update();
/// Transition out of BOOTED → SILENT. Call at end of setup().
@@ -24,6 +24,7 @@ public:
/// Externally trigger silence (e.g. hold-to-silence gesture).
void silenceAlert();
void setScreen(ScreenID s);
private:
void pollTopics();
@@ -37,16 +38,16 @@ private:
String topicUrl(const char* base);
DisplayManager* _display;
NetManager _net;
ScreenState _state;
NetManager _net;
ScreenState _state;
const char* _version = "";
const char* _board = "";
bool _debug = false;
const char* _version = "";
const char* _board = "";
bool _debug = false;
uint32_t _lastPollMs = 0;
uint32_t _lastPollMs = 0;
uint32_t _lastHeartbeatMs = 0;
uint32_t _bootGraceEnd = 0;
uint32_t _bootGraceEnd = 0;
String _alertUrl;
String _silenceUrl;

View File

@@ -3,14 +3,15 @@
struct TouchEvent {
bool pressed = false;
int x = 0;
int y = 0;
int x = 0;
int y = 0;
};
struct HoldState {
bool active = false;
bool completed = false;
float progress = 0.0f; // 0.0 1.0
bool active = false;
bool started = false;
bool completed = false;
float progress = 0.0f; // 0.0 1.0
};
/// Abstract display driver — implemented per-board.
@@ -27,12 +28,12 @@ public:
// ── Touch ──
virtual TouchEvent readTouch() = 0;
/// Returns tile index at (x,y), or -1 if none.
virtual int dashboardTouch(int x, int y) = 0;
virtual int dashboardTouch(int x, int y) = 0;
/// Track a long-press gesture; returns progress/completion.
virtual HoldState updateHold(unsigned long holdMs) = 0;
/// Idle hint animation (e.g. pulsing ring) while alert is showing.
virtual void updateHint() = 0;
virtual int width() = 0;
/// @param active If true, show "holding" animation; if false, show "idle" animation.
virtual void updateHint(int x, int y, bool active) = 0;
virtual int width() = 0;
virtual int height() = 0;
};

View File

@@ -2,8 +2,8 @@
// Umbrella header — board sketches just #include <KlubhausCore.h>
#include "Config.h"
#include "ScreenState.h"
#include "IDisplayDriver.h"
#include "DisplayManager.h"
#include "NetManager.h"
#include "DoorbellLogic.h"
#include "IDisplayDriver.h"
#include "NetManager.h"
#include "ScreenState.h"

View File

@@ -4,44 +4,44 @@
void NetManager::begin(const WiFiCred* creds, int count) {
WiFi.mode(WIFI_STA);
for (int i = 0; i < count; i++)
for(int i = 0; i < count; i++)
_multi.addAP(creds[i].ssid, creds[i].pass);
Serial.println("[WIFI] Connecting...");
unsigned long t0 = millis();
while (_multi.run() != WL_CONNECTED) {
if (millis() - t0 > WIFI_CONNECT_TIMEOUT_MS) {
while(_multi.run() != WL_CONNECTED) {
if(millis() - t0 > WIFI_CONNECT_TIMEOUT_MS) {
Serial.println("[WIFI] Timeout!");
return;
}
delay(250);
}
Serial.printf("[WIFI] Connected: %s %s\n",
getSSID().c_str(), getIP().c_str());
Serial.printf("[WIFI] Connected: %s %s\n", getSSID().c_str(), getIP().c_str());
}
bool NetManager::checkConnection() {
if (WiFi.status() == WL_CONNECTED) return true;
if(WiFi.status() == WL_CONNECTED)
return true;
Serial.println("[WIFI] Reconnecting...");
return _multi.run(WIFI_CONNECT_TIMEOUT_MS) == WL_CONNECTED;
}
bool NetManager::isConnected() { return WiFi.status() == WL_CONNECTED; }
String NetManager::getSSID() { return WiFi.SSID(); }
String NetManager::getIP() { return WiFi.localIP().toString(); }
int NetManager::getRSSI() { return WiFi.RSSI(); }
bool NetManager::isConnected() { return WiFi.status() == WL_CONNECTED; }
String NetManager::getSSID() { return WiFi.SSID(); }
String NetManager::getIP() { return WiFi.localIP().toString(); }
int NetManager::getRSSI() { return WiFi.RSSI(); }
// ── NTP ─────────────────────────────────────────────────────
bool NetManager::syncNTP() {
Serial.println("[NTP] Starting sync...");
if (!_ntp) _ntp = new NTPClient(_udp, "pool.ntp.org", 0, 60000);
if(!_ntp)
_ntp = new NTPClient(_udp, "pool.ntp.org", 0, 60000);
_ntp->begin();
_ntp->forceUpdate();
_ntpReady = _ntp->isTimeSet();
Serial.printf("[NTP] %s: %s UTC\n",
_ntpReady ? "Synced" : "FAILED",
_ntpReady ? _ntp->getFormattedTime().c_str() : "--");
Serial.printf("[NTP] %s: %s UTC\n", _ntpReady ? "Synced" : "FAILED",
_ntpReady ? _ntp->getFormattedTime().c_str() : "--");
return _ntpReady;
}
@@ -54,8 +54,7 @@ String NetManager::getTimeStr() {
bool NetManager::dnsCheck(const char* host) {
IPAddress ip;
bool ok = WiFi.hostByName(host, ip);
Serial.printf("[NET] DNS %s: %s\n", ok ? "OK" : "FAIL",
ok ? ip.toString().c_str() : "");
Serial.printf("[NET] DNS %s: %s\n", ok ? "OK" : "FAIL", ok ? ip.toString().c_str() : "");
return ok;
}
@@ -64,7 +63,8 @@ bool NetManager::tlsCheck(const char* host) {
c.setInsecure();
c.setTimeout(HTTP_TIMEOUT_MS);
bool ok = c.connect(host, 443);
if (ok) c.stop();
if(ok)
c.stop();
Serial.printf("[NET] TLS %s\n", ok ? "OK" : "FAIL");
return ok;
}
@@ -81,7 +81,8 @@ int NetManager::httpGet(const char* url, String& response) {
http.begin(client, url);
int code = http.GET();
if (code > 0) response = http.getString();
if(code > 0)
response = http.getString();
http.end();
return code;
}

View File

@@ -1,12 +1,13 @@
#pragma once
#include "Config.h"
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiMulti.h>
#include <WiFiUdp.h>
#include "Config.h"
class NetManager {
public:
@@ -16,9 +17,9 @@ public:
String getSSID();
String getIP();
int getRSSI();
int getRSSI();
bool syncNTP();
bool syncNTP();
String getTimeStr();
bool dnsCheck(const char* host);
@@ -30,8 +31,8 @@ public:
int httpPost(const char* url, const String& body);
private:
WiFiMulti _multi;
WiFiUDP _udp;
NTPClient* _ntp = nullptr;
bool _ntpReady = false;
WiFiMulti _multi;
WiFiUDP _udp;
NTPClient* _ntp = nullptr;
bool _ntpReady = false;
};

View File

@@ -1,54 +1,54 @@
#pragma once
#include <Arduino.h>
enum class DeviceState {
BOOTED,
SILENT,
ALERTING,
SILENCED
};
enum class DeviceState { BOOTED, SILENT, ALERTING, SILENCED };
enum class ScreenID {
BOOT,
OFF,
ALERT,
DASHBOARD
};
enum class ScreenID { BOOT, OFF, ALERT, DASHBOARD };
struct ScreenState {
DeviceState deviceState = DeviceState::BOOTED;
ScreenID screen = ScreenID::BOOT;
ScreenID screen = ScreenID::BOOT;
String alertTitle;
String alertBody;
uint32_t alertStartMs = 0;
String alertTitle;
String alertBody;
uint32_t alertStartMs = 0;
uint32_t silenceStartMs = 0;
bool backlightOn = false;
int wifiRssi = 0;
String wifiSsid;
String ipAddr;
uint32_t uptimeMs = 0;
uint32_t lastPollMs = 0;
uint32_t lastHeartbeatMs= 0;
bool backlightOn = false;
int wifiRssi = 0;
String wifiSsid;
String ipAddr;
uint32_t uptimeMs = 0;
uint32_t lastPollMs = 0;
uint32_t lastHeartbeatMs = 0;
bool showDashboard = false;
};
inline const char* deviceStateStr(DeviceState s) {
switch (s) {
case DeviceState::BOOTED: return "BOOTED";
case DeviceState::SILENT: return "SILENT";
case DeviceState::ALERTING: return "ALERTING";
case DeviceState::SILENCED: return "SILENCED";
switch(s) {
case DeviceState::BOOTED:
return "BOOTED";
case DeviceState::SILENT:
return "SILENT";
case DeviceState::ALERTING:
return "ALERTING";
case DeviceState::SILENCED:
return "SILENCED";
}
return "?";
}
inline const char* screenIdStr(ScreenID s) {
switch (s) {
case ScreenID::BOOT: return "BOOT";
case ScreenID::OFF: return "OFF";
case ScreenID::ALERT: return "ALERT";
case ScreenID::DASHBOARD: return "DASHBOARD";
switch(s) {
case ScreenID::BOOT:
return "BOOT";
case ScreenID::OFF:
return "OFF";
case ScreenID::ALERT:
return "ALERT";
case ScreenID::DASHBOARD:
return "DASHBOARD";
}
return "?";
}

View File

@@ -7,7 +7,6 @@ description = "Install shared (platform-independent) libraries"
run = """
arduino-cli lib install "ArduinoJson@7.4.1"
arduino-cli lib install "NTPClient@3.2.1"
arduino-cli lib install "ESP32_IO_Expander@0.0.4"
echo "[OK] Shared libraries installed"
"""
@@ -33,15 +32,37 @@ run = """
#!/usr/bin/env bash
set -euo pipefail
LOVyanGFX_DIR="vendor/esp32-s3-lcd-43/LovyanGFX"
# Clone LovyanGFX (latest)
if [ ! -d "vendor/esp32-s3-lcd-43/LovyanGFX" ]; then
if [ ! -d "$LOVyanGFX_DIR" ]; then
echo "Cloning LovyanGFX..."
git clone --depth 1 \
https://github.com/lovyan03/LovyanGFX.git \
vendor/esp32-s3-lcd-43/LovyanGFX
"$LOVyanGFX_DIR"
else
echo "LovyanGFX already exists, skipping"
fi
# Create library.properties that correctly points to source
cat > "$LOVyanGFX_DIR/library.properties" << 'EOF'
name=LovyanGFX
version=1.2.0
author=lovyan03
maintainer=lovyan03
sentence=Display and touch driver library for ESP32
paragraph=Universal graphics library for ESP32 with support for various displays and touch controllers
category=Display
url=https://github.com/lovyan03/LovyanGFX
architectures=esp32
includes=LovyanGFX.hpp
# This tells Arduino to build from src/
# Arduino will look in src/ for .cpp files
EOF
# Create a empty src to ensure sources are found
mkdir -p "$LOVyanGFX_DIR/src"
echo "[OK] LovyanGFX vendored"
"""
@@ -56,13 +77,12 @@ description = "Compile ESP32-32E sketch"
depends = ["install-libs"]
run = """
arduino-cli compile \
--fqbn "esp32:esp32:waveshare_esp32_s3_touch_lcd_43:PSRAM=enabled,FlashSize=16M,USBMode=hwcdc,PartitionScheme=app3M_fat9M_16MB" \
--fqbn "esp32:esp32@2.0.11:esp32:FlashSize=4M,PartitionScheme=default" \
--libraries ./libraries \
--libraries ./vendor/esp32-s3-lcd-43 \
--libraries ./vendor/esp32-32e/TFT_eSPI \
--build-property "compiler.cpp.extra_flags=-DDEBUG_MODE -DBOARD_HAS_PSRAM" \
--build-property "build.extra_flags=-DCONFIG_ESP32S3_OLD_I2C_LEGACY_DEVICE_COMPAT_MODE=1" \
--warnings default \
./boards/esp32-s3-lcd-43
./boards/esp32-32e
"""
[tasks.upload-32e]
@@ -83,14 +103,14 @@ arduino-cli monitor --port "${PORT:-/dev/ttyUSB0}" --config baudrate=115200
# ── ESP32-S3-LCD-4.3 ────────────────────────────────────
[tasks.compile-s3-43]
description = "Compile ESP32-S3-LCD-4.3 sketch"
description = "Compile ESP32-S3-LCD-4.3 sketch (Core 2.x)"
depends = ["install-libs"]
run = """
arduino-cli compile \
--fqbn "esp32:esp32:waveshare_esp32_s3_touch_lcd_43:PSRAM=enabled,FlashSize=16M,USBMode=hwcdc,PartitionScheme=app3M_fat9M_16MB" \
--libraries ./libraries \
--libraries ./vendor/esp32-s3-lcd-43/LovyanGFX \
--build-property "compiler.cpp.extra_flags=-DDEBUG_MODE -DBOARD_HAS_PSRAM" \
--library ./vendor/esp32-s3-lcd-43/LovyanGFX \
--build-property "compiler.cpp.extra_flags=-DDEBUG_MODE -DBOARD_HAS_PSRAM -DLGFX_USE_V1" \
--warnings default \
./boards/esp32-s3-lcd-43
"""
@@ -119,3 +139,17 @@ rm -rf boards/esp32-32e/build
rm -rf boards/esp32-s3-lcd-43/build
echo "[OK] Build artifacts cleaned"
"""
[tasks.format]
run = """
clang-format -i --style=file \
boards/esp32-32e/*.cpp \
boards/esp32-32e/*.h \
boards/esp32-32e/*.ino \
boards/esp32-s3-lcd-43/*.cpp \
boards/esp32-s3-lcd-43/*.h \
boards/esp32-s3-lcd-43/*.ino \
libraries/KlubhausCore/src/*.cpp \
libraries/KlubhausCore/src/*.h \
libraries/KlubhausCore/*.properties
"""

View File

@@ -0,0 +1,2 @@
*
!.gitignore