HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
Ws2812Handler.h
Go to the documentation of this file.
1
43#ifndef COMPONENT_HANDLER_WS2812_HANDLER_H_
44#define COMPONENT_HANDLER_WS2812_HANDLER_H_
45
46#include <cstdint>
47#include <memory>
48#include <type_traits>
49#include <utility>
50#include "core/hf-core-drivers/external/hf-ws2812-rmt-driver/inc/ws2812_cpp.hpp"
51#include "core/hf-core-drivers/external/hf-ws2812-rmt-driver/inc/ws2812_effects.hpp"
52#include "RtosMutex.h"
53
54#if defined(ESP_PLATFORM)
55#include "driver/gpio.h"
56#else
57using gpio_num_t = int;
58#endif
59
64
72public:
76 struct Config {
78 uint32_t num_leds{1};
79 LedType led_type{LedType::RGB};
80 uint8_t brightness{255};
81 int rmt_channel{0};
82 uint16_t t0h{400};
83 uint16_t t1h{800};
84 uint16_t t0l{850};
85 uint16_t t1l{450};
86 };
87
88 //=========================================================================
89 // Construction
90 //=========================================================================
91
96 explicit Ws2812Handler(const Config& config) noexcept;
97
98 ~Ws2812Handler() noexcept;
99
100 // Non-copyable
101 Ws2812Handler(const Ws2812Handler&) = delete;
102 Ws2812Handler& operator=(const Ws2812Handler&) = delete;
103
104 // Non-movable
106 Ws2812Handler& operator=(Ws2812Handler&&) = delete;
107
108 //=========================================================================
109 // Initialization
110 //=========================================================================
111
116 esp_err_t Initialize() noexcept;
117
122 bool EnsureInitialized() noexcept;
123
125 bool Deinitialize() noexcept;
126
128 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_; }
129
130 //=========================================================================
131 // Direct Access
132 //=========================================================================
133
137 [[nodiscard]] uint32_t GetNumLeds() const noexcept { return config_.num_leds; }
138
146 [[nodiscard]] WS2812Strip* GetStrip() noexcept;
147 [[nodiscard]] const WS2812Strip* GetStrip() const noexcept;
148
153 [[nodiscard]] WS2812Strip* GetDriver() noexcept;
154 [[nodiscard]] const WS2812Strip* GetDriver() const noexcept;
155
161 [[nodiscard]] WS2812Animator* GetAnimator() noexcept;
162 [[nodiscard]] const WS2812Animator* GetAnimator() const noexcept;
163
168 template <typename Fn>
169 auto visitDriver(Fn&& fn) noexcept -> decltype(fn(std::declval<WS2812Strip&>())) {
170 using ReturnType = decltype(fn(std::declval<WS2812Strip&>()));
171 MutexLockGuard lock(mutex_);
172 if (!EnsureInitializedLocked() || !strip_) {
173 if constexpr (std::is_void_v<ReturnType>) {
174 return;
175 } else {
176 return ReturnType{};
177 }
178 }
179 return fn(*strip_);
180 }
181
186 template <typename Fn>
187 auto visitAnimator(Fn&& fn) noexcept -> decltype(fn(std::declval<WS2812Animator&>())) {
188 using ReturnType = decltype(fn(std::declval<WS2812Animator&>()));
189 MutexLockGuard lock(mutex_);
191 if constexpr (std::is_void_v<ReturnType>) {
192 return;
193 } else {
194 return ReturnType{};
195 }
196 }
197 return fn(*animator_);
198 }
199
201 void DumpDiagnostics() noexcept;
202
207 const char* GetDescription() const noexcept;
208
213 static Config GetDefaultConfig() noexcept { return Config{}; }
214
215private:
216 bool EnsureInitializedLocked() noexcept;
217
219 bool initialized_{false};
220 mutable RtosMutex mutex_;
221 std::unique_ptr<WS2812Strip> strip_;
222 std::unique_ptr<WS2812Animator> animator_;
223 char description_[64]{};
224};
225
227
228#endif // COMPONENT_HANDLER_WS2812_HANDLER_H_
int gpio_num_t
Definition Ws2812Handler.h:57
Unified handler for WS2812 addressable LED strips.
Definition Ws2812Handler.h:71
char description_[64]
Human-readable handler description.
Definition Ws2812Handler.h:223
WS2812Strip * GetDriver() noexcept
Naming-consistent alias of GetStrip().
Definition Ws2812Handler.cpp:119
auto visitDriver(Fn &&fn) noexcept -> decltype(fn(std::declval< WS2812Strip & >()))
Visit strip driver with a callable.
Definition Ws2812Handler.h:169
WS2812Animator * GetAnimator() noexcept
Get the animator object.
Definition Ws2812Handler.cpp:128
std::unique_ptr< WS2812Strip > strip_
Definition Ws2812Handler.h:221
bool initialized_
Definition Ws2812Handler.h:219
void DumpDiagnostics() noexcept
Dump diagnostics to logger.
Definition Ws2812Handler.cpp:141
bool Deinitialize() noexcept
Deinitialize and release RMT resources.
Definition Ws2812Handler.cpp:87
~Ws2812Handler() noexcept
Definition Ws2812Handler.cpp:26
WS2812Strip * GetStrip() noexcept
Get the underlying LED strip object.
Definition Ws2812Handler.cpp:106
bool EnsureInitialized() noexcept
Ensure strip resources are initialized (lazy init entrypoint).
Definition Ws2812Handler.cpp:75
const char * GetDescription() const noexcept
Get a human-readable description of the handler.
Definition Ws2812Handler.cpp:157
RtosMutex mutex_
Definition Ws2812Handler.h:220
std::unique_ptr< WS2812Animator > animator_
Definition Ws2812Handler.h:222
Config config_
Definition Ws2812Handler.h:218
esp_err_t Initialize() noexcept
Initialize the RMT channel and LED strip.
Definition Ws2812Handler.cpp:32
bool IsInitialized() const noexcept
Check if initialized.
Definition Ws2812Handler.h:128
Ws2812Handler(const Config &config) noexcept
Construct WS2812 handler with configuration.
Definition Ws2812Handler.cpp:16
uint32_t GetNumLeds() const noexcept
Get the number of LEDs in the strip.
Definition Ws2812Handler.h:137
auto visitAnimator(Fn &&fn) noexcept -> decltype(fn(std::declval< WS2812Animator & >()))
Visit animator object with a callable.
Definition Ws2812Handler.h:187
static Config GetDefaultConfig() noexcept
Get a sensible default Config.
Definition Ws2812Handler.h:213
bool EnsureInitializedLocked() noexcept
Definition Ws2812Handler.cpp:80
Configuration structure for the WS2812 handler.
Definition Ws2812Handler.h:76
uint16_t t1l
T1L timing in nanoseconds.
Definition Ws2812Handler.h:85
int rmt_channel
RMT channel number.
Definition Ws2812Handler.h:81
LedType led_type
Definition Ws2812Handler.h:79
uint8_t brightness
Global brightness (0-255)
Definition Ws2812Handler.h:80
uint32_t num_leds
Number of LEDs in the strip.
Definition Ws2812Handler.h:78
uint16_t t1h
T1H timing in nanoseconds.
Definition Ws2812Handler.h:83
uint16_t t0l
T0L timing in nanoseconds.
Definition Ws2812Handler.h:84
uint16_t t0h
T0H timing in nanoseconds.
Definition Ws2812Handler.h:82
gpio_num_t gpio_pin
GPIO pin connected to LED data line.
Definition Ws2812Handler.h:77