Skip to content

Commit 2e894c9

Browse files
committed
make TimedRead and TimedPeek virtual, added new Hardare Serial HAL abstractions
1 parent bd4b325 commit 2e894c9

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,16 @@ int HardwareSerial::peek(void) {
527527
return -1;
528528
}
529529

530-
int HardwareSerial::read(void) {
530+
int HardwareSerial::_read(uint32_t timeout) {
531531
uint8_t c = 0;
532-
if (uartReadBytes(_uart, &c, 1, 0) == 1) {
532+
if (uartReadBytes(_uart, &c, 1, timeout) == 1) {
533533
return c;
534534
} else {
535535
return -1;
536536
}
537537
}
538538

539+
539540
// read characters into buffer
540541
// terminates if size characters have been read, or no further are pending
541542
// returns the number of characters placed in the buffer
@@ -662,3 +663,13 @@ size_t HardwareSerial::setTxBufferSize(size_t new_size) {
662663
_txBufferSize = new_size;
663664
return new_size;
664665
}
666+
667+
int HardwareSerial::timedPeek(void) {
668+
669+
uint8_t out;
670+
if(uartTimedPeek(_uart, &out, _timeout)){
671+
return out;
672+
}
673+
674+
return -1;
675+
}

Diff for: cores/esp32/HardwareSerial.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ class HardwareSerial : public Stream {
311311
int available(void);
312312
int availableForWrite(void);
313313
int peek(void);
314-
int read(void);
314+
int read(void){
315+
return _read(0);
316+
}
315317
size_t read(uint8_t *buffer, size_t size);
316318
inline size_t read(char *buffer, size_t size) {
317319
return read((uint8_t *)buffer, size);
@@ -399,6 +401,14 @@ class HardwareSerial : public Stream {
399401
void _createEventTask(void *args);
400402
void _destroyEventTask(void);
401403
static void _uartEventTask(void *args);
404+
405+
int _read(uint32_t timeout);
406+
407+
virtual int timedRead() override {
408+
return _read(_timeout);
409+
}
410+
411+
virtual int timedPeek() override;
402412
};
403413

404414
extern void serialEventRun(void) __attribute__((weak));

Diff for: cores/esp32/Stream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class Stream : public Print {
4949
protected:
5050
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
5151
unsigned long _startMillis; // used for timeout measurement
52-
int timedRead(); // private method to read stream with timeout
53-
int timedPeek(); // private method to peek stream with timeout
52+
virtual int timedRead(); // private method to read stream with timeout
53+
virtual int timedPeek(); // private method to peek stream with timeout
5454
int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
5555

5656
public:

Diff for: cores/esp32/esp32-hal-uart.c

+28
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,34 @@ uint8_t uartPeek(uart_t *uart) {
952952
return c;
953953
}
954954

955+
bool uartTimedPeek(uart_t *uart, uint8_t * out, uint32_t timeout_ms) {
956+
if (uart == NULL || out == NULL) {
957+
return false;
958+
}
959+
960+
bool valid = false;
961+
962+
UART_MUTEX_LOCK();
963+
964+
if (uart->has_peek) {
965+
*out = uart->peek_byte;
966+
valid = true;
967+
} else {
968+
uint8_t c = 0;
969+
int len = uart_read_bytes(uart->num, &c, 1, pdMS_TO_TICKS(timeout_ms));
970+
if (len > 0) {
971+
uart->has_peek = true;
972+
uart->peek_byte = c;
973+
*out = c;
974+
valid = true;
975+
}
976+
}
977+
978+
UART_MUTEX_UNLOCK();
979+
980+
return valid;
981+
}
982+
955983
void uartWrite(uart_t *uart, uint8_t c) {
956984
if (uart == NULL) {
957985
return;

Diff for: cores/esp32/esp32-hal-uart.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ uint32_t uartAvailableForWrite(uart_t *uart);
5151
size_t uartReadBytes(uart_t *uart, uint8_t *buffer, size_t size, uint32_t timeout_ms);
5252
uint8_t uartRead(uart_t *uart);
5353
uint8_t uartPeek(uart_t *uart);
54+
bool uartTimedPeek(uart_t *uart, uint8_t * out, uint32_t timeout_ms);
5455

5556
void uartWrite(uart_t *uart, uint8_t c);
5657
void uartWriteBuf(uart_t *uart, const uint8_t *data, size_t len);

0 commit comments

Comments
 (0)