Sound Byte Libs 0.5.1-340-gab5b79a
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 *
5 * RgbLed stores per-channel duty cycle (0-255). An output backend reads
6 * it with state() and drives the LED.
7 *
8 * The 3-bit Color enum provides the 8 RGB cube corners at full brightness.
9 * For intermediate colors, use set_rgb().
10 *
11 * Usage:
12 * @code
13 * sbl::control::display::RgbLed led;
14 * led.set_color(sbl::control::display::Color::Red);
15 * led.set_rgb(128, 0, 255); // purple at half red
16 * auto s = led.state(); // {128, 0, 255}
17 * @endcode
18 */
19
20#ifndef SBL_CONTROL_DISPLAY_RGB_LED_HPP_
21#define SBL_CONTROL_DISPLAY_RGB_LED_HPP_
22
23#include <cstdint>
24
25namespace sbl::control::display {
26
27/**
28 * @brief 3-bit color enum - all 8 RGB combinations
29 *
30 * Bit layout: [R:G:B] where bit 2=R, bit 1=G, bit 0=B.
31 * Maps to full brightness (255) per active channel.
32 */
33enum class Color : uint8_t {
34 Off = 0, // 000
35 Blue = 1, // 001
36 Green = 2, // 010
37 Cyan = 3, // 011
38 Red = 4, // 100
39 Magenta = 5, // 101
40 Yellow = 6, // 110
41 White = 7, // 111
42};
43
45 uint8_t r;
46 uint8_t g;
47 uint8_t b;
48};
49
50/**
51 * @brief RGB LED state holder with 8-bit per-channel duty cycle
52 *
53 * @context All public methods are ISR-safe: pure state machine with no hardware I/O.
54 */
55class RgbLed {
56public:
57 RgbLed() : r_(0), g_(0), b_(0) {}
58
59 /** @brief Set per-channel duty cycle (0-255) */
60 void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
61 r_ = r;
62 g_ = g;
63 b_ = b;
64 }
65
66 /** @brief Set from Color enum (full brightness per active channel) */
67 void set_color(Color color) {
68 uint8_t c = static_cast<uint8_t>(color);
69 r_ = (c & 4) ? 255 : 0;
70 g_ = (c & 2) ? 255 : 0;
71 b_ = (c & 1) ? 255 : 0;
72 }
73
74 /** @brief Turn off all channels */
75 void off() {
76 r_ = 0;
77 g_ = 0;
78 b_ = 0;
79 }
80
81 /** @brief Current state */
82 RgbLedState state() const { return {r_, g_, b_}; }
83
84private:
85 uint8_t r_;
86 uint8_t g_;
87 uint8_t b_;
88};
89
90} // namespace sbl::control::display
91
92#endif // SBL_CONTROL_DISPLAY_RGB_LED_HPP_
RGB LED state holder with 8-bit per-channel duty cycle.
Definition rgb_led.hpp:55
void set_rgb(uint8_t r, uint8_t g, uint8_t b)
Set per-channel duty cycle (0-255)
Definition rgb_led.hpp:60
void set_color(Color color)
Set from Color enum (full brightness per active channel)
Definition rgb_led.hpp:67
RgbLedState state() const
Current state.
Definition rgb_led.hpp:82
void off()
Turn off all channels.
Definition rgb_led.hpp:75
Indicators: RGB LED state and colors.
Definition color.hpp:33
Color
3-bit color enum - all 8 RGB combinations
Definition rgb_led.hpp:33