ReactESP 3.0.1
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
19// ESP32 doesn't have the micros64 function defined
20#ifdef ESP32
21uint64_t ICACHE_RAM_ATTR micros64();
22#endif
23
31 virtual ~EventInterface() = default;
32
33 virtual void add(EventLoop* event_loop) = 0;
34 virtual void remove(EventLoop* event_loop) = 0;
35 virtual void tick(EventLoop* event_loop) = 0;
36};
37
41class Event : public EventInterface {
42 protected:
44
45 public:
52
53 // Disabling copy and move semantics
54 Event(const Event&) = delete;
55 Event(Event&&) = delete;
56 Event& operator=(const Event&) = delete;
57 Event& operator=(Event&&) = delete;
58};
59
63class TimedEvent : public Event {
64 protected:
65 const uint64_t interval;
67 bool enabled;
68
69 public:
77 : Event(callback),
78 interval((uint64_t)1000 * (uint64_t)interval),
79 last_trigger_time(micros64()),
80 enabled(true) {}
92
93 bool operator<(const TimedEvent& other) const;
94 void add(EventLoop* event_loop) override;
95 void remove(EventLoop* event_loop) override;
96 uint32_t getTriggerTime() const {
97 return (last_trigger_time + interval) / 1000;
98 }
99 uint64_t getTriggerTimeMicros() const {
100 return (last_trigger_time + interval);
101 }
102 bool isEnabled() const { return enabled; }
103};
104
106 bool operator()(TimedEvent* a, TimedEvent* b) { return *b < *a; }
107};
108
112class DelayEvent : public TimedEvent {
113 public:
120 DelayEvent(uint32_t delay, react_callback callback);
127 DelayEvent(uint64_t delay, react_callback callback);
128
129 void tick(EventLoop* event_loop) override;
130};
131
156
160class UntimedEvent : public Event {
161 public:
163
164 void add(EventLoop* event_loop) override;
165 void remove(EventLoop* event_loop) override;
166};
167
172class StreamEvent : public UntimedEvent {
173 private:
174 Stream& stream;
175
176 public:
184 : UntimedEvent(callback), stream(stream) {}
185
186 void tick(EventLoop* event_loop) override;
187};
188
192class TickEvent : public UntimedEvent {
193 public:
200
201 void tick(EventLoop* event_loop) override;
202};
203
207class ISREvent : public Event {
208 private:
209 const uint8_t pin_number;
210 const int mode;
211#ifdef ESP32
212 // set to true once gpio_install_isr_service is called
213 static bool isr_service_installed;
214 static void isr(void* this_ptr);
215#endif
216
217 public:
226 ISREvent(uint8_t pin_number, int mode, react_callback callback)
227 : Event(callback), pin_number(pin_number), mode(mode) {
228#ifdef ESP32
229 gpio_int_type_t intr_type;
230 switch (mode) {
231 case RISING:
232 intr_type = GPIO_INTR_POSEDGE;
233 break;
234 case FALLING:
235 intr_type = GPIO_INTR_NEGEDGE;
236 break;
237 case CHANGE:
238 intr_type = GPIO_INTR_ANYEDGE;
239 break;
240 default:
241 intr_type = GPIO_INTR_DISABLE;
242 break;
243 }
244 // configure the IO pin
245 gpio_set_intr_type((gpio_num_t)pin_number, intr_type);
246
247 if (!isr_service_installed) {
248 isr_service_installed = true;
249 gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
250 }
251#endif
252 }
253
254 void add(EventLoop* event_loop) override;
255 void remove(EventLoop* event_loop) override;
256 void tick(EventLoop* event_loop) override {}
257};
258
260// EventLoop main event loop implementation
261
266 friend class Event;
267 friend class TimedEvent;
268 friend class RepeatEvent;
269 friend class UntimedEvent;
270 friend class ISREvent;
271
272 public:
277 : timed_queue(), untimed_list(), isr_event_list(), isr_pending_list() {
278 }
279
280 // Disabling copy and move semantics
281 EventLoop(const EventLoop&) = delete;
282 EventLoop(EventLoop&&) = delete;
283 EventLoop& operator=(const EventLoop&) = delete;
285
286 void tick();
287
295 DelayEvent* onDelay(uint32_t delay, react_callback callback);
303 DelayEvent* onDelayMicros(uint64_t delay, react_callback callback);
311 RepeatEvent* onRepeat(uint32_t interval, react_callback callback);
319 RepeatEvent* onRepeatMicros(uint64_t interval, react_callback callback);
327 StreamEvent* onAvailable(Stream& stream, react_callback callback);
338 ISREvent* onInterrupt(uint8_t pin_number, int mode,
339 react_callback callback);
347
353 void remove(Event* event);
354
355 private:
356 std::priority_queue<TimedEvent*, std::vector<TimedEvent*>,
358 timed_queue;
359 std::forward_list<UntimedEvent*> untimed_list;
360 std::forward_list<ISREvent*> isr_event_list;
361 std::forward_list<ISREvent*> isr_pending_list;
362
363 void tickTimed();
364 void tickUntimed();
365 void tickISR();
366 void add(Event* re);
367};
368
369// Provide compatibility aliases for the old naming scheme
370
379
380
381} // namespace reactesp
382
383#endif
Event that is triggered after a certain time delay.
Definition ReactESP.h:112
DelayEvent(uint32_t delay, react_callback callback)
Construct a new Delay Event object.
Definition ReactESP.cpp:39
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:49
Events are code to be called when a given condition is fulfilled.
Definition ReactESP.h:41
const react_callback callback
Definition ReactESP.h:43
Event & operator=(Event &&)=delete
Event & operator=(const Event &)=delete
Event(react_callback callback)
Construct a new Event object.
Definition ReactESP.h:51
Event(Event &&)=delete
Event(const Event &)=delete
Main event loop of a EventLoop program.
Definition ReactESP.h:265
DelayEvent * onDelayMicros(uint64_t delay, react_callback callback)
Create a new DelayEvent.
Definition ReactESP.cpp:152
EventLoop(EventLoop &&)=delete
EventLoop()
Construct a new EventLoop object.
Definition ReactESP.h:276
StreamEvent * onAvailable(Stream &stream, react_callback callback)
Create a new StreamEvent.
Definition ReactESP.cpp:172
TickEvent * onTick(react_callback callback)
Create a new TickEvent.
Definition ReactESP.cpp:185
DelayEvent * onDelay(uint32_t delay, react_callback callback)
Create a new DelayEvent.
Definition ReactESP.cpp:146
RepeatEvent * onRepeatMicros(uint64_t interval, react_callback callback)
Create a new RepeatEvent.
Definition ReactESP.cpp:165
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:178
RepeatEvent * onRepeat(uint32_t interval, react_callback callback)
Create a new RepeatEvent.
Definition ReactESP.cpp:159
void remove(Event *event)
Remove a event from the list of active events.
Definition ReactESP.cpp:191
EventLoop(const EventLoop &)=delete
Event that is triggered on an input pin change.
Definition ReactESP.h:207
void remove(EventLoop *event_loop) override
Definition ReactESP.cpp:101
void tick(EventLoop *event_loop) override
Definition ReactESP.h:256
ISREvent(uint8_t pin_number, int mode, react_callback callback)
Construct a new ISREvent object.
Definition ReactESP.h:226
void add(EventLoop *event_loop) override
Definition ReactESP.cpp:92
Event that is triggered repeatedly.
Definition ReactESP.h:135
RepeatEvent(uint32_t interval, react_callback callback)
Construct a new Repeat Event object.
Definition ReactESP.h:143
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:55
RepeatEvent(uint64_t interval, react_callback callback)
Construct a new Repeat Event object.
Definition ReactESP.h:151
Event that is triggered when there is input available at the given Arduino Stream.
Definition ReactESP.h:172
StreamEvent(Stream &stream, react_callback callback)
Construct a new Stream Event object.
Definition ReactESP.h:183
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:75
Event that is triggered unconditionally at each execution loop.
Definition ReactESP.h:192
TickEvent(react_callback callback)
Construct a new Tick Event object.
Definition ReactESP.h:199
void tick(EventLoop *event_loop) override
Definition ReactESP.cpp:81
TimedEvents are called based on elapsing of time.
Definition ReactESP.h:63
const uint64_t interval
Definition ReactESP.h:65
uint64_t getTriggerTimeMicros() const
Definition ReactESP.h:99
TimedEvent(uint64_t interval, react_callback callback)
Construct a new Timed Event object.
Definition ReactESP.h:87
bool isEnabled() const
Definition ReactESP.h:102
void add(EventLoop *event_loop) override
Definition ReactESP.cpp:29
void remove(EventLoop *event_loop) override
Definition ReactESP.cpp:33
bool operator<(const TimedEvent &other) const
Return the current time since the device restart in microseconds.
Definition ReactESP.cpp:24
uint64_t last_trigger_time
Definition ReactESP.h:66
uint32_t getTriggerTime() const
Definition ReactESP.h:96
TimedEvent(uint32_t interval, react_callback callback)
Construct a new Timed Event object.
Definition ReactESP.h:76
Events that are triggered based on something else than time.
Definition ReactESP.h:160
UntimedEvent(react_callback callback)
Definition ReactESP.h:162
void add(EventLoop *event_loop) override
Definition ReactESP.cpp:66
void remove(EventLoop *event_loop) override
Definition ReactESP.cpp:70
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:27
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:106