Skip to content

Commit 9401f23

Browse files
committed
HWCDC: added optimized timedRead and timedPeek
1 parent 2e894c9 commit 9401f23

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Diff for: cores/esp32/HWCDC.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -558,23 +558,23 @@ int HWCDC::available(void) {
558558
return uxQueueMessagesWaiting(rx_queue);
559559
}
560560

561-
int HWCDC::peek(void) {
561+
int HWCDC::_peek(TickType_t timeout) {
562562
if (rx_queue == NULL) {
563563
return -1;
564564
}
565565
uint8_t c;
566-
if (xQueuePeek(rx_queue, &c, 0)) {
566+
if (xQueuePeek(rx_queue, &c, timeout)) {
567567
return c;
568568
}
569569
return -1;
570570
}
571571

572-
int HWCDC::read(void) {
572+
int HWCDC::_read(TickType_t timeout) {
573573
if (rx_queue == NULL) {
574574
return -1;
575575
}
576576
uint8_t c = 0;
577-
if (xQueueReceive(rx_queue, &c, 0)) {
577+
if (xQueueReceive(rx_queue, &c, timeout)) {
578578
return c;
579579
}
580580
return -1;

Diff for: cores/esp32/HWCDC.h

+17-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class HWCDC : public Stream {
4848
static bool deinit(void *busptr);
4949
static bool isCDC_Connected();
5050

51+
protected:
52+
int _peek(TickType_t timeout);
53+
int _read(TickType_t timeout);
54+
55+
virtual int timedRead() override {
56+
return _read(pdMS_TO_TICKS(_timeout));
57+
}
58+
virtual int timedPeek() override {
59+
return _peek(pdMS_TO_TICKS(_timeout));
60+
}
61+
5162
public:
5263
HWCDC();
5364
~HWCDC();
@@ -63,8 +74,12 @@ class HWCDC : public Stream {
6374

6475
int available(void);
6576
int availableForWrite(void);
66-
int peek(void);
67-
int read(void);
77+
int peek(void){
78+
return _peek(0);
79+
}
80+
int read(void){
81+
return _read(0);
82+
}
6883
size_t read(uint8_t *buffer, size_t size);
6984
size_t write(uint8_t);
7085
size_t write(const uint8_t *buffer, size_t size);

0 commit comments

Comments
 (0)