SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
led_blinker.cpp
Go to the documentation of this file.
1
2#include "led_blinker.h"
3
4#include "sensesp.h"
5#include "sensesp_app.h"
6
7namespace sensesp {
8
9#define max(a, b) ((a) > (b) ? (a) : (b))
10
11BaseBlinker::BaseBlinker(int pin) : pin_{pin} { pinMode(pin, OUTPUT); }
12
17 this->state_ = state;
20}
21
25void BaseBlinker::flip_state() { this->set_state(!this->state_); }
26
31 // indicator for a blip being in progress
32 static bool blipping = false;
33
34 // only allow one blip at a time
35 if (blipping) {
36 return;
37 }
38 blipping = true;
39
40 bool orig_state = this->state_;
41 this->set_state(false);
43 ReactESP::app->onDelay(
45 // only update if no-one has touched the LED in the meanwhile
46 if (this->update_counter_ == current_counter) {
47 this->set_state(true);
48 int new_counter = this->update_counter_;
49 ReactESP::app->onDelay(duration, [this, orig_state, new_counter]() {
50 // again, only update if no-one has touched the LED
51 if (this->update_counter_ == new_counter) {
52 this->set_state(orig_state);
53 }
54 blipping = false;
55 });
56 } else {
57 blipping = false;
58 }
59 });
60}
61
66 bool was_enabled = this->enabled_;
67 this->enabled_ = state;
68 if (this->enabled_) {
69 this->tick();
70 } else {
71 this->set_state(false);
72 if (was_enabled) {
73 reaction_->remove();
74 }
75 }
76}
77
78void BaseBlinker::start() { this->tick(); }
79
81 : BaseBlinker(pin), period_{period} {}
82
83EvenBlinker::EvenBlinker(int pin, unsigned int period)
84 : PeriodicBlinker(pin, period) {}
85
87 if (!enabled_) {
88 return;
89 }
90 this->flip_state();
91 reaction_ = ReactESP::app->onDelay(period_, [this]() { this->tick(); });
92}
93
94RatioBlinker::RatioBlinker(int pin, unsigned int period, float ratio)
95 : PeriodicBlinker(pin, period), ratio_{ratio} {}
96
98 if (!enabled_) {
99 return;
100 }
101 this->flip_state();
102 int on_duration = ratio_ * period_;
104 unsigned int ref_duration = state_ == false ? off_duration : on_duration;
105 reaction_ = ReactESP::app->onDelay(ref_duration, [this]() { this->tick(); });
106}
107
109 : BaseBlinker(pin), pattern_{pattern} {}
110
118 this->pattern_ = pattern;
119 this->restart();
120}
121
123 if (!enabled_) {
124 return;
125 }
126 // When pattern[pattern_ptr] == PATTERN_END, that's the end of the pattern,
127 // so start over at the beginning.
129 pattern_ptr_ = 0;
130 }
131 // odd indices indicate times when LED should be OFF, even when ON
132 bool new_state = (pattern_ptr_ % 2) == 0;
133 this->set_state(new_state);
134 reaction_ = ReactESP::app->onDelay(pattern_[pattern_ptr_++],
135 [this]() { this->tick(); });
136}
137
139 state_ = false;
140 pattern_ptr_ = 0;
141 if (reaction_ != NULL) {
142 reaction_->remove();
143 reaction_ = NULL;
144 this->tick();
145 }
146}
147
148} // namespace sensesp
A base class for LED blinker classes.
Definition led_blinker.h:16
void set_state(bool state)
void set_enabled(bool state)
void blip(int duration=20)
virtual void tick()=0
void start() override
EvenBlinker(int pin, unsigned int period)
void tick() override final
Construct a new transform based on a single function.
PatternBlinker(int pin, int pattern[])
void set_pattern(int pattern[])
unsigned int pattern_ptr_
Definition led_blinker.h:87
void tick() override final
A base class for periodic blinkers.
Definition led_blinker.h:41
PeriodicBlinker(int pin, unsigned int period)
RatioBlinker(int pin, unsigned int period, float ratio=0.)
void tick() override final
#define max(a, b)
#define PATTERN_END
Definition led_blinker.h:11