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 software serial with hardware serial fallback library for the ESP8266
Implementation of dual UART hardware serial for the ESP32 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. Allows for several instances to be active at the same time.

View File

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

View File

@ -1,5 +1,5 @@
name=TasmotaSerial name=TasmotaSerial
version=3.1.0 version=3.2.0
author=Theo Arends author=Theo Arends
maintainer=Theo Arends <theo@arends.com> maintainer=Theo Arends <theo@arends.com>
sentence=Implementation of software serial with hardware serial fallback for ESP8266 and ESP32. 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; m_valid = true;
} }
TasmotaSerial::~TasmotaSerial() { TasmotaSerial::~TasmotaSerial(void) {
#ifdef ESP8266 #ifdef ESP8266
if (!m_hardserial) { if (!m_hardserial) {
if (m_rx_pin > -1) { if (m_rx_pin > -1) {
@ -122,20 +122,23 @@ bool TasmotaSerial::begin(long speed, int stop_bits) {
if (m_hardswap) { if (m_hardswap) {
Serial.swap(); Serial.swap();
} }
if (serial_buffer_size > 256) {
Serial.setRxBufferSize(serial_buffer_size);
}
#endif // ESP8266 #endif // ESP8266
#ifdef ESP32 #ifdef ESP32
if (tasmota_serial_index > 0) { // We only support UART1 and UART2 and keep UART0 for debugging if (tasmota_serial_index > 0) { // We only support UART1 and UART2 and keep UART0 for debugging
m_uart = tasmota_serial_index; m_uart = tasmota_serial_index;
tasmota_serial_index--; tasmota_serial_index--;
TSerial = new HardwareSerial(m_uart); TSerial = new HardwareSerial(m_uart);
if (serial_buffer_size > 256) {
TSerial->setRxBufferSize(serial_buffer_size);
}
if (2 == m_stop_bits) { if (2 == m_stop_bits) {
TSerial->begin(speed, SERIAL_8N2, m_rx_pin, m_tx_pin); TSerial->begin(speed, SERIAL_8N2, m_rx_pin, m_tx_pin);
} else { } else {
TSerial->begin(speed, SERIAL_8N1, m_rx_pin, m_tx_pin); TSerial->begin(speed, SERIAL_8N1, m_rx_pin, m_tx_pin);
} }
if (serial_buffer_size > 256) {
TSerial->setRxBufferSize(serial_buffer_size);
}
} else { } else {
m_valid = false; m_valid = false;
} }
@ -151,11 +154,11 @@ bool TasmotaSerial::begin(long speed, int stop_bits) {
return m_valid; return m_valid;
} }
bool TasmotaSerial::begin() { bool TasmotaSerial::begin(void) {
return begin(TM_SERIAL_BAUDRATE); return begin(TM_SERIAL_BAUDRATE);
} }
bool TasmotaSerial::hardwareSerial() { bool TasmotaSerial::hardwareSerial(void) {
#ifdef ESP8266 #ifdef ESP8266
return m_hardserial; return m_hardserial;
#endif // ESP8266 #endif // ESP8266
@ -164,7 +167,7 @@ bool TasmotaSerial::hardwareSerial() {
#endif // ESP32 #endif // ESP32
} }
void TasmotaSerial::flush() { void TasmotaSerial::flush(void) {
if (m_hardserial) { if (m_hardserial) {
#ifdef ESP8266 #ifdef ESP8266
Serial.flush(); Serial.flush();
@ -177,7 +180,7 @@ void TasmotaSerial::flush() {
} }
} }
int TasmotaSerial::peek() { int TasmotaSerial::peek(void) {
if (m_hardserial) { if (m_hardserial) {
#ifdef ESP8266 #ifdef ESP8266
return Serial.peek(); return Serial.peek();
@ -191,7 +194,7 @@ int TasmotaSerial::peek() {
} }
} }
int TasmotaSerial::read() { int TasmotaSerial::read(void) {
if (m_hardserial) { if (m_hardserial) {
#ifdef ESP8266 #ifdef ESP8266
return Serial.read(); 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) { if (m_hardserial) {
#ifdef ESP8266 #ifdef ESP8266
return Serial.available(); 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) { if (!m_nwmode) {
int32_t loop_read = m_very_high_speed ? serial_buffer_size : 1; int32_t loop_read = m_very_high_speed ? serial_buffer_size : 1;
// Advance the starting point for the samples but compensate for the // Advance the starting point for the samples but compensate for the

View File

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