|
@ -12,7 +12,12 @@ |
|
|
#include "Arduino.h" |
|
|
#include "Arduino.h" |
|
|
#include "BaseSensor.h" |
|
|
#include "BaseSensor.h" |
|
|
|
|
|
|
|
|
|
|
|
#if PMS_USE_SOFT |
|
|
#include <SoftwareSerial.h> |
|
|
#include <SoftwareSerial.h> |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
// Generic data |
|
|
|
|
|
#define PMS_BAUD_RATE 9600 |
|
|
|
|
|
|
|
|
// Type of sensor |
|
|
// Type of sensor |
|
|
#define PMS_TYPE_X003 0 |
|
|
#define PMS_TYPE_X003 0 |
|
@ -46,7 +51,7 @@ const static struct { |
|
|
class PMSX003 { |
|
|
class PMSX003 { |
|
|
|
|
|
|
|
|
protected: |
|
|
protected: |
|
|
SoftwareSerial *_serial = NULL; // Should initialized by child class |
|
|
|
|
|
|
|
|
Stream *_serial = NULL; // Should initialized by child class |
|
|
|
|
|
|
|
|
public: |
|
|
public: |
|
|
|
|
|
|
|
@ -175,7 +180,13 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
_dirty = true; |
|
|
_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) { |
|
|
void setType(unsigned char type) { |
|
|
_type = type; |
|
|
_type = type; |
|
|
_count = pms_specs[_type].slot_count; |
|
|
_count = pms_specs[_type].slot_count; |
|
@ -201,33 +212,48 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
|
|
|
|
|
|
// Initialization method, must be idempotent |
|
|
// Initialization method, must be idempotent |
|
|
void begin() { |
|
|
void begin() { |
|
|
|
|
|
|
|
|
if (!_dirty) return; |
|
|
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(); |
|
|
passiveMode(); |
|
|
|
|
|
|
|
|
_startTime = millis(); |
|
|
_startTime = millis(); |
|
|
_ready = true; |
|
|
_ready = true; |
|
|
_dirty = false; |
|
|
_dirty = false; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Descriptive name of the sensor |
|
|
// Descriptive name of the sensor |
|
|
String description() { |
|
|
String description() { |
|
|
char buffer[28]; |
|
|
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); |
|
|
return String(buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Descriptive name of the slot # index |
|
|
// Descriptive name of the slot # index |
|
|
String slot(unsigned char index) { |
|
|
String slot(unsigned char index) { |
|
|
char buffer[36] = {0}; |
|
|
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); |
|
|
return String(buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -301,7 +327,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
requestRead(); |
|
|
requestRead(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Current value for slot # index |
|
|
// Current value for slot # index |
|
@ -310,6 +336,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected: |
|
|
protected: |
|
|
|
|
|
bool _soft = true; |
|
|
unsigned int _pin_rx; |
|
|
unsigned int _pin_rx; |
|
|
unsigned int _pin_tx; |
|
|
unsigned int _pin_tx; |
|
|
unsigned long _startTime; |
|
|
unsigned long _startTime; |
|
|