From 8a87b865e66c7281837e63649767eec6f1e61366 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Mon, 21 Oct 2024 22:37:44 +0700 Subject: [PATCH] Handle consecutive invalid value update Set measurements type average value to invalid when invalidCounter reached max period --- src/AgValue.cpp | 26 ++++++++++++++++++++------ src/AgValue.h | 4 ++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/AgValue.cpp b/src/AgValue.cpp index 3a47d3e..d728f56 100644 --- a/src/AgValue.cpp +++ b/src/AgValue.cpp @@ -111,9 +111,16 @@ bool Measurements::update(MeasurementType type, int val, int ch) { if (val == invalidValue) { temporary->update.invalidCounter++; - // TODO: Need to check if its more than threshold, to create some indication. Maybe reference to - // max element? - return false; + if (temporary->update.invalidCounter >= temporary->update.max) { + Serial.printf("%s{%d} invalid value update counter reached (%dx)! Setting its average value " + "to invalid!", + measurementTypeStr(type), ch, temporary->update.max); + temporary->update.avg = invalidValue; + return false; + } + + // Still consider updating value to valid + return true; } // Reset invalid counter when update new valid value @@ -174,9 +181,16 @@ bool Measurements::update(MeasurementType type, float val, int ch) { if (val == invalidValue) { temporary->update.invalidCounter++; - // TODO: Need to check if its more than threshold, to create some indication. Maybe reference to - // max element? - return false; + if (temporary->update.invalidCounter >= temporary->update.max) { + Serial.printf("%s{%d} invalid value update counter reached (%dx)! Setting its average value " + "to invalid!", + measurementTypeStr(type), ch, temporary->update.max); + temporary->update.avg = invalidValue; + return false; + } + + // Still consider updating value to valid + return true; } // Reset invalid counter when update new valid value diff --git a/src/AgValue.h b/src/AgValue.h index 27d98b9..236589a 100644 --- a/src/AgValue.h +++ b/src/AgValue.h @@ -69,7 +69,7 @@ public: * @param val (int) the new value * @param ch (int) the MeasurementType channel, not every MeasurementType has more than 1 channel. * Currently maximum channel is 2. Default: 1 (channel 1) - * @return false if new value invalid consecutively reach threshold + * @return false if new value invalid consecutively reach threshold (max period) * @return true otherwise */ bool update(MeasurementType type, int val, int ch = 1); @@ -83,7 +83,7 @@ public: * @param val (float) the new value * @param ch (int) the MeasurementType channel, not every MeasurementType has more than 1 channel. * Currently maximum channel is 2. Default: 1 (channel 1) - * @return false if new value invalid consecutively reach threshold + * @return false if new value invalid consecutively reach threshold (max period) * @return true otherwise */ bool update(MeasurementType type, float val, int ch = 1);