HardFOC Core
API reference
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};
84 uint16_t t0h{400};
85 uint16_t t1h{800};
86 uint16_t t0l{850};
87 uint16_t t1l{450};
88 };
89
90 //=========================================================================
91 // Construction
92 //=========================================================================
93
98 explicit Ws2812Handler(const Config& config) noexcept;
99
100 ~Ws2812Handler() noexcept;
101
102 // Non-copyable
103 Ws2812Handler(const Ws2812Handler&) = delete;
104 Ws2812Handler& operator=(const Ws2812Handler&) = delete;
105
106 // Non-movable
108 Ws2812Handler& operator=(Ws2812Handler&&) = delete;
109
110 //=========================================================================
111 // Initialization
112 //=========================================================================
113
118 esp_err_t Initialize() noexcept;
119
124 bool EnsureInitialized() noexcept;
125
127 bool Deinitialize() noexcept;
128
130 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_; }
131
132 //=========================================================================
133 // Direct Access
134 //=========================================================================
135
139 [[nodiscard]] uint32_t GetNumLeds() const noexcept { return config_.num_leds; }
140
148 [[nodiscard]] WS2812Strip* GetStrip() noexcept;
149 [[nodiscard]] const WS2812Strip* GetStrip() const noexcept;
150
155 [[nodiscard]] WS2812Strip* GetDriver() noexcept;
156 [[nodiscard]] const WS2812Strip* GetDriver() const noexcept;
157
163 [[nodiscard]] WS2812Animator* GetAnimator() noexcept;
164 [[nodiscard]] const WS2812Animator* GetAnimator() const noexcept;
165
170 template <typename Fn>
171 auto visitDriver(Fn&& fn) noexcept -> decltype(fn(std::declval<WS2812Strip&>())) {
172 using ReturnType = decltype(fn(std::declval<WS2812Strip&>()));
173 MutexLockGuard lock(mutex_);
174 if (!EnsureInitializedLocked() || !strip_) {
175 if constexpr (std::is_void_v<ReturnType>) {
176 return;
177 } else {
178 return ReturnType{};
179 }
180 }
181 return fn(*strip_);
182 }
183
188 template <typename Fn>
189 auto visitAnimator(Fn&& fn) noexcept -> decltype(fn(std::declval<WS2812Animator&>())) {
190 using ReturnType = decltype(fn(std::declval<WS2812Animator&>()));
191 MutexLockGuard lock(mutex_);
193 if constexpr (std::is_void_v<ReturnType>) {
194 return;
195 } else {
196 return ReturnType{};
197 }
198 }
199 return fn(*animator_);
200 }
201
203 void DumpDiagnostics() noexcept;
204
209 const char* GetDescription() const noexcept;
210
215 static Config GetDefaultConfig() noexcept { return Config{}; }
216
217private:
218 bool EnsureInitializedLocked() noexcept;
219
221 bool initialized_{false};
222 mutable RtosMutex mutex_;
223 std::unique_ptr<WS2812Strip> strip_;
224 std::unique_ptr<WS2812Animator> animator_;
225 char description_[64]{};
226};
227
229
230#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:225
WS2812Strip * GetDriver() noexcept
Naming-consistent alias of GetStrip().
Definition Ws2812Handler.cpp:127
auto visitDriver(Fn &&fn) noexcept -> decltype(fn(std::declval< WS2812Strip & >()))
Visit strip driver with a callable.
Definition Ws2812Handler.h:171
WS2812Animator * GetAnimator() noexcept
Get the animator object.
Definition Ws2812Handler.cpp:136
std::unique_ptr< WS2812Strip > strip_
Definition Ws2812Handler.h:223
bool initialized_
Definition Ws2812Handler.h:221
void DumpDiagnostics() noexcept
Dump diagnostics to logger.
Definition Ws2812Handler.cpp:149
bool Deinitialize() noexcept
Deinitialize and release RMT resources.
Definition Ws2812Handler.cpp:95
~Ws2812Handler() noexcept
Definition Ws2812Handler.cpp:26
WS2812Strip * GetStrip() noexcept
Get the underlying LED strip object.
Definition Ws2812Handler.cpp:114
bool EnsureInitialized() noexcept
Ensure strip resources are initialized (lazy init entrypoint).
Definition Ws2812Handler.cpp:83
const char * GetDescription() const noexcept
Get a human-readable description of the handler.
Definition Ws2812Handler.cpp:165
RtosMutex mutex_
Definition Ws2812Handler.h:222
std::unique_ptr< WS2812Animator > animator_
Definition Ws2812Handler.h:224
Config config_
Definition Ws2812Handler.h:220
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:130
uint32_t GetNumLeds() const noexcept
Get the number of LEDs in the strip.
Definition Ws2812Handler.h:139
auto visitAnimator(Fn &&fn) noexcept -> decltype(fn(std::declval< WS2812Animator & >()))
Visit animator object with a callable.
Definition Ws2812Handler.h:189
static Config GetDefaultConfig() noexcept
Get a sensible default Config.
Definition Ws2812Handler.h:215
bool EnsureInitializedLocked() noexcept
Definition Ws2812Handler.cpp:88
Configuration structure for the WS2812 handler.
Definition Ws2812Handler.h:76
uint16_t t1l
T1L timing in nanoseconds.
Definition Ws2812Handler.h:87
int rmt_channel
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:85
uint16_t t0l
T0L timing in nanoseconds.
Definition Ws2812Handler.h:86
uint16_t t0h
T0H timing in nanoseconds.
Definition Ws2812Handler.h:84
gpio_num_t gpio_pin
GPIO pin connected to LED data line.
Definition Ws2812Handler.h:77