diff --git a/sketches/doorbell/doorbell.ino b/sketches/doorbell/doorbell.ino index 5b38573..96fe7e5 100644 --- a/sketches/doorbell/doorbell.ino +++ b/sketches/doorbell/doorbell.ino @@ -1,6 +1,7 @@ #include #include #include +#include // ============== CONFIGURATION ============== const char* ssid = "iot-2GHz"; @@ -11,12 +12,33 @@ const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1"; const unsigned long POLL_INTERVAL = 15000; const unsigned long SILENCE_POLL_INTERVAL = 15000; -const unsigned long BLINK_DURATION = 180000; // 3 minutes -const unsigned long BLINK_PERIOD = 1000; // 1 second on/off +const unsigned long BLINK_DURATION = 180000; +const unsigned long BLINK_PERIOD = 1000; -// Hardware pins (confirmed from Waveshare wiki 【0】【1】) -#define BACKLIGHT_PIN 22 // LCD backlight - simple on/off control -#define BUTTON_PIN 9 // BOOT button +// Hardware pins (from working Waveshare demo) +#define BACKLIGHT_PIN 22 +#define BUTTON_PIN 9 + +// Display colors +#define COLOR_BLACK 0x0000 +#define COLOR_RED 0xF800 +#define COLOR_GREEN 0x07E0 +#define COLOR_BLUE 0x001F +#define COLOR_WHITE 0xFFFF + +// =========================================== + +// Arduino_GFX setup for Waveshare ESP32-C6-LCD-1.47 +// ST7789, 172x320, with column offset 34 【2】 +Arduino_DataBus *bus = new Arduino_HWSPI( + 15 /* DC */, 14 /* CS */, + 7 /* SCK */, 6 /* MOSI */, -1 /* MISO */); + +Arduino_GFX *gfx = new Arduino_ST7789( + bus, 21 /* RST */, 0 /* rotation */, true /* IPS */, + 172 /* width */, 320 /* height */, + 34 /* col offset 1 */, 0 /* row offset 1 */, + 34 /* col offset 2 */, 0 /* row offset 2 */); // =========================================== @@ -36,28 +58,30 @@ bool lastButtonState = false; void setup() { Serial.begin(115200); delay(3000); - Serial.println("\n=== BACKLIGHT DOORBELL ==="); + Serial.println("\n=== GFX DOORBELL ==="); - // Init backlight as digital output + // Init backlight pinMode(BACKLIGHT_PIN, OUTPUT); - digitalWrite(BACKLIGHT_PIN, LOW); // Start OFF - Serial.println("Backlight on GPIO 22 ready"); + digitalWrite(BACKLIGHT_PIN, HIGH); + Serial.println("Backlight ON"); // Init button pinMode(BUTTON_PIN, INPUT_PULLUP); - Serial.println("Button on GPIO 9 ready"); + Serial.println("Button ready"); - // Test: flash 3 times to confirm working - Serial.println("Backlight test..."); - for (int i = 0; i < 3; i++) { - digitalWrite(BACKLIGHT_PIN, HIGH); - delay(100); - digitalWrite(BACKLIGHT_PIN, LOW); - delay(100); - } - Serial.println("Test complete - OFF"); + // Init display + Serial.println("Init GFX..."); + gfx->begin(); + Serial.println("GFX begin OK"); - // Start in silent state + gfx->fillScreen(COLOR_BLACK); + Serial.println("Screen cleared"); + + // Test pattern + Serial.println("Display test..."); + testDisplay(); + + // Start silent transitionTo(STATE_SILENT); // WiFi @@ -73,25 +97,21 @@ void setup() { if (WiFi.status() != WL_CONNECTED) { Serial.println("\nWiFi FAILED"); - // Error flash: rapid blinking - for (int i = 0; i < 10; i++) { - digitalWrite(BACKLIGHT_PIN, HIGH); - delay(50); - digitalWrite(BACKLIGHT_PIN, LOW); - delay(50); - } + gfx->fillScreen(COLOR_RED); + gfx->setTextColor(COLOR_WHITE, COLOR_RED); + gfx->setTextSize(2); + gfx->setCursor(10, 140); + gfx->println("NO WIFI"); } else { Serial.println("\nWiFi OK: " + WiFi.localIP().toString()); - // Success: single long flash - digitalWrite(BACKLIGHT_PIN, HIGH); + gfx->setTextColor(COLOR_GREEN, COLOR_BLACK); + gfx->setTextSize(2); + gfx->setCursor(10, 10); + gfx->println("WiFi OK"); delay(500); - digitalWrite(BACKLIGHT_PIN, LOW); + gfx->fillScreen(COLOR_BLACK); } - // Ensure OFF before main loop - digitalWrite(BACKLIGHT_PIN, LOW); - Serial.println("Backlight OFF - ready"); - Serial.println("=== SETUP COMPLETE ==="); Serial.println(String("State: ") + getStateName(currentState)); } @@ -142,15 +162,15 @@ void transitionTo(State newState) { switch (currentState) { case STATE_SILENT: - digitalWrite(BACKLIGHT_PIN, LOW); - Serial.println("Backlight OFF"); + gfx->fillScreen(COLOR_BLACK); + Serial.println("Screen BLACK (silent)"); break; case STATE_ALARM: blinkStartTime = millis(); blinkState = false; - digitalWrite(BACKLIGHT_PIN, HIGH); // Start ON - Serial.println("Backlight BLINK started"); + gfx->fillScreen(COLOR_RED); + Serial.println("Screen ALARM started (red/white blink)"); break; } } @@ -165,28 +185,23 @@ void handleButton() { bool pressed = (digitalRead(BUTTON_PIN) == LOW); if (pressed && !lastButtonState) { - // Button just pressed Serial.println("BUTTON PRESSED"); - digitalWrite(BACKLIGHT_PIN, HIGH); // Solid on while held - Serial.println("Backlight ON (held)"); + gfx->fillScreen(COLOR_WHITE); + Serial.println("Screen WHITE (held)"); } else if (!pressed && lastButtonState) { - // Button just released Serial.println("BUTTON RELEASED"); if (currentState == STATE_ALARM) { - // NEW: Reset to silent when released in alarm state Serial.println("Reset ALARM -> SILENT"); transitionTo(STATE_SILENT); - // Brief confirmation flash - delay(100); - digitalWrite(BACKLIGHT_PIN, HIGH); - delay(100); - digitalWrite(BACKLIGHT_PIN, LOW); + // Green flash confirmation + gfx->fillScreen(COLOR_GREEN); + delay(300); + gfx->fillScreen(COLOR_BLACK); } else { - // Already silent, ensure off - digitalWrite(BACKLIGHT_PIN, LOW); - Serial.println("Backlight OFF (silent)"); + gfx->fillScreen(COLOR_BLACK); + Serial.println("Screen BLACK (silent)"); } } @@ -226,10 +241,13 @@ void checkAlertTopic() { if (message.equalsIgnoreCase("SILENCE")) { Serial.println(" -> SILENCE"); transitionTo(STATE_SILENT); - // Confirmation flash - digitalWrite(BACKLIGHT_PIN, HIGH); - delay(200); - digitalWrite(BACKLIGHT_PIN, LOW); + gfx->fillScreen(COLOR_GREEN); + gfx->setTextColor(COLOR_BLACK, COLOR_GREEN); + gfx->setTextSize(3); + gfx->setCursor(20, 140); + gfx->println("SILENCE"); + delay(1000); + gfx->fillScreen(COLOR_BLACK); } else { Serial.println(" -> ALARM"); transitionTo(STATE_ALARM); @@ -254,10 +272,13 @@ void checkSilenceTopic() { if (currentState == STATE_ALARM) { Serial.println(" -> Stop alarm"); transitionTo(STATE_SILENT); - // Confirmation flash - digitalWrite(BACKLIGHT_PIN, HIGH); - delay(200); - digitalWrite(BACKLIGHT_PIN, LOW); + gfx->fillScreen(COLOR_GREEN); + gfx->setTextColor(COLOR_BLACK, COLOR_GREEN); + gfx->setTextSize(3); + gfx->setCursor(20, 140); + gfx->println("SILENCED"); + delay(1000); + gfx->fillScreen(COLOR_BLACK); } } @@ -284,16 +305,40 @@ String fetchNtfy(const char* url) { return payload; } -// ============== BLINK CONTROL ============== +// ============== DISPLAY FUNCTIONS ============== + +void testDisplay() { + Serial.println(" RED"); + gfx->fillScreen(COLOR_RED); + delay(200); + + Serial.println(" GREEN"); + gfx->fillScreen(COLOR_GREEN); + delay(200); + + Serial.println(" BLUE"); + gfx->fillScreen(COLOR_BLUE); + delay(200); + + Serial.println(" WHITE"); + gfx->fillScreen(COLOR_WHITE); + delay(200); + + Serial.println(" BLACK"); + gfx->fillScreen(COLOR_BLACK); + delay(200); + + Serial.println("Test complete"); +} void updateBlink(unsigned long now) { unsigned long elapsed = now - blinkStartTime; - bool newState = ((elapsed / BLINK_PERIOD) % 2) == 0; // Even = ON, odd = OFF + bool newState = ((elapsed / BLINK_PERIOD) % 2) == 1; if (newState != blinkState) { blinkState = newState; - digitalWrite(BACKLIGHT_PIN, blinkState ? HIGH : LOW); - Serial.println(blinkState ? "BLINK: ON" : "BLINK: OFF"); + gfx->fillScreen(blinkState ? COLOR_WHITE : COLOR_RED); + Serial.println(blinkState ? "WHITE" : "RED"); } }