HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
LedManager.h
Go to the documentation of this file.
1
19#ifndef FLUX_LED_MANAGER_H_
20#define FLUX_LED_MANAGER_H_
21
22#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/HardwareTypes.h"
23#include "handlers/logger/Logger.h"
24#include "RtosMutex.h"
26#include "core/hf-core-drivers/internal/hf-pincfg/src/hf_functional_pin_config_flux_v1.hpp"
27
28#include <cstdint>
29#include <atomic>
30
31//==============================================================================
32// LED ERROR CODES
33//==============================================================================
34
46
52constexpr const char* LedErrorToString(LedError error) noexcept {
53 switch (error) {
54 case LedError::SUCCESS: return "Success";
55 case LedError::NOT_INITIALIZED: return "Not initialized";
56 case LedError::INITIALIZATION_FAILED: return "Initialization failed";
57 case LedError::INVALID_PARAMETER: return "Invalid parameter";
58 case LedError::HARDWARE_ERROR: return "Hardware error";
59 case LedError::ANIMATION_FAILED: return "Animation failed";
60 case LedError::INVALID_COLOR: return "Invalid color";
61 case LedError::INVALID_BRIGHTNESS: return "Invalid brightness";
62 default: return "Unknown error";
63 }
64}
65
66//==============================================================================
67// LED ANIMATION TYPES
68//==============================================================================
69
71enum class LedAnimation : uint8_t {
72 OFF = 0,
73 SOLID,
74 BLINK,
75 BREATH,
76 RAINBOW,
77 STATUS_OK,
82};
83
89constexpr const char* LedAnimationToString(LedAnimation animation) noexcept {
90 switch (animation) {
91 case LedAnimation::OFF: return "Off";
92 case LedAnimation::SOLID: return "Solid";
93 case LedAnimation::BLINK: return "Blink";
94 case LedAnimation::BREATH: return "Breath";
95 case LedAnimation::RAINBOW: return "Rainbow";
96 case LedAnimation::STATUS_OK: return "Status OK";
97 case LedAnimation::STATUS_WARN: return "Status Warning";
98 case LedAnimation::STATUS_ERROR: return "Status Error";
99 case LedAnimation::STATUS_BOOT: return "Status Boot";
100 case LedAnimation::STATUS_CALIBRATE: return "Status Calibrate";
101 default: return "Unknown";
102 }
103}
104
105//==============================================================================
106// COLOR HELPERS
107//==============================================================================
108
114struct LedColor {
115 uint8_t red;
116 uint8_t green;
117 uint8_t blue;
118
120 constexpr LedColor() noexcept : red(0), green(0), blue(0) {}
121
128 constexpr LedColor(uint8_t r, uint8_t g, uint8_t b) noexcept : red(r), green(g), blue(b) {}
129
134 [[nodiscard]] constexpr uint32_t ToRgb() const noexcept {
135 return (static_cast<uint32_t>(red) << 16) |
136 (static_cast<uint32_t>(green) << 8) |
137 static_cast<uint32_t>(blue);
138 }
139
145 static constexpr LedColor FromRgb(uint32_t rgb) noexcept {
146 return LedColor(
147 static_cast<uint8_t>((rgb >> 16) & 0xFF),
148 static_cast<uint8_t>((rgb >> 8) & 0xFF),
149 static_cast<uint8_t>(rgb & 0xFF));
150 }
151};
152
154namespace LedColors {
155 static constexpr LedColor BLACK(0, 0, 0);
156 static constexpr LedColor RED(255, 0, 0);
157 static constexpr LedColor GREEN(0, 255, 0);
158 static constexpr LedColor BLUE(0, 0, 255);
159 static constexpr LedColor YELLOW(255, 255, 0);
160 static constexpr LedColor CYAN(0, 255, 255);
161 static constexpr LedColor MAGENTA(255, 0, 255);
162 static constexpr LedColor WHITE(255, 255, 255);
163 static constexpr LedColor ORANGE(255, 165, 0);
164 static constexpr LedColor PURPLE(128, 0, 128);
165} // namespace LedColors
166
167//==============================================================================
168// LED DIAGNOSTICS
169//==============================================================================
170
184
185//==============================================================================
186// LED MANAGER
187//==============================================================================
188
203public:
204 //==========================================================================
205 // SINGLETON AND LIFECYCLE
206 //==========================================================================
207
212 static LedManager& GetInstance() noexcept;
213
214 LedManager(const LedManager&) = delete;
215 LedManager& operator=(const LedManager&) = delete;
218
223 [[nodiscard]] bool EnsureInitialized() noexcept;
224
229 [[nodiscard]] bool Shutdown() noexcept;
230
232 [[nodiscard]] bool Deinitialize() noexcept { return Shutdown(); }
233
236 [[nodiscard]] bool IsInitialized() const noexcept { return is_initialized_.load(std::memory_order_acquire); }
237
243 [[nodiscard]] LedError GetSystemDiagnostics(LedSystemDiagnostics& diagnostics) const noexcept;
244
249 [[nodiscard]] LedError GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
250
255 [[nodiscard]] flux::hal::ManagerError GetManagerError() const noexcept {
256 return manager_error_.load(std::memory_order_acquire);
257 }
258
259 //==========================================================================
260 // BASIC LED OPERATIONS
261 //==========================================================================
262
269 [[nodiscard]] LedError SetColor(const LedColor& color, uint32_t led_index = 0) noexcept;
270
277 [[nodiscard]] LedError SetColor(uint32_t rgb, uint32_t led_index = 0) noexcept;
278
283 [[nodiscard]] LedError TurnOff() noexcept;
284
290 [[nodiscard]] LedError GetCurrentColor(LedColor& color) const noexcept;
291
292 //==========================================================================
293 // BRIGHTNESS
294 //==========================================================================
295
301 [[nodiscard]] LedError SetBrightnessPercent(uint8_t brightness_percent) noexcept;
302
308 [[nodiscard]] LedError SetBrightnessRaw(uint8_t brightness_raw) noexcept;
309
311 [[nodiscard]] LedError SetBrightness(uint8_t brightness) noexcept { return SetBrightnessRaw(brightness); }
312
318 [[nodiscard]] LedError SetMaxBrightness(uint8_t max_brightness) noexcept;
319
325 [[nodiscard]] LedError GetCurrentBrightnessPercent(uint8_t& brightness_percent) const noexcept {
326 brightness_percent = RawToPercent(current_brightness_);
327 return LedError::SUCCESS;
328 }
329
335 [[nodiscard]] LedError GetCurrentBrightnessRaw(uint8_t& brightness_raw) const noexcept {
336 brightness_raw = current_brightness_;
337 return LedError::SUCCESS;
338 }
339
340 //==========================================================================
341 // ANIMATION
342 //==========================================================================
343
350 [[nodiscard]] LedError StartAnimation(LedAnimation animation, const LedColor& color = LedColors::WHITE) noexcept;
351
356 [[nodiscard]] LedError StopAnimation() noexcept;
357
359 [[nodiscard]] bool IsAnimationActive() const noexcept { return current_animation_ != LedAnimation::OFF; }
360
365 [[nodiscard]] LedError UpdateAnimation() noexcept;
366
367 //==========================================================================
368 // STATUS INDICATION
369 //==========================================================================
370
376 [[nodiscard]] LedError SetStatus(LedAnimation animation) noexcept { return StartAnimation(animation); }
377
379 [[nodiscard]] LedError IndicateBoot() noexcept { return StartAnimation(LedAnimation::STATUS_BOOT); }
381 [[nodiscard]] LedError IndicateReady() noexcept { return StartAnimation(LedAnimation::STATUS_OK); }
388
389 //==========================================================================
390 // UTILITY
391 //==========================================================================
392
398 [[nodiscard]] static uint32_t ColorWheel(uint8_t position) noexcept;
399
401 void DumpStatistics() const noexcept;
402
404 [[nodiscard]] hf_pin_num_t GetCurrentGpioPin() const noexcept { return current_led_gpio_; }
405
407 [[nodiscard]] uint32_t GetLedCount() const noexcept { return kNumLeds; }
408
409private:
410 LedManager() noexcept = default;
411 ~LedManager() = default;
412
413 [[nodiscard]] bool Initialize() noexcept;
414 [[nodiscard]] LedError UpdateAnimationStep() noexcept;
415 void UpdateStatistics(bool success) noexcept;
416 void UpdateLastError(LedError error_code) noexcept;
417 [[nodiscard]] uint64_t GetSystemUptimeMs() const noexcept;
418
419 [[nodiscard]] uint8_t PercentToRaw(uint8_t percent) const noexcept {
420 if (percent > 100) percent = 100;
421 return static_cast<uint8_t>((static_cast<uint32_t>(percent) * max_brightness_) / 100);
422 }
423
424 [[nodiscard]] uint8_t RawToPercent(uint8_t raw) const noexcept {
425 if (max_brightness_ == 0) return 0;
426 return static_cast<uint8_t>((static_cast<uint32_t>(raw) * 100) / max_brightness_);
427 }
428
429 [[nodiscard]] static hf_pin_num_t GetLedGpioPin() noexcept {
430 const auto* mapping = GetGpioMapping(HfFunctionalGpioPin::WS2812_LED_DAT);
431 if (mapping && mapping->chip_type == static_cast<uint8_t>(HfGpioChipType::ESP32_INTERNAL)) {
432 return static_cast<hf_pin_num_t>(mapping->physical_pin);
433 }
434 return kFallbackLedPin;
435 }
436
437 //==========================================================================
438 // CONSTANTS
439 //==========================================================================
440
441 static constexpr hf_pin_num_t kFallbackLedPin = 3;
442 static constexpr uint32_t kNumLeds = 1;
443 static constexpr uint8_t DEFAULT_MAX_BRIGHTNESS = 255;
444 static constexpr uint32_t ANIMATION_UPDATE_INTERVAL = 50; // ms
445 static constexpr uint32_t BREATH_SPEED = 5;
446 static constexpr uint32_t BLINK_SPEED = 500; // ms
447
448 //==========================================================================
449 // STATE
450 //==========================================================================
451
452 std::atomic<bool> is_initialized_{false};
453 mutable RtosMutex mutex_;
454
455 bool strip_ready_{false};
456
461 uint16_t animation_step_{0};
464
465 std::atomic<uint32_t> total_operations_{0};
466 std::atomic<uint32_t> successful_operations_{0};
467 std::atomic<uint32_t> failed_operations_{0};
468 std::atomic<uint32_t> animation_cycles_{0};
469 std::atomic<LedError> last_error_{LedError::SUCCESS};
470 std::atomic<flux::hal::ManagerError> manager_error_{flux::hal::ManagerError::NotInitialized};
473};
474
475//==============================================================================
476// CONVENIENCE
477//==============================================================================
478
483[[nodiscard]] inline LedManager& GetLedManager() noexcept {
485}
486
487#endif // FLUX_LED_MANAGER_H_
LedError
Error codes returned by LedManager operations.
Definition LedManager.h:36
@ 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:71
@ 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:483
constexpr const char * LedAnimationToString(LedAnimation animation) noexcept
Convert a LedAnimation to a human-readable C-string.
Definition LedManager.h:89
constexpr const char * LedErrorToString(LedError error) noexcept
Convert a LedError to a human-readable C-string.
Definition LedManager.h:52
Singleton that drives the on-board WS2812 status LED.
Definition LedManager.h:202
LedError IndicateBoot() noexcept
Show the boot indicator (blue blink).
Definition LedManager.h:379
bool strip_ready_
The binding's strip was started by Initialize()
Definition LedManager.h:455
LedError UpdateAnimation() noexcept
Advance the animation by one tick. Call periodically (~50 ms).
Definition LedManager.cpp:233
LedError IndicateWarning() noexcept
Show the warning indicator (yellow blink).
Definition LedManager.h:383
LedError SetBrightness(uint8_t brightness) noexcept
Alias for SetBrightnessRaw().
Definition LedManager.h:311
std::atomic< uint32_t > successful_operations_
Definition LedManager.h:466
LedError StopAnimation() noexcept
Stop the current animation and turn the LED off.
Definition LedManager.cpp:220
LedError UpdateAnimationStep() noexcept
Definition LedManager.cpp:309
LedColor current_color_
Definition LedManager.h:458
static constexpr uint32_t BLINK_SPEED
Definition LedManager.h:446
uint16_t animation_step_
Definition LedManager.h:461
std::atomic< LedError > last_error_
Definition LedManager.h:469
static constexpr uint32_t kNumLeds
Definition LedManager.h:442
LedError GetSystemDiagnostics(LedSystemDiagnostics &diagnostics) const noexcept
Populate a diagnostics snapshot.
Definition LedManager.cpp:65
bool Shutdown() noexcept
Turn off the LED and release driver resources.
Definition LedManager.cpp:42
void DumpStatistics() const noexcept
Log current LED state and statistics to the console.
Definition LedManager.cpp:264
static constexpr uint32_t BREATH_SPEED
Definition LedManager.h:445
LedError GetLastError() const noexcept
Get the most recent error code from any LED operation.
Definition LedManager.h:249
bool Deinitialize() noexcept
Alias for Shutdown().
Definition LedManager.h:232
LedError GetCurrentBrightnessPercent(uint8_t &brightness_percent) const noexcept
Read current brightness as a percentage of max.
Definition LedManager.h:325
LedError GetCurrentColor(LedColor &color) const noexcept
Read the currently displayed colour.
Definition LedManager.cpp:144
LedManager(LedManager &&)=delete
hf_pin_num_t GetCurrentGpioPin() const noexcept
GPIO pin the LED strip is attached to.
Definition LedManager.h:404
bool IsAnimationActive() const noexcept
Check whether an animation is currently running.
Definition LedManager.h:359
LedError GetCurrentBrightnessRaw(uint8_t &brightness_raw) const noexcept
Read current brightness as a raw 0–255 value.
Definition LedManager.h:335
bool IsInitialized() const noexcept
Check whether the LED driver is initialised.
Definition LedManager.h:236
void UpdateLastError(LedError error_code) noexcept
Definition LedManager.cpp:350
static uint32_t ColorWheel(uint8_t position) noexcept
Map a 0–255 wheel position to a rainbow RGB colour.
Definition LedManager.cpp:251
std::atomic< bool > is_initialized_
Definition LedManager.h:452
LedError StartAnimation(LedAnimation animation, const LedColor &color=LedColors::WHITE) noexcept
Start a named animation with an optional base colour.
Definition LedManager.cpp:189
LedError TurnOff() noexcept
Turn the LED off (set colour to black).
Definition LedManager.cpp:118
std::atomic< flux::hal::ManagerError > manager_error_
Definition LedManager.h:470
LedAnimation current_animation_
Definition LedManager.h:457
uint8_t PercentToRaw(uint8_t percent) const noexcept
Definition LedManager.h:419
LedError IndicateCalibration() noexcept
Show the calibration indicator (cyan breathe).
Definition LedManager.h:387
uint32_t GetLedCount() const noexcept
Number of LEDs in the strip (always 1 on Flux V1).
Definition LedManager.h:407
std::atomic< uint32_t > total_operations_
Definition LedManager.h:465
static constexpr hf_pin_num_t kFallbackLedPin
Board default if the pin map lacks the LED.
Definition LedManager.h:441
LedManager & operator=(const LedManager &)=delete
std::atomic< uint32_t > animation_cycles_
Definition LedManager.h:468
LedError SetBrightnessPercent(uint8_t brightness_percent) noexcept
Set brightness as a percentage of max.
Definition LedManager.cpp:154
std::atomic< uint32_t > failed_operations_
Definition LedManager.h:467
int8_t animation_direction_
Definition LedManager.h:462
RtosMutex mutex_
Definition LedManager.h:453
uint8_t current_brightness_
Definition LedManager.h:459
LedManager() noexcept=default
static constexpr uint8_t DEFAULT_MAX_BRIGHTNESS
Definition LedManager.h:443
LedError SetBrightnessRaw(uint8_t brightness_raw) noexcept
Set brightness as a raw 0–255 value.
Definition LedManager.cpp:167
uint8_t max_brightness_
Definition LedManager.h:460
flux::hal::ManagerError GetManagerError() const noexcept
Lifecycle / bind error of the last EnsureInitialized() (shared catalog).
Definition LedManager.h:255
LedManager(const LedManager &)=delete
uint64_t system_start_time_
Definition LedManager.h:471
uint64_t last_animation_update_
Definition LedManager.h:463
LedError SetMaxBrightness(uint8_t max_brightness) noexcept
Set the upper ceiling for brightness.
Definition LedManager.cpp:178
static constexpr uint32_t ANIMATION_UPDATE_INTERVAL
Definition LedManager.h:444
hf_pin_num_t current_led_gpio_
Definition LedManager.h:472
static hf_pin_num_t GetLedGpioPin() noexcept
Definition LedManager.h:429
bool Initialize() noexcept
Definition LedManager.cpp:281
uint64_t GetSystemUptimeMs() const noexcept
Definition LedManager.cpp:354
LedManager & operator=(LedManager &&)=delete
void UpdateStatistics(bool success) noexcept
Definition LedManager.cpp:344
LedError IndicateError() noexcept
Show the error indicator (red fast-blink).
Definition LedManager.h:385
uint8_t RawToPercent(uint8_t raw) const noexcept
Definition LedManager.h:424
LedError SetColor(const LedColor &color, uint32_t led_index=0) noexcept
Set LED colour from an LedColor struct.
Definition LedManager.cpp:84
static LedManager & GetInstance() noexcept
Access the singleton instance (Meyers singleton, thread-safe).
Definition LedManager.cpp:26
bool EnsureInitialized() noexcept
Initialise the WS2812 LED strip (idempotent).
Definition LedManager.cpp:35
LedError SetStatus(LedAnimation animation) noexcept
Set a status animation by enum (generic variant).
Definition LedManager.h:376
LedError IndicateReady() noexcept
Show the ready indicator (solid green).
Definition LedManager.h:381
Shared HAL types for Flux V1: the manager error catalog and the facade result.
Predefined named colour constants.
Definition LedManager.h:154
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.
ManagerError
Lifecycle / bind failure catalog shared by every manager (GetManagerError()).
Definition flux_hal_types.hpp:37
@ NotInitialized
Manager not initialised (or shut down)
Simple RGB colour triplet.
Definition LedManager.h:114
uint8_t blue
Blue channel [0–255].
Definition LedManager.h:117
uint8_t green
Green channel [0–255].
Definition LedManager.h:116
constexpr uint32_t ToRgb() const noexcept
Pack into a 24-bit 0xRRGGBB integer.
Definition LedManager.h:134
static constexpr LedColor FromRgb(uint32_t rgb) noexcept
Unpack from a 24-bit 0xRRGGBB integer.
Definition LedManager.h:145
constexpr LedColor(uint8_t r, uint8_t g, uint8_t b) noexcept
Construct from individual R/G/B components.
Definition LedManager.h:128
uint8_t red
Red channel [0–255].
Definition LedManager.h:115
constexpr LedColor() noexcept
Default-construct black (0, 0, 0).
Definition LedManager.h:120
Snapshot of LED subsystem health and counters.
Definition LedManager.h:172
bool animation_active
true if an animation is currently running.
Definition LedManager.h:175
uint32_t total_operations
Lifetime count of all API calls.
Definition LedManager.h:177
uint32_t successful_operations
Calls that returned SUCCESS.
Definition LedManager.h:178
uint32_t current_color
Current colour as packed 0xRRGGBB.
Definition LedManager.h:182
LedAnimation current_animation
Currently active animation mode.
Definition LedManager.h:176
uint32_t current_brightness
Current brightness value [0–255].
Definition LedManager.h:181
bool led_initialized
true after WS2812 strip init succeeds.
Definition LedManager.h:174
bool system_healthy
true when driver initialised and operational.
Definition LedManager.h:173
uint32_t failed_operations
Calls that returned an error.
Definition LedManager.h:179
uint32_t animation_cycles
Number of animation update ticks processed.
Definition LedManager.h:180