mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-21 04:32:09 +02:00
Update the API use Stream instead of Hardware/Software serial
This commit is contained in:
@ -4,32 +4,33 @@
|
||||
/**
|
||||
* @brief Initializes the sensor and attempts to read data.
|
||||
*
|
||||
* @param HardwareSerial UART stream
|
||||
* @param stream UART stream
|
||||
* @return true Sucecss
|
||||
* @return false Failure
|
||||
*/
|
||||
bool PMSBase::begin(HardwareSerial &serial) {
|
||||
bool PMSBase::begin(Stream *stream) {
|
||||
Serial.printf("initializing PM sensor\n");
|
||||
|
||||
failCount = 0;
|
||||
_connected = false;
|
||||
|
||||
// Run and check sensor data for 4sec
|
||||
int bytesCleared = serial.available();
|
||||
serial.flush();
|
||||
if (bytesCleared) {
|
||||
Serial.printf("cleared %d byte(s)\n", bytesCleared);
|
||||
// empty first
|
||||
int bytesCleared = 0;
|
||||
while (stream->read() != -1) {
|
||||
bytesCleared++;
|
||||
}
|
||||
Serial.printf("cleared %d byte(s)\n", bytesCleared);
|
||||
|
||||
// explicitly put the sensor into active mode, this seems to be be needed for
|
||||
// the Cubic PM2009X
|
||||
Serial.printf("Setting active mode\n");
|
||||
uint8_t activeModeCommand[] = {0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71};
|
||||
size_t bytesWritten =
|
||||
serial.write(activeModeCommand, sizeof(activeModeCommand));
|
||||
// explicitly put the sensor into active mode, this seems to be be needed for the Cubic PM2009X
|
||||
Serial.printf("setting active mode\n");
|
||||
uint8_t activeModeCommand[] = { 0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71 };
|
||||
size_t bytesWritten = stream->write(activeModeCommand, sizeof(activeModeCommand));
|
||||
Serial.printf("%d byte(s) written\n", bytesWritten);
|
||||
|
||||
// Run and check sensor data for 4sec
|
||||
unsigned long lastInit = millis();
|
||||
while (true) {
|
||||
readPackage(serial);
|
||||
readPackage(stream);
|
||||
if (_connected) {
|
||||
break;
|
||||
}
|
||||
@ -37,15 +38,9 @@ bool PMSBase::begin(HardwareSerial &serial) {
|
||||
delay(1);
|
||||
unsigned long ms = (unsigned long)(millis() - lastInit);
|
||||
if (ms >= 4000) {
|
||||
Serial.println("Initialize PMS sensor: Timeout");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_connected) {
|
||||
Serial.println("Initialize PMS sensor");
|
||||
}
|
||||
|
||||
return _connected;
|
||||
}
|
||||
|
||||
@ -54,7 +49,7 @@ bool PMSBase::begin(HardwareSerial &serial) {
|
||||
*
|
||||
* @param serial
|
||||
*/
|
||||
void PMSBase::readPackage(HardwareSerial &serial) {
|
||||
void PMSBase::readPackage(Stream *serial) {
|
||||
/** If readPackage has process as period larger than READ_PACKAGE_TIMEOUT,
|
||||
* should be clear the lastPackage and readBufferIndex */
|
||||
if (lastReadPackage) {
|
||||
@ -83,9 +78,9 @@ void PMSBase::readPackage(HardwareSerial &serial) {
|
||||
/** Count to call delay() to release the while loop MCU resource for avoid the
|
||||
* watchdog time reset */
|
||||
uint8_t delayCount = 0;
|
||||
while (serial.available()) {
|
||||
while (serial->available()) {
|
||||
/** Get value */
|
||||
uint8_t value = (uint8_t)serial.read();
|
||||
uint8_t value = (uint8_t)serial->read();
|
||||
|
||||
/** Process receiving package... */
|
||||
switch (readBufferIndex) {
|
||||
|
@ -10,8 +10,8 @@
|
||||
*/
|
||||
class PMSBase {
|
||||
public:
|
||||
bool begin(HardwareSerial& serial);
|
||||
void readPackage(HardwareSerial& serial);
|
||||
bool begin(Stream *stream);
|
||||
void readPackage(Stream *stream);
|
||||
void updateFailCount(void);
|
||||
void resetFailCount(void);
|
||||
int getFailCount(void);
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include "../Main/utils.h"
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <SoftwareSerial.h>
|
||||
/**
|
||||
* @brief Init sensor
|
||||
*
|
||||
@ -60,17 +59,16 @@ bool PMS5003::begin(void) {
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
bsp->Pms5003.uart_tx_pin;
|
||||
SoftwareSerial *uart =
|
||||
this->_serial =
|
||||
new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin);
|
||||
uart->begin(9600);
|
||||
if (pms.begin(uart) == false) {
|
||||
this->_serial->begin(9600);
|
||||
if (pms.begin(this->_serial) == false) {
|
||||
AgLog("PMS failed");
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
this->_serial->begin(9600);
|
||||
if (pms.begin(*this->_serial) == false) {
|
||||
if (pms.begin(this->_serial) == false) {
|
||||
AgLog("PMS failed");
|
||||
return false;
|
||||
}
|
||||
@ -187,7 +185,7 @@ void PMS5003::end(void) {
|
||||
* @brief Check and read PMS sensor data. This method should be callack from
|
||||
* loop process to continoue check sensor data if it's available
|
||||
*/
|
||||
void PMS5003::handle(void) { pms.readPackage(*this->_serial); }
|
||||
void PMS5003::handle(void) { pms.readPackage(this->_serial); }
|
||||
|
||||
void PMS5003::updateFailCount(void) {
|
||||
pms.updateFailCount();
|
||||
|
@ -4,6 +4,9 @@
|
||||
#include "../Main/BoardDef.h"
|
||||
#include "PMS.h"
|
||||
#include "Stream.h"
|
||||
#ifdef ESP8266
|
||||
#include <SoftwareSerial.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief The class define how to handle PMS5003 sensor bas on @ref PMS class
|
||||
@ -41,6 +44,7 @@ private:
|
||||
#if defined(ESP8266)
|
||||
Stream *_debugStream;
|
||||
const char *TAG = "PMS5003";
|
||||
SoftwareSerial *_serial;
|
||||
#else
|
||||
HardwareSerial *_serial;
|
||||
#endif
|
||||
|
@ -67,11 +67,10 @@ bool PMS5003T::begin(void) {
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
bsp->Pms5003.uart_tx_pin;
|
||||
SoftwareSerial *uart =
|
||||
this->_serial =
|
||||
new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin);
|
||||
uart->begin(9600);
|
||||
if (pms.begin(uart) == false) {
|
||||
this->_serial->begin(9600);
|
||||
if (pms.begin(this->_serial) == false) {
|
||||
AgLog("PMS failed");
|
||||
return false;
|
||||
}
|
||||
@ -98,7 +97,7 @@ bool PMS5003T::begin(void) {
|
||||
this->_serial->begin(9600, SERIAL_8N1, bsp->SenseAirS8.uart_rx_pin,
|
||||
bsp->SenseAirS8.uart_tx_pin);
|
||||
}
|
||||
if (pms.begin(*this->_serial) == false) {
|
||||
if (pms.begin(this->_serial) == false) {
|
||||
AgLog("PMS failed");
|
||||
return false;
|
||||
}
|
||||
@ -230,7 +229,7 @@ void PMS5003T::end(void) {
|
||||
* @brief Check and read PMS sensor data. This method should be callack from
|
||||
* loop process to continoue check sensor data if it's available
|
||||
*/
|
||||
void PMS5003T::handle(void) { pms.readPackage(*this->_serial); }
|
||||
void PMS5003T::handle(void) { pms.readPackage(this->_serial); }
|
||||
|
||||
void PMS5003T::updateFailCount(void) {
|
||||
pms.updateFailCount();
|
||||
|
@ -6,6 +6,9 @@
|
||||
#include "PMS5003TBase.h"
|
||||
#include "Stream.h"
|
||||
#include <HardwareSerial.h>
|
||||
#ifdef ESP8266
|
||||
#include <SoftwareSerial.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief The class define how to handle PMS5003T sensor bas on @ref PMS class
|
||||
@ -47,6 +50,7 @@ private:
|
||||
#if defined(ESP8266)
|
||||
Stream *_debugStream;
|
||||
const char *TAG = "PMS5003T";
|
||||
SoftwareSerial *_serial;
|
||||
#else
|
||||
HardwareSerial *_serial;
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user