move pm25ToAQI into PMSUtils

This commit is contained in:
Phat Nguyen
2024-02-29 14:45:44 +07:00
parent 0bda7a1c4b
commit 351af57591
6 changed files with 40 additions and 57 deletions

View File

@ -1,5 +1,6 @@
#include "PMS5003.h" #include "PMS5003.h"
#include "Arduino.h" #include "Arduino.h"
#include "PMSUtils.h"
#if defined(ESP8266) #if defined(ESP8266)
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
@ -63,7 +64,8 @@ bool PMS5003::begin(void) {
#if defined(ESP8266) #if defined(ESP8266)
bsp->Pms5003.uart_tx_pin; bsp->Pms5003.uart_tx_pin;
SoftwareSerial *uart = new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin); SoftwareSerial *uart =
new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin);
uart->begin(9600); uart->begin(9600);
if (pms.begin(uart) == false) { if (pms.begin(uart) == false) {
AgLog("PMS failed"); AgLog("PMS failed");
@ -81,31 +83,6 @@ bool PMS5003::begin(void) {
return true; return true;
} }
/**
* @brief Convert PM2.5 to US AQI
*
* @param pm02
* @return int
*/
int PMS5003::pm25ToAQI(int pm02) {
if (pm02 <= 12.0)
return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4)
return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4)
return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4)
return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4)
return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4)
return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4)
return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else
return 500;
}
/** /**
* @brief Read all package data then call to @ref getPMxxx to get the target * @brief Read all package data then call to @ref getPMxxx to get the target
* data * data
@ -155,7 +132,7 @@ int PMS5003::getPm03ParticleCount(void) { return pmsData.PM_RAW_0_3; }
* @param pm25 PM2.5 index * @param pm25 PM2.5 index
* @return int PM2.5 US AQI * @return int PM2.5 US AQI
*/ */
int PMS5003::convertPm25ToUsAqi(int pm25) { return this->pm25ToAQI(pm25); } int PMS5003::convertPm25ToUsAqi(int pm25) { return pm25ToAQI(pm25); }
/** /**
* @brief Check device initialized or not * @brief Check device initialized or not

View File

@ -2,8 +2,8 @@
#define _AIR_GRADIENT_PMS5003_H_ #define _AIR_GRADIENT_PMS5003_H_
#include "../Main/BoardDef.h" #include "../Main/BoardDef.h"
#include "Stream.h"
#include "PMS.h" #include "PMS.h"
#include "Stream.h"
/** /**
* @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 +41,5 @@ private:
bool begin(void); bool begin(void);
bool isBegin(void); bool isBegin(void);
int pm25ToAQI(int pm02);
}; };
#endif /** _AIR_GRADIENT_PMS5003_H_ */ #endif /** _AIR_GRADIENT_PMS5003_H_ */

View File

@ -1,5 +1,6 @@
#include "PMS5003T.h" #include "PMS5003T.h"
#include "Arduino.h" #include "Arduino.h"
#include "PMSUtils.h"
#if defined(ESP8266) #if defined(ESP8266)
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
@ -105,31 +106,6 @@ bool PMS5003T::begin(void) {
return true; return true;
} }
/**
* @brief Convert PM2.5 to US AQI
*
* @param pm02
* @return int
*/
int PMS5003T::pm25ToAQI(int pm02) {
if (pm02 <= 12.0)
return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4)
return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4)
return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4)
return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4)
return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4)
return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4)
return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else
return 500;
}
/** /**
* @brief Read all package data then call to @ref getPMxxx to get the target * @brief Read all package data then call to @ref getPMxxx to get the target
* data * data
@ -179,7 +155,7 @@ int PMS5003T::getPm03ParticleCount(void) { return pmsData.PM_RAW_0_3; }
* @param pm25 PM2.5 index * @param pm25 PM2.5 index
* @return int PM2.5 US AQI * @return int PM2.5 US AQI
*/ */
int PMS5003T::convertPm25ToUsAqi(int pm25) { return this->pm25ToAQI(pm25); } int PMS5003T::convertPm25ToUsAqi(int pm25) { return pm25ToAQI(pm25); }
/** /**
* @brief Get temperature, Must call this method after @ref readData() success * @brief Get temperature, Must call this method after @ref readData() success

View File

@ -1,10 +1,10 @@
#ifndef _PMS5003T_H_ #ifndef _PMS5003T_H_
#define _PMS5003T_H_ #define _PMS5003T_H_
#include <HardwareSerial.h>
#include "../Main/BoardDef.h" #include "../Main/BoardDef.h"
#include "PMS.h" #include "PMS.h"
#include "Stream.h" #include "Stream.h"
#include <HardwareSerial.h>
/** /**
* @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
@ -42,7 +42,6 @@ private:
#endif #endif
bool begin(void); bool begin(void);
int pm25ToAQI(int pm02);
PMS pms; PMS pms;
PMS::DATA pmsData; PMS::DATA pmsData;
bool isBegin(void); bool isBegin(void);

26
src/PMS/PMSUtils.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "PMSUtils.h"
/**
* @brief Convert PM2.5 to US AQI
*
* @param pm02
* @return int
*/
int pm25ToAQI(int pm02) {
if (pm02 <= 12.0)
return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4)
return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4)
return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4)
return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4)
return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4)
return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4)
return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else
return 500;
}

6
src/PMS/PMSUtils.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _PMS_UTILS_H_
#define _PMS_UTILS_H_
int pm25ToAQI(int pm02);
#endif /** _PMS_UTILS_H_ */