SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
led_blinker.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_LED_BLINKER_H_
2#define SENSESP_SYSTEM_LED_BLINKER_H_
3
4#include <Arduino.h>
5#include <ReactESP.h>
6
7#include "crgb.h"
9
10namespace sensesp {
11
12#define PATTERN_END (-1)
13
18 public:
19 BaseBlinker(int pin) : pin_{pin} {
20 pinMode(pin, OUTPUT);
21 event_loop()->onDelay(1, [this]() { this->tick(); });
22 }
26 void set_state(bool state) {
27 this->state_ = state;
28 digitalWrite(pin_, state);
30 }
31
35 void flip_state() { this->set_state(!this->state_); }
36
40 void blip(int duration = 20) {
41 // only allow one blip at a time
42 if (blipping_) {
43 return;
44 }
45 blipping_ = true;
46
47 bool const orig_state = this->state_;
48 this->set_state(false);
49 int const current_counter = this->update_counter_;
50 event_loop()->onDelay(
51 duration, [this, duration, orig_state, current_counter]() {
52 // only update if no-one has touched the LED in the meanwhile
53 if (this->update_counter_ == current_counter) {
54 this->set_state(true);
55 int const new_counter = this->update_counter_;
56 event_loop()->onDelay(duration, [this, orig_state, new_counter]() {
57 // again, only update if no-one has touched the LED
58 if (this->update_counter_ == new_counter) {
59 this->set_state(orig_state);
60 }
61 blipping_ = false;
62 });
63 } else {
64 blipping_ = false;
65 }
66 });
67 }
68
72 void set_enabled(bool state) {
73 bool const was_enabled = this->enabled_;
74 this->enabled_ = state;
75 if (this->enabled_) {
76 this->tick();
77 } else {
78 this->set_state(false);
79 if (was_enabled) {
80 event_->remove(event_loop());
81 }
82 }
83 }
88 virtual void tick() = 0;
89
90 protected:
91 int pin_;
92 bool enabled_ = true;
93 bool state_ = false;
95 bool blipping_ = false;
96 reactesp::Event* event_ = NULL;
97};
98
99class LEDPattern;
100using FragmentCallback = std::function<void(uint32_t, CRGB&)>;
101
103 public:
104 LEDPatternFragment(uint32_t duration_ms, FragmentCallback callback)
105 : duration_ms_(duration_ms), callback_(callback) {}
106
107 // Copy constructor
110
111 // Assignment operator
114 callback_ = other.callback_;
115 return *this;
116 }
117
118 uint32_t duration_ms_;
120};
121
123 public:
125 LEDPattern(const std::vector<LEDPatternFragment>& fragments)
126 : fragments_(fragments) {}
127 LEDPattern(const std::initializer_list<LEDPatternFragment>& fragments)
128 : fragments_(fragments) {}
129
130 // Assignment operator
132 fragments_ = other.fragments_;
135 return *this;
136 }
137
138 bool apply(CRGB& crgb, bool oneshot = false) {
139 // Initialize fragment begin time if it's the first time
140 if (fragment_begin_ms_ == 0) {
141 fragment_begin_ms_ = millis();
142 }
143 unsigned long current_fragment_duration_ms =
144 fragments_[current_fragment_idx_].duration_ms_;
145 while (millis() - fragment_begin_ms_ >= current_fragment_duration_ms) {
147 if (current_fragment_idx_ >= fragments_.size()) {
149 if (oneshot) {
150 return false;
151 }
152 }
153 fragment_begin_ms_ += current_fragment_duration_ms;
154 current_fragment_duration_ms =
155 fragments_[current_fragment_idx_].duration_ms_;
156 }
157 // Call the callback function for the current fragment, passing the elapsed
158 // time and a reference to the CRGB object
160 crgb);
161 return true;
162 }
163
164 protected:
165 std::vector<LEDPatternFragment> fragments_;
167 unsigned long fragment_begin_ms_ = 0;
168};
169
187 public:
188 LEDBlinker(CRGB& crgb, LEDPattern pattern, std::function<void()> show_func)
189 : crgb_(crgb), pattern_(pattern), show_func_(show_func) {}
190 void tick() {
191 // Always start with the last color
195 std::list<LEDPattern>::iterator it = modifiers_.begin();
196 while (it != modifiers_.end()) {
197 LEDPattern* mod = &*it;
198 bool result = mod->apply(crgb_, true);
199 if (!result) {
200 it = modifiers_.erase(it);
201 } else {
202 it++;
203 }
204 }
205 show_func_();
206 }
207
208 void set_pattern(const LEDPattern& pattern) { pattern_ = pattern; }
209
210 void add_modifier(const LEDPattern& modifier) {
211 modifiers_.push_back(modifier);
212 }
213
214 protected:
216 CRGB last_color_ = CRGB::Black; // Previous set color before modifiers
218 std::list<LEDPattern> modifiers_ = {};
219
220 std::function<void()> show_func_;
221};
222
223LEDPatternFragment frag_solid_color(uint32_t duration_ms, const CRGB& color);
224LEDPatternFragment frag_linear_fade(uint32_t duration_ms,
225 uint32_t fade_duration_ms,
226 const CRGB& target_color);
227LEDPatternFragment frag_linear_invert(uint32_t duration_ms,
228 bool reverse = false);
229LEDPatternFragment frag_blend(uint32_t duration_ms, const CRGB& target_color,
230 bool reverse = false);
231LEDPatternFragment frag_nop(uint32_t duration_ms);
232} // namespace sensesp
233
234#endif
A base class for LED blinker classes.
Definition led_blinker.h:17
void set_state(bool state)
Definition led_blinker.h:26
void set_enabled(bool state)
Definition led_blinker.h:72
void blip(int duration=20)
Definition led_blinker.h:40
virtual void tick()=0
reactesp::Event * event_
Definition led_blinker.h:96
Blink the LED with a pattern specified by a vector of callback functions.
std::function< void()> show_func_
void add_modifier(const LEDPattern &modifier)
void set_pattern(const LEDPattern &pattern)
std::list< LEDPattern > modifiers_
LEDBlinker(CRGB &crgb, LEDPattern pattern, std::function< void()> show_func)
LEDPatternFragment(const LEDPatternFragment &other)
LEDPatternFragment & operator=(const LEDPatternFragment &other)
FragmentCallback callback_
LEDPatternFragment(uint32_t duration_ms, FragmentCallback callback)
LEDPattern(const std::vector< LEDPatternFragment > &fragments)
unsigned long fragment_begin_ms_
bool apply(CRGB &crgb, bool oneshot=false)
uint32_t current_fragment_idx_
std::vector< LEDPatternFragment > fragments_
LEDPattern(const std::initializer_list< LEDPatternFragment > &fragments)
LEDPattern & operator=(const LEDPattern &other)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
std::function< void(uint32_t, CRGB &)> FragmentCallback
LEDPatternFragment frag_linear_fade(uint32_t duration_ms, uint32_t fade_duration_ms, const CRGB &target_color)
LEDPatternFragment frag_nop(uint32_t duration_ms)
LEDPatternFragment frag_blend(uint32_t duration_ms, const CRGB &target_color, bool reverse)
LEDPatternFragment frag_linear_invert(uint32_t duration_ms, bool reverse)
LEDPatternFragment frag_solid_color(uint32_t duration_ms, const CRGB &color)
Minimal RGB color struct.
Definition crgb.h:59
static const CRGB Black
Definition crgb.h:89