Skip to content

RFQ: make TimedRead and TimedPeek virtual #11236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cores/esp32/HWCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions cores/esp32/HWCDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
12 changes: 11 additions & 1 deletion cores/esp32/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions cores/esp32/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/esp32-hal-uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading