mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-15 00:46:31 +02:00
move pm25ToAQI
into PMSUtils
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
#include "PMS5003.h"
|
||||
#include "Arduino.h"
|
||||
#include "PMSUtils.h"
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <SoftwareSerial.h>
|
||||
@ -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
|
||||
|
@ -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_ */
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "PMS5003T.h"
|
||||
#include "Arduino.h"
|
||||
#include "PMSUtils.h"
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <SoftwareSerial.h>
|
||||
@ -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
|
||||
|
@ -1,10 +1,10 @@
|
||||
#ifndef _PMS5003T_H_
|
||||
#define _PMS5003T_H_
|
||||
|
||||
#include <HardwareSerial.h>
|
||||
#include "../Main/BoardDef.h"
|
||||
#include "PMS.h"
|
||||
#include "Stream.h"
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
26
src/PMS/PMSUtils.cpp
Normal file
26
src/PMS/PMSUtils.cpp
Normal 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
6
src/PMS/PMSUtils.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef _PMS_UTILS_H_
|
||||
#define _PMS_UTILS_H_
|
||||
|
||||
int pm25ToAQI(int pm02);
|
||||
|
||||
#endif /** _PMS_UTILS_H_ */
|
Reference in New Issue
Block a user