Skip to content

Commit 3e567e9

Browse files
Add SerialEvent() callback to loop processing (#7505)
* Add SerialEvent() callback to loop processing Match the AVR SerialEvent implicit callback. Callback is executed in normal user mode, not IRQ, so standard processing can be uses. Fixes #752 after 5 years. :) * Fix style
1 parent 9afb084 commit 3e567e9

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Diff for: cores/esp8266/HardwareSerial.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
#include "HardwareSerial.h"
3333
#include "Esp.h"
3434

35+
36+
// SerialEvent functions are weak, so when the user doesn't define them,
37+
// the linker just sets their address to 0 (which is checked below).
38+
// The Serialx_available is just a wrapper around Serialx.available(),
39+
// but we can refer to it weakly so we don't pull in the entire
40+
// HardwareSerial instance if the user doesn't also refer to it.
41+
void serialEvent() __attribute__((weak));
42+
3543
HardwareSerial::HardwareSerial(int uart_nr)
3644
: _uart_nr(uart_nr), _rx_size(256)
3745
{}
@@ -162,6 +170,14 @@ size_t HardwareSerial::readBytes(char* buffer, size_t size)
162170

163171
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
164172
HardwareSerial Serial(UART0);
173+
174+
// Executed at end of loop() processing when > 0 bytes available in the Serial port
175+
void serialEventRun(void)
176+
{
177+
if (serialEvent && Serial.available()) {
178+
serialEvent();
179+
}
180+
}
165181
#endif
166182
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1)
167183
HardwareSerial Serial1(UART1);

Diff for: cores/esp8266/HardwareSerial.h

+2
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,6 @@ class HardwareSerial: public Stream
207207
extern HardwareSerial Serial;
208208
extern HardwareSerial Serial1;
209209

210+
extern void serialEventRun(void) __attribute__((weak));
211+
210212
#endif

Diff for: cores/esp8266/core_esp8266_main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ static void loop_wrapper() {
195195
}
196196
loop();
197197
loop_end();
198+
if (serialEventRun) {
199+
serialEventRun();
200+
}
198201
esp_schedule();
199202
}
200203

0 commit comments

Comments
 (0)