From 57f5c7dc98bd8545816a8834ae5f41877b66c302 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 4 Jul 2020 12:22:26 +0300 Subject: [PATCH] sns/cse7766: fix pin naming --- code/espurna/config/hardware.h | 6 +++--- code/espurna/config/sensors.h | 5 +++-- code/espurna/sensor.cpp | 2 +- code/espurna/sensors/CSE7766Sensor.h | 20 ++++++++++++++------ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 6367660b..2b66f5c5 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -549,7 +549,7 @@ #ifndef CSE7766_SUPPORT #define CSE7766_SUPPORT 1 #endif - #define CSE7766_PIN 1 + #define CSE7766_RX_PIN 3 #elif defined(ITEAD_SONOFF_DUAL) @@ -975,7 +975,7 @@ // CSE7766 #define CSE7766_SUPPORT 1 - #define CSE7766_PIN 1 + #define CSE7766_RX_PIN 3 #elif defined(ITEAD_SONOFF_S31_LITE) @@ -3201,7 +3201,7 @@ #ifndef CSE7766_SUPPORT #define CSE7766_SUPPORT 1 #endif - #define CSE7766_PIN 1 + #define CSE7766_RX_PIN 3 // ----------------------------------------------------------------------------- // Teckin SP21 diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 7455ce20..239b97bb 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -284,8 +284,9 @@ #define CSE7766_SUPPORT 0 #endif -#ifndef CSE7766_PIN -#define CSE7766_PIN 1 // TX pin from the CSE7766 +#ifndef CSE7766_RX_PIN +#define CSE7766_RX_PIN 3 // RX pin connected to the CSE7766 + // As we never transmit anything, this is the only pin used #endif #ifndef CSE7766_PIN_INVERSE diff --git a/code/espurna/sensor.cpp b/code/espurna/sensor.cpp index df57528f..6d324c27 100644 --- a/code/espurna/sensor.cpp +++ b/code/espurna/sensor.cpp @@ -1571,7 +1571,7 @@ void _sensorLoad() { #if CSE7766_SUPPORT { CSE7766Sensor * sensor = new CSE7766Sensor(); - sensor->setRX(CSE7766_PIN); + sensor->setRX(CSE7766_RX_PIN); _sensors.push_back(sensor); } #endif diff --git a/code/espurna/sensors/CSE7766Sensor.h b/code/espurna/sensors/CSE7766Sensor.h index 1165b3dc..c3b96a75 100644 --- a/code/espurna/sensors/CSE7766Sensor.h +++ b/code/espurna/sensors/CSE7766Sensor.h @@ -132,8 +132,12 @@ class CSE7766Sensor : public BaseEmonSensor { if (_serial) delete _serial; - if (1 == _pin_rx) { + if (3 == _pin_rx) { Serial.begin(CSE7766_BAUDRATE); + } else if (13 == _pin_rx) { + Serial.begin(CSE7766_BAUDRATE); + Serial.flush(); + Serial.swap(); } else { _serial = new SoftwareSerial(_pin_rx, -1, _inverted); _serial->enableIntTx(false); @@ -148,7 +152,7 @@ class CSE7766Sensor : public BaseEmonSensor { // Descriptive name of the sensor String description() { char buffer[28]; - if (1 == _pin_rx) { + if (_serial_is_hardware()) { snprintf(buffer, sizeof(buffer), "CSE7766 @ HwSerial"); } else { snprintf(buffer, sizeof(buffer), "CSE7766 @ SwSerial(%u,NULL)", _pin_rx); @@ -368,8 +372,12 @@ class CSE7766Sensor : public BaseEmonSensor { // --------------------------------------------------------------------- + bool _serial_is_hardware() { + return (3 == _pin_rx) || (13 == _pin_rx); + } + bool _serial_available() { - if (1 == _pin_rx) { + if (_serial_is_hardware()) { return Serial.available(); } else { return _serial->available(); @@ -377,7 +385,7 @@ class CSE7766Sensor : public BaseEmonSensor { } void _serial_flush() { - if (1 == _pin_rx) { + if (_serial_is_hardware()) { return Serial.flush(); } else { return _serial->flush(); @@ -385,7 +393,7 @@ class CSE7766Sensor : public BaseEmonSensor { } uint8_t _serial_read() { - if (1 == _pin_rx) { + if (_serial_is_hardware()) { return Serial.read(); } else { return _serial->read(); @@ -394,7 +402,7 @@ class CSE7766Sensor : public BaseEmonSensor { // --------------------------------------------------------------------- - unsigned int _pin_rx = CSE7766_PIN; + int _pin_rx = CSE7766_RX_PIN; bool _inverted = CSE7766_PIN_INVERSE; SoftwareSerial * _serial = NULL;