ReactESP 3.0.0
Asynchronous programming for the ESP microcontrollers
Loading...
Searching...
No Matches
ReactESP.h
Go to the documentation of this file.
1#ifndef REACTESP_H_
2#define REACTESP_H_
3
4#include <Arduino.h>
5
6#include <forward_list>
7#include <functional>
8#include <queue>
9
10namespace reactesp {
11
12using react_callback = std::function<void()>;
13using isr_react_callback = void (*)(void*);
14
15// forward declarations
16
17class EventLoop;
18
26 virtual ~EventInterface() = default;
27
28 virtual void add(EventLoop* event_loop) = 0;
29 virtual void remove(EventLoop* event_loop) = 0;
30 virtual void tick(EventLoop* event_loop) = 0;
31};
32
36class Event : public EventInterface {
37 protected:
39
40 public:
47
48 // Disabling copy and move semantics
49 Event(const Event&) = delete;
50 Event(Event&&) = delete;
51 Event& operator=(const Event&) = delete;
52 Event& operator=(Event&&) = delete;
53};
54
58class TimedEvent : public Event {
59 protected:
60 const uint64_t interval;
62 bool enabled;
63
64 public:
72 : Event(callback),
73 interval((uint64_t)1000 * (uint64_t)interval),
74 last_trigger_time(micros()),
75 enabled(true) {}
87
88 bool operator<(const TimedEvent& other) const;
89 void add(EventLoop* event_loop) override;
90 void remove(EventLoop* event_loop) override;
91 uint32_t getTriggerTime() const {
92 return (last_trigger_time + interval) / 1000;
93 }
94 uint64_t getTriggerTimeMicros() const {
95 return (last_trigger_time + interval);
96 }
97 bool isEnabled() const { return enabled; }
98};
99
101 bool operator()(TimedEvent* a, TimedEvent* b) { return *b < *a; }
102};
103
107class DelayEvent : public TimedEvent {
108 public:
115 DelayEvent(uint32_t delay, react_callback callback);
122 DelayEvent(uint64_t delay, react_callback callback);
123
124 void tick(EventLoop* event_loop) override;
125};
126
151
155class UntimedEvent : public Event {
156 public:
158
159 void add(EventLoop* event_loop) override;
160 void remove(EventLoop* event_loop) override;
161};
162
167class StreamEvent : public UntimedEvent {
168 private:
169 Stream& stream;
170
171 public:
179 : UntimedEvent(callback), stream(stream) {}
180
181 void tick(EventLoop* event_loop) override;
182};
183
187class TickEvent : public UntimedEvent {
188 public:
195
196 void tick(EventLoop* event_loop) override;
197};
198
202class ISREvent : public Event {
203 private:
204 const uint8_t pin_number;
205 const int mode;
206#ifdef ESP32
207 // set to true once gpio_install_isr_service is called
208 static bool isr_service_installed;
209 static void isr(void* this_ptr);
210#endif
211
212 public:
221 ISREvent(uint8_t pin_number, int mode, react_callback callback)
222 : Event(callback), pin_number(pin_number), mode(mode) {
223#ifdef ESP32
224 gpio_int_type_t intr_type;
225 switch (mode) {
226 case RISING:
227 intr_type = GPIO_INTR_POSEDGE;
228 break;
229 case FALLING:
230 intr_type = GPIO_INTR_NEGEDGE;
231 break;
232 case CHANGE:
233 intr_type = GPIO_INTR_ANYEDGE;
234 break;
235 default:
236 intr_type = GPIO_INTR_DISABLE;
237 break;
238 }
239 // configure the IO pin
240 gpio_set_intr_type((gpio_num_t)pin_number, intr_type);
241
242 if (!isr_service_installed) {
243 isr_service_installed = true;
244 gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
245 }
246#endif
247 }
248
249 void add(EventLoop* event_loop) override;
250 void remove(EventLoop* event_loop) override;
251 void tick(EventLoop* event_loop) override {}
252};
253
255// EventLoop main event loop implementation
256
261 friend class Event;
262 friend class TimedEvent;
263 friend class RepeatEvent;
264 friend class UntimedEvent;
265 friend class ISREvent;
266
267 public:
272 : timed_queue(), untimed_list(), isr_event_list(), isr_pending_list() {
273 }
274
275 // Disabling copy and move semantics
276 EventLoop(const EventLoop&) = delete;
277 EventLoop(EventLoop&&) = delete;
278 EventLoop& operator=(const EventLoop&) = delete;
280
281 void tick();
282
290 DelayEvent* onDelay(uint32_t delay, react_callback callback);
298 DelayEvent* onDelayMicros(uint64_t delay, react_callback callback);
306 RepeatEvent* onRepeat(uint32_t interval, react_callback callback);
314 RepeatEvent* onRepeatMicros(uint64_t interval, react_callback callback);
322 StreamEvent* onAvailable(Stream& stream, react_callback callback);
333 ISREvent* onInterrupt(uint8_t pin_number, int mode,
334 react_callback callback);
342
348 void remove(Event* event);
349
350 private:
351 std::priority_queue<TimedEvent*, std::vector<TimedEvent*>,
353 timed_queue;
354 std::forward_list<UntimedEvent*> untimed_list;
355 std::forward_list<ISREvent*> isr_event_list;
356 std::forward_list<ISREvent*> isr_pending_list;
357
358 void tickTimed();
359 void tickUntimed();
360 void tickISR();
361 void add(Event* re);
362};
363
364// Provide compatibility aliases for the old naming scheme
365
374
375
376} // namespace reactesp
377
378#endif
Event that is triggered after a certain time delay.
Definition ReactESP.h:107
DelayEvent(uint32_t delay, react_callback callback)
Construct a new Delay Event object.
Definition ReactESP.cpp:28
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:38
Events are code to be called when a given condition is fulfilled.
Definition ReactESP.h:36
const react_callback callback
Definition ReactESP.h:38
Event & operator=(Event &&)=delete
Event & operator=(const Event &)=delete
Event(react_callback callback)
Construct a new Event object.
Definition ReactESP.h:46
Event(Event &&)=delete
Event(const Event &)=delete
Main event loop of a EventLoop program.
Definition ReactESP.h:260
DelayEvent * onDelayMicros(uint64_t delay, react_callback callback)
Create a new DelayEvent.
Definition ReactESP.cpp:142
EventLoop(EventLoop &&)=delete
EventLoop()
Construct a new EventLoop object.
Definition ReactESP.h:271
StreamEvent * onAvailable(Stream &stream, react_callback callback)
Create a new StreamEvent.
Definition ReactESP.cpp:162
TickEvent * onTick(react_callback callback)
Create a new TickEvent.
Definition ReactESP.cpp:175
DelayEvent * onDelay(uint32_t delay, react_callback callback)
Create a new DelayEvent.
Definition ReactESP.cpp:136
RepeatEvent * onRepeatMicros(uint64_t interval, react_callback callback)
Create a new RepeatEvent.
Definition ReactESP.cpp:155
EventLoop & operator=(const EventLoop &)=delete
EventLoop & operator=(EventLoop &&)=delete
ISREvent * onInterrupt(uint8_t pin_number, int mode, react_callback callback)
Create a new ISREvent (interrupt event)
Definition ReactESP.cpp:168
RepeatEvent * onRepeat(uint32_t interval, react_callback callback)
Create a new RepeatEvent.
Definition ReactESP.cpp:149
void remove(Event *event)
Remove a event from the list of active events.
Definition ReactESP.cpp:181
EventLoop(const EventLoop &)=delete
Event that is triggered on an input pin change.
Definition ReactESP.h:202
void remove(EventLoop *event_loop) override
Definition ReactESP.cpp:90
void tick(EventLoop *event_loop) override
Definition ReactESP.h:251
ISREvent(uint8_t pin_number, int mode, react_callback callback)
Construct a new ISREvent object.
Definition ReactESP.h:221
void add(EventLoop *event_loop) override
Definition ReactESP.cpp:81
Event that is triggered repeatedly.
Definition ReactESP.h:130
RepeatEvent(uint32_t interval, react_callback callback)
Construct a new Repeat Event object.
Definition ReactESP.h:138
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:44
RepeatEvent(uint64_t interval, react_callback callback)
Construct a new Repeat Event object.
Definition ReactESP.h:146
Event that is triggered when there is input available at the given Arduino Stream.
Definition ReactESP.h:167
StreamEvent(Stream &stream, react_callback callback)
Construct a new Stream Event object.
Definition ReactESP.h:178
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:64
Event that is triggered unconditionally at each execution loop.
Definition ReactESP.h:187
TickEvent(react_callback callback)
Construct a new Tick Event object.
Definition ReactESP.h:194
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:70
TimedEvents are called based on elapsing of time.
Definition ReactESP.h:58
const uint64_t interval
Definition ReactESP.h:60
uint64_t getTriggerTimeMicros() const
Definition ReactESP.h:94
TimedEvent(uint64_t interval, react_callback callback)
Construct a new Timed Event object.
Definition ReactESP.h:82
bool isEnabled() const
Definition ReactESP.h:97
void add(EventLoop *event_loop) override
Definition ReactESP.cpp:18
void remove(EventLoop *event_loop) override
Definition ReactESP.cpp:22
bool operator<(const TimedEvent &other) const
Definition ReactESP.cpp:13
uint64_t last_trigger_time
Definition ReactESP.h:61
uint32_t getTriggerTime() const
Definition ReactESP.h:91
TimedEvent(uint32_t interval, react_callback callback)
Construct a new Timed Event object.
Definition ReactESP.h:71
Events that are triggered based on something else than time.
Definition ReactESP.h:155
UntimedEvent(react_callback callback)
Definition ReactESP.h:157
void add(EventLoop *event_loop) override
Definition ReactESP.cpp:55
void remove(EventLoop *event_loop) override
Definition ReactESP.cpp:59
EventLoop event_loop
Definition main.cpp:19
std::function< void()> react_callback
Definition ReactESP.h:12
void(*)(void *) isr_react_callback
Definition ReactESP.h:13
EventInterface defines the interface for all events.
Definition ReactESP.h:22
virtual ~EventInterface()=default
Default virtual destructor.
virtual void add(EventLoop *event_loop)=0
virtual void tick(EventLoop *event_loop)=0
virtual void remove(EventLoop *event_loop)=0
bool operator()(TimedEvent *a, TimedEvent *b)
Definition ReactESP.h:101