Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
output.hpp
Go to the documentation of this file.
1/**
2 * @file output.hpp
3 * @brief RGB LED output convenience functions
4 * @ingroup hal
5 *
6 * Composes RgbLed state with a driver backend in a single call.
7 * Two overloads:
8 * - GPIO backend: bang-bang write (on/off via GpioHandle)
9 * - SoftPwm backend: 8-bit duty cycle via channel indices
10 *
11 * Usage:
12 * // GPIO (bang-bang)
13 * sbl::led::write<sbl::driver::Gpio>(led, handle_r, handle_g, handle_b);
14 *
15 * // SoftPwm (8-bit BCM)
16 * sbl::led::write<sbl::driver::SoftPwm>(led, ch_r, ch_g, ch_b);
17 */
18
19#ifndef SBL_HAL_LED_OUTPUT_HPP_
20#define SBL_HAL_LED_OUTPUT_HPP_
21
22#include <cstdint>
23#include <sbl/types.hpp>
25
26namespace sbl {
27namespace led {
28
32
33/**
34 * @brief Write RgbLed state to GPIO pins (bang-bang)
35 *
36 * Reads the LED's current state and writes each channel to its GPIO pin.
37 * Duty values > 0 are treated as ON. Gpio::write() handles active_low.
38 *
39 * @tparam Driver GPIO driver type (e.g., sbl::driver::Gpio)
40 * @param led RGB LED instance to read state from
41 * @param r GPIO handle for red channel
42 * @param g GPIO handle for green channel
43 * @param b GPIO handle for blue channel
44 *
45 * @note ISR-safe — three GPIO register writes
46 */
47template<typename Driver>
48inline void write(const RgbLed& led, const GpioHandle& r,
49 const GpioHandle& g, const GpioHandle& b) {
50 auto s = led.state();
51 Driver::write(r, s.r > 0);
52 Driver::write(g, s.g > 0);
53 Driver::write(b, s.b > 0);
54}
55
56/**
57 * @brief Write RgbLed state via SoftPwm channels (8-bit BCM)
58 *
59 * Pushes per-channel duty cycles to the BCM engine.
60 * The Driver must provide set_duty(uint8_t channel, uint8_t duty).
61 *
62 * @tparam Driver SoftPwm-compatible driver type (e.g., sbl::driver::SoftPwm)
63 * @param led RGB LED instance to read state from
64 * @param r_ch SoftPwm channel index for red
65 * @param g_ch SoftPwm channel index for green
66 * @param b_ch SoftPwm channel index for blue
67 *
68 * @note ISR-safe — three register writes to SoftPwm duty array
69 */
70template<typename Driver>
71inline void write(const RgbLed& led, uint8_t r_ch, uint8_t g_ch, uint8_t b_ch) {
72 auto s = led.state();
73 Driver::set_duty(r_ch, s.r);
74 Driver::set_duty(g_ch, s.g);
75 Driver::set_duty(b_ch, s.b);
76}
77
78} // namespace led
79} // namespace sbl
80
81#endif // SBL_HAL_LED_OUTPUT_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
Color
3-bit color enum — all 8 RGB combinations
Definition rgb_led.hpp:34
void write(const RgbLed &led, const GpioHandle &r, const GpioHandle &g, const GpioHandle &b)
Write RgbLed state to GPIO pins (bang-bang)
Definition output.hpp:48
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
RGB LED component — state holder with 8-bit color depth.
GPIO pin handle with port, pin number, and polarity.
Definition handle.hpp:29
Common types for SBL hardware abstraction.