diff --git a/src/PMS/PMS5003.cpp b/src/PMS/PMS5003.cpp index 128d1b1..268f9bb 100644 --- a/src/PMS/PMS5003.cpp +++ b/src/PMS/PMS5003.cpp @@ -1,5 +1,6 @@ #include "PMS5003.h" #include "Arduino.h" +#include "PMSUtils.h" #if defined(ESP8266) #include @@ -63,7 +64,8 @@ bool PMS5003::begin(void) { #if defined(ESP8266) 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); if (pms.begin(uart) == false) { AgLog("PMS failed"); @@ -81,31 +83,6 @@ bool PMS5003::begin(void) { 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 * data @@ -155,7 +132,7 @@ int PMS5003::getPm03ParticleCount(void) { return pmsData.PM_RAW_0_3; } * @param pm25 PM2.5 index * @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 diff --git a/src/PMS/PMS5003.h b/src/PMS/PMS5003.h index 69e6de4..15b907c 100644 --- a/src/PMS/PMS5003.h +++ b/src/PMS/PMS5003.h @@ -2,8 +2,8 @@ #define _AIR_GRADIENT_PMS5003_H_ #include "../Main/BoardDef.h" -#include "Stream.h" #include "PMS.h" +#include "Stream.h" /** * @brief The class define how to handle PMS5003 sensor bas on @ref PMS class @@ -41,6 +41,5 @@ private: bool begin(void); bool isBegin(void); - int pm25ToAQI(int pm02); }; #endif /** _AIR_GRADIENT_PMS5003_H_ */ diff --git a/src/PMS/PMS5003T.cpp b/src/PMS/PMS5003T.cpp index 57040a5..03b0fd2 100644 --- a/src/PMS/PMS5003T.cpp +++ b/src/PMS/PMS5003T.cpp @@ -1,5 +1,6 @@ #include "PMS5003T.h" #include "Arduino.h" +#include "PMSUtils.h" #if defined(ESP8266) #include @@ -105,31 +106,6 @@ bool PMS5003T::begin(void) { 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 * data @@ -179,7 +155,7 @@ int PMS5003T::getPm03ParticleCount(void) { return pmsData.PM_RAW_0_3; } * @param pm25 PM2.5 index * @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 diff --git a/src/PMS/PMS5003T.h b/src/PMS/PMS5003T.h index a93cd40..ec4d78e 100644 --- a/src/PMS/PMS5003T.h +++ b/src/PMS/PMS5003T.h @@ -1,10 +1,10 @@ #ifndef _PMS5003T_H_ #define _PMS5003T_H_ -#include #include "../Main/BoardDef.h" #include "PMS.h" #include "Stream.h" +#include /** * @brief The class define how to handle PMS5003T sensor bas on @ref PMS class @@ -42,7 +42,6 @@ private: #endif bool begin(void); - int pm25ToAQI(int pm02); PMS pms; PMS::DATA pmsData; bool isBegin(void); diff --git a/src/PMS/PMSUtils.cpp b/src/PMS/PMSUtils.cpp new file mode 100644 index 0000000..f47813f --- /dev/null +++ b/src/PMS/PMSUtils.cpp @@ -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; +} diff --git a/src/PMS/PMSUtils.h b/src/PMS/PMSUtils.h new file mode 100644 index 0000000..ffb5a84 --- /dev/null +++ b/src/PMS/PMSUtils.h @@ -0,0 +1,6 @@ +#ifndef _PMS_UTILS_H_ +#define _PMS_UTILS_H_ + +int pm25ToAQI(int pm02); + +#endif /** _PMS_UTILS_H_ */