mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-20 12:12:08 +02:00
Ignore parameter values out of range #190
This commit is contained in:
49
src/Main/utils.cpp
Normal file
49
src/Main/utils.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include "utils.h"
|
||||
|
||||
utils::utils(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
utils::~utils()
|
||||
{
|
||||
}
|
||||
|
||||
float utils::correctTemperature(float value) {
|
||||
if (value < -40) {
|
||||
return -40;
|
||||
}
|
||||
if (value > 100) {
|
||||
return 125;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
float utils::correctHumidity(float value) {
|
||||
if (value < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (value > 100) {
|
||||
return 100;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int16_t utils::correctCO2(int16_t value) {
|
||||
if (value < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (value > 10000) {
|
||||
return 10000;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int utils::correctPMS(int value) {
|
||||
if (value < 10) {
|
||||
return 10;
|
||||
}
|
||||
if (value > 1000) {
|
||||
return 1000;
|
||||
}
|
||||
return value;
|
||||
}
|
21
src/Main/utils.h
Normal file
21
src/Main/utils.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef _UTILS_H_
|
||||
#define _UTILS_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class utils
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
utils(/* args */);
|
||||
~utils();
|
||||
|
||||
static float correctTemperature(float value);
|
||||
static float correctHumidity(float value);
|
||||
static int16_t correctCO2(int16_t value);
|
||||
static int correctPMS(int value);
|
||||
};
|
||||
|
||||
|
||||
#endif /** _UTILS_H_ */
|
@ -1,5 +1,6 @@
|
||||
#include "PMS5003.h"
|
||||
#include "Arduino.h"
|
||||
#include "../Main/utils.h"
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <SoftwareSerial.h>
|
||||
@ -87,28 +88,30 @@ bool PMS5003::begin(void) {
|
||||
*
|
||||
* @return int PM1.0 index
|
||||
*/
|
||||
int PMS5003::getPm01Ae(void) { return pms.getPM0_1(); }
|
||||
int PMS5003::getPm01Ae(void) { return utils::correctPMS(pms.getPM0_1()); }
|
||||
|
||||
/**
|
||||
* @brief Read PM2.5 must call this function after @ref readData success
|
||||
*
|
||||
* @return int PM2.5 index
|
||||
*/
|
||||
int PMS5003::getPm25Ae(void) { return pms.getPM2_5(); }
|
||||
int PMS5003::getPm25Ae(void) { return utils::correctPMS(pms.getPM2_5()); }
|
||||
|
||||
/**
|
||||
* @brief Read PM10.0 must call this function after @ref readData success
|
||||
*
|
||||
* @return int PM10.0 index
|
||||
*/
|
||||
int PMS5003::getPm10Ae(void) { return pms.getPM10(); }
|
||||
int PMS5003::getPm10Ae(void) { return utils::correctPMS(pms.getPM10()); }
|
||||
|
||||
/**
|
||||
* @brief Read PM0.3 must call this function after @ref readData success
|
||||
*
|
||||
* @return int PM0.3 index
|
||||
*/
|
||||
int PMS5003::getPm03ParticleCount(void) { return pms.getCount0_3(); }
|
||||
int PMS5003::getPm03ParticleCount(void) {
|
||||
return utils::correctPMS(pms.getCount0_3());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert PM2.5 to US AQI
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "PMS5003T.h"
|
||||
#include "Arduino.h"
|
||||
#include "../Main/utils.h"
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <SoftwareSerial.h>
|
||||
@ -112,28 +113,30 @@ bool PMS5003T::begin(void) {
|
||||
*
|
||||
* @return int PM1.0 index
|
||||
*/
|
||||
int PMS5003T::getPm01Ae(void) { return pms.getPM0_1(); }
|
||||
int PMS5003T::getPm01Ae(void) { return utils::correctPMS(pms.getPM0_1()); }
|
||||
|
||||
/**
|
||||
* @brief Read PM2.5 must call this function after @ref readData success
|
||||
*
|
||||
* @return int PM2.5 index
|
||||
*/
|
||||
int PMS5003T::getPm25Ae(void) { return pms.getPM2_5(); }
|
||||
int PMS5003T::getPm25Ae(void) { return utils::correctPMS(pms.getPM2_5()); }
|
||||
|
||||
/**
|
||||
* @brief Read PM10.0 must call this function after @ref readData success
|
||||
*
|
||||
* @return int PM10.0 index
|
||||
*/
|
||||
int PMS5003T::getPm10Ae(void) { return pms.getPM10(); }
|
||||
int PMS5003T::getPm10Ae(void) { return utils::correctPMS(pms.getPM10()); }
|
||||
|
||||
/**
|
||||
* @brief Read PM 0.3 Count must call this function after @ref readData success
|
||||
*
|
||||
* @return int PM 0.3 Count index
|
||||
*/
|
||||
int PMS5003T::getPm03ParticleCount(void) { return pms.getCount0_3(); }
|
||||
int PMS5003T::getPm03ParticleCount(void) {
|
||||
return utils::correctPMS(pms.getCount0_3());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert PM2.5 to US AQI
|
||||
@ -149,7 +152,7 @@ int PMS5003T::convertPm25ToUsAqi(int pm25) { return pms.pm25ToAQI(pm25); }
|
||||
* @return float Degree Celcius
|
||||
*/
|
||||
float PMS5003T::getTemperature(void) {
|
||||
return pms.getTemp()/10.0f;
|
||||
return utils::correctTemperature(pms.getTemp() / 10.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -158,7 +161,7 @@ float PMS5003T::getTemperature(void) {
|
||||
* @return float Percent (%)
|
||||
*/
|
||||
float PMS5003T::getRelativeHumidity(void) {
|
||||
return pms.getHum()/10.0f;
|
||||
return utils::correctHumidity(pms.getHum() / 10.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "S8.h"
|
||||
#include "mb_crc.h"
|
||||
#include "../Main/utils.h"
|
||||
#if defined(ESP8266)
|
||||
#include <SoftwareSerial.h>
|
||||
#else
|
||||
@ -245,7 +246,7 @@ int16_t S8::getCo2(void) {
|
||||
AgLog("Error getting CO2 value!");
|
||||
}
|
||||
|
||||
return co2;
|
||||
return utils::correctCO2(co2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "Sht.h"
|
||||
|
||||
#include "../Libraries/arduino-sht/SHTSensor.h"
|
||||
#include "../Main/utils.h"
|
||||
|
||||
/** Cast _sensor to SHTSensor */
|
||||
#define shtSensor() ((SHTSensor *)(this->_sensor))
|
||||
@ -131,14 +132,18 @@ void Sht::end(void) {
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
float Sht::getTemperature(void) { return shtSensor()->getTemperature(); }
|
||||
float Sht::getTemperature(void) {
|
||||
return utils::correctTemperature(shtSensor()->getTemperature());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get humidity
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
float Sht::getRelativeHumidity(void) { return shtSensor()->getHumidity(); }
|
||||
float Sht::getRelativeHumidity(void) {
|
||||
return utils::correctHumidity(shtSensor()->getHumidity());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Measure temperature and humidity
|
||||
|
Reference in New Issue
Block a user