From 83368bc3d3c1d16392c27fb860b3ffdae3d33d58 Mon Sep 17 00:00:00 2001 From: Jan-Petter Gundersen Date: Tue, 24 Nov 2026 16:46:21 +0100 Subject: [PATCH 1/2] Status-LED refactor with no external dependencies --- include/esp/StatusHandler.h | 19 +++++++++++++++-- platformio.ini | 3 +-- src/esp/StatusHandler.cpp | 42 +++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/include/esp/StatusHandler.h b/include/esp/StatusHandler.h index 173b12d..e5f982a 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 black{0U, 0U, 0U}; + static constexpr Color blue{0U, 0U, 0xFFU}; + static constexpr Color green{0U, 0xFFU, 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{black}; 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..8f74059 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 != black) { - --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 != black) { 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 = black; 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; } } From 98c9f6b53bdbe9533357306c7911428689c190f7 Mon Sep 17 00:00:00 2001 From: Jan-Petter Gundersen Date: Tue, 24 Nov 2026 16:57:36 +0100 Subject: [PATCH 2/2] Rename black to none --- include/esp/StatusHandler.h | 4 ++-- src/esp/StatusHandler.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/esp/StatusHandler.h b/include/esp/StatusHandler.h index e5f982a..7928a01 100644 --- a/include/esp/StatusHandler.h +++ b/include/esp/StatusHandler.h @@ -18,9 +18,9 @@ class StatusHandler constexpr bool operator==(const Color &) const = default; }; - static constexpr Color black{0U, 0U, 0U}; 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}; @@ -30,7 +30,7 @@ class StatusHandler std::atomic tick{false}; - static inline Color color{black}; + static inline Color color{none}; static void onTimer(void *arg); diff --git a/src/esp/StatusHandler.cpp b/src/esp/StatusHandler.cpp index 8f74059..c3931ac 100644 --- a/src/esp/StatusHandler.cpp +++ b/src/esp/StatusHandler.cpp @@ -30,7 +30,7 @@ void StatusHandler::handle() { if (!pending && tick.exchange(false, std::memory_order_acq_rel)) { - if (color != black) + if (color != none) { if (color.r != 0U) { @@ -57,7 +57,7 @@ void StatusHandler::handle() { esp_timer_stop(timer); tick.store(false, std::memory_order_release); - if (color != black) + if (color != none) { esp_timer_start_once(timer, 0b1ULL << 18U); } @@ -97,7 +97,7 @@ void StatusHandler::setNone(bool force) { if (color.r == 0U || (color.r == color.g && color.g == color.b) || force) { - color = black; + color = none; pending = true; } }