diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index f8ff56e2..4f00e77b 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -517,14 +517,21 @@ #define PMS_SMART_SLEEP 0 #endif +#ifndef PMS_USE_SOFT +#define PMS_USE_SOFT 0 // If PMS_USE_SOFT == 1, DEBUG_SERIAL_SUPPORT must be 0 +#endif + #ifndef PMS_RX_PIN -#define PMS_RX_PIN 13 +#define PMS_RX_PIN 13 // Software serial RX GPIO (if PMS_USE_SOFT == 1) #endif #ifndef PMS_TX_PIN -#define PMS_TX_PIN 15 +#define PMS_TX_PIN 15 // Software serial TX GPIO (if PMS_USE_SOFT == 1) #endif +#ifndef PMS_HW_PORT +#define PMS_HW_PORT Serial // Hardware serial port (if PMS_USE_SOFT == 0) +#endif //------------------------------------------------------------------------------ // PZEM004T based power monitor // Enable support by passing PZEM004T_SUPPORT=1 build flag diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 5ce69055..e9fe54b3 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -533,8 +533,12 @@ void _sensorLoad() { #if PMSX003_SUPPORT { PMSX003Sensor * sensor = new PMSX003Sensor(); - sensor->setRX(PMS_RX_PIN); - sensor->setTX(PMS_TX_PIN); + #if PMS_USE_SOFT + sensor->setRX(PMS_RX_PIN); + sensor->setTX(PMS_TX_PIN); + #else + sensor->setSerial(& PMS_HW_PORT); + #endif sensor->setType(PMS_TYPE); _sensors.push_back(sensor); } diff --git a/code/espurna/sensors/PMSX003Sensor.h b/code/espurna/sensors/PMSX003Sensor.h index 9f6ddd4b..fab7c5c5 100644 --- a/code/espurna/sensors/PMSX003Sensor.h +++ b/code/espurna/sensors/PMSX003Sensor.h @@ -12,7 +12,12 @@ #include "Arduino.h" #include "BaseSensor.h" +#if PMS_USE_SOFT #include +#endif + +// Generic data +#define PMS_BAUD_RATE 9600 // Type of sensor #define PMS_TYPE_X003 0 @@ -46,7 +51,7 @@ const static struct { class PMSX003 { protected: - SoftwareSerial *_serial = NULL; // Should initialized by child class + Stream *_serial = NULL; // Should initialized by child class public: @@ -175,7 +180,13 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { _dirty = true; } - // Should call setType after constrcutor immediately to enable corresponding slot count + void setSerial(HardwareSerial * serial) { + _soft = false; + _serial = serial; + _dirty = true; + } + + // Should call setType after constructor immediately to enable corresponding slot count void setType(unsigned char type) { _type = type; _count = pms_specs[_type].slot_count; @@ -201,33 +212,48 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { // Initialization method, must be idempotent void begin() { - if (!_dirty) return; - if (_serial) delete _serial; + if (_soft) { + if (_serial) delete _serial; + + _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 64); + static_cast(_serial)->enableIntTx(false); + } + + if (_soft) { + static_cast(_serial)->begin(PMS_BAUD_RATE); + } else { + static_cast(_serial)->begin(PMS_BAUD_RATE); + } - _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 64); - _serial->enableIntTx(false); - _serial->begin(9600); passiveMode(); _startTime = millis(); _ready = true; _dirty = false; - } // Descriptive name of the sensor String description() { char buffer[28]; - snprintf(buffer, sizeof(buffer), "%s @ SwSerial(%u,%u)", pms_specs[_type].name, _pin_rx, _pin_tx); + if (_soft) { + snprintf(buffer, sizeof(buffer), "%s @ SwSerial(%u,%u)", pms_specs[_type].name, _pin_rx, _pin_tx); + } else { + snprintf(buffer, sizeof(buffer), "%s @ HwSerial", pms_specs[_type].name); + } + return String(buffer); } // Descriptive name of the slot # index String slot(unsigned char index) { char buffer[36] = {0}; - snprintf(buffer, sizeof(buffer), "%d @ %s @ SwSerial(%u,%u)", int(index + 1), pms_specs[_type].name, _pin_rx, _pin_tx); + if (_soft) { + snprintf(buffer, sizeof(buffer), "%d @ %s @ SwSerial(%u,%u)", int(index + 1), pms_specs[_type].name, _pin_rx, _pin_tx); + } else { + snprintf(buffer, sizeof(buffer), "%d @ %s @ HwSerial", int(index + 1), pms_specs[_type].name); + } return String(buffer); } @@ -301,7 +327,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { #endif requestRead(); - + } // Current value for slot # index @@ -310,6 +336,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { } protected: + bool _soft = true; unsigned int _pin_rx; unsigned int _pin_tx; unsigned long _startTime;