From 0607d36734d52d67be1b88764bf446fac7eb62f3 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" <19971886+dok-net@users.noreply.github.com> Date: Mon, 20 Jan 2020 14:54:50 +0100 Subject: [PATCH] HardwareSerial::write(const char*, ...) API compatibility to AVR, ESP8266, et al (#3585) * API compatibility to AVR, ESP8266, et al * Add non-blocking HardwareSerial::read(buffer, size) extension (ESP8266 portability) * Refactor for fewer indirect calls. --- cores/esp32/HardwareSerial.cpp | 18 ++++++++++++++++++ cores/esp32/HardwareSerial.h | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 860c2c0f..be80ca27 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -131,6 +131,24 @@ int HardwareSerial::read(void) 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 +// the buffer is NOT null terminated. +size_t HardwareSerial::read(uint8_t *buffer, size_t size) +{ + size_t avail = available(); + if (size < avail) { + avail = size; + } + size_t count = 0; + while(count < avail) { + *buffer++ = uartRead(_uart); + count++; + } + return count; +} + void HardwareSerial::flush(void) { uartFlush(_uart); diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 7d9f26d2..82047674 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -62,11 +62,19 @@ public: int availableForWrite(void); int peek(void); int read(void); + size_t read(uint8_t *buffer, size_t size); + inline size_t read(char * buffer, size_t size) + { + return read((uint8_t*) buffer, size); + } void flush(void); void flush( bool txOnly); size_t write(uint8_t); size_t write(const uint8_t *buffer, size_t size); - + inline size_t write(const char * buffer, size_t size) + { + return write((uint8_t*) buffer, size); + } inline size_t write(const char * s) { return write((uint8_t*) s, strlen(s));