Add read buffer function to TasmotaSerial

This commit is contained in:
Theo Arends 2021-02-06 18:09:45 +01:00
parent d198183241
commit ef10b5ad16
7 changed files with 45 additions and 21 deletions

View File

@ -2,6 +2,7 @@
Implementation of software serial with hardware serial fallback library for the ESP8266
Implementation of dual UART hardware serial for the ESP32
Implementation of single UART hardware serial for the ESP32-S2
Allows for several instances to be active at the same time.

View File

@ -1,6 +1,6 @@
{
"name": "TasmotaSerial",
"version": "3.1.0",
"version": "3.2.0",
"keywords": [
"serial", "io", "TasmotaSerial"
],

View File

@ -1,5 +1,5 @@
name=TasmotaSerial
version=3.1.0
version=3.2.0
author=Theo Arends
maintainer=Theo Arends <theo@arends.com>
sentence=Implementation of software serial with hardware serial fallback for ESP8266 and ESP32.

View File

@ -91,7 +91,7 @@ TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fal
m_valid = true;
}
TasmotaSerial::~TasmotaSerial() {
TasmotaSerial::~TasmotaSerial(void) {
#ifdef ESP8266
if (!m_hardserial) {
if (m_rx_pin > -1) {
@ -122,20 +122,23 @@ bool TasmotaSerial::begin(long speed, int stop_bits) {
if (m_hardswap) {
Serial.swap();
}
if (serial_buffer_size > 256) {
Serial.setRxBufferSize(serial_buffer_size);
}
#endif // ESP8266
#ifdef ESP32
if (tasmota_serial_index > 0) { // We only support UART1 and UART2 and keep UART0 for debugging
m_uart = tasmota_serial_index;
tasmota_serial_index--;
TSerial = new HardwareSerial(m_uart);
if (serial_buffer_size > 256) {
TSerial->setRxBufferSize(serial_buffer_size);
}
if (2 == m_stop_bits) {
TSerial->begin(speed, SERIAL_8N2, m_rx_pin, m_tx_pin);
} else {
TSerial->begin(speed, SERIAL_8N1, m_rx_pin, m_tx_pin);
}
if (serial_buffer_size > 256) {
TSerial->setRxBufferSize(serial_buffer_size);
}
} else {
m_valid = false;
}
@ -151,11 +154,11 @@ bool TasmotaSerial::begin(long speed, int stop_bits) {
return m_valid;
}
bool TasmotaSerial::begin() {
bool TasmotaSerial::begin(void) {
return begin(TM_SERIAL_BAUDRATE);
}
bool TasmotaSerial::hardwareSerial() {
bool TasmotaSerial::hardwareSerial(void) {
#ifdef ESP8266
return m_hardserial;
#endif // ESP8266
@ -164,7 +167,7 @@ bool TasmotaSerial::hardwareSerial() {
#endif // ESP32
}
void TasmotaSerial::flush() {
void TasmotaSerial::flush(void) {
if (m_hardserial) {
#ifdef ESP8266
Serial.flush();
@ -177,7 +180,7 @@ void TasmotaSerial::flush() {
}
}
int TasmotaSerial::peek() {
int TasmotaSerial::peek(void) {
if (m_hardserial) {
#ifdef ESP8266
return Serial.peek();
@ -191,7 +194,7 @@ int TasmotaSerial::peek() {
}
}
int TasmotaSerial::read() {
int TasmotaSerial::read(void) {
if (m_hardserial) {
#ifdef ESP8266
return Serial.read();
@ -207,7 +210,26 @@ int TasmotaSerial::read() {
}
}
int TasmotaSerial::available() {
size_t TasmotaSerial::read(char* buffer, size_t size) {
if (m_hardserial) {
#ifdef ESP8266
return Serial.read(buffer, size);
#endif // ESP8266
#ifdef ESP32
return TSerial->read(buffer, size);
#endif // ESP32
} else {
if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) { return 0; }
size_t count = 0;
for( ; size && (m_in_pos == m_out_pos) ; --size, ++count) {
*buffer++ = m_buffer[m_out_pos];
m_out_pos = (m_out_pos +1) % serial_buffer_size;
}
return count;
}
}
int TasmotaSerial::available(void) {
if (m_hardserial) {
#ifdef ESP8266
return Serial.available();
@ -283,7 +305,7 @@ size_t TasmotaSerial::write(uint8_t b) {
}
}
void ICACHE_RAM_ATTR TasmotaSerial::rxRead() {
void ICACHE_RAM_ATTR TasmotaSerial::rxRead(void) {
if (!m_nwmode) {
int32_t loop_read = m_very_high_speed ? serial_buffer_size : 1;
// Advance the starting point for the samples but compensate for the

View File

@ -41,16 +41,17 @@ class TasmotaSerial : public Stream {
virtual ~TasmotaSerial();
bool begin(long speed, int stop_bits = 1);
bool begin();
bool hardwareSerial();
int peek();
bool begin(void);
bool hardwareSerial(void);
int peek(void);
virtual size_t write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();
size_t write(uint8_t byte) override;
int read(void) override;
size_t read(char* buffer, size_t size);
int available(void) override;
void flush(void) override;
void rxRead();
void rxRead(void);
uint32_t getLoopReadMetric(void) const { return m_bit_follow_metric; }