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
7#include "roo_blink/rgb/led.h"
8#include "roo_logging.h"
9#include "roo_scheduler.h"
10#include "roo_time.h"
11
12namespace roo_blink {
13
14/// Single step of an RGB blink sequence.
15class RgbStep {
16 public:
17 friend constexpr RgbStep RgbSetTo(Color color);
18 friend constexpr RgbStep RgbHold(roo_time::Duration duration);
19 friend constexpr RgbStep RgbTurnOff();
20 friend constexpr RgbStep RgbFadeTo(Color color, roo_time::Duration duration);
21 friend constexpr RgbStep RgbFadeOff(roo_time::Duration duration);
22
23 private:
24 friend class RgbBlinker;
25
26 enum Type { kSet, kHold, kFade };
27
28 constexpr RgbStep(Type type, Color color, uint16_t duration_millis);
29
30 Type type_;
31 Color target_color_;
32 uint16_t duration_millis_;
33};
34
35/// Sequence of steps for RGB blinking.
37 public:
38 void add(RgbStep step) { sequence_.push_back(std::move(step)); }
39
40 private:
41 std::vector<RgbStep> sequence_;
42
43 friend class RgbBlinker;
44};
45
46/// Creates a step that sets the LED to the specified color instantly.
47constexpr RgbStep RgbSetTo(Color color);
48
49/// Creates a step that disables the LED. Equivalent to RgbSetTo(Color()).
50constexpr RgbStep RgbTurnOff();
51
52/// Creates a step that fades to the target color over the duration.
53constexpr RgbStep RgbFadeTo(Color color, roo_time::Duration duration);
54
55/// Creates a step that fades the LED off over the duration.
56constexpr RgbStep RgbFadeOff(roo_time::Duration duration);
57
58/// Creates a step that holds the current color for the duration.
59constexpr RgbStep RgbHold(roo_time::Duration duration);
60
61/// Runs blink sequences on an RGB LED.
63 public:
64 /// Constructs a RgbBlinker using the default scheduler.
65 RgbBlinker(RgbLed& led);
66
67 /// Constructs a RgbBlinker using the specified scheduler.
68 RgbBlinker(RgbLed& led, roo_scheduler::Scheduler& scheduler);
69
70 /// Repeats the sequence indefinitely.
71 void loop(RgbBlinkSequence sequence);
72
73 /// Repeats the sequence the specified number of times.
74 void repeat(RgbBlinkSequence sequence, int repetitions,
75 Color terminal_color = Color());
76
77 /// Executes the sequence once.
78 void execute(RgbBlinkSequence sequence, Color terminal_color = Color());
79
80 /// Enables the LED, setting it to the specified color.
81 void setColor(Color color);
82
83 /// Disables the LED.
84 void turnOff();
85
86 private:
87 void updateSequence(RgbBlinkSequence sequence, int repetitions,
88 Color terminal_color);
89 void step();
90
91 RgbLed& led_;
92 roo_scheduler::SingletonTask stepper_;
93 std::vector<RgbStep> sequence_;
94 Color current_color_;
95 Color terminal_color_;
96 size_t repetitions_;
97 size_t pos_;
98
99 mutable roo::mutex mutex_;
100
101 bool fade_in_progress_;
102 Color fade_start_color_;
103 Color fade_target_color_;
104 roo_time::Uptime fade_start_time_;
105 roo_time::Uptime fade_end_time_;
106};
107
108/// Creates a symmetric blink sequence with optional ramp-up/down segments.
109RgbBlinkSequence RgbBlink(roo_time::Duration period, Color color,
110 int duty_percent = 50, int rampup_percent_on = 0,
111 int rampup_percent_off = 0);
112
113// Implementation details.
114
115constexpr RgbStep::RgbStep(Type type, Color color, uint16_t duration_millis)
116 : type_(type), target_color_(color), duration_millis_(duration_millis) {}
117
118constexpr RgbStep RgbSetTo(Color color) {
119 return RgbStep(RgbStep::kSet, color, 0);
120}
121
122constexpr RgbStep RgbTurnOff() { return RgbSetTo(Color()); }
123
124constexpr RgbStep RgbHold(roo_time::Duration duration) {
125 return RgbStep(RgbStep::kHold, Color(), (uint16_t)duration.inMillis());
126}
127
128constexpr RgbStep RgbFadeTo(Color color, roo_time::Duration duration) {
129 return RgbStep(RgbStep::kFade, color, (uint16_t)duration.inMillis());
130}
131
132constexpr RgbStep RgbFadeOff(roo_time::Duration duration) {
133 return RgbFadeTo(Color(), duration);
134}
135
136} // namespace roo_blink