From 9594bd84eed8fdd7e73b681d43613c16f89bbb17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Kopi=C4=87?= Date: Thu, 7 Nov 2024 17:45:47 +0100 Subject: [PATCH] Improve power managment, fix display override --- src/main.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3461c47..40ae279 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ WiFiClient client; // Define NTP Client to get time WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600); +NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 43200000); // Define these in config.h const String Location = LOCATION; @@ -60,10 +60,11 @@ void initSystems() { // Timers for dynamic updates unsigned long lastWeatherUpdate = 0; -unsigned long lastDisplayUpdate = 0; +unsigned long lastDisplayOverride = 0; const unsigned long weatherUpdateInterval = 1800000; // 30 minutes in milliseconds -const unsigned long displayOffInterval = 300000; // 5 minutes in milliseconds for power-saving mode +const unsigned long displayOverrideTimeout = 60000; // 1 minute in milliseconds bool is_display_off = false; +bool display_override = false; void commonButtonHandler() { unsigned long currentMillis = millis(); @@ -114,6 +115,7 @@ void commonButtonHandler() { if (is_display_off) { display.ssd1306_command(SSD1306_DISPLAYON); // Turn on display is_display_off = false; + display_override = true; beep(1300); } } @@ -145,6 +147,7 @@ void commonButtonHandler() { beep(1300); display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display is_display_off = true; + display_override = false; beep(1000); } } @@ -158,13 +161,9 @@ void commonButtonHandler() { void powerSaveCheck() { int currentHour = timeClient.getHours(); - if ((currentHour >= 23 || currentHour < 6) && !is_display_off) { + if ((currentHour >= 22 || currentHour < 8) && !is_display_off && !display_override) { display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display for power-saving is_display_off = true; - lastDisplayUpdate = millis(); // Track when display was turned off - } else if (is_display_off && (millis() - lastDisplayUpdate > displayOffInterval)) { - display.ssd1306_command(SSD1306_DISPLAYON); // Turn display back on - is_display_off = false; } } @@ -323,6 +322,10 @@ void setup(void) { void loop() { timeClient.update(); + if (display_override && (millis() - lastDisplayOverride > displayOverrideTimeout)){ + display_override = false; + lastDisplayOverride = millis(); + } if (!is_display_off && (millis() - lastWeatherUpdate > weatherUpdateInterval)) { fetchWeatherData(); lastWeatherUpdate = millis();