From a513943cbaf096743e779fba155310a093729797 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sat, 9 Nov 2024 23:27:29 +0700 Subject: [PATCH] New agvalue member to get avg values --- src/AgValue.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++-- src/AgValue.h | 8 ++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/AgValue.cpp b/src/AgValue.cpp index 3812ba3..b0b281d 100644 --- a/src/AgValue.cpp +++ b/src/AgValue.cpp @@ -396,6 +396,52 @@ float Measurements::getFloat(MeasurementType type, int ch) { return temporary->listValues.back(); } +float Measurements::getAverage(MeasurementType type, int ch) { + // Sanity check to validate channel, assert if invalid + validateChannel(ch); + + // Follow array indexing just for get address of the value type + ch = ch - 1; + + // Define data point source. Data type doesn't matter because only to get the average value + FloatValue *temporary = nullptr; + Update update; + float measurementAverage; + switch (type) { + case CO2: + measurementAverage = _co2.update.avg; + break; + case TVOC: + measurementAverage = _tvoc.update.avg; + break; + case NOx: + measurementAverage = _nox.update.avg; + break; + case PM25: + measurementAverage = _pm_25[ch].update.avg; + break; + case Temperature: + measurementAverage = _temperature[ch].update.avg; + break; + case Humidity: + measurementAverage = _humidity[ch].update.avg; + break; + default: + // Invalidate, measurements type not handled + measurementAverage = -1000; + break; + }; + + // Sanity check if measurement type is not defined + if (measurementAverage == -1000) { + Serial.printf("ERROR! %s is not defined on get average value function\n", measurementTypeStr(type)); + delay(1000); + assert(0); + } + + return measurementAverage; +} + String Measurements::pms5003FirmwareVersion(int fwCode) { return pms5003FirmwareVersionBase("PMS5003x", fwCode); } @@ -488,9 +534,9 @@ float Measurements::getCorrectedPM25(AirGradient &ag, Configuration &config, boo float corrected; float humidity; float pm003Count; - int channel = ch - 1; // Array index if (useAvg) { // Directly call from the index + int channel = ch - 1; // Array index pm25 = _pm_25[channel].update.avg; humidity = _humidity[channel].update.avg; pm003Count = _pm_03_pc[channel].update.avg; @@ -511,7 +557,7 @@ float Measurements::getCorrectedPM25(AirGradient &ag, Configuration &config, boo corrected = ag.pms5003.compensate(pm25, humidity); break; default: { - // All SLR correction using the same flow + // All SLR correction using the same flow, hence default condition corrected = ag.pms5003.slrCorrection(pm25, pm003Count, pmCorrection.scalingFactor, pmCorrection.intercept); if (pmCorrection.useEPA) { diff --git a/src/AgValue.h b/src/AgValue.h index 3b129b3..8733001 100644 --- a/src/AgValue.h +++ b/src/AgValue.h @@ -114,6 +114,14 @@ public: */ float getFloat(MeasurementType type, int ch = 1); + /** + * @brief Get the target measurement average value + * + * @param type measurement type that will be retrieve + * @param ch target type value channel + * @return moving average value of target measurements type + */ + float getAverage(MeasurementType type, int ch = 1); /** * @brief Get the Corrected PM25 object based on the correction algorithm from configuration