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