Skip to content

Commit d5de62c

Browse files
authored
Adds onReceive() parameter for timeout only
1 parent 807d750 commit d5de62c

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

Diff for: cores/esp32/HardwareSerial.h

+11-15
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef enum {
6464
UART_PARITY_ERROR
6565
} hardwareSerial_error_t;
6666

67-
typedef std::function<void(bool)> OnReceiveCb;
67+
typedef std::function<void()> OnReceiveCb;
6868
typedef std::function<void(hardwareSerial_error_t)> OnReceiveErrorCb;
6969

7070
class HardwareSerial: public Stream
@@ -76,23 +76,19 @@ class HardwareSerial: public Stream
7676
// onReceive will setup a callback that will be called whenever an UART interruption occurs (UART_INTR_RXFIFO_FULL or UART_INTR_RXFIFO_TOUT)
7777
// UART_INTR_RXFIFO_FULL interrupt triggers at UART_FULL_THRESH_DEFAULT bytes received (defined as 120 bytes by default in IDF)
7878
// UART_INTR_RXFIFO_TOUT interrupt triggers at UART_TOUT_THRESH_DEFAULT symbols passed without any reception (defined as 10 symbos by default in IDF)
79-
// The prototype of the callback function passed must be as defined by OnReceiveCb (void callbackFunction(bool timeout))
80-
// The bool parameter in the callback function prototype informs the callback function if the callback was called because a timeout (true) or because the RX FIFO reached 120 bytes (false)
81-
// param function is the callback to be called after reception timeout
82-
void onReceive(OnReceiveCb function);
83-
84-
// setRxTimeout sets the timeout after which onReceive callback will be called (after receiving data, it waits for this time of UART rx inactivity to call the callback fnc)
85-
// param symbols_timeout defines a timeout threshold in uart symbol periods. Setting 0 symbol timeout disables the callback call by timeout.
86-
// Maximum timeout setting is calculacted automatically by IDF. If set above the maximum, it is ignored and an error is printed on Serial0 (check console).
87-
// Examples: Maximum for 11 bits symbol is 92 (SERIAL_8N2, SERIAL_8E1, SERIAL_8O1, etc), Maximum for 10 bits symbol is 101 (SERIAL_8N1).
88-
// For example symbols_timeout=1 defines a timeout equal to transmission time of one symbol (~11 bit) on current baudrate.
89-
// For a baudrate of 9600, SERIAL_8N1 (10 bit symbol) and symbols_timeout = 3, the timeout would be 3 / (9600 / 10) = 3.125 ms
90-
void setRxTimeout(uint8_t symbols_timeout);
79+
// onlyOnTimeout parameter will define how onReceive will behave:
80+
// Default: true -- The callback will only be called when RX Timeout happens.
81+
// Whole stream of bytes will be ready for being read on the callback function at once.
82+
// This option may lead to Rx Overflow depending on the Rx Buffer Size and number of bytes received in the streaming
83+
// false -- The callback will be called when FIFO reaches 120 bytes and also on RX Timeout.
84+
// The stream of incommig bytes will be "split" into blocks of 120 bytes on each callback.
85+
// This option avoid any sort of Rx Overflow, but leaves the UART packet reassembling work to the Application.
86+
void onReceive(OnReceiveCb function, bool onlyOnTimeout = true);
9187

9288
// onReceive will be called on error events (see hardwareSerial_error_t)
9389
void onReceiveError(OnReceiveErrorCb function);
9490

95-
// eventQueueReset clears all events in the queue (the events that trigger onReceive and onReceiveError)
91+
// eventQueueReset clears all events in the queue (the events that trigger onReceive and onReceiveError) - maybe usefull in some use cases
9692
void eventQueueReset();
9793

9894
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
@@ -155,7 +151,7 @@ class HardwareSerial: public Stream
155151
uart_t* _uart;
156152
size_t _rxBufferSize;
157153
OnReceiveCb _onReceiveCB;
158-
uint8_t _rxTimeout;
154+
bool _onReceiveTimeout;
159155
OnReceiveErrorCb _onReceiveErrorCB;
160156
TaskHandle_t _eventTask;
161157

0 commit comments

Comments
 (0)