diff --git a/include/esp/StatusHandler.h b/include/esp/StatusHandler.h index 173b12d..7928a01 100644 --- a/include/esp/StatusHandler.h +++ b/include/esp/StatusHandler.h @@ -3,19 +3,34 @@ #ifdef ARDUINO_ARCH_ESP32 #include -#include +#include #include class StatusHandler { private: + struct Color + { + uint8_t r{0U}; + uint8_t g{0U}; + uint8_t b{0U}; + + constexpr bool operator==(const Color &) const = default; + }; + + static constexpr Color blue{0U, 0U, 0xFFU}; + static constexpr Color green{0U, 0xFFU, 0U}; + static constexpr Color none{0U, 0U, 0U}; + static constexpr Color red{0xFFU, 0U, 0U}; + static constexpr Color white{0xFFU, 0xFFU, 0xFFU}; + static inline bool pending{true}; esp_timer_handle_t timer{nullptr}; std::atomic tick{false}; - static inline CRGB color{CRGB::Black}; + static inline Color color{none}; static void onTimer(void *arg); diff --git a/platformio.ini b/platformio.ini index 62ddeee..41bdfb2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,6 +1,6 @@ [platformio] name = Bekant -description = ESP32 mod for the IKEA Bekant desk featuring the Megadesk replacement controller +description = ESP32 mod for the IKEA Bekant desk core_dir = .platformio [env] @@ -16,7 +16,6 @@ lib_deps = esp32async/AsyncTCP @ 3.6.1 bblanchon/ArduinoJson @ 7.4.3 bertmelis/espMqttClient @ 1.7.3 - fastled/FastLED @ 3.10.10 ;upload_protocol = espota ;upload_port = bekant.local ;upload_flags = --auth="password" diff --git a/src/esp/StatusHandler.cpp b/src/esp/StatusHandler.cpp index 76a721a..c3931ac 100644 --- a/src/esp/StatusHandler.cpp +++ b/src/esp/StatusHandler.cpp @@ -4,18 +4,13 @@ #include "esp/secrets.h" -#include -#include +#include /** - * @brief Configures the optional status LED output and registers it with FastLED. + * @brief Configures the fade timer for the status LED. */ void StatusHandler::begin() { -#ifdef PIN_LED - pinMode(PIN_LED, OUTPUT); - FastLED.addLeds(&color, 1); -#endif // PIN_LED const esp_timer_create_args_t args{ .callback = onTimer, .arg = this, @@ -35,23 +30,34 @@ void StatusHandler::handle() { if (!pending && tick.exchange(false, std::memory_order_acq_rel)) { - if (color != CRGB::Black) + if (color != none) { - --color; + if (color.r != 0U) + { + --color.r; + } + if (color.g != 0U) + { + --color.g; + } + if (color.b != 0U) + { + --color.b; + } pending = true; } } if (pending) { #ifdef PIN_LED - FastLED.show(); + rgbLedWriteOrdered(PIN_LED, rgb_led_color_order_t::LED_COLOR_ORDER_GRB, color.r, color.g, color.b); #endif // PIN_LED pending = false; if (timer != nullptr) { esp_timer_stop(timer); tick.store(false, std::memory_order_release); - if (color != CRGB::Black) + if (color != none) { esp_timer_start_once(timer, 0b1ULL << 18U); } @@ -69,7 +75,7 @@ void StatusHandler::onTimer(void *arg) */ void StatusHandler::setBlue() { - color = CRGB::Blue; + color = blue; pending = true; } @@ -78,7 +84,7 @@ void StatusHandler::setBlue() */ void StatusHandler::setGreen() { - color = CRGB::Green; + color = green; pending = true; } @@ -89,9 +95,9 @@ void StatusHandler::setGreen() */ void StatusHandler::setNone(bool force) { - if (color[0U] == 0U || (color[0U] == color[1U] && color[1U] == color[2U]) || force) + if (color.r == 0U || (color.r == color.g && color.g == color.b) || force) { - color = CRGB::Black; + color = none; pending = true; } } @@ -101,7 +107,7 @@ void StatusHandler::setNone(bool force) */ void StatusHandler::setRed() { - color = CRGB::Red; + color = red; pending = true; } @@ -112,9 +118,9 @@ void StatusHandler::setRed() */ void StatusHandler::setWhite(bool force) { - if ((color[0U] == color[1U] && color[1U] == color[2U]) || force) + if ((color.r == color.g && color.g == color.b) || force) { - color = CRGB::White; + color = white; pending = true; } }