forked from airgradienthq/arduino
updateValue return bool to indicate max average is set or not
Add more comments Update naming
This commit is contained in:
@ -79,7 +79,7 @@ String Measurements::agValueTypeStr(AgValueType type) {
|
||||
return str;
|
||||
}
|
||||
|
||||
void Measurements::updateValue(AgValueType type, int val) {
|
||||
bool Measurements::updateValue(AgValueType type, int val) {
|
||||
// Define data point source
|
||||
IntegerValue *temporary = nullptr;
|
||||
float invalidValue = 0;
|
||||
@ -135,32 +135,35 @@ void Measurements::updateValue(AgValueType type, int val) {
|
||||
if (val != invalidValue) {
|
||||
temporary->lastValue = val;
|
||||
temporary->sumValues = temporary->sumValues + val;
|
||||
temporary->read.success = temporary->read.success + 1;
|
||||
temporary->update.success = temporary->update.success + 1;
|
||||
}
|
||||
|
||||
// Increment read counter
|
||||
temporary->read.counter = temporary->read.counter + 1;
|
||||
// Increment update.counter
|
||||
temporary->update.counter = temporary->update.counter + 1;
|
||||
|
||||
// Calculate value average when maximum set is reached
|
||||
if (temporary->read.counter >= temporary->read.max) {
|
||||
if (temporary->update.counter >= temporary->update.max) {
|
||||
// Calculate the average
|
||||
temporary->avg = temporary->sumValues / temporary->read.success;
|
||||
temporary->avg = temporary->sumValues / temporary->update.success;
|
||||
|
||||
// This is just for notifying
|
||||
int miss = temporary->read.max - temporary->read.success;
|
||||
int miss = temporary->update.max - temporary->update.success;
|
||||
if (miss != 0) {
|
||||
Serial.printf("%s reading miss %d out of %d update\n", agValueTypeStr(type), miss,
|
||||
temporary->read.max);
|
||||
Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss,
|
||||
temporary->update.max);
|
||||
}
|
||||
|
||||
// Resets the sum data and read variables
|
||||
// Resets average related variable calculation
|
||||
temporary->sumValues = 0;
|
||||
temporary->read.counter = 0;
|
||||
temporary->read.success = 0;
|
||||
temporary->update.counter = 0;
|
||||
temporary->update.success = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Measurements::updateValue(AgValueType type, float val) {
|
||||
bool Measurements::updateValue(AgValueType type, float val) {
|
||||
// Define data point source
|
||||
FloatValue *temporary = nullptr;
|
||||
float invalidValue = 0;
|
||||
@ -188,29 +191,32 @@ void Measurements::updateValue(AgValueType type, float val) {
|
||||
if (val != invalidValue) {
|
||||
temporary->lastValue = val;
|
||||
temporary->sumValues = temporary->sumValues + val;
|
||||
temporary->read.success = temporary->read.success + 1;
|
||||
temporary->update.success = temporary->update.success + 1;
|
||||
}
|
||||
|
||||
// Increment read counter
|
||||
temporary->read.counter = temporary->read.counter + 1;
|
||||
// Increment update.counter
|
||||
temporary->update.counter = temporary->update.counter + 1;
|
||||
|
||||
// Calculate value average when maximum set is reached
|
||||
if (temporary->read.counter >= temporary->read.max) {
|
||||
if (temporary->update.counter >= temporary->update.max) {
|
||||
// Calculate the average
|
||||
temporary->avg = temporary->sumValues / temporary->read.success;
|
||||
temporary->avg = temporary->sumValues / temporary->update.success;
|
||||
|
||||
// This is just for notifying
|
||||
int miss = temporary->read.max - temporary->read.success;
|
||||
int miss = temporary->update.max - temporary->update.success;
|
||||
if (miss != 0) {
|
||||
Serial.printf("%s reading miss %d out of %d update\n", agValueTypeStr(type), miss,
|
||||
temporary->read.max);
|
||||
Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss,
|
||||
temporary->update.max);
|
||||
}
|
||||
|
||||
// Resets the sum data and read variables
|
||||
// Resets average related variable calculation
|
||||
temporary->sumValues = 0;
|
||||
temporary->read.counter = 0;
|
||||
temporary->read.success = 0;
|
||||
temporary->update.counter = 0;
|
||||
temporary->update.success = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
|
||||
|
@ -7,28 +7,28 @@
|
||||
|
||||
class Measurements {
|
||||
private:
|
||||
// Generic struct for reading indication for respective value
|
||||
// TODO: Reading naming is confusing, because its not actually reading but updating with new value
|
||||
struct Reading {
|
||||
int counter; // How many reading attempts done
|
||||
int success; // How many reading that success from each attempts
|
||||
int max; // Maximum reading attempt
|
||||
// Generic struct for update indication for respective value
|
||||
struct Update {
|
||||
int counter; // How many update attempts done
|
||||
int success; // How many update value that actually give valid value
|
||||
int max; // Maximum update before calculating average
|
||||
};
|
||||
|
||||
// Reading type for sensor value that outputs float
|
||||
struct FloatValue {
|
||||
float lastValue; // Last reading value
|
||||
float sumValues; // Total value of each reading
|
||||
float avg; // The last average calculation after maximum reading attempt reached
|
||||
Reading read;
|
||||
float lastValue; // Last update value
|
||||
float sumValues; // Total value from each update
|
||||
float avg; // The last average calculation after maximum update attempt reached
|
||||
Update update;
|
||||
};
|
||||
|
||||
// Reading type for sensor value that outputs integer
|
||||
struct IntegerValue {
|
||||
int lastValue; // Last reading value
|
||||
unsigned long sumValues; // Total value of each reading // TODO: explain why unsigned long
|
||||
int avg; // The last average calculation after maximum reading attempt reached
|
||||
Reading read;
|
||||
int lastValue; // Last update value
|
||||
unsigned long sumValues; // Total value from each update; unsigned long to accomodate TVOx and
|
||||
// NOx raw data
|
||||
int avg; // The last average calculation after maximum update attempt reached
|
||||
Update update;
|
||||
};
|
||||
|
||||
public:
|
||||
@ -71,8 +71,29 @@ public:
|
||||
PM03,
|
||||
};
|
||||
|
||||
void updateValue(AgValueType type, int val);
|
||||
void updateValue(AgValueType type, float val);
|
||||
/**
|
||||
* @brief update target type value with new value.
|
||||
* Each AgValueType has last raw value and last average that are calculated based on max number of
|
||||
* set This function is for AgValueType that use INT as the data type
|
||||
*
|
||||
* @param type (AgValueType) value type that will be updated
|
||||
* @param val (int) the new value
|
||||
* @return true if update counter reached and new average value is calculated
|
||||
* @return false otherwise
|
||||
*/
|
||||
bool updateValue(AgValueType type, int val);
|
||||
|
||||
/**
|
||||
* @brief update target type value with new value.
|
||||
* Each AgValueType has last raw value and last average that are calculated based on max number of
|
||||
* set This function is for AgValueType that use FLOAT as the data type
|
||||
*
|
||||
* @param type (AgValueType) value type that will be updated
|
||||
* @param val (float) the new value
|
||||
* @return true if update counter reached and new average value is calculated
|
||||
* @return false otherwise
|
||||
*/
|
||||
bool updateValue(AgValueType type, float val);
|
||||
|
||||
float Temperature;
|
||||
int Humidity;
|
||||
|
Reference in New Issue
Block a user