5#include "roo_logging.h"
7using namespace roo_time;
15 stepper_(scheduler, [this]() { step(); }),
20 updateSequence(std::move(sequence), -1,
Color());
24 Color terminal_color) {
25 updateSequence(std::move(sequence), repetitions - 1, terminal_color);
29 updateSequence(std::move(sequence), 0, terminal_color);
37 Color terminal_color) {
38 roo::lock_guard<roo::mutex> lock(mutex_);
39 sequence_ = std::move(sequence.sequence_);
40 terminal_color_ = terminal_color;
41 current_color_ = terminal_color;
42 repetitions_ = repetitions;
43 fade_in_progress_ =
false;
45 if (!sequence_.empty() && !stepper_.is_scheduled()) {
46 stepper_.scheduleNow(roo_scheduler::PRIORITY_ELEVATED);
48 current_color_ = terminal_color;
53void RgbBlinker::step() {
54 roo::lock_guard<roo::mutex> lock(mutex_);
55 if (fade_in_progress_) {
56 roo_time::Uptime now = roo_time::Uptime::Now();
57 if (now >= fade_end_time_) {
58 fade_in_progress_ =
false;
59 current_color_ = fade_target_color_;
62 float progress = (now - fade_start_time_).inMillisFloat() /
63 (fade_end_time_ - fade_start_time_).inMillisFloat();
64 uint8_t new_r = (uint8_t)((
float)fade_start_color_.
r() +
65 ((float)fade_target_color_.
r() -
66 (float)fade_start_color_.
r()) *
68 uint8_t new_g = (uint8_t)((
float)fade_start_color_.
g() +
69 ((float)fade_target_color_.
g() -
70 (float)fade_start_color_.
g()) *
72 uint8_t new_b = (uint8_t)((
float)fade_start_color_.
b() +
73 ((float)fade_target_color_.
b() -
74 (float)fade_start_color_.
b()) *
76 current_color_ = Color(new_r, new_g, new_b);
78 stepper_.scheduleAfter(roo_time::Millis(20),
79 roo_scheduler::PRIORITY_ELEVATED);
83 uint16_t next_delay = 0;
85 if (pos_ >= sequence_.size()) {
89 const RgbStep& s = sequence_[pos_];
92 current_color_ = s.target_color_;
96 case RgbStep::kHold: {
97 next_delay = s.duration_millis_;
102 fade_in_progress_ =
true;
103 fade_start_color_ = current_color_;
104 fade_target_color_ = s.target_color_;
105 fade_start_time_ = roo_time::Uptime::Now();
107 fade_start_time_ + roo_time::Millis(s.duration_millis_);
113 if (pos_ == sequence_.size() && repetitions_ != 0) {
114 if (repetitions_ > 0) --repetitions_;
117 }
while (next_delay == 0);
118 stepper_.scheduleAfter(roo_time::Millis(next_delay),
119 roo_scheduler::PRIORITY_ELEVATED);
123 int duty_percent,
int rampup_percent_on,
124 int rampup_percent_off) {
125 CHECK_GE(duty_percent, 0);
126 CHECK_LE(duty_percent, 100);
127 CHECK_GE(rampup_percent_on, 0);
128 CHECK_LE(rampup_percent_on, 100);
129 CHECK_GE(rampup_percent_off, 0);
130 CHECK_LE(rampup_percent_off, 100);
131 int millis = period.inMillis();
132 int millis_1st = duty_percent * millis / 100;
133 int millis_1st_rampup = rampup_percent_on * millis_1st / 100;
134 int millis_2nd = millis - millis_1st;
135 int millis_2nd_rampup = rampup_percent_off * millis_2nd / 100;
139 if (millis_1st_rampup > 0) {
140 result.
add(
RgbFadeTo(color, Millis(millis_1st_rampup)));
144 if (millis_1st_rampup < millis_1st) {
145 result.
add(
RgbHold(Millis(millis_1st - millis_1st_rampup)));
148 if (millis_2nd_rampup > 0) {
153 if (millis_2nd_rampup < millis_2nd) {
154 result.
add(
RgbHold(Millis(millis_2nd - millis_2nd_rampup)));
Simple 24-bit RGB color value.
uint8_t r() const
Returns the red component.
uint8_t b() const
Returns the blue component.
uint8_t g() const
Returns the green component.
Sequence of steps for RGB blinking.
Runs blink sequences on an RGB LED.
RgbBlinker(RgbLed &led)
Constructs a RgbBlinker using the default scheduler.
void repeat(RgbBlinkSequence sequence, int repetitions, Color terminal_color=Color())
Repeats the sequence the specified number of times.
void setColor(Color color)
Enables the LED, setting it to the specified color.
void loop(RgbBlinkSequence sequence)
Repeats the sequence indefinitely.
void execute(RgbBlinkSequence sequence, Color terminal_color=Color())
Executes the sequence once.
void turnOff()
Disables the LED.
Abstract interface representing an RGB LED.
virtual void setColor(Color color)
Sets the LED to the specified color.
roo_scheduler::Scheduler & DefaultScheduler()
Returns the default scheduler used for blinking operations.
constexpr RgbStep RgbTurnOff()
Creates a step that disables the LED. Equivalent to RgbSetTo(Color()).
constexpr RgbStep RgbSetTo(Color color)
Creates a step that sets the LED to the specified color instantly.
constexpr RgbStep RgbFadeTo(Color color, roo_time::Duration duration)
Creates a step that fades to the target color over the duration.
constexpr RgbStep RgbHold(roo_time::Duration duration)
Creates a step that holds the current color for the duration.
RgbBlinkSequence RgbBlink(roo_time::Duration period, Color color, int duty_percent, int rampup_percent_on, int rampup_percent_off)
Creates a symmetric blink sequence with optional ramp-up/down segments.
constexpr RgbStep RgbFadeOff(roo_time::Duration duration)
Creates a step that fades the LED off over the duration.