Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions include/esp/StatusHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@
#ifdef ARDUINO_ARCH_ESP32

#include <atomic>
#include <crgb.h>
#include <cstdint>
#include <esp_timer.h>

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<bool> tick{false};

static inline CRGB color{CRGB::Black};
static inline Color color{none};

static void onTimer(void *arg);

Expand Down
3 changes: 1 addition & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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"
Expand Down
42 changes: 24 additions & 18 deletions src/esp/StatusHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

#include "esp/secrets.h"

#include <FastLED.h>
#include <esp32-hal-gpio.h>
#include <esp32-hal-rgb-led.h>

/**
* @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<WS2812, PIN_LED, fl::EOrder::GRB>(&color, 1);
#endif // PIN_LED
const esp_timer_create_args_t args{
.callback = onTimer,
.arg = this,
Expand All @@ -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);
}
Expand All @@ -69,7 +75,7 @@ void StatusHandler::onTimer(void *arg)
*/
void StatusHandler::setBlue()
{
color = CRGB::Blue;
color = blue;
pending = true;
}

Expand All @@ -78,7 +84,7 @@ void StatusHandler::setBlue()
*/
void StatusHandler::setGreen()
{
color = CRGB::Green;
color = green;
pending = true;
}

Expand All @@ -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;
}
}
Expand All @@ -101,7 +107,7 @@ void StatusHandler::setNone(bool force)
*/
void StatusHandler::setRed()
{
color = CRGB::Red;
color = red;
pending = true;
}

Expand All @@ -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;
}
}
Expand Down