From 050191671331bd9408489087d0fdcba586dcda7f Mon Sep 17 00:00:00 2001 From: arendst Date: Wed, 10 Jan 2018 15:24:12 +0100 Subject: [PATCH] Update TasmotaSerial to 1.0.1 Update TasmotaSerial to 1.0.1 --- .../README.md | 0 .../examples/swsertest/swsertest.ino | 0 .../keywords.txt | 0 .../library.json | 0 .../library.properties | 0 .../src/TasmotaSerial.cpp | 53 +++++++++++++------ .../src/TasmotaSerial.h | 21 ++++++-- 7 files changed, 54 insertions(+), 20 deletions(-) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/README.md (100%) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/examples/swsertest/swsertest.ino (100%) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/keywords.txt (100%) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/library.json (100%) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/library.properties (100%) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/src/TasmotaSerial.cpp (84%) rename lib/{TasmotaSerial-1.0.0 => TasmotaSerial-1.0.1}/src/TasmotaSerial.h (82%) diff --git a/lib/TasmotaSerial-1.0.0/README.md b/lib/TasmotaSerial-1.0.1/README.md similarity index 100% rename from lib/TasmotaSerial-1.0.0/README.md rename to lib/TasmotaSerial-1.0.1/README.md diff --git a/lib/TasmotaSerial-1.0.0/examples/swsertest/swsertest.ino b/lib/TasmotaSerial-1.0.1/examples/swsertest/swsertest.ino similarity index 100% rename from lib/TasmotaSerial-1.0.0/examples/swsertest/swsertest.ino rename to lib/TasmotaSerial-1.0.1/examples/swsertest/swsertest.ino diff --git a/lib/TasmotaSerial-1.0.0/keywords.txt b/lib/TasmotaSerial-1.0.1/keywords.txt similarity index 100% rename from lib/TasmotaSerial-1.0.0/keywords.txt rename to lib/TasmotaSerial-1.0.1/keywords.txt diff --git a/lib/TasmotaSerial-1.0.0/library.json b/lib/TasmotaSerial-1.0.1/library.json similarity index 100% rename from lib/TasmotaSerial-1.0.0/library.json rename to lib/TasmotaSerial-1.0.1/library.json diff --git a/lib/TasmotaSerial-1.0.0/library.properties b/lib/TasmotaSerial-1.0.1/library.properties similarity index 100% rename from lib/TasmotaSerial-1.0.0/library.properties rename to lib/TasmotaSerial-1.0.1/library.properties diff --git a/lib/TasmotaSerial-1.0.0/src/TasmotaSerial.cpp b/lib/TasmotaSerial-1.0.1/src/TasmotaSerial.cpp similarity index 84% rename from lib/TasmotaSerial-1.0.0/src/TasmotaSerial.cpp rename to lib/TasmotaSerial-1.0.1/src/TasmotaSerial.cpp index 1b55818e0..5508a478a 100644 --- a/lib/TasmotaSerial-1.0.0/src/TasmotaSerial.cpp +++ b/lib/TasmotaSerial-1.0.1/src/TasmotaSerial.cpp @@ -82,35 +82,50 @@ TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin) if (!((isValidGPIOpin(receive_pin)) && (isValidGPIOpin(transmit_pin) || transmit_pin == 16))) { return; } - m_buffer = (uint8_t*)malloc(TM_SERIAL_BUFFER_SIZE); - if (m_buffer == NULL) { - return; - } - m_valid = true; m_rx_pin = receive_pin; m_tx_pin = transmit_pin; m_in_pos = m_out_pos = 0; - // Use getCycleCount() loop to get as exact timing as possible - m_bit_time = ESP.getCpuFreqMHz() *1000000 /TM_SERIAL_BAUDRATE; - pinMode(m_rx_pin, INPUT); - ObjList[m_rx_pin] = this; - attachInterrupt(m_rx_pin, ISRList[m_rx_pin], FALLING); - pinMode(m_tx_pin, OUTPUT); - digitalWrite(m_tx_pin, HIGH); + if (m_rx_pin > -1) { + m_buffer = (uint8_t*)malloc(TM_SERIAL_BUFFER_SIZE); + if (m_buffer == NULL) { + return; + } + // Use getCycleCount() loop to get as exact timing as possible + m_bit_time = ESP.getCpuFreqMHz() *1000000 /TM_SERIAL_BAUDRATE; + pinMode(m_rx_pin, INPUT); + ObjList[m_rx_pin] = this; + attachInterrupt(m_rx_pin, ISRList[m_rx_pin], FALLING); + } + if (m_tx_pin > -1) { + pinMode(m_tx_pin, OUTPUT); + digitalWrite(m_tx_pin, HIGH); + } + m_valid = true; } bool TasmotaSerial::isValidGPIOpin(int pin) { - return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 15); + return (pin >= -1 && pin <= 5) || (pin >= 12 && pin <= 15); } bool TasmotaSerial::begin() { return m_valid; } +void TasmotaSerial::flush() { + m_in_pos = m_out_pos = 0; +} + +int TasmotaSerial::peek() { + if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) { + return -1; + } + return m_buffer[m_out_pos]; +} + int TasmotaSerial::read() { - if (m_in_pos == m_out_pos) { + if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) { return -1; } uint8_t ch = m_buffer[m_out_pos]; @@ -130,8 +145,11 @@ int TasmotaSerial::available() //#define TM_SERIAL_WAIT { while (ESP.getCycleCount()-start < wait) optimistic_yield(1); wait += m_bit_time; } // Watchdog timeouts #define TM_SERIAL_WAIT { while (ESP.getCycleCount()-start < wait); wait += m_bit_time; } -size_t TasmotaSerial::txWrite(uint8_t b) +size_t TasmotaSerial::write(uint8_t b) { + if (-1 == m_tx_pin) { + return 0; + } unsigned long wait = m_bit_time; digitalWrite(m_tx_pin, HIGH); unsigned long start = ESP.getCycleCount(); @@ -149,8 +167,12 @@ size_t TasmotaSerial::txWrite(uint8_t b) return 1; } +/* size_t TasmotaSerial::write(const uint8_t *buffer, size_t size) { + if (-1 == m_tx_pin) { + return 0; + } size_t n = 0; // Flush input buffer on every write m_in_pos = m_out_pos = 0; @@ -159,6 +181,7 @@ size_t TasmotaSerial::write(const uint8_t *buffer, size_t size) } return n; } +*/ #ifdef TM_SERIAL_USE_IRAM void ICACHE_RAM_ATTR TasmotaSerial::rxRead() diff --git a/lib/TasmotaSerial-1.0.0/src/TasmotaSerial.h b/lib/TasmotaSerial-1.0.1/src/TasmotaSerial.h similarity index 82% rename from lib/TasmotaSerial-1.0.0/src/TasmotaSerial.h rename to lib/TasmotaSerial-1.0.1/src/TasmotaSerial.h index beada7792..a56dcbe2e 100644 --- a/lib/TasmotaSerial-1.0.0/src/TasmotaSerial.h +++ b/lib/TasmotaSerial-1.0.1/src/TasmotaSerial.h @@ -26,19 +26,30 @@ \*********************************************************************************************/ #define TM_SERIAL_BAUDRATE 9600 -#define TM_SERIAL_BUFFER_SIZE 20 +#define TM_SERIAL_BUFFER_SIZE 64 //#define TM_SERIAL_USE_IRAM // Enable to use iram (+368 bytes) -class TasmotaSerial { +#include + +class TasmotaSerial : public Stream { public: TasmotaSerial(int receive_pin, int transmit_pin); bool begin(); - size_t write(const uint8_t *buffer, size_t size = 1); - int read(); - int available(); + int peek(); + + virtual size_t write(uint8_t byte); + virtual int read(); + virtual int available(); + virtual void flush(); + +// size_t write(const uint8_t *buffer, size_t size = 1); +// int read(); +// int available(); void rxRead(); + using Print::write; + private: bool isValidGPIOpin(int pin); size_t txWrite(uint8_t byte);