HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
Loading...
Searching...
No Matches
LedManager.h
Go to the documentation of this file.
1
18#ifndef VORTEX_LED_MANAGER_H_
19#define VORTEX_LED_MANAGER_H_
20
21#include "handlers/logger/Logger.h"
22#include "RtosMutex.h"
23#include "core/hf-core-drivers/internal/hf-pincfg/src/hf_functional_pin_config_vortex_v1.hpp"
24
25#include <cstdint>
26#include <atomic>
27
28//==============================================================================
29// LED ERROR CODES
30//==============================================================================
31
43
49constexpr const char* LedErrorToString(LedError error) noexcept {
50 switch (error) {
51 case LedError::SUCCESS: return "Success";
52 case LedError::NOT_INITIALIZED: return "Not initialized";
53 case LedError::INITIALIZATION_FAILED: return "Initialization failed";
54 case LedError::INVALID_PARAMETER: return "Invalid parameter";
55 case LedError::HARDWARE_ERROR: return "Hardware error";
56 case LedError::ANIMATION_FAILED: return "Animation failed";
57 case LedError::INVALID_COLOR: return "Invalid color";
58 case LedError::INVALID_BRIGHTNESS: return "Invalid brightness";
59 default: return "Unknown error";
60 }
61}
62
63//==============================================================================
64// LED ANIMATION TYPES
65//==============================================================================
66
68enum class LedAnimation : uint8_t {
69 OFF = 0,
70 SOLID,
71 BLINK,
72 BREATH,
73 RAINBOW,
74 STATUS_OK,
79};
80
86constexpr const char* LedAnimationToString(LedAnimation animation) noexcept {
87 switch (animation) {
88 case LedAnimation::OFF: return "Off";
89 case LedAnimation::SOLID: return "Solid";
90 case LedAnimation::BLINK: return "Blink";
91 case LedAnimation::BREATH: return "Breath";
92 case LedAnimation::RAINBOW: return "Rainbow";
93 case LedAnimation::STATUS_OK: return "Status OK";
94 case LedAnimation::STATUS_WARN: return "Status Warning";
95 case LedAnimation::STATUS_ERROR: return "Status Error";
96 case LedAnimation::STATUS_BOOT: return "Status Boot";
97 case LedAnimation::STATUS_CALIBRATE: return "Status Calibrate";
98 default: return "Unknown";
99 }
100}
101
102//==============================================================================
103// COLOR HELPERS
104//==============================================================================
105
111struct LedColor {
112 uint8_t red;
113 uint8_t green;
114 uint8_t blue;
115
117 constexpr LedColor() noexcept : red(0), green(0), blue(0) {}
118
125 constexpr LedColor(uint8_t r, uint8_t g, uint8_t b) noexcept : red(r), green(g), blue(b) {}
126
131 [[nodiscard]] constexpr uint32_t ToRgb() const noexcept {
132 return (static_cast<uint32_t>(red) << 16) |
133 (static_cast<uint32_t>(green) << 8) |
134 static_cast<uint32_t>(blue);
135 }
136
142 static constexpr LedColor FromRgb(uint32_t rgb) noexcept {
143 return LedColor(
144 static_cast<uint8_t>((rgb >> 16) & 0xFF),
145 static_cast<uint8_t>((rgb >> 8) & 0xFF),
146 static_cast<uint8_t>(rgb & 0xFF));
147 }
148};
149
151namespace LedColors {
152 static constexpr LedColor BLACK(0, 0, 0);
153 static constexpr LedColor RED(255, 0, 0);
154 static constexpr LedColor GREEN(0, 255, 0);
155 static constexpr LedColor BLUE(0, 0, 255);
156 static constexpr LedColor YELLOW(255, 255, 0);
157 static constexpr LedColor CYAN(0, 255, 255);
158 static constexpr LedColor MAGENTA(255, 0, 255);
159 static constexpr LedColor WHITE(255, 255, 255);
160 static constexpr LedColor ORANGE(255, 165, 0);
161 static constexpr LedColor PURPLE(128, 0, 128);
162} // namespace LedColors
163
164//==============================================================================
165// LED DIAGNOSTICS
166//==============================================================================
167
181
182//==============================================================================
183// LED MANAGER
184//==============================================================================
185
200public:
201 //==========================================================================
202 // SINGLETON AND LIFECYCLE
203 //==========================================================================
204
209 static LedManager& GetInstance() noexcept;
210
211 LedManager(const LedManager&) = delete;
212 LedManager& operator=(const LedManager&) = delete;
215
220 [[nodiscard]] bool EnsureInitialized() noexcept;
221
226 [[nodiscard]] bool Shutdown() noexcept;
227
229 [[nodiscard]] bool Deinitialize() noexcept { return Shutdown(); }
230
233 [[nodiscard]] bool IsInitialized() const noexcept { return is_initialized_.load(std::memory_order_acquire); }
234
240 [[nodiscard]] LedError GetSystemDiagnostics(LedSystemDiagnostics& diagnostics) const noexcept;
241
246 [[nodiscard]] LedError GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
247
248 //==========================================================================
249 // BASIC LED OPERATIONS
250 //==========================================================================
251
258 [[nodiscard]] LedError SetColor(const LedColor& color, uint32_t led_index = 0) noexcept;
259
266 [[nodiscard]] LedError SetColor(uint32_t rgb, uint32_t led_index = 0) noexcept;
267
272 [[nodiscard]] LedError TurnOff() noexcept;
273
279 [[nodiscard]] LedError GetCurrentColor(LedColor& color) const noexcept;
280
281 //==========================================================================
282 // BRIGHTNESS
283 //==========================================================================
284
290 [[nodiscard]] LedError SetBrightnessPercent(uint8_t brightness_percent) noexcept;
291
297 [[nodiscard]] LedError SetBrightnessRaw(uint8_t brightness_raw) noexcept;
298
300 [[nodiscard]] LedError SetBrightness(uint8_t brightness) noexcept { return SetBrightnessRaw(brightness); }
301
307 [[nodiscard]] LedError SetMaxBrightness(uint8_t max_brightness) noexcept;
308
314 [[nodiscard]] LedError GetCurrentBrightnessPercent(uint8_t& brightness_percent) const noexcept {
315 brightness_percent = RawToPercent(current_brightness_);
316 return LedError::SUCCESS;
317 }
318
324 [[nodiscard]] LedError GetCurrentBrightnessRaw(uint8_t& brightness_raw) const noexcept {
325 brightness_raw = current_brightness_;
326 return LedError::SUCCESS;
327 }
328
329 //==========================================================================
330 // ANIMATION
331 //==========================================================================
332
339 [[nodiscard]] LedError StartAnimation(LedAnimation animation, const LedColor& color = LedColors::WHITE) noexcept;
340
345 [[nodiscard]] LedError StopAnimation() noexcept;
346
348 [[nodiscard]] bool IsAnimationActive() const noexcept { return current_animation_ != LedAnimation::OFF; }
349
354 [[nodiscard]] LedError UpdateAnimation() noexcept;
355
356 //==========================================================================
357 // STATUS INDICATION
358 //==========================================================================
359
365 [[nodiscard]] LedError SetStatus(LedAnimation animation) noexcept { return StartAnimation(animation); }
366
368 [[nodiscard]] LedError IndicateBoot() noexcept { return StartAnimation(LedAnimation::STATUS_BOOT); }
370 [[nodiscard]] LedError IndicateReady() noexcept { return StartAnimation(LedAnimation::STATUS_OK); }
377
378 //==========================================================================
379 // UTILITY
380 //==========================================================================
381
387 [[nodiscard]] static uint32_t ColorWheel(uint8_t position) noexcept;
388
390 void DumpStatistics() const noexcept;
391
393 [[nodiscard]] int GetCurrentGpioPin() const noexcept { return current_led_gpio_; }
394
396 [[nodiscard]] uint32_t GetLedCount() const noexcept { return kNumLeds; }
397
398private:
399 LedManager() noexcept = default;
400 ~LedManager() = default;
401
402 [[nodiscard]] bool Initialize() noexcept;
403 [[nodiscard]] LedError UpdateAnimationStep() noexcept;
404 void UpdateStatistics(bool success) noexcept;
405 void UpdateLastError(LedError error_code) noexcept;
406 [[nodiscard]] uint64_t GetSystemUptimeMs() const noexcept;
407
408 [[nodiscard]] uint8_t PercentToRaw(uint8_t percent) const noexcept {
409 if (percent > 100) percent = 100;
410 return static_cast<uint8_t>((static_cast<uint32_t>(percent) * max_brightness_) / 100);
411 }
412
413 [[nodiscard]] uint8_t RawToPercent(uint8_t raw) const noexcept {
414 if (max_brightness_ == 0) return 0;
415 return static_cast<uint8_t>((static_cast<uint32_t>(raw) * 100) / max_brightness_);
416 }
417
418 [[nodiscard]] static int GetLedGpioPin() noexcept {
419 const auto* mapping = GetGpioMapping(HfFunctionalGpioPin::WS2812_LED_DAT);
420 if (mapping && mapping->chip_type == static_cast<uint8_t>(HfGpioChipType::ESP32_INTERNAL)) {
421 return static_cast<int>(mapping->physical_pin);
422 }
423 return -1;
424 }
425
427 [[nodiscard]] bool ShowPixel(uint32_t rgb, uint8_t brightness) noexcept;
428
429 //==========================================================================
430 // CONSTANTS
431 //==========================================================================
432
433 static constexpr uint32_t kNumLeds = 1;
434 static constexpr uint8_t DEFAULT_MAX_BRIGHTNESS = 255;
435 static constexpr uint32_t ANIMATION_UPDATE_INTERVAL = 50; // ms
436 static constexpr uint32_t BREATH_SPEED = 5;
437 static constexpr uint32_t BLINK_SPEED = 500; // ms
438
439 //==========================================================================
440 // STATE
441 //==========================================================================
442
443 std::atomic<bool> is_initialized_{false};
444 mutable RtosMutex mutex_;
445
446
451 uint16_t animation_step_{0};
454
455 std::atomic<uint32_t> total_operations_{0};
456 std::atomic<uint32_t> successful_operations_{0};
457 std::atomic<uint32_t> failed_operations_{0};
458 std::atomic<uint32_t> animation_cycles_{0};
459 std::atomic<LedError> last_error_{LedError::SUCCESS};
462};
463
464//==============================================================================
465// CONVENIENCE
466//==============================================================================
467
472[[nodiscard]] inline LedManager& GetLedManager() noexcept {
474}
475
476#endif // VORTEX_LED_MANAGER_H_
LedError
Error codes returned by LedManager operations.
Definition LedManager.h:33
@ NOT_INITIALIZED
Manager has not been initialized yet.
@ ANIMATION_FAILED
Animation start/update failed.
@ INVALID_COLOR
Colour value rejected.
@ HARDWARE_ERROR
RMT peripheral or strip communication error.
@ INVALID_BRIGHTNESS
Brightness value out of [0, max] range.
@ SUCCESS
Operation completed successfully.
@ INITIALIZATION_FAILED
WS2812 strip/RMT setup failed.
@ INVALID_PARAMETER
Argument out of valid range.
LedAnimation
Available LED animation modes.
Definition LedManager.h:68
@ STATUS_WARN
Warning indicator (yellow blink).
@ BREATH
Smooth brightness ramp up/down.
@ OFF
LED turned off (no animation).
@ STATUS_BOOT
Boot-up indicator (blue blink).
@ STATUS_ERROR
Error indicator (red fast-blink).
@ STATUS_OK
System-ready indicator (solid green).
@ SOLID
Constant colour, no animation.
@ BLINK
On/off blink at BLINK_SPEED interval.
@ STATUS_CALIBRATE
Calibration indicator (cyan breathe).
@ RAINBOW
Continuous colour-wheel cycle.
LedManager & GetLedManager() noexcept
Convenience accessor — equivalent to LedManager::GetInstance().
Definition LedManager.h:472
constexpr const char * LedAnimationToString(LedAnimation animation) noexcept
Convert a LedAnimation to a human-readable C-string.
Definition LedManager.h:86
constexpr const char * LedErrorToString(LedError error) noexcept
Convert a LedError to a human-readable C-string.
Definition LedManager.h:49
Singleton that drives the on-board WS2812 LED via ESP-IDF RMT.
Definition LedManager.h:199
LedError IndicateBoot() noexcept
Show the boot indicator (blue blink).
Definition LedManager.h:368
LedError UpdateAnimation() noexcept
Advance the animation by one tick. Call periodically (~50 ms).
Definition LedManager.cpp:200
LedError IndicateWarning() noexcept
Show the warning indicator (yellow blink).
Definition LedManager.h:372
LedError SetBrightness(uint8_t brightness) noexcept
Alias for SetBrightnessRaw().
Definition LedManager.h:300
std::atomic< uint32_t > successful_operations_
Definition LedManager.h:456
LedError StopAnimation() noexcept
Stop the current animation and turn the LED off.
Definition LedManager.cpp:189
LedError UpdateAnimationStep() noexcept
Definition LedManager.cpp:279
LedColor current_color_
Definition LedManager.h:448
static constexpr uint32_t BLINK_SPEED
Definition LedManager.h:437
uint16_t animation_step_
Definition LedManager.h:451
std::atomic< LedError > last_error_
Definition LedManager.h:459
static constexpr uint32_t kNumLeds
Definition LedManager.h:433
LedError GetSystemDiagnostics(LedSystemDiagnostics &diagnostics) const noexcept
Populate a diagnostics snapshot.
Definition LedManager.cpp:55
bool Shutdown() noexcept
Turn off the LED and release driver resources.
Definition LedManager.cpp:40
bool ShowPixel(uint32_t rgb, uint8_t brightness) noexcept
Latch one colour at a brightness onto the LED through the board binding.
Definition LedManager.cpp:275
static int GetLedGpioPin() noexcept
Definition LedManager.h:418
void DumpStatistics() const noexcept
Log current LED state and statistics to the console.
Definition LedManager.cpp:231
static constexpr uint32_t BREATH_SPEED
Definition LedManager.h:436
LedError GetLastError() const noexcept
Get the most recent error code from any LED operation.
Definition LedManager.h:246
bool Deinitialize() noexcept
Alias for Shutdown().
Definition LedManager.h:229
int GetCurrentGpioPin() const noexcept
GPIO pin the LED strip is attached to.
Definition LedManager.h:393
LedError GetCurrentBrightnessPercent(uint8_t &brightness_percent) const noexcept
Read current brightness as a percentage of max.
Definition LedManager.h:314
LedError GetCurrentColor(LedColor &color) const noexcept
Read the currently displayed colour.
Definition LedManager.cpp:118
LedManager(LedManager &&)=delete
bool IsAnimationActive() const noexcept
Check whether an animation is currently running.
Definition LedManager.h:348
LedError GetCurrentBrightnessRaw(uint8_t &brightness_raw) const noexcept
Read current brightness as a raw 0–255 value.
Definition LedManager.h:324
bool IsInitialized() const noexcept
Check whether the LED driver is initialised.
Definition LedManager.h:233
void UpdateLastError(LedError error_code) noexcept
Definition LedManager.cpp:321
static uint32_t ColorWheel(uint8_t position) noexcept
Map a 0–255 wheel position to a rainbow RGB colour.
Definition LedManager.cpp:218
std::atomic< bool > is_initialized_
Definition LedManager.h:443
LedError StartAnimation(LedAnimation animation, const LedColor &color=LedColors::WHITE) noexcept
Start a named animation with an optional base colour.
Definition LedManager.cpp:160
LedError TurnOff() noexcept
Turn the LED off (set colour to black).
Definition LedManager.cpp:100
LedAnimation current_animation_
Definition LedManager.h:447
uint8_t PercentToRaw(uint8_t percent) const noexcept
Definition LedManager.h:408
LedError IndicateCalibration() noexcept
Show the calibration indicator (cyan breathe).
Definition LedManager.h:376
uint32_t GetLedCount() const noexcept
Number of LEDs in the strip (always 1 on Vortex V1).
Definition LedManager.h:396
std::atomic< uint32_t > total_operations_
Definition LedManager.h:455
LedManager & operator=(const LedManager &)=delete
std::atomic< uint32_t > animation_cycles_
Definition LedManager.h:458
LedError SetBrightnessPercent(uint8_t brightness_percent) noexcept
Set brightness as a percentage of max.
Definition LedManager.cpp:128
std::atomic< uint32_t > failed_operations_
Definition LedManager.h:457
int8_t animation_direction_
Definition LedManager.h:452
RtosMutex mutex_
Definition LedManager.h:444
uint8_t current_brightness_
Definition LedManager.h:449
LedManager() noexcept=default
static constexpr uint8_t DEFAULT_MAX_BRIGHTNESS
Definition LedManager.h:434
LedError SetBrightnessRaw(uint8_t brightness_raw) noexcept
Set brightness as a raw 0–255 value.
Definition LedManager.cpp:139
uint8_t max_brightness_
Definition LedManager.h:450
LedManager(const LedManager &)=delete
uint64_t system_start_time_
Definition LedManager.h:460
uint64_t last_animation_update_
Definition LedManager.h:453
LedError SetMaxBrightness(uint8_t max_brightness) noexcept
Set the upper ceiling for brightness.
Definition LedManager.cpp:149
static constexpr uint32_t ANIMATION_UPDATE_INTERVAL
Definition LedManager.h:435
bool Initialize() noexcept
Definition LedManager.cpp:248
uint64_t GetSystemUptimeMs() const noexcept
Definition LedManager.cpp:325
LedManager & operator=(LedManager &&)=delete
void UpdateStatistics(bool success) noexcept
Definition LedManager.cpp:315
int current_led_gpio_
Definition LedManager.h:461
LedError IndicateError() noexcept
Show the error indicator (red fast-blink).
Definition LedManager.h:374
uint8_t RawToPercent(uint8_t raw) const noexcept
Definition LedManager.h:413
LedError SetColor(const LedColor &color, uint32_t led_index=0) noexcept
Set LED colour from an LedColor struct.
Definition LedManager.cpp:74
static LedManager & GetInstance() noexcept
Access the singleton instance (Meyers singleton, thread-safe).
Definition LedManager.cpp:24
bool EnsureInitialized() noexcept
Initialise the WS2812 LED strip (idempotent).
Definition LedManager.cpp:33
LedError SetStatus(LedAnimation animation) noexcept
Set a status animation by enum (generic variant).
Definition LedManager.h:365
LedError IndicateReady() noexcept
Show the ready indicator (solid green).
Definition LedManager.h:370
Predefined named colour constants.
Definition LedManager.h:151
static constexpr LedColor GREEN(0, 255, 0)
Pure green.
static constexpr LedColor WHITE(255, 255, 255)
Full white.
static constexpr LedColor YELLOW(255, 255, 0)
Yellow.
static constexpr LedColor MAGENTA(255, 0, 255)
Magenta.
static constexpr LedColor PURPLE(128, 0, 128)
Purple.
static constexpr LedColor ORANGE(255, 165, 0)
Orange.
static constexpr LedColor BLACK(0, 0, 0)
Off / black.
static constexpr LedColor BLUE(0, 0, 255)
Pure blue.
static constexpr LedColor RED(255, 0, 0)
Pure red.
static constexpr LedColor CYAN(0, 255, 255)
Cyan.
Simple RGB colour triplet.
Definition LedManager.h:111
uint8_t blue
Blue channel [0–255].
Definition LedManager.h:114
uint8_t green
Green channel [0–255].
Definition LedManager.h:113
constexpr uint32_t ToRgb() const noexcept
Pack into a 24-bit 0xRRGGBB integer.
Definition LedManager.h:131
static constexpr LedColor FromRgb(uint32_t rgb) noexcept
Unpack from a 24-bit 0xRRGGBB integer.
Definition LedManager.h:142
constexpr LedColor(uint8_t r, uint8_t g, uint8_t b) noexcept
Construct from individual R/G/B components.
Definition LedManager.h:125
uint8_t red
Red channel [0–255].
Definition LedManager.h:112
constexpr LedColor() noexcept
Default-construct black (0, 0, 0).
Definition LedManager.h:117
Snapshot of LED subsystem health and counters.
Definition LedManager.h:169
bool animation_active
true if an animation is currently running.
Definition LedManager.h:172
uint32_t total_operations
Lifetime count of all API calls.
Definition LedManager.h:174
uint32_t successful_operations
Calls that returned SUCCESS.
Definition LedManager.h:175
uint32_t current_color
Current colour as packed 0xRRGGBB.
Definition LedManager.h:179
LedAnimation current_animation
Currently active animation mode.
Definition LedManager.h:173
uint32_t current_brightness
Current brightness value [0–255].
Definition LedManager.h:178
bool led_initialized
true after WS2812 strip init succeeds.
Definition LedManager.h:171
bool system_healthy
true when driver initialised and operational.
Definition LedManager.h:170
uint32_t failed_operations
Calls that returned an error.
Definition LedManager.h:176
uint32_t animation_cycles
Number of animation update ticks processed.
Definition LedManager.h:177