ReactESP
3.2.0
Asynchronous programming for the ESP microcontrollers
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <Arduino.h>
2
#include <
ReactESP.h
>
3
#include "esp_system.h"
4
#include "esp_pm.h"
5
6
using namespace
reactesp
;
7
8
#define LED_PIN 2
9
#define OUT_PIN 18
// D5
10
#define INPUT_PIN1 12
// D6
11
#define INPUT_PIN2 13
// D7
12
13
14
#define NUM_TIMERS 20
15
16
int
tick_counter
= 0;
17
int
timer_ticks
[
NUM_TIMERS
];
18
19
EventLoop
event_loop
;
20
21
void
reporter
() {
22
Serial.printf(
"Timer ticks: "
);
23
for
(
int
i=0; i<
NUM_TIMERS
; i++) {
24
Serial.printf(
"%d "
,
timer_ticks
[i]);
25
timer_ticks
[i] = 0;
26
}
27
Serial.printf(
"\n"
);
28
Serial.printf(
"Core: %d\n"
, xPortGetCoreID());
29
Serial.printf(
"Uptime: %d\n"
, millis());
30
Serial.printf(
"Free mem: %d\n"
, esp_get_free_heap_size());
31
Serial.printf(
"Ticks per second: %d\n"
,
tick_counter
);
32
tick_counter
= 0;
33
}
34
35
void
setup_timers
(
EventLoop
&
event_loop
) {
36
// create twenty timers
37
38
for
(
int
i=0; i<
NUM_TIMERS
; i++) {
39
timer_ticks
[i] = 0;
40
int
delay = (i+1)*(i+1);
41
event_loop
.
onRepeat
(delay, [i]() {
42
timer_ticks
[i]++;
43
});
44
}
45
46
// create one more timer to report the counted ticks
47
48
event_loop
.
onRepeat
(1000,
reporter
);
49
}
50
51
void
setup_io_pins
(
EventLoop
*
event_loop
) {
52
static
ISREvent
* ire2 =
nullptr
;
53
static
int
out_pin_state = 0;
54
55
56
// change OUT_PIN state every 900 ms
57
pinMode(
OUT_PIN
, OUTPUT);
58
event_loop
->
onRepeat
(900, [] () {
59
out_pin_state = !out_pin_state;
60
digitalWrite(
OUT_PIN
, out_pin_state);
61
});
62
63
auto
reporter
= [] (
int
pin) {
64
Serial.printf(
"Pin %d changed state.\n"
, pin);
65
};
66
67
// create an interrupt that always reports if PIN1 is rising
68
event_loop
->
onInterrupt
(
INPUT_PIN1
, RISING, std::bind(
reporter
,
INPUT_PIN1
));
69
70
// every 9s, toggle reporting PIN2 falling edge
71
event_loop
->
onRepeat
(9000, [
event_loop
, &
reporter
]() {
72
if
(ire2==
nullptr
) {
73
ire2 =
event_loop
->
onInterrupt
(
INPUT_PIN2
, FALLING, std::bind(
reporter
,
INPUT_PIN2
));
74
}
else
{
75
ire2->
remove
(
event_loop
);
76
ire2 =
nullptr
;
77
}
78
});
79
80
}
81
82
void
setup_serial
(
EventLoop
&
event_loop
) {
83
// if something is received on the serial port, turn the led off for one second
84
event_loop
.
onAvailable
(Serial, [&
event_loop
] () {
85
static
int
event_counter = 0;
86
87
Serial.write(Serial.read());
88
digitalWrite(
LED_PIN
, HIGH);
89
90
event_counter++;
91
92
int
current = event_counter;
93
94
event_loop
.
onDelay
(1000, [current] () {
95
if
(event_counter==current) {
96
digitalWrite(
LED_PIN
, LOW);
97
}
98
});
99
});
100
}
101
102
void
setup_tick
(
EventLoop
&
event_loop
) {
103
// increase the tick counter on every tick
104
event_loop
.
onTick
([]() {
105
tick_counter
++;
106
});
107
}
108
109
void
execute_task
(
void
* arg) {
110
EventLoop
task_app;
111
112
Serial.println(
"Defining a repeat event in a task"
);
113
task_app.
onRepeat
(1000, []() {
114
Serial.printf(
"C%d Task running.\n"
, xPortGetCoreID());
115
});
116
Serial.println(
"Done"
);
117
118
while
(
true
) {
119
task_app.
tick
();
120
delay(1);
121
}
122
}
123
124
void
setup
() {
125
Serial.begin(115200);
126
Serial.println(
"Starting"
);
127
pinMode(
LED_PIN
, OUTPUT);
128
129
setup_timers
(
event_loop
);
130
setup_io_pins
(&
event_loop
);
131
setup_serial
(
event_loop
);
132
setup_tick
(
event_loop
);
133
//
134
//xTaskCreatePinnedToCore(execute_task, "SecondTask", 4096, NULL,
135
// 1, NULL, 0);
136
137
esp_pm_config_esp32_t pm_config;
138
pm_config.max_freq_mhz = 240;
139
pm_config.min_freq_mhz = 80;
140
pm_config.light_sleep_enable =
true
;
141
142
esp_pm_configure(&pm_config);
143
144
}
145
146
void
loop
() {
147
event_loop
.
tick
();
148
}
ReactESP.h
reactesp::EventLoop
Asynchronous event loop supporting timed (repeating and non-repeating), interrupt and stream events.
Definition
event_loop.h:14
reactesp::EventLoop::tick
void tick()
Definition
event_loop.cpp:43
reactesp::EventLoop::onAvailable
StreamEvent * onAvailable(Stream &stream, react_callback callback)
Create a new StreamEvent.
Definition
event_loop.cpp:74
reactesp::EventLoop::onTick
TickEvent * onTick(react_callback callback)
Create a new TickEvent.
Definition
event_loop.cpp:87
reactesp::EventLoop::onDelay
DelayEvent * onDelay(uint32_t delay, react_callback callback)
Create a new DelayEvent.
Definition
event_loop.cpp:49
reactesp::EventLoop::onInterrupt
ISREvent * onInterrupt(uint8_t pin_number, int mode, react_callback callback)
Create a new ISREvent (interrupt event)
Definition
event_loop.cpp:80
reactesp::EventLoop::onRepeat
RepeatEvent * onRepeat(uint32_t interval, react_callback callback)
Create a new RepeatEvent.
Definition
event_loop.cpp:61
reactesp::ISREvent
Event that is triggered on an input pin change.
Definition
events.h:229
reactesp::ISREvent::remove
void remove(EventLoop *event_loop) override
Definition
events.cpp:96
setup_serial
void setup_serial(EventLoop &event_loop)
Definition
main.cpp:82
NUM_TIMERS
#define NUM_TIMERS
Definition
main.cpp:14
INPUT_PIN2
#define INPUT_PIN2
Definition
main.cpp:11
OUT_PIN
#define OUT_PIN
Definition
main.cpp:9
setup_timers
void setup_timers(EventLoop &event_loop)
Definition
main.cpp:35
setup
void setup()
Definition
main.cpp:124
reporter
void reporter()
Definition
main.cpp:21
tick_counter
int tick_counter
Definition
main.cpp:16
event_loop
EventLoop event_loop
Definition
main.cpp:19
timer_ticks
int timer_ticks[NUM_TIMERS]
Definition
main.cpp:17
LED_PIN
#define LED_PIN
Definition
main.cpp:8
execute_task
void execute_task(void *arg)
Definition
main.cpp:109
setup_tick
void setup_tick(EventLoop &event_loop)
Definition
main.cpp:102
setup_io_pins
void setup_io_pins(EventLoop *event_loop)
Definition
main.cpp:51
INPUT_PIN1
#define INPUT_PIN1
Definition
main.cpp:10
loop
void loop()
Definition
main.cpp:146
reactesp
Definition
event_loop.cpp:5
src
main.cpp
Generated by
1.12.0