Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for hardware serial on PMSX003 devices #1122

Merged
merged 1 commit into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
49 changes: 38 additions & 11 deletions code/espurna/sensors/PMSX003Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -301,7 +327,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 {
#endif

requestRead();

}

// Current value for slot # index
Expand All @@ -310,6 +336,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 {
}

protected:
bool _soft = true;
unsigned int _pin_rx;
unsigned int _pin_tx;
unsigned long _startTime;
Expand Down