-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5.10.0c * Consolidate device serial (MH-Z19, SenseAir and Pzem004T) into TasmotaSerial library * Consolidate PWM device recognition * Fix Wemo Emulation (#1357) * Add support for Arilux LC06 (#1414)
- Loading branch information
Showing
39 changed files
with
760 additions
and
821 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# TasmotaSerial | ||
|
||
Implementation of software serial library for the ESP8266 at 9600 baud | ||
|
||
Allows for several instances to be active at the same time. | ||
|
||
Please note that due to the fact that the ESP always have other activities ongoing, there will be some inexactness in interrupt | ||
timings. This may lead to bit errors when having heavy data traffic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
#include <TasmotaSerial.h> | ||
|
||
TasmotaSerial swSer(14, 12); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
swSer.begin(); | ||
|
||
Serial.println("\nTasmota serial test started"); | ||
|
||
for (char ch = ' '; ch <= 'z'; ch++) { | ||
swSer.write(ch); | ||
} | ||
swSer.println(""); | ||
|
||
} | ||
|
||
void loop() { | ||
while (swSer.available() > 0) { | ||
Serial.write(swSer.read()); | ||
} | ||
while (Serial.available() > 0) { | ||
swSer.write(Serial.read()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
####################################### | ||
# Syntax Coloring Map for TasmotaSerial | ||
# (esp8266) | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
TasmotaSerial KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
begin KEYWORD2 | ||
read KEYWORD2 | ||
write KEYWORD2 | ||
available KEYWORD2 | ||
flush KEYWORD2 | ||
peek KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "TasmotaSerial", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"serial", "io", "TasmotaSerial" | ||
], | ||
"description": "Implementation of software serial for ESP8266 at 9600 baud.", | ||
"repository": | ||
{ | ||
"type": "git", | ||
"url": "https://github.com/arendst/Sonoff-Tasmota/lib/TasmotaSerial" | ||
}, | ||
"frameworks": "arduino", | ||
"platforms": "espressif8266" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=TasmotaSerial | ||
version=1.0 | ||
author=Theo Arends | ||
maintainer=Theo Arends <theo@arends.com> | ||
sentence=Implementation of software serial for ESP8266 at 9600 baud. | ||
paragraph= | ||
category=Signal Input/Output | ||
url= | ||
architectures=esp8266 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
TasmotaSerial.cpp - Minimal implementation of software serial for Tasmota | ||
Copyright (C) 2018 Theo Arends | ||
This library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <Arduino.h> | ||
|
||
// The Arduino standard GPIO routines are not enough, | ||
// must use some from the Espressif SDK as well | ||
extern "C" { | ||
#include "gpio.h" | ||
} | ||
|
||
#include <TasmotaSerial.h> | ||
|
||
// As the Arduino attachInterrupt has no parameter, lists of objects | ||
// and callbacks corresponding to each possible GPIO pins have to be defined | ||
TasmotaSerial *ObjList[16]; | ||
|
||
#ifdef TM_SERIAL_USE_IRAM | ||
void ICACHE_RAM_ATTR sws_isr_0() { ObjList[0]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_1() { ObjList[1]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_2() { ObjList[2]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_3() { ObjList[3]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_4() { ObjList[4]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_5() { ObjList[5]->rxRead(); }; | ||
// Pin 6 to 11 can not be used | ||
void ICACHE_RAM_ATTR sws_isr_12() { ObjList[12]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_13() { ObjList[13]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_14() { ObjList[14]->rxRead(); }; | ||
void ICACHE_RAM_ATTR sws_isr_15() { ObjList[15]->rxRead(); }; | ||
#else | ||
void sws_isr_0() { ObjList[0]->rxRead(); }; | ||
void sws_isr_1() { ObjList[1]->rxRead(); }; | ||
void sws_isr_2() { ObjList[2]->rxRead(); }; | ||
void sws_isr_3() { ObjList[3]->rxRead(); }; | ||
void sws_isr_4() { ObjList[4]->rxRead(); }; | ||
void sws_isr_5() { ObjList[5]->rxRead(); }; | ||
// Pin 6 to 11 can not be used | ||
void sws_isr_12() { ObjList[12]->rxRead(); }; | ||
void sws_isr_13() { ObjList[13]->rxRead(); }; | ||
void sws_isr_14() { ObjList[14]->rxRead(); }; | ||
void sws_isr_15() { ObjList[15]->rxRead(); }; | ||
#endif // TM_SERIAL_USE_IRAM | ||
|
||
static void (*ISRList[16])() = { | ||
sws_isr_0, | ||
sws_isr_1, | ||
sws_isr_2, | ||
sws_isr_3, | ||
sws_isr_4, | ||
sws_isr_5, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
sws_isr_12, | ||
sws_isr_13, | ||
sws_isr_14, | ||
sws_isr_15 | ||
}; | ||
|
||
TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin) | ||
{ | ||
m_valid = false; | ||
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); | ||
} | ||
|
||
bool TasmotaSerial::isValidGPIOpin(int pin) | ||
{ | ||
return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 15); | ||
} | ||
|
||
bool TasmotaSerial::begin() { | ||
return m_valid; | ||
} | ||
|
||
int TasmotaSerial::read() | ||
{ | ||
if (m_in_pos == m_out_pos) { | ||
return -1; | ||
} | ||
uint8_t ch = m_buffer[m_out_pos]; | ||
m_out_pos = (m_out_pos +1) % TM_SERIAL_BUFFER_SIZE; | ||
return ch; | ||
} | ||
|
||
int TasmotaSerial::available() | ||
{ | ||
int avail = m_in_pos - m_out_pos; | ||
if (avail < 0) { | ||
avail += TM_SERIAL_BUFFER_SIZE; | ||
} | ||
return avail; | ||
} | ||
|
||
//#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) | ||
{ | ||
unsigned long wait = m_bit_time; | ||
digitalWrite(m_tx_pin, HIGH); | ||
unsigned long start = ESP.getCycleCount(); | ||
// Start bit; | ||
digitalWrite(m_tx_pin, LOW); | ||
TM_SERIAL_WAIT; | ||
for (int i = 0; i < 8; i++) { | ||
digitalWrite(m_tx_pin, (b & 1) ? HIGH : LOW); | ||
TM_SERIAL_WAIT; | ||
b >>= 1; | ||
} | ||
// Stop bit | ||
digitalWrite(m_tx_pin, HIGH); | ||
TM_SERIAL_WAIT; | ||
return 1; | ||
} | ||
|
||
size_t TasmotaSerial::write(const uint8_t *buffer, size_t size) | ||
{ | ||
size_t n = 0; | ||
// Flush input buffer on every write | ||
m_in_pos = m_out_pos = 0; | ||
while(size--) { | ||
n += txWrite(*buffer++); | ||
} | ||
return n; | ||
} | ||
|
||
#ifdef TM_SERIAL_USE_IRAM | ||
void ICACHE_RAM_ATTR TasmotaSerial::rxRead() | ||
{ | ||
#else | ||
void TasmotaSerial::rxRead() | ||
{ | ||
#endif | ||
// Advance the starting point for the samples but compensate for the | ||
// initial delay which occurs before the interrupt is delivered | ||
unsigned long wait = m_bit_time + m_bit_time/3 - 500; | ||
unsigned long start = ESP.getCycleCount(); | ||
uint8_t rec = 0; | ||
for (int i = 0; i < 8; i++) { | ||
TM_SERIAL_WAIT; | ||
rec >>= 1; | ||
if (digitalRead(m_rx_pin)) { | ||
rec |= 0x80; | ||
} | ||
} | ||
// Stop bit | ||
TM_SERIAL_WAIT; | ||
// Store the received value in the buffer unless we have an overflow | ||
int next = (m_in_pos+1) % TM_SERIAL_BUFFER_SIZE; | ||
if (next != m_out_pos) { | ||
m_buffer[m_in_pos] = rec; | ||
m_in_pos = next; | ||
} | ||
// Must clear this bit in the interrupt register, | ||
// it gets set even when interrupts are disabled | ||
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << m_rx_pin); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
TasmotaSerial.h - Minimal implementation of software serial for Tasmota | ||
Copyright (C) 2018 Theo Arends | ||
This library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef TasmotaSerial_h | ||
#define TasmotaSerial_h | ||
/*********************************************************************************************\ | ||
* TasmotaSerial supports 9600 baud with fixed buffer size of 20 bytes using optional no iram | ||
* | ||
* Based on EspSoftwareSerial v3.3.1 by Peter Lerup (https://github.com/plerup/espsoftwareserial) | ||
\*********************************************************************************************/ | ||
|
||
#define TM_SERIAL_BAUDRATE 9600 | ||
#define TM_SERIAL_BUFFER_SIZE 20 | ||
//#define TM_SERIAL_USE_IRAM // Enable to use iram (+368 bytes) | ||
|
||
class TasmotaSerial { | ||
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(); | ||
|
||
void rxRead(); | ||
|
||
private: | ||
bool isValidGPIOpin(int pin); | ||
size_t txWrite(uint8_t byte); | ||
|
||
// Member variables | ||
bool m_valid; | ||
int m_rx_pin; | ||
int m_tx_pin; | ||
unsigned long m_bit_time; | ||
unsigned int m_in_pos; | ||
unsigned int m_out_pos; | ||
uint8_t *m_buffer; | ||
}; | ||
|
||
#endif // TasmotaSerial_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.