diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 170e323a035..75941323f12 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -558,23 +558,23 @@ int HWCDC::available(void) { return uxQueueMessagesWaiting(rx_queue); } -int HWCDC::peek(void) { +int HWCDC::_peek(TickType_t timeout) { if (rx_queue == NULL) { return -1; } uint8_t c; - if (xQueuePeek(rx_queue, &c, 0)) { + if (xQueuePeek(rx_queue, &c, timeout)) { return c; } return -1; } -int HWCDC::read(void) { +int HWCDC::_read(TickType_t timeout) { if (rx_queue == NULL) { return -1; } uint8_t c = 0; - if (xQueueReceive(rx_queue, &c, 0)) { + if (xQueueReceive(rx_queue, &c, timeout)) { return c; } return -1; diff --git a/cores/esp32/HWCDC.h b/cores/esp32/HWCDC.h index 29caae34062..0bffc4cddc7 100644 --- a/cores/esp32/HWCDC.h +++ b/cores/esp32/HWCDC.h @@ -48,6 +48,17 @@ class HWCDC : public Stream { static bool deinit(void *busptr); static bool isCDC_Connected(); +protected: + int _peek(TickType_t timeout); + int _read(TickType_t timeout); + + virtual int timedRead() override { + return _read(pdMS_TO_TICKS(_timeout)); + } + virtual int timedPeek() override { + return _peek(pdMS_TO_TICKS(_timeout)); + } + public: HWCDC(); ~HWCDC(); @@ -63,8 +74,12 @@ class HWCDC : public Stream { int available(void); int availableForWrite(void); - int peek(void); - int read(void); + int peek(void){ + return _peek(0); + } + int read(void){ + return _read(0); + } size_t read(uint8_t *buffer, size_t size); size_t write(uint8_t); size_t write(const uint8_t *buffer, size_t size); diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 6d762da21fb..e6581e3a83b 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -527,15 +527,16 @@ int HardwareSerial::peek(void) { return -1; } -int HardwareSerial::read(void) { +int HardwareSerial::_read(uint32_t timeout) { uint8_t c = 0; - if (uartReadBytes(_uart, &c, 1, 0) == 1) { + if (uartReadBytes(_uart, &c, 1, timeout) == 1) { return c; } else { return -1; } } + // read characters into buffer // terminates if size characters have been read, or no further are pending // returns the number of characters placed in the buffer @@ -662,3 +663,13 @@ size_t HardwareSerial::setTxBufferSize(size_t new_size) { _txBufferSize = new_size; return new_size; } + +int HardwareSerial::timedPeek(void) { + + uint8_t out; + if(uartTimedPeek(_uart, &out, _timeout)){ + return out; + } + + return -1; +} \ No newline at end of file diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index e974f112701..886683703ef 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -311,7 +311,9 @@ class HardwareSerial : public Stream { int available(void); int availableForWrite(void); int peek(void); - int read(void); + int read(void){ + return _read(0); + } size_t read(uint8_t *buffer, size_t size); inline size_t read(char *buffer, size_t size) { return read((uint8_t *)buffer, size); @@ -399,6 +401,14 @@ class HardwareSerial : public Stream { void _createEventTask(void *args); void _destroyEventTask(void); static void _uartEventTask(void *args); + + int _read(uint32_t timeout); + + virtual int timedRead() override { + return _read(_timeout); + } + + virtual int timedPeek() override; }; extern void serialEventRun(void) __attribute__((weak)); diff --git a/cores/esp32/Stream.h b/cores/esp32/Stream.h index 37346cdb99f..edaae732e93 100644 --- a/cores/esp32/Stream.h +++ b/cores/esp32/Stream.h @@ -49,8 +49,8 @@ class Stream : public Print { protected: unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read unsigned long _startMillis; // used for timeout measurement - int timedRead(); // private method to read stream with timeout - int timedPeek(); // private method to peek stream with timeout + virtual int timedRead(); // private method to read stream with timeout + virtual int timedPeek(); // private method to peek stream with timeout int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout public: diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index d2e98a2341a..947bbb32a52 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -952,6 +952,34 @@ uint8_t uartPeek(uart_t *uart) { return c; } +bool uartTimedPeek(uart_t *uart, uint8_t * out, uint32_t timeout_ms) { + if (uart == NULL || out == NULL) { + return false; + } + + bool valid = false; + + UART_MUTEX_LOCK(); + + if (uart->has_peek) { + *out = uart->peek_byte; + valid = true; + } else { + uint8_t c = 0; + int len = uart_read_bytes(uart->num, &c, 1, pdMS_TO_TICKS(timeout_ms)); + if (len > 0) { + uart->has_peek = true; + uart->peek_byte = c; + *out = c; + valid = true; + } + } + + UART_MUTEX_UNLOCK(); + + return valid; +} + void uartWrite(uart_t *uart, uint8_t c) { if (uart == NULL) { return; diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 41b005aa151..63cf809fe49 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -51,6 +51,7 @@ uint32_t uartAvailableForWrite(uart_t *uart); size_t uartReadBytes(uart_t *uart, uint8_t *buffer, size_t size, uint32_t timeout_ms); uint8_t uartRead(uart_t *uart); uint8_t uartPeek(uart_t *uart); +bool uartTimedPeek(uart_t *uart, uint8_t * out, uint32_t timeout_ms); void uartWrite(uart_t *uart, uint8_t c); void uartWriteBuf(uart_t *uart, const uint8_t *data, size_t len);