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