5#include "roo_logging.h"
6#include "roo_threads.h"
8using namespace roo_time;
16 stepper_(scheduler, [this]() { step(); }),
21 updateSequence(std::move(sequence), -1, 0);
25 uint16_t terminal_level) {
26 updateSequence(std::move(sequence), repetitions - 1, terminal_level);
30 updateSequence(std::move(sequence), 0, terminal_level);
33void Blinker::set(uint16_t intensity) { updateSequence({}, 0, intensity); }
39void Blinker::updateSequence(
BlinkSequence sequence,
int repetitions,
40 uint16_t terminal_level) {
41 roo::lock_guard<roo::mutex> lock(mutex_);
42 sequence_ = std::move(sequence.sequence_);
43 terminal_level_ = terminal_level;
44 current_level_ = terminal_level_;
45 repetitions_ = repetitions;
46 fade_in_progress_ =
false;
48 if (!sequence_.empty() && !stepper_.is_scheduled()) {
49 stepper_.scheduleNow(roo_scheduler::PRIORITY_ELEVATED);
51 current_level_ = terminal_level_;
57 roo::lock_guard<roo::mutex> lock(mutex_);
58 if (fade_in_progress_) {
59 roo_time::Uptime now = roo_time::Uptime::Now();
60 if (now >= fade_end_time_) {
61 fade_in_progress_ =
false;
62 current_level_ = fade_target_level_;
65 float progress = (now - fade_start_time_).inMillisFloat() /
66 (fade_end_time_ - fade_start_time_).inMillisFloat();
67 current_level_ = fade_start_level_ +
68 (fade_target_level_ - fade_start_level_) * progress;
70 stepper_.scheduleAfter(roo_time::Millis(20),
71 roo_scheduler::PRIORITY_ELEVATED);
75 uint16_t next_delay = 0;
77 if (pos_ >= sequence_.size()) {
79 current_level_ = terminal_level_;
83 const Step& s = sequence_[pos_];
86 current_level_ = s.target_level_;
91 next_delay = s.duration_millis_;
96 if (led_.
fade(s.target_level_, roo_time::Millis(s.duration_millis_))) {
97 next_delay = s.duration_millis_;
99 fade_in_progress_ =
true;
100 fade_start_level_ = current_level_;
101 fade_target_level_ = s.target_level_;
102 fade_start_time_ = roo_time::Uptime::Now();
104 fade_start_time_ + roo_time::Millis(s.duration_millis_);
111 if (pos_ == sequence_.size() && repetitions_ != 0) {
112 if (repetitions_ > 0) --repetitions_;
115 }
while (next_delay == 0);
116 stepper_.scheduleAfter(roo_time::Millis(next_delay),
117 roo_scheduler::PRIORITY_ELEVATED);
121 int rampup_percent_on,
int rampup_percent_off) {
122 CHECK_GE(duty_percent, 0);
123 CHECK_LE(duty_percent, 100);
124 CHECK_GE(rampup_percent_on, 0);
125 CHECK_LE(rampup_percent_on, 100);
126 CHECK_GE(rampup_percent_off, 0);
127 CHECK_LE(rampup_percent_off, 100);
128 int millis = period.inMillis();
129 int millis_1st = duty_percent * millis / 100;
130 int millis_1st_rampup = rampup_percent_on * millis_1st / 100;
131 int millis_2nd = millis - millis_1st;
132 int millis_2nd_rampup = rampup_percent_off * millis_2nd / 100;
136 if (millis_1st_rampup > 0) {
137 result.
add(
FadeOn(Millis(millis_1st_rampup)));
141 if (millis_1st_rampup < millis_1st) {
142 result.
add(
Hold(Millis(millis_1st - millis_1st_rampup)));
145 if (millis_2nd_rampup > 0) {
146 result.
add(
FadeOff(Millis(millis_2nd_rampup)));
150 if (millis_2nd_rampup < millis_2nd) {
151 result.
add(
Hold(Millis(millis_2nd - millis_2nd_rampup)));
Sequence of steps for monochrome blinking.
Runs blink sequences on a monochrome LED.
void loop(BlinkSequence sequence)
Repeats the sequence indefinitely.
void execute(BlinkSequence sequence, uint16_t terminal_level=0)
Executes the sequence once.
Blinker(Led &led)
Constructs a Blinker using the default scheduler.
void turnOff()
Disables the LED.
void turnOn()
Enables the LED at the maximum intensity.
void repeat(BlinkSequence sequence, int repetitions, uint16_t terminal_level=0)
Repeats the sequence the specified number of times.
void set(uint16_t intensity)
Enables the LED at the specified intensity.
Abstract interface representing a monochrome LED.
virtual void setLevel(uint16_t level)=0
Sets the LED brightness in the range 0 (off) to 65535 (max).
virtual bool fade(uint16_t target_level, roo_time::Duration duration)=0
Initiates a linear fade to the target level over the duration.
constexpr Step FadeOn(roo_time::Duration duration)
Creates a step that fades linearly to the maximum brightness over duration.
roo_scheduler::Scheduler & DefaultScheduler()
Returns the default scheduler used for blinking operations.
constexpr Step TurnOff()
Creates a step that sets the LED to completely off instantly.
constexpr Step FadeOff(roo_time::Duration duration)
Creates a step that fades linearly down to off over the duration.
constexpr Step Hold(roo_time::Duration duration)
Creates a step that maintains the current brightness for the duration.
constexpr Step TurnOn()
Creates a step that sets the LED to the maximum brightness instantly.
BlinkSequence Blink(roo_time::Duration period, int duty_percent, int rampup_percent_on, int rampup_percent_off)
Creates a symmetric blink sequence with optional ramp-up/down segments.