Skip to content

Commit 0607d36

Browse files
dok-netme-no-dev
authored andcommittedJan 20, 2020
HardwareSerial::write(const char*, ...) API compatibility to AVR, ESP8266, et al (espressif#3585)
* API compatibility to AVR, ESP8266, et al * Add non-blocking HardwareSerial::read(buffer, size) extension (ESP8266 portability) * Refactor for fewer indirect calls.
1 parent 6e77f7f commit 0607d36

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
 

‎cores/esp32/HardwareSerial.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ int HardwareSerial::read(void)
131131
return -1;
132132
}
133133

134+
// read characters into buffer
135+
// terminates if size characters have been read, or no further are pending
136+
// returns the number of characters placed in the buffer
137+
// the buffer is NOT null terminated.
138+
size_t HardwareSerial::read(uint8_t *buffer, size_t size)
139+
{
140+
size_t avail = available();
141+
if (size < avail) {
142+
avail = size;
143+
}
144+
size_t count = 0;
145+
while(count < avail) {
146+
*buffer++ = uartRead(_uart);
147+
count++;
148+
}
149+
return count;
150+
}
151+
134152
void HardwareSerial::flush(void)
135153
{
136154
uartFlush(_uart);

‎cores/esp32/HardwareSerial.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ class HardwareSerial: public Stream
6262
int availableForWrite(void);
6363
int peek(void);
6464
int read(void);
65+
size_t read(uint8_t *buffer, size_t size);
66+
inline size_t read(char * buffer, size_t size)
67+
{
68+
return read((uint8_t*) buffer, size);
69+
}
6570
void flush(void);
6671
void flush( bool txOnly);
6772
size_t write(uint8_t);
6873
size_t write(const uint8_t *buffer, size_t size);
69-
74+
inline size_t write(const char * buffer, size_t size)
75+
{
76+
return write((uint8_t*) buffer, size);
77+
}
7078
inline size_t write(const char * s)
7179
{
7280
return write((uint8_t*) s, strlen(s));

0 commit comments

Comments
 (0)
Please sign in to comment.