roo_blink
API Documentation for roo_blink
Loading...
Searching...
No Matches
blinker.h
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4
5#include <vector>
6
8#include "roo_logging.h"
9#include "roo_scheduler.h"
10#include "roo_threads.h"
11#include "roo_time.h"
12
13namespace roo_blink {
14
15/// Single step of a monochrome blink sequence.
16class Step {
17 public:
18 friend constexpr Step TurnOn();
19 friend constexpr Step TurnOff();
20 friend constexpr Step FadeOn(roo_time::Duration duration);
21 friend constexpr Step FadeOff(roo_time::Duration duration);
22
23 friend constexpr Step SetTo(uint16_t level);
24 friend constexpr Step FadeTo(uint16_t level, roo_time::Duration duration);
25 friend constexpr Step Hold(roo_time::Duration duration);
26
27 private:
28 friend class Blinker;
29
30 enum Type { kSet, kHold, kFade };
31
32 constexpr Step(Type type, uint16_t target_level, uint16_t duration_millis);
33
34 Type type_;
35 uint16_t target_level_;
36 uint16_t duration_millis_;
37};
38
39/// Sequence of steps for monochrome blinking.
41 public:
42 void add(Step step) { sequence_.push_back(std::move(step)); }
43
44 private:
45 std::vector<Step> sequence_;
46
47 friend class Blinker;
48};
49
50/// Creates a step that sets the LED to the maximum brightness instantly.
51constexpr Step TurnOn();
52
53/// Creates a step that sets the LED to completely off instantly.
54constexpr Step TurnOff();
55
56/// Creates a step that sets the LED to the specified brightness instantly.
57constexpr Step SetTo(uint16_t level);
58
59/// Creates a step that fades linearly to the target level over the duration.
60constexpr Step FadeTo(uint16_t level, roo_time::Duration duration);
61
62/// Creates a step that fades linearly to the maximum brightness over duration.
63constexpr Step FadeOn(roo_time::Duration duration);
64
65/// Creates a step that fades linearly down to off over the duration.
66constexpr Step FadeOff(roo_time::Duration duration);
67
68/// Creates a step that maintains the current brightness for the duration.
69constexpr Step Hold(roo_time::Duration duration);
70
71/// Runs blink sequences on a monochrome LED.
72class Blinker {
73 public:
74 /// Constructs a Blinker using the default scheduler.
75 Blinker(Led& led);
76
77 /// Constructs a Blinker using the specified scheduler.
78 Blinker(Led& led, roo_scheduler::Scheduler& scheduler);
79
80 /// Repeats the sequence indefinitely.
81 void loop(BlinkSequence sequence);
82
83 /// Repeats the sequence the specified number of times.
84 void repeat(BlinkSequence sequence, int repetitions,
85 uint16_t terminal_level = 0);
86
87 /// Executes the sequence once.
88 void execute(BlinkSequence sequence, uint16_t terminal_level = 0);
89
90 /// Enables the LED at the specified intensity.
91 void set(uint16_t intensity);
92
93 /// Enables the LED at the maximum intensity.
94 void turnOn();
95
96 /// Disables the LED.
97 void turnOff();
98
99 private:
100 void updateSequence(BlinkSequence sequence, int repetitions,
101 uint16_t terminal_level);
102 void step();
103
104 Led& led_;
105 roo_scheduler::SingletonTask stepper_;
106 std::vector<Step> sequence_;
107 uint16_t current_level_;
108 uint16_t terminal_level_;
109 size_t repetitions_;
110 size_t pos_;
111
112 mutable roo::mutex mutex_;
113
114 // For when hardware fading is not supported.
115 bool fade_in_progress_;
116 uint16_t fade_start_level_;
117 uint16_t fade_target_level_;
118 roo_time::Uptime fade_start_time_;
119 roo_time::Uptime fade_end_time_;
120};
121
122/// Creates a symmetric blink sequence with optional ramp-up/down segments.
123BlinkSequence Blink(roo_time::Duration period, int duty_percent = 50,
124 int rampup_percent_on = 0, int rampup_percent_off = 0);
125
126// Implementation details.
127
128constexpr Step::Step(Type type, uint16_t target_level, uint16_t duration_millis)
129 : type_(type),
130 target_level_(target_level),
131 duration_millis_(duration_millis) {}
132
133constexpr Step TurnOn() { return Step(Step::kSet, 65535, 0); }
134constexpr Step TurnOff() { return Step(Step::kSet, 0, 0); }
135
136constexpr Step SetTo(uint16_t level) { return Step(Step::kSet, level, 0); }
137
138constexpr Step FadeTo(uint16_t level, roo_time::Duration duration) {
139 return Step(Step::kFade, level, (uint16_t)duration.inMillis());
140}
141
142constexpr Step FadeOn(roo_time::Duration duration) {
143 return FadeTo(65535, duration);
144}
145
146constexpr Step FadeOff(roo_time::Duration duration) {
147 return FadeTo(0, duration);
148}
149
150constexpr Step Hold(roo_time::Duration duration) {
151 return Step(Step::kHold, 0, (uint16_t)duration.inMillis());
152}
153
154} // namespace roo_blink