From 6925b1ac9ae65c79bcb19254becd8774c02555f3 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sun, 13 Oct 2024 23:30:03 +0700 Subject: [PATCH] Provide channel for neccessary ValueType To support OA that have 2 PMS sensor --- src/AgValue.cpp | 80 ++++++++++++++++++++++++++++++++++++------------- src/AgValue.h | 21 ++++++++----- 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/src/AgValue.cpp b/src/AgValue.cpp index 0190852..eae3166 100644 --- a/src/AgValue.cpp +++ b/src/AgValue.cpp @@ -8,10 +8,12 @@ void Measurements::maxUpdate(AgValueType type, int max) { switch (type) { case AgValueType::Temperature: - _temperature.update.max = max; + _temperature[0].update.max = max; + _temperature[1].update.max = max; break; case AgValueType::Humidity: - _humidity.update.max = max; + _humidity[0].update.max = max; + _humidity[1].update.max = max; break; case AgValueType::CO2: _co2.update.max = max; @@ -29,21 +31,35 @@ void Measurements::maxUpdate(AgValueType type, int max) { _nox_raw.update.max = max; break; case AgValueType::PM25: - _pm_25.update.max = max; + _pm_25[0].update.max = max; + _pm_25[1].update.max = max; break; case AgValueType::PM01: - _pm_01.update.max = max; + _pm_01[0].update.max = max; + _pm_01[1].update.max = max; break; case AgValueType::PM10: - _pm_10.update.max = max; + _pm_10[0].update.max = max; + _pm_10[1].update.max = max; break; case AgValueType::PM03_PC: - _pm_03_pc.update.max = max; + _pm_03_pc[0].update.max = max; + _pm_03_pc[1].update.max = max; break; }; } -bool Measurements::updateValue(AgValueType type, int val) { +bool Measurements::updateValue(AgValueType type, int val, int ch) { + // Validate channel + if (ch != 1 || ch != 2) { + Serial.printf(" Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch); + // TODO: Perhaps just do assert + return false; + } + + // Follow array indexing just for get address of the value type + ch = ch - 1; + // Define data point source IntegerValue *temporary = nullptr; float invalidValue = 0; @@ -69,19 +85,19 @@ bool Measurements::updateValue(AgValueType type, int val) { invalidValue = utils::getInvalidNOx(); break; case AgValueType::PM25: - temporary = &_pm_25; + temporary = &_pm_25[ch]; invalidValue = utils::getInvalidPmValue(); break; case AgValueType::PM01: - temporary = &_pm_01; + temporary = &_pm_01[ch]; invalidValue = utils::getInvalidPmValue(); break; case AgValueType::PM10: - temporary = &_pm_10; + temporary = &_pm_10[ch]; invalidValue = utils::getInvalidPmValue(); break; case AgValueType::PM03_PC: - temporary = &_pm_03_pc; + temporary = &_pm_03_pc[ch]; invalidValue = utils::getInvalidPmValue(); break; default: @@ -92,9 +108,12 @@ bool Measurements::updateValue(AgValueType type, int val) { if (temporary == nullptr) { Serial.printf("%s is not defined for integer data type\n", agValueTypeStr(type)); // TODO: Just assert? - return; + return false; } + // Restore channel value for debugging purpose + ch = ch + 1; + // Update new value when value provided is not the invalid one if (val != invalidValue) { temporary->lastValue = val; @@ -107,14 +126,17 @@ bool Measurements::updateValue(AgValueType type, int val) { // Calculate value average when maximum set is reached if (temporary->update.counter >= temporary->update.max) { + // TODO: Need to check if SUCCESS == 0, what should we do? // Calculate the average temporary->avg = temporary->sumValues / temporary->update.success; + Serial.printf("%s{%d} count reached! Average value %d\n", agValueTypeStr(type), ch, + temporary->avg); - // This is just for notifying + // Notify if there's are invalid value when updating int miss = temporary->update.max - temporary->update.success; if (miss != 0) { - Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss, - temporary->update.max); + Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch, + miss, temporary->update.max); } // Resets average related variable calculation @@ -127,17 +149,27 @@ bool Measurements::updateValue(AgValueType type, int val) { return false; } -bool Measurements::updateValue(AgValueType type, float val) { +bool Measurements::updateValue(AgValueType type, float val, int ch) { + // Validate channel + if (ch != 1 || ch != 2) { + Serial.printf(" Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch); + // TODO: Perhaps just do assert + return false; + } + + // Follow array indexing just for get address of the value type + ch = ch - 1; + // Define data point source FloatValue *temporary = nullptr; float invalidValue = 0; switch (type) { case AgValueType::Temperature: - temporary = &_temperature; + temporary = &_temperature[ch]; invalidValue = utils::getInvalidTemperature(); break; case AgValueType::Humidity: - temporary = &_humidity; + temporary = &_humidity[ch]; invalidValue = utils::getInvalidHumidity(); break; default: @@ -148,9 +180,12 @@ bool Measurements::updateValue(AgValueType type, float val) { if (temporary == nullptr) { Serial.printf("%s is not defined for float data type\n", agValueTypeStr(type)); // TODO: Just assert? - return; + return false; } + // Restore channel value for debugging purpose + ch = ch + 1; + // Update new value when value provided is not the invalid one if (val != invalidValue) { temporary->lastValue = val; @@ -163,14 +198,17 @@ bool Measurements::updateValue(AgValueType type, float val) { // Calculate value average when maximum set is reached if (temporary->update.counter >= temporary->update.max) { + // TODO: Need to check if SUCCESS == 0 // Calculate the average temporary->avg = temporary->sumValues / temporary->update.success; + Serial.printf("%s{%d} count reached! Average value %0.2f\n", agValueTypeStr(type), + temporary->avg); // This is just for notifying int miss = temporary->update.max - temporary->update.success; if (miss != 0) { - Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss, - temporary->update.max); + Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch, + miss, temporary->update.max); } // Resets average related variable calculation diff --git a/src/AgValue.h b/src/AgValue.h index 225c041..247497d 100644 --- a/src/AgValue.h +++ b/src/AgValue.h @@ -86,10 +86,12 @@ public: * * @param type (AgValueType) value type that will be updated * @param val (int) the new value + * @param ch (int) the AgValueType channel, not every AgValueType has more than 1 channel. + * Currently maximum channel is 2. Default: 1 (channel 1) * @return true if update counter reached and new average value is calculated * @return false otherwise */ - bool updateValue(AgValueType type, int val); + bool updateValue(AgValueType type, int val, int ch = 1); /** * @brief update target type value with new value. @@ -98,10 +100,12 @@ public: * * @param type (AgValueType) value type that will be updated * @param val (float) the new value + * @param ch (int) the AgValueType channel, not every AgValueType has more than 1 channel. + * Currently maximum channel is 2. Default: 1 (channel 1) * @return true if update counter reached and new average value is calculated * @return false otherwise */ - bool updateValue(AgValueType type, float val); + bool updateValue(AgValueType type, float val, int ch = 1); float Temperature; int Humidity; @@ -144,17 +148,18 @@ public: String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void *_ag, void *_config); private: - FloatValue _temperature; - FloatValue _humidity; + // Some declared as an array (channel), because FW_MODE_O_1PPx has two PMS5003T + FloatValue _temperature[2]; + FloatValue _humidity[2]; IntegerValue _co2; IntegerValue _tvoc; IntegerValue _tvoc_raw; IntegerValue _nox; IntegerValue _nox_raw; - IntegerValue _pm_25; - IntegerValue _pm_01; - IntegerValue _pm_10; - IntegerValue _pm_03_pc; // particle count 0.3 + IntegerValue _pm_25[2]; + IntegerValue _pm_01[2]; + IntegerValue _pm_10[2]; + IntegerValue _pm_03_pc[2]; // particle count 0.3 /** * @brief Get PMS5003 firmware version string