2018-03-20 13:31:11 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdrv_08_serial_bridge.ino - serial bridge support for Tasmota
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends and Dániel Zoltán Tolnai
|
2018-03-20 13:31:11 +00:00
|
|
|
|
|
|
|
This program 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_SERIAL_BRIDGE
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Serial Bridge using Software Serial library (TasmotaSerial)
|
|
|
|
\*********************************************************************************************/
|
2018-11-07 09:30:03 +00:00
|
|
|
|
|
|
|
#define XDRV_08 8
|
|
|
|
|
2019-03-31 10:59:04 +01:00
|
|
|
const uint8_t SERIAL_BRIDGE_BUFFER_SIZE = 130;
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2019-08-11 17:12:18 +01:00
|
|
|
const char kSerialBridgeCommands[] PROGMEM = "|" // No prefix
|
|
|
|
D_CMND_SSERIALSEND "|" D_CMND_SBAUDRATE;
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2019-08-11 17:12:18 +01:00
|
|
|
void (* const SerialBridgeCommand[])(void) PROGMEM =
|
|
|
|
{ &CmndSSerialSend, &CmndSBaudrate };
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2019-08-11 17:12:18 +01:00
|
|
|
#include <TasmotaSerial.h>
|
2019-08-01 16:33:44 +01:00
|
|
|
|
2019-03-26 17:26:50 +00:00
|
|
|
TasmotaSerial *SerialBridgeSerial = nullptr;
|
2018-03-20 13:31:11 +00:00
|
|
|
|
|
|
|
unsigned long serial_bridge_polling_window = 0;
|
2019-03-26 17:26:50 +00:00
|
|
|
char *serial_bridge_buffer = nullptr;
|
2019-02-09 15:08:09 +00:00
|
|
|
int serial_bridge_in_byte_counter = 0;
|
|
|
|
bool serial_bridge_active = true;
|
|
|
|
bool serial_bridge_raw = false;
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void SerialBridgeInput(void)
|
2018-03-20 13:31:11 +00:00
|
|
|
{
|
|
|
|
while (SerialBridgeSerial->available()) {
|
|
|
|
yield();
|
2019-02-05 13:13:53 +00:00
|
|
|
uint8_t serial_in_byte = SerialBridgeSerial->read();
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2019-02-09 15:08:09 +00:00
|
|
|
if ((serial_in_byte > 127) && !serial_bridge_raw) { // Discard binary data above 127 if no raw reception allowed
|
2018-03-20 13:31:11 +00:00
|
|
|
serial_bridge_in_byte_counter = 0;
|
|
|
|
SerialBridgeSerial->flush();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-09 15:08:09 +00:00
|
|
|
if (serial_in_byte || serial_bridge_raw) { // Any char between 1 and 127 or any char (0 - 255)
|
|
|
|
|
|
|
|
if ((serial_bridge_in_byte_counter < SERIAL_BRIDGE_BUFFER_SIZE -1) && // Add char to string if it still fits and ...
|
|
|
|
((isprint(serial_in_byte) && (128 == Settings.serial_delimiter)) || // Any char between 32 and 127
|
2019-04-13 14:52:32 +01:00
|
|
|
((serial_in_byte != Settings.serial_delimiter) && (128 != Settings.serial_delimiter)) || // Any char between 1 and 127 and not being delimiter
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_raw)) { // Any char between 0 and 255
|
2018-03-20 13:31:11 +00:00
|
|
|
serial_bridge_buffer[serial_bridge_in_byte_counter++] = serial_in_byte;
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_polling_window = millis(); // Wait for more data
|
2018-03-20 13:31:11 +00:00
|
|
|
} else {
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_polling_window = 0; // Publish now
|
2018-03-20 13:31:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serial_bridge_in_byte_counter && (millis() > (serial_bridge_polling_window + SERIAL_POLLING))) {
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_buffer[serial_bridge_in_byte_counter] = 0; // Serial data completed
|
2019-08-10 16:34:59 +01:00
|
|
|
char hex_char[(serial_bridge_in_byte_counter * 2) + 2];
|
2019-10-21 17:24:03 +01:00
|
|
|
Response_P(PSTR(",\"" D_JSON_SSERIALRECEIVED "\":\"%s\"}"),
|
2019-08-13 18:53:12 +01:00
|
|
|
(serial_bridge_raw) ? ToHex_P((unsigned char*)serial_bridge_buffer, serial_bridge_in_byte_counter, hex_char, sizeof(hex_char)) : serial_bridge_buffer);
|
2018-03-20 13:31:11 +00:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SSERIALRECEIVED));
|
2019-04-09 16:47:38 +01:00
|
|
|
XdrvRulesProcess();
|
2018-03-20 13:31:11 +00:00
|
|
|
serial_bridge_in_byte_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void SerialBridgeInit(void)
|
|
|
|
{
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_active = false;
|
2018-03-20 13:31:11 +00:00
|
|
|
if ((pin[GPIO_SBR_RX] < 99) && (pin[GPIO_SBR_TX] < 99)) {
|
2019-02-09 15:08:09 +00:00
|
|
|
SerialBridgeSerial = new TasmotaSerial(pin[GPIO_SBR_RX], pin[GPIO_SBR_TX]);
|
2019-08-28 11:02:27 +01:00
|
|
|
if (SerialBridgeSerial->begin(Settings.sbaudrate * 300)) { // Baud rate is stored div 300 so it fits into 16 bits
|
2019-02-09 15:08:09 +00:00
|
|
|
if (SerialBridgeSerial->hardwareSerial()) {
|
|
|
|
ClaimSerial();
|
|
|
|
serial_bridge_buffer = serial_in_buffer; // Use idle serial buffer to save RAM
|
|
|
|
} else {
|
|
|
|
serial_bridge_buffer = (char*)(malloc(SERIAL_BRIDGE_BUFFER_SIZE));
|
2018-11-27 11:09:36 +00:00
|
|
|
}
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_active = true;
|
|
|
|
SerialBridgeSerial->flush();
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-08-01 16:33:44 +01:00
|
|
|
void CmndSSerialSend(void)
|
2018-03-20 13:31:11 +00:00
|
|
|
{
|
2019-08-01 16:33:44 +01:00
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) {
|
2019-02-09 15:08:09 +00:00
|
|
|
serial_bridge_raw = (XdrvMailbox.index > 3);
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2018-03-20 13:31:11 +00:00
|
|
|
if (1 == XdrvMailbox.index) {
|
2019-02-09 15:08:09 +00:00
|
|
|
SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); // "Hello Tiger"
|
|
|
|
SerialBridgeSerial->write("\n"); // "\n"
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
2019-02-09 15:08:09 +00:00
|
|
|
else if ((2 == XdrvMailbox.index) || (4 == XdrvMailbox.index)) {
|
|
|
|
SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); // "Hello Tiger" or "A0"
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
2019-02-09 15:08:09 +00:00
|
|
|
else if (3 == XdrvMailbox.index) { // "Hello\f"
|
2018-03-20 13:31:11 +00:00
|
|
|
SerialBridgeSerial->write(Unescape(XdrvMailbox.data, &XdrvMailbox.data_len), XdrvMailbox.data_len);
|
|
|
|
}
|
2019-02-09 15:08:09 +00:00
|
|
|
else if (5 == XdrvMailbox.index) {
|
2019-01-17 16:48:34 +00:00
|
|
|
char *p;
|
|
|
|
char stemp[3];
|
|
|
|
uint8_t code;
|
|
|
|
|
|
|
|
char *codes = RemoveSpace(XdrvMailbox.data);
|
|
|
|
int size = strlen(XdrvMailbox.data);
|
|
|
|
|
2019-10-15 07:27:32 +01:00
|
|
|
while (size > 1) {
|
2019-03-08 18:24:02 +00:00
|
|
|
strlcpy(stemp, codes, sizeof(stemp));
|
2019-01-17 16:48:34 +00:00
|
|
|
code = strtol(stemp, &p, 16);
|
2019-02-09 15:08:09 +00:00
|
|
|
SerialBridgeSerial->write(code); // "AA004566" as hex values
|
2019-01-17 16:48:34 +00:00
|
|
|
size -= 2;
|
|
|
|
codes += 2;
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndDone();
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 16:33:44 +01:00
|
|
|
}
|
2018-11-27 11:09:36 +00:00
|
|
|
|
2019-08-01 16:33:44 +01:00
|
|
|
void CmndSBaudrate(void)
|
|
|
|
{
|
2019-08-28 11:02:27 +01:00
|
|
|
if (XdrvMailbox.payload >= 300) {
|
|
|
|
XdrvMailbox.payload /= 300; // Make it a valid baudrate
|
2019-08-28 09:17:31 +01:00
|
|
|
Settings.sbaudrate = XdrvMailbox.payload;
|
2019-08-28 11:02:27 +01:00
|
|
|
SerialBridgeSerial->begin(Settings.sbaudrate * 300); // Reinitialize serial port with new baud rate
|
2019-08-01 16:33:44 +01:00
|
|
|
}
|
2019-08-28 11:02:27 +01:00
|
|
|
ResponseCmndNumber(Settings.sbaudrate * 300);
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xdrv08(uint8_t function)
|
2018-03-20 13:31:11 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-03-20 13:31:11 +00:00
|
|
|
|
|
|
|
if (serial_bridge_active) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_LOOP:
|
2019-02-09 15:08:09 +00:00
|
|
|
if (SerialBridgeSerial) { SerialBridgeInput(); }
|
2018-03-20 13:31:11 +00:00
|
|
|
break;
|
2019-03-30 12:03:45 +00:00
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
SerialBridgeInit();
|
|
|
|
break;
|
2018-03-20 13:31:11 +00:00
|
|
|
case FUNC_COMMAND:
|
2019-08-01 16:33:44 +01:00
|
|
|
result = DecodeCommand(kSerialBridgeCommands, SerialBridgeCommand);
|
2018-03-20 13:31:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SERIAL_BRIDGE
|