Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
rgb_led.hpp
Go to the documentation of this file.
1/**
2 * @file rgb_led.hpp
3 * @brief RGB LED component — state holder with 8-bit color depth
4 * @ingroup components
5 *
6 * RgbLed stores per-channel duty cycle (0-255). The output backend
7 * (SoftPwm for BCM, or GPIO for bang-bang) reads state via state().
8 *
9 * The 3-bit Color enum provides the 8 RGB cube corners at full brightness.
10 * For intermediate colors, use set_rgb().
11 *
12 * Usage:
13 * sbl::components::display::RgbLed led;
14 * led.set_color(sbl::components::display::Color::Red);
15 * led.set_rgb(128, 0, 255); // purple at half red
16 * auto s = led.state(); // {128, 0, 255}
17 */
18
19#ifndef SBL_COMPONENTS_DISPLAY_RGB_LED_HPP_
20#define SBL_COMPONENTS_DISPLAY_RGB_LED_HPP_
21
22#include <cstdint>
23
24namespace sbl {
25namespace components {
26namespace display {
27
28/**
29 * @brief 3-bit color enum — all 8 RGB combinations
30 *
31 * Bit layout: [R:G:B] where bit 2=R, bit 1=G, bit 0=B.
32 * Maps to full brightness (255) per active channel.
33 */
34enum class Color : uint8_t {
35 Off = 0, // 000
36 Blue = 1, // 001
37 Green = 2, // 010
38 Cyan = 3, // 011
39 Red = 4, // 100
40 Magenta = 5, // 101
41 Yellow = 6, // 110
42 White = 7, // 111
43};
44
46 uint8_t r;
47 uint8_t g;
48 uint8_t b;
49};
50
51/**
52 * @brief RGB LED state holder with 8-bit per-channel duty cycle
53 *
54 * @note All public methods are ISR-safe — pure state machine with no hardware I/O.
55 */
56class RgbLed {
57public:
58 RgbLed() : r_(0), g_(0), b_(0) {}
59
60 /** @brief Set per-channel duty cycle (0-255) */
61 void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
62 r_ = r;
63 g_ = g;
64 b_ = b;
65 }
66
67 /** @brief Set from Color enum (full brightness per active channel) */
68 void set_color(Color color) {
69 uint8_t c = static_cast<uint8_t>(color);
70 r_ = (c & 4) ? 255 : 0;
71 g_ = (c & 2) ? 255 : 0;
72 b_ = (c & 1) ? 255 : 0;
73 }
74
75 /** @brief Turn off all channels */
76 void off() {
77 r_ = 0;
78 g_ = 0;
79 b_ = 0;
80 }
81
82 /** @brief Current state */
83 RgbLedState state() const { return {r_, g_, b_}; }
84
85private:
86 uint8_t r_;
87 uint8_t g_;
88 uint8_t b_;
89};
90
91} // namespace display
92} // namespace components
93} // namespace sbl
94
95#endif // SBL_COMPONENTS_DISPLAY_RGB_LED_HPP_
RGB LED state holder with 8-bit per-channel duty cycle.
Definition rgb_led.hpp:56
RgbLedState state() const
Current state.
Definition rgb_led.hpp:83
void set_color(Color color)
Set from Color enum (full brightness per active channel)
Definition rgb_led.hpp:68
void off()
Turn off all channels.
Definition rgb_led.hpp:76
void set_rgb(uint8_t r, uint8_t g, uint8_t b)
Set per-channel duty cycle (0-255)
Definition rgb_led.hpp:61
Color
3-bit color enum — all 8 RGB combinations
Definition rgb_led.hpp:34
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24