From d023643090370e32408b129ba4167fa971431e5b Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 5 Jan 2025 21:19:28 +0100 Subject: [PATCH] Refactor LedHandler to a class --- src/lib/block_notify.cpp | 3 +- src/lib/config.cpp | 78 +-- src/lib/config.hpp | 8 +- src/lib/epd.cpp | 50 +- src/lib/led_handler.cpp | 1324 +++++++++++++++++--------------------- src/lib/led_handler.hpp | 142 ++-- src/lib/nostr_notify.cpp | 3 +- src/lib/ota.cpp | 10 +- src/lib/timers.cpp | 5 +- src/lib/webserver.cpp | 228 +++++-- src/main.cpp | 15 +- 11 files changed, 931 insertions(+), 935 deletions(-) diff --git a/src/lib/block_notify.cpp b/src/lib/block_notify.cpp index ec72138..111a7a7 100644 --- a/src/lib/block_notify.cpp +++ b/src/lib/block_notify.cpp @@ -1,4 +1,5 @@ #include "block_notify.hpp" +#include "led_handler.hpp" char *wsServer; esp_websocket_client_handle_t blockNotifyClient = NULL; @@ -217,7 +218,7 @@ void processNewBlock(uint32_t newBlockHeight) { if (preferences.getBool("ledFlashOnUpd", DEFAULT_LED_FLASH_ON_UPD)) { vTaskDelay(pdMS_TO_TICKS(250)); // Wait until screens are updated - queueLedEffect(LED_FLASH_BLOCK_NOTIFY); + getLedHandler().queueEffect(LED_FLASH_BLOCK_NOTIFY); } } } diff --git a/src/lib/config.cpp b/src/lib/config.cpp index f0db973..5a3fa7d 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -1,4 +1,5 @@ #include "config.hpp" +#include "led_handler.hpp" #define MAX_ATTEMPTS_WIFI_CONNECTION 20 @@ -50,7 +51,8 @@ void setup() setupDisplays(); if (preferences.getBool("ledTestOnPower", DEFAULT_LED_TEST_ON_POWER)) { - queueLedEffect(LED_POWER_TEST); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_POWER_TEST); } { std::lock_guard lockMcp(mcpMutex); @@ -60,7 +62,8 @@ void setup() preferences.remove("txPower"); WiFi.eraseAP(); - queueLedEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); } } @@ -76,7 +79,8 @@ void setup() else if (mcp1.read1(1) == LOW) { preferences.clear(); - queueLedEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); nvs_flash_erase(); delay(1000); @@ -116,13 +120,13 @@ void setup() #ifdef HAS_FRONTLIGHT if (!preferences.getBool("flAlwaysOn", DEFAULT_FL_ALWAYS_ON)) { - frontlightFadeOutAll(preferences.getUInt("flEffectDelay"), true); + auto& ledHandler = getLedHandler(); + ledHandler.frontlightFadeOutAll(preferences.getUInt("flEffectDelay"), true); flArray.allOFF(); } #endif forceFullRefresh(); - } void setupWifi() @@ -144,7 +148,8 @@ void setupWifi() // if (!preferences.getBool("wifiConfigured", DEFAULT_WIFI_CONFIGURED) { - queueLedEffect(LED_EFFECT_WIFI_WAIT_FOR_CONFIG); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_EFFECT_WIFI_WAIT_FOR_CONFIG); bool buttonPress = false; { @@ -279,7 +284,8 @@ void syncTime() while (!getLocalTime(&timeinfo)) { - queueLedEffect(LED_EFFECT_CONFIGURING); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_EFFECT_CONFIGURING); configTime(preferences.getInt("gmtOffset", DEFAULT_TIME_OFFSET_SECONDS), 0, NTP_SERVER); delay(500); @@ -420,13 +426,14 @@ void setupTimers() void finishSetup() { + auto& ledHandler = getLedHandler(); if (preferences.getBool("ledStatus", DEFAULT_LED_STATUS)) { - restoreLedState(); + ledHandler.restoreLedState(); } else { - clearLeds(); + ledHandler.clear(); } } @@ -475,22 +482,9 @@ void setupHardware() Serial.println(F("Error loading WebUI")); } - // { - // File f = LittleFS.open("/qr.txt", "w"); - - // if(f) { - // if (f.print("Hello")) { - // Serial.println(F("Written QR to FS")); - // Serial.printf("\nLittleFS free: %zu\n", LittleFS.totalBytes() - LittleFS.usedBytes()); - // } - // } else { - // Serial.println(F("Can't write QR to FS")); - // } - - // f.close(); - // } - - setupLeds(); + // Initialize LED handler + auto& ledHandler = getLedHandler(); + ledHandler.setup(); WiFi.setHostname(getMyHostname().c_str()); if (!psramInit()) @@ -548,7 +542,8 @@ void setupHardware() #endif #ifdef HAS_FRONTLIGHT - setupFrontlight(); + // Initialize frontlight through LedHandler + ledHandler.initializeFrontlight(); Wire.beginTransmission(0x5C); byte error = Wire.endTransmission(); @@ -570,6 +565,7 @@ void setupHardware() void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { static bool first_connect = true; + auto& ledHandler = getLedHandler(); // Get ledHandler reference once at the start Serial.printf("[WiFi-event] event: %d\n", event); @@ -595,7 +591,7 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) if (!first_connect) { Serial.println(F("Disconnected from WiFi access point")); - queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); + ledHandler.queueEffect(LED_EFFECT_WIFI_CONNECT_ERROR); uint8_t reason = info.wifi_sta_disconnected.reason; if (reason) Serial.printf("Disconnect reason: %s, ", @@ -611,13 +607,13 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) Serial.print("Obtained IP address: "); Serial.println(WiFi.localIP()); if (!first_connect) - queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); + ledHandler.queueEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); first_connect = false; break; } case ARDUINO_EVENT_WIFI_STA_LOST_IP: Serial.println(F("Lost IP address and IP address is reset to 0")); - queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); + ledHandler.queueEffect(LED_EFFECT_WIFI_CONNECT_ERROR); WiFi.reconnect(); break; case ARDUINO_EVENT_WIFI_AP_START: @@ -667,30 +663,6 @@ uint getLastTimeSync() } #ifdef HAS_FRONTLIGHT -void setupFrontlight() -{ - if (!flArray.begin(PCA9685_MODE1_AUTOINCR | PCA9685_MODE1_ALLCALL, PCA9685_MODE2_TOTEMPOLE)) - { - Serial.println(F("FL driver error")); - return; - } - flArray.setFrequency(200); - Serial.println(F("FL driver active")); - - if (!preferences.isKey("flMaxBrightness")) - { - preferences.putUInt("flMaxBrightness", DEFAULT_FL_MAX_BRIGHTNESS); - } - if (!preferences.isKey("flEffectDelay")) - { - preferences.putUInt("flEffectDelay", DEFAULT_FL_EFFECT_DELAY); - } - - if (!preferences.isKey("flFlashOnUpd")) - { - preferences.putBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE); - } -} float getLightLevel() { diff --git a/src/lib/config.hpp b/src/lib/config.hpp index d8e27b6..95c877d 100644 --- a/src/lib/config.hpp +++ b/src/lib/config.hpp @@ -52,10 +52,10 @@ void setupTimers(); void finishSetup(); void setupMcp(); #ifdef HAS_FRONTLIGHT -void setupFrontlight(); +extern BH1750 bh1750; +extern bool hasLuxSensor; float getLightLevel(); bool hasLightLevel(); -extern PCA9685 flArray; #endif String getMyHostname(); @@ -98,6 +98,10 @@ extern MCP23017 mcp1; extern MCP23017 mcp2; #endif +#ifdef HAS_FRONTLIGHT +extern PCA9685 flArray; +#endif + // Expose DataSourceType enum extern DataSourceType getDataSource(); extern void setDataSource(DataSourceType source); \ No newline at end of file diff --git a/src/lib/epd.cpp b/src/lib/epd.cpp index 04d3c5c..9808aa7 100644 --- a/src/lib/epd.cpp +++ b/src/lib/epd.cpp @@ -140,8 +140,6 @@ const GFXfont *FONT_SATSYMBOL; std::mutex epdUpdateMutex; std::mutex epdMutex[NUM_SCREENS]; -uint8_t qrcode[800]; - #ifdef IS_BTCLOCK_V8 #define EPD_TASK_STACK_SIZE 4096 #else @@ -159,8 +157,6 @@ void forceFullRefresh() } } -GFXfont font90; - void loadFonts(const String& fontName) { if (fontName == FontNames::ANTONIO) { // Load Antonio fonts @@ -628,35 +624,45 @@ bool renderIcon(const uint dispNum, const String &text, bool partial) void renderQr(const uint dispNum, const String &text, bool partial) { #ifdef USE_QR + // Dynamically allocate QR buffer + uint8_t* qrcode = (uint8_t*)malloc(qrcodegen_BUFFER_LEN_MAX); + if (!qrcode) { + log_e("Failed to allocate QR buffer"); + return; + } uint8_t tempBuffer[800]; bool ok = qrcodegen_encodeText( text.substring(2).c_str(), tempBuffer, qrcode, qrcodegen_Ecc_LOW, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true); - const int size = qrcodegen_getSize(qrcode); + if (ok) { + const int size = qrcodegen_getSize(qrcode); + const int padding = floor(float(displays[dispNum].width() - (size * 4)) / 2); + const int paddingY = + floor(float(displays[dispNum].height() - (size * 4)) / 2); + displays[dispNum].setRotation(2); - const int padding = floor(float(displays[dispNum].width() - (size * 4)) / 2); - const int paddingY = - floor(float(displays[dispNum].height() - (size * 4)) / 2); - displays[dispNum].setRotation(2); + displays[dispNum].setPartialWindow(0, 0, displays[dispNum].width(), + displays[dispNum].height()); + displays[dispNum].fillScreen(GxEPD_WHITE); + const int border = 0; - displays[dispNum].setPartialWindow(0, 0, displays[dispNum].width(), - displays[dispNum].height()); - displays[dispNum].fillScreen(GxEPD_WHITE); - const int border = 0; - - for (int y = -border; y < size * 4 + border; y++) - { - for (int x = -border; x < size * 4 + border; x++) + for (int y = -border; y < size * 4 + border; y++) { - displays[dispNum].drawPixel( - padding + x, paddingY + y, - qrcodegen_getModule(qrcode, floor(float(x) / 4), floor(float(y) / 4)) - ? GxEPD_BLACK - : GxEPD_WHITE); + for (int x = -border; x < size * 4 + border; x++) + { + displays[dispNum].drawPixel( + padding + x, paddingY + y, + qrcodegen_getModule(qrcode, floor(float(x) / 4), floor(float(y) / 4)) + ? GxEPD_BLACK + : GxEPD_WHITE); + } } } + + // Free the buffer after we're done + free(qrcode); #endif } diff --git a/src/lib/led_handler.cpp b/src/lib/led_handler.cpp index 806ee9c..3cdb953 100644 --- a/src/lib/led_handler.cpp +++ b/src/lib/led_handler.cpp @@ -1,227 +1,431 @@ #include "led_handler.hpp" -TaskHandle_t ledTaskHandle = NULL; -QueueHandle_t ledTaskQueue = NULL; -Adafruit_NeoPixel pixels(NEOPIXEL_COUNT, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); -uint ledTaskParams; +// Singleton instance +LedHandler& LedHandler::getInstance() { + static LedHandler instance; + return instance; +} + +LedHandler::LedHandler() + : pixels(NEOPIXEL_COUNT, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800) + , ledTaskHandle(nullptr) + , ledTaskQueue(nullptr) + , ledTaskParams(0) + , dndEnabled(false) + , dndTimeBasedEnabled(false) + , dndTimeRange{23, 0, 7, 0} // Default: 23:00 to 07:00 +#ifdef HAS_FRONTLIGHT + , frontlightOn(false) + , flInTransition(false) +#endif +{ +} + +void LedHandler::setup() { + loadDNDSettings(); + pixels.begin(); + pixels.setBrightness(preferences.getUInt("ledBrightness", DEFAULT_LED_BRIGHTNESS)); + pixels.clear(); + pixels.show(); + setupTask(); + + if (preferences.getBool("ledTestOnPower", DEFAULT_LED_TEST_ON_POWER)) { + while (!ledTaskQueue) { + delay(1); + } + queueEffect(LED_POWER_TEST); + } +} + +void LedHandler::setupTask() { + ledTaskQueue = xQueueCreate(5, sizeof(uint)); + xTaskCreate(ledTask, "LedTask", 2048, this, 10, &ledTaskHandle); +} + +void LedHandler::ledTask(void* pvParameters) { + auto* handler = static_cast(pvParameters); + while (true) { + if (handler->ledTaskQueue != nullptr) { + if (xQueueReceive(handler->ledTaskQueue, &handler->ledTaskParams, portMAX_DELAY) == pdPASS) { + if (preferences.getBool("disableLeds", DEFAULT_DISABLE_LEDS)) { + continue; + } + + std::array oldLights; + for (int i = 0; i < NEOPIXEL_COUNT; i++) { + oldLights[i] = handler->pixels.getPixelColor(i); + } #ifdef HAS_FRONTLIGHT -constexpr uint16_t FL_FADE_STEP = 25; - -bool frontlightOn = false; -bool flInTransition = false; - -void frontlightFlash(int flDelayTime) -{ - if (preferences.getBool("flDisable")) - return; - - if (frontlightOn) - { - frontlightFadeOutAll(flDelayTime, true); - frontlightFadeInAll(flDelayTime, true); - } - else - { - frontlightFadeInAll(flDelayTime, true); - frontlightFadeOutAll(flDelayTime, true); - } -} - -void frontlightFadeInAll() -{ - frontlightFadeInAll(preferences.getUInt("flEffectDelay")); -} - -void frontlightFadeOutAll() -{ - frontlightFadeOutAll(preferences.getUInt("flEffectDelay")); -} - -void frontlightFadeIn(uint num) -{ - frontlightFadeIn(num, preferences.getUInt("flEffectDelay")); -} - -void frontlightFadeOut(uint num) -{ - frontlightFadeOut(num, preferences.getUInt("flEffectDelay")); -} - -void frontlightSetBrightness(uint brightness) -{ - if (isDNDActive()) { - return; // Don't change brightness during DND mode - } - if (brightness > 4096) - { - return; - } - - for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) - { - flArray.setPWM(ledPin, 0, brightness); - } -} - -void frontlightFadeInAll(int flDelayTime) -{ - frontlightFadeInAll(flDelayTime, false); -} - -void frontlightFadeInAll(int flDelayTime, bool staggered) -{ - if (preferences.getBool("flDisable") || frontlightIsOn() || flInTransition) - return; - - flInTransition = true; - - const int maxBrightness = preferences.getUInt("flMaxBrightness"); - - if (staggered) - { - int step = FL_FADE_STEP; - int staggerDelay = flDelayTime / NUM_SCREENS; - - for (int dutyCycle = 0; dutyCycle <= maxBrightness + (NUM_SCREENS - 1) * maxBrightness / NUM_SCREENS; dutyCycle += step) - { - for (int ledPin = 0; ledPin < NUM_SCREENS; ledPin++) - { - int ledBrightness = dutyCycle - ledPin * maxBrightness / NUM_SCREENS; - if (ledBrightness < 0) - ledBrightness = 0; - else if (ledBrightness > maxBrightness) - ledBrightness = maxBrightness; - - flArray.setPWM(ledPin + 1, 0, ledBrightness); - } - vTaskDelay(pdMS_TO_TICKS(staggerDelay)); - } - } - else - { - for (int dutyCycle = 0; dutyCycle <= maxBrightness; dutyCycle += FL_FADE_STEP) - { - for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) - { - flArray.setPWM(ledPin, 0, dutyCycle); - } - vTaskDelay(pdMS_TO_TICKS(flDelayTime)); - } - } - frontlightOn = true; - flInTransition = false; -} - -void frontlightFadeOutAll(int flDelayTime) -{ - frontlightFadeOutAll(flDelayTime, false); -} - -void frontlightFadeOutAll(int flDelayTime, bool staggered) -{ - if (preferences.getBool("flDisable")) - return; - if (!frontlightIsOn()) - return; - if (flInTransition) - return; - flInTransition = true; - - if (staggered) - { - int maxBrightness = preferences.getUInt("flMaxBrightness"); - int step = FL_FADE_STEP; - int staggerDelay = flDelayTime / NUM_SCREENS; - - for (int dutyCycle = maxBrightness; dutyCycle >= 0; dutyCycle -= step) - { - for (int ledPin = 0; ledPin < NUM_SCREENS; ledPin++) - { - int ledBrightness = dutyCycle - (NUM_SCREENS - 1 - ledPin) * maxBrightness / NUM_SCREENS; - if (ledBrightness < 0) - ledBrightness = 0; - else if (ledBrightness > maxBrightness) - ledBrightness = maxBrightness; - - flArray.setPWM(ledPin + 1, 0, ledBrightness); - } - vTaskDelay(pdMS_TO_TICKS(staggerDelay)); - } - } - else - { - for (int dutyCycle = preferences.getUInt("flMaxBrightness"); dutyCycle >= 0; dutyCycle -= FL_FADE_STEP) - { - for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) - { - flArray.setPWM(ledPin, 0, dutyCycle); - } - vTaskDelay(pdMS_TO_TICKS(flDelayTime)); - } - } - - flArray.allOFF(); - frontlightOn = false; - flInTransition = false; -} - -std::vector frontlightGetStatus() -{ - std::vector statuses; - for (int ledPin = 1; ledPin <= NUM_SCREENS; ledPin++) - { - uint16_t a = 0, b = 0; - flArray.getPWM(ledPin, &a, &b); - statuses.push_back(round(b - a / 4096)); - } - - return statuses; -} - -bool frontlightIsOn() -{ - return frontlightOn; -} - -void frontlightFadeIn(uint num, int flDelayTime) -{ - if (isDNDActive()) { - return; // Don't change brightness during DND mode - } - if (preferences.getBool("flDisable")) - return; - for (int dutyCycle = 0; dutyCycle <= preferences.getUInt("flMaxBrightness"); dutyCycle += 5) - { - flArray.setPWM(num, 0, dutyCycle); - vTaskDelay(pdMS_TO_TICKS(flDelayTime)); - } -} - -void frontlightFadeOut(uint num, int flDelayTime) -{ - if (isDNDActive()) { - return; // Don't change brightness during DND mode - } - if (preferences.getBool("flDisable")) - return; - if (!frontlightIsOn()) - return; - - for (int dutyCycle = preferences.getUInt("flMaxBrightness"); dutyCycle >= 0; dutyCycle -= 5) - { - flArray.setPWM(num, 0, dutyCycle); - vTaskDelay(pdMS_TO_TICKS(flDelayTime)); - } -} + uint flDelayTime = preferences.getUInt("flEffectDelay"); #endif -// Do Not Disturb mode variables -bool dndEnabled = false; -bool dndTimeBasedEnabled = false; -DNDTimeRange dndTimeRange = {23, 0, 7, 0}; // Default: 23:00 to 07:00 + switch (handler->ledTaskParams) { + case LED_POWER_TEST: +#ifdef HAS_FRONTLIGHT + handler->frontlightFadeInAll(preferences.getUInt("flEffectDelay"), true); +#endif + handler->rainbow(20); + handler->pixels.clear(); + break; -void loadDNDSettings() { + case LED_EFFECT_WIFI_CONNECT_ERROR: + handler->blinkDelayTwoColor(100, 3, handler->pixels.Color(8, 161, 236), + handler->pixels.Color(255, 0, 0)); + break; + + case LED_EFFECT_CONFIGURING: + for (int i = NEOPIXEL_COUNT; i--; i > 0) { + for (int j = NEOPIXEL_COUNT; j--; j > 0) { + uint32_t c = handler->pixels.Color(0, 0, 0); + if (i == j) + c = handler->pixels.Color(0, 0, 255); + handler->pixels.setPixelColor(j, c); + } + handler->pixels.show(); + delay(100); + } + handler->pixels.clear(); + handler->pixels.show(); + break; + + case LED_FLASH_ERROR: + handler->blinkDelayColor(250, 3, 255, 0, 0); + break; + + case LED_EFFECT_HEARTBEAT: + handler->blinkDelayColor(150, 2, 0, 0, 255); + break; + + case LED_DATA_BLOCK_ERROR: + handler->blinkDelayColor(150, 2, 128, 0, 128); + break; + + case LED_DATA_PRICE_ERROR: + handler->blinkDelayColor(150, 2, 177, 90, 31); + break; + + case LED_FLASH_IDENTIFY: + handler->blinkDelayTwoColor(100, 2, handler->pixels.Color(255, 0, 0), + handler->pixels.Color(0, 255, 255)); + handler->blinkDelayTwoColor(100, 2, handler->pixels.Color(0, 255, 0), + handler->pixels.Color(0, 0, 255)); + break; + + case LED_EFFECT_WIFI_CONNECT_SUCCESS: + case LED_FLASH_SUCCESS: + handler->blinkDelayColor(150, 3, 0, 255, 0); + break; + + case LED_PROGRESS_100: + handler->pixels.setPixelColor(0, handler->pixels.Color(0, 255, 0)); + [[fallthrough]]; + case LED_PROGRESS_75: + handler->pixels.setPixelColor(1, handler->pixels.Color(0, 255, 0)); + [[fallthrough]]; + case LED_PROGRESS_50: + handler->pixels.setPixelColor(2, handler->pixels.Color(0, 255, 0)); + [[fallthrough]]; + case LED_PROGRESS_25: + handler->pixels.setPixelColor(3, handler->pixels.Color(0, 255, 0)); + handler->pixels.show(); + break; + + case LED_EFFECT_NOSTR_ZAP: + { +#ifdef HAS_FRONTLIGHT + bool frontlightWasOn = false; + if (preferences.getBool("flFlashOnZap", DEFAULT_FL_FLASH_ON_ZAP)) { + if (handler->frontlightOn) { + frontlightWasOn = true; + handler->frontlightFadeOutAll(flDelayTime, true); + } else { + handler->frontlightFadeInAll(flDelayTime, true); + } + } +#endif + for (int flash = 0; flash < random(7, 10); flash++) { + handler->lightningStrike(); + delay(random(50, 150)); + } +#ifdef HAS_FRONTLIGHT + if (preferences.getBool("flFlashOnZap", DEFAULT_FL_FLASH_ON_ZAP)) { + vTaskDelay(pdMS_TO_TICKS(10)); + if (frontlightWasOn) { + handler->frontlightFadeInAll(flDelayTime, true); + } else { + handler->frontlightFadeOutAll(flDelayTime, true); + } + } +#endif + break; + } + + case LED_FLASH_UPDATE: + handler->blinkDelayTwoColor(250, 3, handler->pixels.Color(0, 230, 0), + handler->pixels.Color(230, 230, 0)); + break; + + case LED_FLASH_BLOCK_NOTIFY: + { +#ifdef HAS_FRONTLIGHT + bool frontlightWasOn = false; + if (preferences.getBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE)) { + if (handler->frontlightOn) { + frontlightWasOn = true; + handler->frontlightFadeOutAll(flDelayTime, true); + } else { + handler->frontlightFadeInAll(flDelayTime, true); + } + } +#endif + handler->blinkDelayTwoColor(250, 3, handler->pixels.Color(224, 67, 0), + handler->pixels.Color(8, 2, 0)); +#ifdef HAS_FRONTLIGHT + if (preferences.getBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE)) { + vTaskDelay(pdMS_TO_TICKS(10)); + if (frontlightWasOn) { + handler->frontlightFadeInAll(flDelayTime, true); + } else { + handler->frontlightFadeOutAll(flDelayTime, true); + } + } +#endif + break; + } + + case LED_EFFECT_WIFI_WAIT_FOR_CONFIG: + handler->blinkDelayTwoColor(100, 1, handler->pixels.Color(8, 161, 236), + handler->pixels.Color(156, 225, 240)); + break; + + case LED_EFFECT_WIFI_ERASE_SETTINGS: + handler->blinkDelay(100, 3); + break; + + case LED_EFFECT_WIFI_CONNECTING: + for (int i = NEOPIXEL_COUNT; i >= 0; i--) { + for (int j = NEOPIXEL_COUNT; j >= 0; j--) { + if (j == i) { + handler->pixels.setPixelColor(i, handler->pixels.Color(16, 197, 236)); + } else { + handler->pixels.setPixelColor(j, handler->pixels.Color(0, 0, 0)); + } + } + handler->pixels.show(); + vTaskDelay(pdMS_TO_TICKS(100)); + } + break; + + case LED_EFFECT_PAUSE_TIMER: + for (int i = NEOPIXEL_COUNT; i >= 0; i--) { + for (int j = NEOPIXEL_COUNT; j >= 0; j--) { + uint32_t c = handler->pixels.Color(0, 0, 0); + if (i == j) + c = handler->pixels.Color(0, 255, 0); + handler->pixels.setPixelColor(j, c); + } + handler->pixels.show(); + delay(100); + } + handler->pixels.setPixelColor(0, handler->pixels.Color(255, 0, 0)); + handler->pixels.show(); + delay(900); + handler->pixels.clear(); + handler->pixels.show(); + break; + + case LED_EFFECT_START_TIMER: + handler->pixels.clear(); + handler->pixels.setPixelColor((NEOPIXEL_COUNT - 1), handler->pixels.Color(255, 0, 0)); + handler->pixels.show(); + delay(900); + for (int i = NEOPIXEL_COUNT; i--; i > 0) { + for (int j = NEOPIXEL_COUNT; j--; j > 0) { + uint32_t c = handler->pixels.Color(0, 0, 0); + if (i == j) + c = handler->pixels.Color(0, 255, 0); + handler->pixels.setPixelColor(j, c); + } + handler->pixels.show(); + delay(100); + } + handler->pixels.clear(); + handler->pixels.show(); + break; + } + + // Restore previous state unless power test + for (int i = 0; i < NEOPIXEL_COUNT; i++) { + handler->pixels.setPixelColor(i, oldLights[i]); + } + handler->pixels.show(); + } + } + } +} + +bool LedHandler::queueEffect(uint effect) { + if (isDNDActive()) { + return false; + } + if (ledTaskQueue == nullptr) { + return false; + } + xQueueSend(ledTaskQueue, &effect, portMAX_DELAY); + return true; +} + +void LedHandler::clear() { + preferences.putBool("ledStatus", false); + pixels.clear(); + pixels.show(); +} + +void LedHandler::setLights(int r, int g, int b) { + setLights(pixels.Color(r, g, b)); +} + +void LedHandler::setLights(uint32_t color) { + bool ledStatus = true; + for (int i = 0; i < NEOPIXEL_COUNT; i++) { + pixels.setPixelColor(i, color); + } + pixels.show(); + + if (color == pixels.Color(0, 0, 0)) { + ledStatus = false; + } else { + saveLedState(); + } + preferences.putBool("ledStatus", ledStatus); +} + +void LedHandler::saveLedState() { + for (int i = 0; i < pixels.numPixels(); i++) { + int pixelColor = pixels.getPixelColor(i); + char key[12]; + snprintf(key, 12, "%s%d", "ledColor_", i); + preferences.putUInt(key, pixelColor); + } + xTaskNotifyGive(eventSourceTaskHandle); +} + +void LedHandler::restoreLedState() { + for (int i = 0; i < pixels.numPixels(); i++) { + char key[12]; + snprintf(key, 12, "%s%d", "ledColor_", i); + uint pixelColor = preferences.getUInt(key, pixels.Color(0, 0, 0)); + pixels.setPixelColor(i, pixelColor); + } + pixels.show(); +} + +void LedHandler::rainbow(int wait) { + for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) { + pixels.rainbow(firstPixelHue); + pixels.show(); + delayMicroseconds(wait); + } +} + +void LedHandler::theaterChase(uint32_t color, int wait) { + for (int a = 0; a < 10; a++) { + for (int b = 0; b < 3; b++) { + pixels.clear(); + for (int c = b; c < pixels.numPixels(); c += 3) { + pixels.setPixelColor(c, color); + } + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(wait)); + } + } +} + +void LedHandler::theaterChaseRainbow(int wait) { + int firstPixelHue = 0; + for (int a = 0; a < 30; a++) { + for (int b = 0; b < 3; b++) { + pixels.clear(); + for (int c = b; c < pixels.numPixels(); c += 3) { + int hue = firstPixelHue + c * 65536L / pixels.numPixels(); + uint32_t color = pixels.gamma32(pixels.ColorHSV(hue)); + pixels.setPixelColor(c, color); + } + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(wait)); + firstPixelHue += 65536 / 90; + } + } +} + +void LedHandler::lightningStrike() { + uint32_t PURPLE = pixels.Color(128, 0, 128); + uint32_t YELLOW = pixels.Color(255, 226, 41); + + for (int i = 0; i < pixels.numPixels(); i++) { + pixels.setPixelColor(i, random(2) == 0 ? YELLOW : PURPLE); + } + pixels.show(); + delay(random(10, 50)); +} + +void LedHandler::blinkDelay(int d, int times) { + for (int j = 0; j < times; j++) { + pixels.setPixelColor(0, pixels.Color(255, 0, 0)); + pixels.setPixelColor(1, pixels.Color(0, 255, 0)); + pixels.setPixelColor(2, pixels.Color(255, 0, 0)); + pixels.setPixelColor(3, pixels.Color(0, 255, 0)); + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(d)); + + pixels.setPixelColor(0, pixels.Color(255, 255, 0)); + pixels.setPixelColor(1, pixels.Color(0, 255, 255)); + pixels.setPixelColor(2, pixels.Color(255, 255, 0)); + pixels.setPixelColor(3, pixels.Color(0, 255, 255)); + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(d)); + } + pixels.clear(); + pixels.show(); +} + +void LedHandler::blinkDelayColor(int d, int times, uint r, uint g, uint b) { + for (int j = 0; j < times; j++) { + for (int i = 0; i < NEOPIXEL_COUNT; i++) { + pixels.setPixelColor(i, pixels.Color(r, g, b)); + } + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(d)); + + pixels.clear(); + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(d)); + } + pixels.clear(); + pixels.show(); +} + +void LedHandler::blinkDelayTwoColor(int d, int times, const uint32_t& c1, const uint32_t& c2) { + for (int j = 0; j < times; j++) { + for (int i = 0; i < NEOPIXEL_COUNT; i++) { + pixels.setPixelColor(i, c1); + } + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(d)); + + for (int i = 0; i < NEOPIXEL_COUNT; i++) { + pixels.setPixelColor(i, c2); + } + pixels.show(); + vTaskDelay(pdMS_TO_TICKS(d)); + } + pixels.clear(); + pixels.show(); +} + +// DND Implementation +void LedHandler::loadDNDSettings() { dndEnabled = preferences.getBool("dndEnabled", false); dndTimeBasedEnabled = preferences.getBool("dndTimeEnabled", false); @@ -231,29 +435,29 @@ void loadDNDSettings() { dndTimeRange.endMinute = preferences.getUChar("dndEndMin", 0); } -void setDNDEnabled(bool enabled) { +void LedHandler::setDNDEnabled(bool enabled) { dndEnabled = enabled; preferences.putBool("dndEnabled", enabled); if (enabled && isDNDActive()) { - clearLeds(); - #ifdef HAS_FRONTLIGHT + clear(); +#ifdef HAS_FRONTLIGHT frontlightFadeOutAll(); - #endif +#endif } } -void setDNDTimeBasedEnabled(bool enabled) { +void LedHandler::setDNDTimeBasedEnabled(bool enabled) { dndTimeBasedEnabled = enabled; preferences.putBool("dndTimeEnabled", enabled); if (enabled && isDNDActive()) { - clearLeds(); - #ifdef HAS_FRONTLIGHT + clear(); +#ifdef HAS_FRONTLIGHT frontlightFadeOutAll(); - #endif +#endif } } -void setDNDTimeRange(uint8_t startHour, uint8_t startMinute, uint8_t endHour, uint8_t endMinute) { +void LedHandler::setDNDTimeRange(uint8_t startHour, uint8_t startMinute, uint8_t endHour, uint8_t endMinute) { dndTimeRange.startHour = startHour; dndTimeRange.startMinute = startMinute; dndTimeRange.endHour = endHour; @@ -265,21 +469,19 @@ void setDNDTimeRange(uint8_t startHour, uint8_t startMinute, uint8_t endHour, ui preferences.putUChar("dndEndMin", endMinute); } -bool isTimeInDNDRange(uint8_t hour, uint8_t minute) { +bool LedHandler::isTimeInDNDRange(uint8_t hour, uint8_t minute) const { uint16_t currentTime = hour * 60 + minute; uint16_t startTime = dndTimeRange.startHour * 60 + dndTimeRange.startMinute; uint16_t endTime = dndTimeRange.endHour * 60 + dndTimeRange.endMinute; if (startTime <= endTime) { - // Simple case: start time is before end time (e.g., 09:00 to 17:00) return currentTime >= startTime && currentTime < endTime; } else { - // Complex case: start time is after end time (e.g., 23:00 to 07:00) return currentTime >= startTime || currentTime < endTime; } } -bool isDNDActive() { +bool LedHandler::isDNDActive() const { if (dndEnabled) { return true; } @@ -295,518 +497,174 @@ bool isDNDActive() { return false; } -void ledTask(void *parameter) -{ - while (1) - { - if (ledTaskQueue != NULL) - { - if (xQueueReceive(ledTaskQueue, &ledTaskParams, portMAX_DELAY) == - pdPASS) - { - - if (preferences.getBool("disableLeds", DEFAULT_DISABLE_LEDS)) - { - continue; - } - - std::array oldLights; - - // get current state - for (int i = 0; i < NEOPIXEL_COUNT; i++) - { - oldLights[i] = pixels.getPixelColor(i); - } #ifdef HAS_FRONTLIGHT - uint flDelayTime = preferences.getUInt("flEffectDelay"); -#endif - switch (ledTaskParams) - { - case LED_POWER_TEST: -#ifdef HAS_FRONTLIGHT - frontlightFadeInAll(preferences.getUInt("flEffectDelay"), true); -#endif - ledRainbow(20); - pixels.clear(); - break; - case LED_EFFECT_WIFI_CONNECT_ERROR: - blinkDelayTwoColor(100, 3, pixels.Color(8, 161, 236), - pixels.Color(255, 0, 0)); - break; - case LED_EFFECT_CONFIGURING: - for (int i = NEOPIXEL_COUNT; i--; i > 0) - { - for (int j = NEOPIXEL_COUNT; j--; j > 0) - { - uint32_t c = pixels.Color(0, 0, 0); - if (i == j) - c = pixels.Color(0, 0, 255); +// Frontlight implementation +void LedHandler::frontlightFlash(int flDelayTime) { + if (preferences.getBool("flDisable")) { + return; + } - pixels.setPixelColor(j, c); - } + if (frontlightOn) { + frontlightFadeOutAll(flDelayTime, true); + frontlightFadeInAll(flDelayTime, true); + } else { + frontlightFadeInAll(flDelayTime, true); + frontlightFadeOutAll(flDelayTime, true); + } +} - pixels.show(); +void LedHandler::frontlightFadeInAll() { + frontlightFadeInAll(preferences.getUInt("flEffectDelay")); +} - delay(100); - } +void LedHandler::frontlightFadeOutAll() { + frontlightFadeOutAll(preferences.getUInt("flEffectDelay")); +} - pixels.clear(); - pixels.show(); - break; - case LED_FLASH_ERROR: - blinkDelayColor(250, 3, 255, 0, 0); - break; - case LED_EFFECT_HEARTBEAT: - blinkDelayColor(150, 2, 0, 0, 255); - break; - case LED_DATA_BLOCK_ERROR: - blinkDelayColor(150, 2, 128, 0, 128); - break; - case LED_DATA_PRICE_ERROR: - blinkDelayColor(150, 2, 177, 90, 31); - break; - case LED_FLASH_IDENTIFY: - blinkDelayTwoColor(100, 2, pixels.Color(255, 0, 0), - pixels.Color(0, 255, 255)); - blinkDelayTwoColor(100, 2, pixels.Color(0, 255, 0), - pixels.Color(0, 0, 255)); - break; - case LED_EFFECT_WIFI_CONNECT_SUCCESS: - case LED_FLASH_SUCCESS: - blinkDelayColor(150, 3, 0, 255, 0); - break; - case LED_PROGRESS_100: - pixels.setPixelColor(0, pixels.Color(0, 255, 0)); - case LED_PROGRESS_75: - pixels.setPixelColor(1, pixels.Color(0, 255, 0)); - case LED_PROGRESS_50: - pixels.setPixelColor(2, pixels.Color(0, 255, 0)); - case LED_PROGRESS_25: - pixels.setPixelColor(3, pixels.Color(0, 255, 0)); - pixels.show(); - break; - case LED_EFFECT_NOSTR_ZAP: - { -#ifdef HAS_FRONTLIGHT - bool frontlightWasOn = false; +void LedHandler::frontlightFadeIn(uint num) { + frontlightFadeIn(num, preferences.getUInt("flEffectDelay")); +} - if (preferences.getBool("flFlashOnZap", DEFAULT_FL_FLASH_ON_ZAP)) - { - if (frontlightOn) - { - frontlightWasOn = true; - frontlightFadeOutAll(flDelayTime, true); +void LedHandler::frontlightFadeOut(uint num) { + frontlightFadeOut(num, preferences.getUInt("flEffectDelay")); +} + +void LedHandler::frontlightSetBrightness(uint brightness) { + if (isDNDActive() || brightness > 4096) { + return; + } + + for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) { + flArray.setPWM(ledPin, 0, brightness); + } +} + +std::vector LedHandler::frontlightGetStatus() { + std::vector statuses; + for (int ledPin = 1; ledPin <= NUM_SCREENS; ledPin++) { + uint16_t a = 0, b = 0; + flArray.getPWM(ledPin, &a, &b); + statuses.push_back(round(b - a / 4096)); + } + return statuses; +} + +void LedHandler::frontlightFadeInAll(int flDelayTime, bool staggered) { + if (preferences.getBool("flDisable") || frontlightIsOn() || flInTransition) { + return; + } + + flInTransition = true; + const int maxBrightness = preferences.getUInt("flMaxBrightness"); + + if (staggered) { + int step = FL_FADE_STEP; + int staggerDelay = flDelayTime / NUM_SCREENS; + + for (int dutyCycle = 0; dutyCycle <= maxBrightness + (NUM_SCREENS - 1) * maxBrightness / NUM_SCREENS; dutyCycle += step) { + for (int ledPin = 0; ledPin < NUM_SCREENS; ledPin++) { + int ledBrightness = dutyCycle - ledPin * maxBrightness / NUM_SCREENS; + if (ledBrightness < 0) { + ledBrightness = 0; + } else if (ledBrightness > maxBrightness) { + ledBrightness = maxBrightness; + } + flArray.setPWM(ledPin + 1, 0, ledBrightness); } - else - { - frontlightFadeInAll(flDelayTime, true); - } - } -#endif - for (int flash = 0; flash < random(7, 10); flash++) - { - lightningStrike(); - delay(random(50, 150)); - } - // blinkDelayColor(250, 3, 142, 48, 235); - // blinkDelayTwoColor(250, 3, pixels.Color(142, 48, 235), - // pixels.Color(169, 21, 255)); -#ifdef HAS_FRONTLIGHT - if (preferences.getBool("flFlashOnZap", DEFAULT_FL_FLASH_ON_ZAP)) - { - vTaskDelay(pdMS_TO_TICKS(10)); - if (frontlightWasOn) - { - frontlightFadeInAll(flDelayTime, true); - } - else - { - frontlightFadeOutAll(flDelayTime, true); - } - } -#endif - break; + vTaskDelay(pdMS_TO_TICKS(staggerDelay)); } - case LED_FLASH_UPDATE: - blinkDelayTwoColor(250, 3, pixels.Color(0, 230, 0), - pixels.Color(230, 230, 0)); - break; - case LED_FLASH_BLOCK_NOTIFY: - { -#ifdef HAS_FRONTLIGHT - bool frontlightWasOn = false; + } else { + for (int dutyCycle = 0; dutyCycle <= maxBrightness; dutyCycle += FL_FADE_STEP) { + for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) { + flArray.setPWM(ledPin, 0, dutyCycle); + } + vTaskDelay(pdMS_TO_TICKS(flDelayTime)); + } + } + frontlightOn = true; + flInTransition = false; +} - if (preferences.getBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE)) - { - if (frontlightOn) - { - frontlightWasOn = true; - frontlightFadeOutAll(flDelayTime, true); +void LedHandler::frontlightFadeOutAll(int flDelayTime, bool staggered) { + if (preferences.getBool("flDisable") || !frontlightIsOn() || flInTransition) { + return; + } + + flInTransition = true; + if (staggered) { + int maxBrightness = preferences.getUInt("flMaxBrightness"); + int step = FL_FADE_STEP; + int staggerDelay = flDelayTime / NUM_SCREENS; + + for (int dutyCycle = maxBrightness; dutyCycle >= 0; dutyCycle -= step) { + for (int ledPin = 0; ledPin < NUM_SCREENS; ledPin++) { + int ledBrightness = dutyCycle - (NUM_SCREENS - 1 - ledPin) * maxBrightness / NUM_SCREENS; + if (ledBrightness < 0) { + ledBrightness = 0; + } else if (ledBrightness > maxBrightness) { + ledBrightness = maxBrightness; + } + flArray.setPWM(ledPin + 1, 0, ledBrightness); } - else - { - frontlightFadeInAll(flDelayTime, true); + vTaskDelay(pdMS_TO_TICKS(staggerDelay)); + } + } else { + for (int dutyCycle = preferences.getUInt("flMaxBrightness"); dutyCycle >= 0; dutyCycle -= FL_FADE_STEP) { + for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) { + flArray.setPWM(ledPin, 0, dutyCycle); } - } + vTaskDelay(pdMS_TO_TICKS(flDelayTime)); + } + } + + flArray.allOFF(); + frontlightOn = false; + flInTransition = false; +} + +void LedHandler::frontlightFadeIn(uint num, int flDelayTime) { + if (isDNDActive() || preferences.getBool("flDisable")) { + return; + } + + for (int dutyCycle = 0; dutyCycle <= preferences.getUInt("flMaxBrightness"); dutyCycle += 5) { + flArray.setPWM(num, 0, dutyCycle); + vTaskDelay(pdMS_TO_TICKS(flDelayTime)); + } +} + +void LedHandler::frontlightFadeOut(uint num, int flDelayTime) { + if (isDNDActive() || preferences.getBool("flDisable") || !frontlightIsOn()) { + return; + } + + for (int dutyCycle = preferences.getUInt("flMaxBrightness"); dutyCycle >= 0; dutyCycle -= 5) { + flArray.setPWM(num, 0, dutyCycle); + vTaskDelay(pdMS_TO_TICKS(flDelayTime)); + } +} + +void LedHandler::initializeFrontlight() { + if (!flArray.begin(PCA9685_MODE1_AUTOINCR | PCA9685_MODE1_ALLCALL, PCA9685_MODE2_TOTEMPOLE)) + { + Serial.println(F("FL driver error")); + return; + } + flArray.setFrequency(200); + Serial.println(F("FL driver active")); + + if (!preferences.isKey("flMaxBrightness")) + { + preferences.putUInt("flMaxBrightness", DEFAULT_FL_MAX_BRIGHTNESS); + } + if (!preferences.isKey("flEffectDelay")) + { + preferences.putUInt("flEffectDelay", DEFAULT_FL_EFFECT_DELAY); + } + + if (!preferences.isKey("flFlashOnUpd")) + { + preferences.putBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE); + } +} #endif - blinkDelayTwoColor(250, 3, pixels.Color(224, 67, 0), - pixels.Color(8, 2, 0)); -#ifdef HAS_FRONTLIGHT - if (preferences.getBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE)) - { - vTaskDelay(pdMS_TO_TICKS(10)); - if (frontlightWasOn) - { - frontlightFadeInAll(flDelayTime, true); - } - else - { - frontlightFadeOutAll(flDelayTime, true); - } - } -#endif - break; - } - case LED_EFFECT_WIFI_WAIT_FOR_CONFIG: - blinkDelayTwoColor(100, 1, pixels.Color(8, 161, 236), - pixels.Color(156, 225, 240)); - break; - case LED_EFFECT_WIFI_ERASE_SETTINGS: - blinkDelay(100, 3); - break; - case LED_EFFECT_WIFI_CONNECTING: - for (int i = NEOPIXEL_COUNT; i >= 0; i--) - { - for (int j = NEOPIXEL_COUNT; j >= 0; j--) - { - if (j == i) - { - pixels.setPixelColor(i, pixels.Color(16, 197, 236)); - } - else - { - pixels.setPixelColor(j, pixels.Color(0, 0, 0)); - } - } - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(100)); - } - break; - case LED_EFFECT_PAUSE_TIMER: - for (int i = NEOPIXEL_COUNT; i >= 0; i--) - { - for (int j = NEOPIXEL_COUNT; j >= 0; j--) - { - uint32_t c = pixels.Color(0, 0, 0); - if (i == j) - c = pixels.Color(0, 255, 0); - pixels.setPixelColor(j, c); - } - - pixels.show(); - - delay(100); - } - pixels.setPixelColor(0, pixels.Color(255, 0, 0)); - pixels.show(); - - delay(900); - - pixels.clear(); - pixels.show(); - break; - case LED_EFFECT_START_TIMER: - pixels.clear(); - pixels.setPixelColor((NEOPIXEL_COUNT - 1), pixels.Color(255, 0, 0)); - pixels.show(); - - delay(900); - - for (int i = NEOPIXEL_COUNT; i--; i > 0) - { - for (int j = NEOPIXEL_COUNT; j--; j > 0) - { - uint32_t c = pixels.Color(0, 0, 0); - if (i == j) - c = pixels.Color(0, 255, 0); - - pixels.setPixelColor(j, c); - } - - pixels.show(); - - delay(100); - } - - pixels.clear(); - pixels.show(); - break; - } - - // revert to previous state unless power test - - for (int i = 0; i < NEOPIXEL_COUNT; i++) - { - pixels.setPixelColor(i, oldLights[i]); - } - - pixels.show(); - } - } - } -} - -void setupLeds() -{ - loadDNDSettings(); - pixels.begin(); - pixels.setBrightness(preferences.getUInt("ledBrightness", DEFAULT_LED_BRIGHTNESS)); - pixels.clear(); - pixels.show(); - setupLedTask(); - if (preferences.getBool("ledTestOnPower", DEFAULT_LED_TEST_ON_POWER)) - { - while (!ledTaskQueue) - { - delay(1); - // wait until queue is available - } - queueLedEffect(LED_POWER_TEST); - } -} - -void setupLedTask() -{ - ledTaskQueue = xQueueCreate(5, sizeof(uint)); - - xTaskCreate(ledTask, "LedTask", 2048, NULL, 10, &ledTaskHandle); -} - -void blinkDelay(int d, int times) -{ - for (int j = 0; j < times; j++) - { - pixels.setPixelColor(0, pixels.Color(255, 0, 0)); - pixels.setPixelColor(1, pixels.Color(0, 255, 0)); - pixels.setPixelColor(2, pixels.Color(255, 0, 0)); - pixels.setPixelColor(3, pixels.Color(0, 255, 0)); - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(d)); - - pixels.setPixelColor(0, pixels.Color(255, 255, 0)); - pixels.setPixelColor(1, pixels.Color(0, 255, 255)); - pixels.setPixelColor(2, pixels.Color(255, 255, 0)); - pixels.setPixelColor(3, pixels.Color(0, 255, 255)); - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(d)); - } - pixels.clear(); - pixels.show(); -} - -void blinkDelayColor(int d, int times, uint r, uint g, uint b) -{ - for (int j = 0; j < times; j++) - { - for (int i = 0; i < NEOPIXEL_COUNT; i++) - { - pixels.setPixelColor(i, pixels.Color(r, g, b)); - } - - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(d)); - - pixels.clear(); - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(d)); - } - pixels.clear(); - pixels.show(); -} - -void blinkDelayTwoColor(int d, int times, const uint32_t& c1, const uint32_t& c2) -{ - for (int j = 0; j < times; j++) - { - for (int i = 0; i < NEOPIXEL_COUNT; i++) - { - pixels.setPixelColor(i, c1); - } - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(d)); - - for (int i = 0; i < NEOPIXEL_COUNT; i++) - { - pixels.setPixelColor(i, c2); - } - pixels.show(); - vTaskDelay(pdMS_TO_TICKS(d)); - } - pixels.clear(); - pixels.show(); -} - -void clearLeds() -{ - preferences.putBool("ledStatus", false); - pixels.clear(); - pixels.show(); -} - -void setLights(int r, int g, int b) { setLights(pixels.Color(r, g, b)); } - -void setLights(uint32_t color) -{ - bool ledStatus = true; - - for (int i = 0; i < NEOPIXEL_COUNT; i++) - { - pixels.setPixelColor(i, color); - } - pixels.show(); - - if (color == pixels.Color(0, 0, 0)) - { - ledStatus = false; - } - else - { - saveLedState(); - } - preferences.putBool("ledStatus", ledStatus); -} - -void saveLedState() -{ - for (int i = 0; i < pixels.numPixels(); i++) - { - int pixelColor = pixels.getPixelColor(i); - char key[12]; - snprintf(key, 12, "%s%d", "ledColor_", i); - preferences.putUInt(key, pixelColor); - } - - xTaskNotifyGive(eventSourceTaskHandle); -} - -void restoreLedState() -{ - for (int i = 0; i < pixels.numPixels(); i++) - { - char key[12]; - snprintf(key, 12, "%s%d", "ledColor_", i); - uint pixelColor = preferences.getUInt(key, pixels.Color(0, 0, 0)); - pixels.setPixelColor(i, pixelColor); - } - - pixels.show(); -} - -QueueHandle_t getLedTaskQueue() { return ledTaskQueue; } - -bool queueLedEffect(uint effect) { - if (isDNDActive()) { - return false; // Don't queue any effects during DND mode - } - if (ledTaskQueue == NULL) - { - return false; - } - - uint flashType = effect; - xQueueSend(ledTaskQueue, &flashType, portMAX_DELAY); - return true; -} - -void ledRainbow(int wait) -{ - // Hue of first pixel runs 5 complete loops through the color wheel. - // Color wheel has a range of 65536 but it's OK if we roll over, so - // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time - // means we'll make 5*65536/256 = 1280 passes through this loop: - for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; - firstPixelHue += 256) - { - // strip.rainbow() can take a single argument (first pixel hue) or - // optionally a few extras: number of rainbow repetitions (default 1), - // saturation and value (brightness) (both 0-255, similar to the - // ColorHSV() function, default 255), and a true/false flag for whether - // to apply gamma correction to provide 'truer' colors (default true). - pixels.rainbow(firstPixelHue); - // Above line is equivalent to: - // strip.rainbow(firstPixelHue, 1, 255, 255, true); - pixels.show(); // Update strip with new contents - delayMicroseconds(wait); - // vTaskDelay(pdMS_TO_TICKS(wait)); // Pause for a moment - } -} - -void ledTheaterChase(uint32_t color, int wait) -{ - for (int a = 0; a < 10; a++) - { // Repeat 10 times... - for (int b = 0; b < 3; b++) - { // 'b' counts from 0 to 2... - pixels.clear(); // Set all pixels in RAM to 0 (off) - // 'c' counts up from 'b' to end of strip in steps of 3... - for (int c = b; c < pixels.numPixels(); c += 3) - { - pixels.setPixelColor(c, color); // Set pixel 'c' to value 'color' - } - pixels.show(); // Update strip with new contents - vTaskDelay(pdMS_TO_TICKS(wait)); // Pause for a moment - } - } -} - -void ledTheaterChaseRainbow(int wait) -{ - int firstPixelHue = 0; // First pixel starts at red (hue 0) - for (int a = 0; a < 30; a++) - { // Repeat 30 times... - for (int b = 0; b < 3; b++) - { // 'b' counts from 0 to 2... - pixels.clear(); // Set all pixels in RAM to 0 (off) - // 'c' counts up from 'b' to end of strip in increments of 3... - for (int c = b; c < pixels.numPixels(); c += 3) - { - // hue of pixel 'c' is offset by an amount to make one full - // revolution of the color wheel (range 65536) along the length - // of the strip (strip.numPixels() steps): - int hue = firstPixelHue + c * 65536L / pixels.numPixels(); - uint32_t color = pixels.gamma32(pixels.ColorHSV(hue)); // hue -> RGB - pixels.setPixelColor(c, color); // Set pixel 'c' to value 'color' - } - pixels.show(); // Update strip with new contents - vTaskDelay(pdMS_TO_TICKS(wait)); // Pause for a moment - firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames - } - } -} - -void lightningStrike() -{ - uint32_t PURPLE = pixels.Color(128, 0, 128); - uint32_t YELLOW = pixels.Color(255, 226, 41); - - // Randomly choose which LEDs to light up - for (int i = 0; i < pixels.numPixels(); i++) - { - if (random(2) == 0) - { // 50% chance for each LED - pixels.setPixelColor(i, YELLOW); - } - else - { - pixels.setPixelColor(i, PURPLE); - } - } - pixels.show(); - - delay(random(10, 50)); // Flash duration - - // Return to purple background - // setAllPixels(PURPLE); -} - -Adafruit_NeoPixel getPixels() { return pixels; } diff --git a/src/lib/led_handler.hpp b/src/lib/led_handler.hpp index fe72ffa..e599e2a 100644 --- a/src/lib/led_handler.hpp +++ b/src/lib/led_handler.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "lib/shared.hpp" #include "lib/webserver.hpp" @@ -15,12 +16,11 @@ #define NEOPIXEL_COUNT 4 #endif +// LED effect constants const int LED_FLASH_ERROR = 0; const int LED_FLASH_SUCCESS = 1; const int LED_FLASH_UPDATE = 2; - const int LED_EFFECT_CONFIGURING = 10; - const int LED_FLASH_BLOCK_NOTIFY = 4; const int LED_EFFECT_START_TIMER = 5; const int LED_EFFECT_PAUSE_TIMER = 6; @@ -30,61 +30,15 @@ const int LED_EFFECT_WIFI_CONNECTING = 101; const int LED_EFFECT_WIFI_CONNECT_ERROR = 102; const int LED_EFFECT_WIFI_CONNECT_SUCCESS = 103; const int LED_EFFECT_WIFI_ERASE_SETTINGS = 104; - const int LED_PROGRESS_25 = 200; const int LED_PROGRESS_50 = 201; const int LED_PROGRESS_75 = 202; const int LED_PROGRESS_100 = 203; - const int LED_DATA_PRICE_ERROR = 300; const int LED_DATA_BLOCK_ERROR = 301; - const int LED_EFFECT_NOSTR_ZAP = 400; - const int LED_FLASH_IDENTIFY = 990; const int LED_POWER_TEST = 999; -extern TaskHandle_t ledTaskHandle; -extern Adafruit_NeoPixel pixels; - -void ledTask(void *pvParameters); -void setupLeds(); -void setupLedTask(); -void blinkDelay(int d, int times); -void blinkDelayColor(int d, int times, uint r, uint g, uint b); -void blinkDelayTwoColor(int d, int times, const uint32_t& c1, const uint32_t& c2); -void clearLeds(); -void saveLedState(); -void restoreLedState(); -QueueHandle_t getLedTaskQueue(); -bool queueLedEffect(uint effect); -void setLights(int r, int g, int b); -void setLights(uint32_t color); -void ledRainbow(int wait); -void ledTheaterChaseRainbow(int wait); -void ledTheaterChase(uint32_t color, int wait); -Adafruit_NeoPixel getPixels(); -void lightningStrike(); - -#ifdef HAS_FRONTLIGHT -void frontlightFlash(int flDelayTime); -void frontlightFadeInAll(); -void frontlightFadeOutAll(); -void frontlightFadeIn(uint num); -void frontlightFadeOut(uint num); - -std::vector frontlightGetStatus(); - -void frontlightSetBrightness(uint brightness); -bool frontlightIsOn(); - -void frontlightFadeInAll(int flDelayTime); -void frontlightFadeInAll(int flDelayTime, bool staggered); -void frontlightFadeOutAll(int flDelayTime); -void frontlightFadeOutAll(int flDelayTime, bool staggered); - -void frontlightFadeIn(uint num, int flDelayTime); -void frontlightFadeOut(uint num, int flDelayTime); -#endif // Do Not Disturb mode settings struct DNDTimeRange { @@ -94,12 +48,88 @@ struct DNDTimeRange { uint8_t endMinute; }; -extern bool dndEnabled; -extern bool dndTimeBasedEnabled; -extern DNDTimeRange dndTimeRange; +class LedHandler { +public: + static LedHandler& getInstance(); + + // Delete copy constructor and assignment operator + LedHandler(const LedHandler&) = delete; + LedHandler& operator=(const LedHandler&) = delete; -void setDNDEnabled(bool enabled); -void setDNDTimeBasedEnabled(bool enabled); -void setDNDTimeRange(uint8_t startHour, uint8_t startMinute, uint8_t endHour, uint8_t endMinute); -bool isDNDActive(); -bool isTimeInDNDRange(uint8_t hour, uint8_t minute); \ No newline at end of file + void setup(); + void setupTask(); + bool queueEffect(uint effect); + void clear(); + void setLights(int r, int g, int b); + void setLights(uint32_t color); + void saveLedState(); + void restoreLedState(); + QueueHandle_t getTaskQueue() const { return ledTaskQueue; } + Adafruit_NeoPixel& getPixels() { return pixels; } + + // DND methods + void setDNDEnabled(bool enabled); + void setDNDTimeBasedEnabled(bool enabled); + void setDNDTimeRange(uint8_t startHour, uint8_t startMinute, uint8_t endHour, uint8_t endMinute); + bool isDNDActive() const; + bool isTimeInDNDRange(uint8_t hour, uint8_t minute) const; + + // DND getters + bool isDNDEnabled() const { return dndEnabled; } + bool isDNDTimeBasedEnabled() const { return dndTimeBasedEnabled; } + uint8_t getDNDStartHour() const { return dndTimeRange.startHour; } + uint8_t getDNDStartMinute() const { return dndTimeRange.startMinute; } + uint8_t getDNDEndHour() const { return dndTimeRange.endHour; } + uint8_t getDNDEndMinute() const { return dndTimeRange.endMinute; } + + // Effect methods + void rainbow(int wait); + void theaterChase(uint32_t color, int wait); + void theaterChaseRainbow(int wait); + void lightningStrike(); + void blinkDelay(int d, int times); + void blinkDelayColor(int d, int times, uint r, uint g, uint b); + void blinkDelayTwoColor(int d, int times, const uint32_t& c1, const uint32_t& c2); + +#ifdef HAS_FRONTLIGHT + void frontlightFlash(int flDelayTime); + void frontlightFadeInAll(); + void frontlightFadeOutAll(); + void frontlightFadeIn(uint num); + void frontlightFadeOut(uint num); + std::vector frontlightGetStatus(); + void frontlightSetBrightness(uint brightness); + bool frontlightIsOn() const { return frontlightOn; } + void frontlightFadeInAll(int flDelayTime, bool staggered = false); + void frontlightFadeOutAll(int flDelayTime, bool staggered = false); + void frontlightFadeIn(uint num, int flDelayTime); + void frontlightFadeOut(uint num, int flDelayTime); + void initializeFrontlight(); +#endif + +private: + LedHandler(); // Private constructor for singleton + void loadDNDSettings(); + static void ledTask(void* pvParameters); + + Adafruit_NeoPixel pixels; + TaskHandle_t ledTaskHandle; + QueueHandle_t ledTaskQueue; + uint ledTaskParams; + + // DND members + bool dndEnabled; + bool dndTimeBasedEnabled; + DNDTimeRange dndTimeRange; + +#ifdef HAS_FRONTLIGHT + static constexpr uint16_t FL_FADE_STEP = 25; + bool frontlightOn; + bool flInTransition; +#endif +}; + +// Global accessor function +inline LedHandler& getLedHandler() { + return LedHandler::getInstance(); +} \ No newline at end of file diff --git a/src/lib/nostr_notify.cpp b/src/lib/nostr_notify.cpp index b9a7a46..3c996a2 100644 --- a/src/lib/nostr_notify.cpp +++ b/src/lib/nostr_notify.cpp @@ -1,4 +1,5 @@ #include "nostr_notify.hpp" +#include "led_handler.hpp" std::vector pools; nostr::Transport *transport; @@ -286,7 +287,7 @@ void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event) vTaskDelay(pdMS_TO_TICKS(315 * NUM_SCREENS) + pdMS_TO_TICKS(250)); if (preferences.getBool("ledFlashOnZap", DEFAULT_LED_FLASH_ON_ZAP)) { - queueLedEffect(LED_EFFECT_NOSTR_ZAP); + getLedHandler().queueEffect(LED_EFFECT_NOSTR_ZAP); } if (timerPeriod > 0) { diff --git a/src/lib/ota.cpp b/src/lib/ota.cpp index c481b72..b42a505 100644 --- a/src/lib/ota.cpp +++ b/src/lib/ota.cpp @@ -1,4 +1,5 @@ #include "ota.hpp" +#include "led_handler.hpp" TaskHandle_t taskOtaHandle = NULL; bool isOtaUpdating = false; @@ -31,6 +32,9 @@ void setupOTA() void onOTAProgress(unsigned int progress, unsigned int total) { uint percentage = progress / (total / 100); + auto& ledHandler = getLedHandler(); + auto& pixels = ledHandler.getPixels(); + pixels.fill(pixels.Color(0, 255, 0)); if (percentage < 100) { @@ -84,15 +88,15 @@ void handleOTATask(void *parameter) { if (msg.updateType == UPDATE_ALL) { isOtaUpdating = true; - queueLedEffect(LED_FLASH_UPDATE); + getLedHandler().queueEffect(LED_FLASH_UPDATE); int resultWebUi = downloadUpdateHandler(UPDATE_WEBUI); - queueLedEffect(LED_FLASH_UPDATE); + getLedHandler().queueEffect(LED_FLASH_UPDATE); int resultFw = downloadUpdateHandler(UPDATE_FIRMWARE); if (resultWebUi == 0 && resultFw == 0) { ESP.restart(); } else { - queueLedEffect(LED_FLASH_ERROR); + getLedHandler().queueEffect(LED_FLASH_ERROR); vTaskDelay(pdMS_TO_TICKS(3000)); ESP.restart(); } diff --git a/src/lib/timers.cpp b/src/lib/timers.cpp index 4e67951..850ead3 100644 --- a/src/lib/timers.cpp +++ b/src/lib/timers.cpp @@ -1,4 +1,5 @@ #include "timers.hpp" +#include "led_handler.hpp" esp_timer_handle_t screenRotateTimer; esp_timer_handle_t minuteTimer; @@ -49,11 +50,11 @@ void setTimerActive(bool status) { if (status) { esp_timer_start_periodic(screenRotateTimer, getTimerSeconds() * usPerSecond); - queueLedEffect(LED_EFFECT_START_TIMER); + getLedHandler().queueEffect(LED_EFFECT_START_TIMER); preferences.putBool("timerActive", true); } else { esp_timer_stop(screenRotateTimer); - queueLedEffect(LED_EFFECT_PAUSE_TIMER); + getLedHandler().queueEffect(LED_EFFECT_PAUSE_TIMER); preferences.putBool("timerActive", false); } diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index 2ba0a4d..4897811 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -1,4 +1,6 @@ #include "webserver.hpp" +#include "lib/led_handler.hpp" +#include "lib/shared.hpp" static const char* JSON_CONTENT = "application/json"; @@ -231,6 +233,7 @@ void asyncFirmwareUpdateHandler(AsyncWebServerRequest *request, String filename, JsonDocument getStatusObject() { + auto& ledHandler = getLedHandler(); JsonDocument root; root["currentScreen"] = ScreenHandler::getCurrentScreen(); @@ -238,25 +241,21 @@ JsonDocument getStatusObject() root["timerRunning"] = isTimerActive(); root["isOTAUpdating"] = getIsOTAUpdating(); root["espUptime"] = esp_timer_get_time() / 1000000; - // root["currentPrice"] = getPrice(); - // root["currentBlockHeight"] = getBlockHeight(); root["espFreeHeap"] = ESP.getFreeHeap(); root["espHeapSize"] = ESP.getHeapSize(); - // root["espFreePsram"] = ESP.getFreePsram(); - // root["espPsramSize"] = ESP.getPsramSize(); JsonObject conStatus = root["connectionStatus"].to(); conStatus["price"] = isPriceNotifyConnected(); conStatus["blocks"] = isBlockNotifyConnected(); conStatus["V2"] = V2Notify::isV2NotifyConnected(); - conStatus["nostr"] = nostrConnected(); root["rssi"] = WiFi.RSSI(); root["currency"] = getCurrencyCode(ScreenHandler::getCurrentCurrency()); + #ifdef HAS_FRONTLIGHT - std::vector statuses = frontlightGetStatus(); + std::vector statuses = ledHandler.frontlightGetStatus(); uint16_t arr[NUM_SCREENS]; std::copy(statuses.begin(), statuses.end(), arr); @@ -270,22 +269,24 @@ JsonDocument getStatusObject() #endif // Add DND status - root["dnd"]["enabled"] = dndEnabled; - root["dnd"]["timeBasedEnabled"] = dndTimeBasedEnabled; - root["dnd"]["startTime"] = String(dndTimeRange.startHour) + ":" + - (dndTimeRange.startMinute < 10 ? "0" : "") + String(dndTimeRange.startMinute); - root["dnd"]["endTime"] = String(dndTimeRange.endHour) + ":" + - (dndTimeRange.endMinute < 10 ? "0" : "") + String(dndTimeRange.endMinute); - root["dnd"]["active"] = isDNDActive(); + root["dnd"]["enabled"] = ledHandler.isDNDEnabled(); + root["dnd"]["timeBasedEnabled"] = ledHandler.isDNDTimeBasedEnabled(); + root["dnd"]["startTime"] = String(ledHandler.getDNDStartHour()) + ":" + + (ledHandler.getDNDStartMinute() < 10 ? "0" : "") + String(ledHandler.getDNDStartMinute()); + root["dnd"]["endTime"] = String(ledHandler.getDNDEndHour()) + ":" + + (ledHandler.getDNDEndMinute() < 10 ? "0" : "") + String(ledHandler.getDNDEndMinute()); + root["dnd"]["active"] = ledHandler.isDNDActive(); return root; } JsonDocument getLedStatusObject() { + auto& ledHandler = getLedHandler(); + auto& pixels = ledHandler.getPixels(); + JsonDocument root; JsonArray colors = root["data"].to(); - // Adafruit_NeoPixel pix = getPixels(); for (uint i = 0; i < pixels.numPixels(); i++) { @@ -295,13 +296,7 @@ JsonDocument getLedStatusObject() uint blue = pixColor & 0xFF; char hexColor[8]; snprintf(hexColor, sizeof(hexColor), "#%02X%02X%02X", red, green, blue); - - - JsonObject object = colors.add(); - object["red"] = red; - object["green"] = green; - object["blue"] = blue; - object["hex"] = hexColor; + colors.add(hexColor); } return root; @@ -621,24 +616,26 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json) // Handle DND settings if (settings.containsKey("dnd")) { JsonObject dndObj = settings["dnd"]; + auto& ledHandler = getLedHandler(); + if (dndObj.containsKey("timeBasedEnabled")) { - setDNDTimeBasedEnabled(dndObj["timeBasedEnabled"].as()); + ledHandler.setDNDTimeBasedEnabled(dndObj["timeBasedEnabled"].as()); } if (dndObj.containsKey("startHour") && dndObj.containsKey("startMinute") && dndObj.containsKey("endHour") && dndObj.containsKey("endMinute")) { - setDNDTimeRange( - dndObj["startHour"].as(), - dndObj["startMinute"].as(), - dndObj["endHour"].as(), - dndObj["endMinute"].as() - ); + ledHandler.setDNDTimeRange( + dndObj["startHour"].as(), + dndObj["startMinute"].as(), + dndObj["endHour"].as(), + dndObj["endMinute"].as()); } } request->send(HTTP_OK); if (settingsChanged) { - queueLedEffect(LED_FLASH_SUCCESS); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_FLASH_SUCCESS); } } @@ -659,7 +656,8 @@ void onApiRestart(AsyncWebServerRequest *request) void onApiIdentify(AsyncWebServerRequest *request) { - queueLedEffect(LED_FLASH_IDENTIFY); + auto& ledHandler = getLedHandler(); + ledHandler.queueEffect(LED_FLASH_IDENTIFY); request->send(HTTP_OK); } @@ -797,12 +795,13 @@ void onApiSettingsGet(AsyncWebServerRequest *request) root["ceDisableSSL"] = preferences.getBool("ceDisableSSL", DEFAULT_CUSTOM_ENDPOINT_DISABLE_SSL); // Add DND settings - root["dnd"]["enabled"] = dndEnabled; - root["dnd"]["timeBasedEnabled"] = dndTimeBasedEnabled; - root["dnd"]["startHour"] = dndTimeRange.startHour; - root["dnd"]["startMinute"] = dndTimeRange.startMinute; - root["dnd"]["endHour"] = dndTimeRange.endHour; - root["dnd"]["endMinute"] = dndTimeRange.endMinute; + auto& ledHandler = getLedHandler(); + root["dnd"]["enabled"] = ledHandler.isDNDEnabled(); + root["dnd"]["timeBasedEnabled"] = ledHandler.isDNDTimeBasedEnabled(); + root["dnd"]["startHour"] = ledHandler.getDNDStartHour(); + root["dnd"]["startMinute"] = ledHandler.getDNDStartMinute(); + root["dnd"]["endHour"] = ledHandler.getDNDEndHour(); + root["dnd"]["endMinute"] = ledHandler.getDNDEndMinute(); AsyncResponseStream *response = request->beginResponseStream(JSON_CONTENT); @@ -929,7 +928,8 @@ void onApiRestartDataSources(AsyncWebServerRequest *request) void onApiLightsOff(AsyncWebServerRequest *request) { - setLights(0, 0, 0); + auto& ledHandler = getLedHandler(); + ledHandler.setLights(0, 0, 0); request->send(HTTP_OK); } @@ -944,13 +944,15 @@ void onApiLightsSetColor(AsyncWebServerRequest *request) if (rgbColor.compareTo("off") == 0) { - setLights(0, 0, 0); + auto& ledHandler = getLedHandler(); + ledHandler.setLights(0, 0, 0); } else { uint r, g, b; sscanf(rgbColor.c_str(), "%02x%02x%02x", &r, &g, &b); - setLights(r, g, b); + auto& ledHandler = getLedHandler(); + ledHandler.setLights(r, g, b); } JsonDocument doc; @@ -968,6 +970,9 @@ void onApiLightsSetColor(AsyncWebServerRequest *request) void onApiLightsSetJson(AsyncWebServerRequest *request, JsonVariant &json) { + auto& ledHandler = getLedHandler(); + auto& pixels = ledHandler.getPixels(); + JsonArray lights = json.as(); if (lights.size() != pixels.numPixels()) @@ -1016,7 +1021,7 @@ void onApiLightsSetJson(AsyncWebServerRequest *request, JsonVariant &json) } pixels.show(); - saveLedState(); + ledHandler.saveLedState(); request->send(HTTP_OK); } @@ -1080,19 +1085,21 @@ void onApiShowCurrency(AsyncWebServerRequest *request) #ifdef HAS_FRONTLIGHT void onApiFrontlightOn(AsyncWebServerRequest *request) { - frontlightFadeInAll(); + auto& ledHandler = getLedHandler(); + ledHandler.frontlightFadeInAll(); request->send(HTTP_OK); } void onApiFrontlightStatus(AsyncWebServerRequest *request) { + auto& ledHandler = getLedHandler(); AsyncResponseStream *response = request->beginResponseStream(JSON_CONTENT); JsonDocument root; - std::vector statuses = frontlightGetStatus(); + std::vector statuses = ledHandler.frontlightGetStatus(); uint16_t arr[NUM_SCREENS]; std::copy(statuses.begin(), statuses.end(), arr); @@ -1105,7 +1112,8 @@ void onApiFrontlightStatus(AsyncWebServerRequest *request) void onApiFrontlightFlash(AsyncWebServerRequest *request) { - frontlightFlash(preferences.getUInt("flEffectDelay")); + auto& ledHandler = getLedHandler(); + ledHandler.frontlightFlash(preferences.getUInt("flEffectDelay")); request->send(HTTP_OK); } @@ -1114,7 +1122,8 @@ void onApiFrontlightSetBrightness(AsyncWebServerRequest *request) { if (request->hasParam("b")) { - frontlightSetBrightness(request->getParam("b")->value().toInt()); + auto& ledHandler = getLedHandler(); + ledHandler.frontlightSetBrightness(request->getParam("b")->value().toInt()); request->send(HTTP_OK); } else @@ -1125,21 +1134,51 @@ void onApiFrontlightSetBrightness(AsyncWebServerRequest *request) void onApiFrontlightOff(AsyncWebServerRequest *request) { - frontlightFadeOutAll(); + auto& ledHandler = getLedHandler(); + ledHandler.frontlightFadeOutAll(); request->send(HTTP_OK); } #endif +void onApiDNDTimeBasedEnable(AsyncWebServerRequest *request) { + auto& ledHandler = getLedHandler(); + ledHandler.setDNDTimeBasedEnabled(true); + request->send(200); +} + +void onApiDNDTimeBasedDisable(AsyncWebServerRequest *request) { + auto& ledHandler = getLedHandler(); + ledHandler.setDNDTimeBasedEnabled(false); + request->send(200); +} + +void onApiDNDSetTimeRange(AsyncWebServerRequest *request) { + if (request->hasParam("startHour") && request->hasParam("startMinute") && + request->hasParam("endHour") && request->hasParam("endMinute")) { + auto& ledHandler = getLedHandler(); + uint8_t startHour = request->getParam("startHour")->value().toInt(); + uint8_t startMinute = request->getParam("startMinute")->value().toInt(); + uint8_t endHour = request->getParam("endHour")->value().toInt(); + uint8_t endMinute = request->getParam("endMinute")->value().toInt(); + + ledHandler.setDNDTimeRange(startHour, startMinute, endHour, endMinute); + request->send(200); + } else { + request->send(400); + } +} + void onApiDNDStatus(AsyncWebServerRequest *request) { + auto& ledHandler = getLedHandler(); JsonDocument doc; - doc["enabled"] = dndEnabled; - doc["timeBasedEnabled"] = dndTimeBasedEnabled; - doc["startTime"] = String(dndTimeRange.startHour) + ":" + - (dndTimeRange.startMinute < 10 ? "0" : "") + String(dndTimeRange.startMinute); - doc["endTime"] = String(dndTimeRange.endHour) + ":" + - (dndTimeRange.endMinute < 10 ? "0" : "") + String(dndTimeRange.endMinute); - doc["active"] = isDNDActive(); + doc["enabled"] = ledHandler.isDNDEnabled(); + doc["timeBasedEnabled"] = ledHandler.isDNDTimeBasedEnabled(); + doc["startTime"] = String(ledHandler.getDNDStartHour()) + ":" + + (ledHandler.getDNDStartMinute() < 10 ? "0" : "") + String(ledHandler.getDNDStartMinute()); + doc["endTime"] = String(ledHandler.getDNDEndHour()) + ":" + + (ledHandler.getDNDEndMinute() < 10 ? "0" : "") + String(ledHandler.getDNDEndMinute()); + doc["active"] = ledHandler.isDNDActive(); String response; serializeJson(doc, response); @@ -1147,11 +1186,92 @@ void onApiDNDStatus(AsyncWebServerRequest *request) { } void onApiDNDEnable(AsyncWebServerRequest *request) { - setDNDEnabled(true); + auto& ledHandler = getLedHandler(); + ledHandler.setDNDEnabled(true); request->send(200); } void onApiDNDDisable(AsyncWebServerRequest *request) { - setDNDEnabled(false); + auto& ledHandler = getLedHandler(); + ledHandler.setDNDEnabled(false); request->send(200); +} + +void onApiLightsGet(AsyncWebServerRequest *request) +{ + auto& ledHandler = getLedHandler(); + auto& pixels = ledHandler.getPixels(); + + DynamicJsonDocument doc(1024); + JsonArray lights = doc.createNestedArray("lights"); + + for (uint i = 0; i < pixels.numPixels(); i++) + { + uint32_t pixColor = pixels.getPixelColor(pixels.numPixels() - i - 1); + JsonObject light = lights.createNestedObject(); + light["r"] = (uint8_t)(pixColor >> 16); + light["g"] = (uint8_t)(pixColor >> 8); + light["b"] = (uint8_t)pixColor; + } + + String output; + serializeJson(doc, output); + request->send(200, "application/json", output); +} + +void onApiLightsPost(AsyncWebServerRequest *request, uint8_t *data, size_t len, + size_t index, size_t total) +{ + auto& ledHandler = getLedHandler(); + auto& pixels = ledHandler.getPixels(); + + DynamicJsonDocument doc(1024); + DeserializationError error = deserializeJson(doc, data); + if (error) + { + request->send(400); + return; + } + + JsonArray lights = doc["lights"]; + if (lights.size() != pixels.numPixels()) + { + request->send(400); + return; + } + + for (uint i = 0; i < pixels.numPixels(); i++) + { + JsonObject light = lights[i]; + uint8_t red = light["r"]; + uint8_t green = light["g"]; + uint8_t blue = light["b"]; + + pixels.setPixelColor((pixels.numPixels() - i - 1), + pixels.Color(red, green, blue)); + } + pixels.show(); + + request->send(200); +} + +void onApiSettings(AsyncWebServerRequest *request, JsonVariant &json) +{ + JsonObject settings = json.as(); + auto& ledHandler = getLedHandler(); + + if (settings.containsKey("dnd")) { + JsonObject dndObj = settings["dnd"]; + if (dndObj.containsKey("timeBasedEnabled")) { + ledHandler.setDNDTimeBasedEnabled(dndObj["timeBasedEnabled"].as()); + } + if (dndObj.containsKey("startHour") && dndObj.containsKey("startMinute") && + dndObj.containsKey("endHour") && dndObj.containsKey("endMinute")) { + ledHandler.setDNDTimeRange( + dndObj["startHour"].as(), + dndObj["startMinute"].as(), + dndObj["endHour"].as(), + dndObj["endMinute"].as()); + } + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4943a51..c892a03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #define WEBSERVER_H #include "ESPAsyncWebServer.h" #include "lib/config.hpp" +#include "lib/led_handler.hpp" uint wifiLostConnection; uint priceNotifyLostConnection = 0; @@ -58,13 +59,14 @@ void handleFrontlight() { if (hasLightLevel() && preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) != 0) { uint lightLevel = getLightLevel(); uint luxThreshold = preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE); + auto& ledHandler = getLedHandler(); if (lightLevel <= 1 && preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK)) { - if (frontlightIsOn()) frontlightFadeOutAll(); - } else if (lightLevel < luxThreshold && !frontlightIsOn()) { - frontlightFadeInAll(); - } else if (frontlightIsOn() && lightLevel > luxThreshold) { - frontlightFadeOutAll(); + if (ledHandler.frontlightIsOn()) ledHandler.frontlightFadeOutAll(); + } else if (lightLevel < luxThreshold && !ledHandler.frontlightIsOn()) { + ledHandler.frontlightFadeInAll(); + } else if (ledHandler.frontlightIsOn() && lightLevel > luxThreshold) { + ledHandler.frontlightFadeOutAll(); } } #endif @@ -100,9 +102,7 @@ void checkMissedBlocks() { } } - void monitorDataConnections() { - // Price notification monitoring if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected()) { handlePriceNotifyDisconnection(); @@ -137,7 +137,6 @@ extern "C" void app_main() { bool thirdPartySource = getDataSource() == THIRD_PARTY_SOURCE; - while (true) { if (eventSourceTaskHandle != NULL) { xTaskNotifyGive(eventSourceTaskHandle);