SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
led_blinker.cpp
Go to the documentation of this file.
1
2#include "sensesp.h"
3
4#include "led_blinker.h"
5
6#include "sensesp_app.h"
7
8namespace sensesp {
9
10#define max(a, b) ((a) > (b) ? (a) : (b))
11
12BaseBlinker::BaseBlinker(int pin) : pin_{pin} {
13 pinMode(pin, OUTPUT);
14 event_loop()->onDelay(1, [this]() { this->tick(); });
15}
16
20void BaseBlinker::set_state(bool state) {
21 this->state_ = state;
22 digitalWrite(pin_, state);
24}
25
29void BaseBlinker::flip_state() { this->set_state(!this->state_); }
30
34void BaseBlinker::blip(int duration) {
35 // indicator for a blip being in progress
36 static bool blipping = false;
37
38 // only allow one blip at a time
39 if (blipping) {
40 return;
41 }
42 blipping = true;
43
44 bool const orig_state = this->state_;
45 this->set_state(false);
46 int const current_counter = this->update_counter_;
47 event_loop()->onDelay(
48 duration, [this, duration, orig_state, current_counter]() {
49 // only update if no-one has touched the LED in the meanwhile
50 if (this->update_counter_ == current_counter) {
51 this->set_state(true);
52 int const new_counter = this->update_counter_;
53 event_loop()->onDelay(
54 duration, [this, orig_state, new_counter]() {
55 // again, only update if no-one has touched the LED
56 if (this->update_counter_ == new_counter) {
57 this->set_state(orig_state);
58 }
59 blipping = false;
60 });
61 } else {
62 blipping = false;
63 }
64 });
65}
66
70void BaseBlinker::set_enabled(bool state) {
71 bool const was_enabled = this->enabled_;
72 this->enabled_ = state;
73 if (this->enabled_) {
74 this->tick();
75 } else {
76 this->set_state(false);
77 if (was_enabled) {
78 event_->remove(event_loop());
79 }
80 }
81}
82
83PeriodicBlinker::PeriodicBlinker(int pin, unsigned int period)
84 : BaseBlinker(pin), period_{period} {}
85
86EvenBlinker::EvenBlinker(int pin, unsigned int period)
87 : PeriodicBlinker(pin, period) {}
88
90 if (!enabled_) {
91 return;
92 }
93 this->flip_state();
94 event_ = event_loop()->onDelay(
95 period_, [this]() { this->tick(); });
96}
97
98RatioBlinker::RatioBlinker(int pin, unsigned int period, float ratio)
99 : PeriodicBlinker(pin, period), ratio_{ratio} {}
100
102 if (!enabled_) {
103 return;
104 }
105 this->flip_state();
106 int const on_duration = ratio_ * period_;
107 int const off_duration = max(0, period_ - on_duration);
108 unsigned int const ref_duration =
109 state_ == false ? off_duration : on_duration;
110 event_ = event_loop()->onDelay(
111 ref_duration, [this]() { this->tick(); });
112}
113
114PatternBlinker::PatternBlinker(int pin, int pattern[])
115 : BaseBlinker(pin), pattern_{pattern} {}
116
123void PatternBlinker::set_pattern(int pattern[]) {
124 this->pattern_ = pattern;
125 this->restart();
126}
127
129 if (!enabled_) {
130 return;
131 }
132 // When pattern[pattern_ptr] == PATTERN_END, that's the end of the pattern,
133 // so start over at the beginning.
135 pattern_ptr_ = 0;
136 }
137 // odd indices indicate times when LED should be OFF, even when ON
138 bool const new_state = (pattern_ptr_ % 2) == 0;
139 this->set_state(new_state);
140 event_ = event_loop()->onDelay(
141 pattern_[pattern_ptr_++], [this]() { this->tick(); });
142}
143
145 state_ = false;
146 pattern_ptr_ = 0;
147 if (event_ != NULL) {
148 event_->remove(event_loop());
149 event_ = NULL;
150 this->tick();
151 }
152}
153
154} // namespace sensesp
A base class for LED blinker classes.
Definition led_blinker.h:15
void set_state(bool state)
void set_enabled(bool state)
void blip(int duration=20)
virtual void tick()=0
reactesp::Event * event_
Definition led_blinker.h:33
EvenBlinker(int pin, unsigned int period)
void tick() override final
PatternBlinker(int pin, int pattern[])
void set_pattern(int pattern[])
unsigned int pattern_ptr_
Definition led_blinker.h:85
void tick() override final
A base class for periodic blinkers.
Definition led_blinker.h:39
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:10
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9