mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-05 16:04:19 +01:00
Add support for hardware serial on PMSX003 devices
This commit is contained in:
@@ -509,14 +509,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
|
||||
|
||||
@@ -531,8 +531,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);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,12 @@
|
||||
#include "Arduino.h"
|
||||
#include "BaseSensor.h"
|
||||
|
||||
#if PMS_USE_SOFT
|
||||
#include <SoftwareSerial.h>
|
||||
#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<SoftwareSerial*>(_serial)->enableIntTx(false);
|
||||
}
|
||||
|
||||
if (_soft) {
|
||||
static_cast<SoftwareSerial*>(_serial)->begin(PMS_BAUD_RATE);
|
||||
} else {
|
||||
static_cast<HardwareSerial*>(_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;
|
||||
|
||||
Reference in New Issue
Block a user