From 9fd74000ab552bf3f5ea2e3bf3de36368d3c2ad9 Mon Sep 17 00:00:00 2001 From: paradajz <2544094+paradajz@users.noreply.github.com> Date: Mon, 10 Jan 2022 16:26:08 +0100 Subject: [PATCH] analog: make the usage of filters configurable --- scripts/gen_target.sh | 14 ++++++++++++++ src/application/io/analog/Filter.h | 28 +++++++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/scripts/gen_target.sh b/scripts/gen_target.sh index 9d90cb16a..ee7e4251a 100755 --- a/scripts/gen_target.sh +++ b/scripts/gen_target.sh @@ -910,6 +910,20 @@ then printf "%s\n" "DEFINES += ADC_EXT_REF" >> "$OUT_FILE_MAKEFILE_DEFINES" fi + analog_filter_median=$($YAML_PARSER "$TARGET_DEF_FILE" analog.filter.median) + analog_filter_ema=$($YAML_PARSER "$TARGET_DEF_FILE" analog.filter.ema) + + # use filters by default if not specified + if [[ $analog_filter_median != "false" ]] + then + printf "%s\n" "DEFINES += ANALOG_USE_MEDIAN_FILTER" >> "$OUT_FILE_MAKEFILE_DEFINES" + fi + + if [[ $analog_filter_ema != "false" ]] + then + printf "%s\n" "DEFINES += ANALOG_USE_EMA_FILTER" >> "$OUT_FILE_MAKEFILE_DEFINES" + fi + analog_in_type=$($YAML_PARSER "$TARGET_DEF_FILE" analog.type) declare -i nr_of_analog_inputs diff --git a/src/application/io/analog/Filter.h b/src/application/io/analog/Filter.h index 3c6012d32..4bb767fca 100644 --- a/src/application/io/analog/Filter.h +++ b/src/application/io/analog/Filter.h @@ -26,11 +26,8 @@ limitations under the License. #include "io/analog/Analog.h" #include "core/src/general/Helpers.h" -#ifndef MEDIAN_SAMPLE_COUNT +#ifdef ANALOG_USE_MEDIAN_FILTER #define MEDIAN_SAMPLE_COUNT 3 -#endif - -#ifndef MEDIAN_MIDDLE_VALUE #define MEDIAN_MIDDLE_VALUE 1 #endif @@ -90,6 +87,7 @@ namespace IO } #ifdef ADC_SUPPORTED +#ifdef ANALOG_USE_MEDIAN_FILTER auto compare = [](const void* a, const void* b) { if (*(uint16_t*)a < *(uint16_t*)b) return -1; @@ -98,6 +96,7 @@ namespace IO return 0; }; +#endif const bool fastFilter = (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)) ? (core::timing::currentRunTimeMs() - _lastStableMovementTime[index]) < FAST_FILTER_ENABLE_AFTER_MS : true; #else @@ -118,8 +117,10 @@ namespace IO if (abs(value - _lastStableValue[index]) < stepDiff) { #ifdef ADC_SUPPORTED +#ifdef ANALOG_USE_MEDIAN_FILTER if (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)) _medianSampleCounter[index] = 0; +#endif #endif return false; @@ -130,6 +131,7 @@ namespace IO { // don't filter the readings for touchscreen data +#ifdef ANALOG_USE_MEDIAN_FILTER if (!fastFilter) { _analogSample[index][_medianSampleCounter[index]++] = value; @@ -147,12 +149,15 @@ namespace IO } } else +#endif { filteredValue = value; } +#ifdef ANALOG_USE_EMA_FILTER if (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)) filteredValue = _emaFilter[index].value(filteredValue); +#endif } else { @@ -198,7 +203,9 @@ namespace IO #ifdef ADC_SUPPORTED if (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)) { - _medianSampleCounter[index] = 0; +#ifdef ANALOG_USE_MEDIAN_FILTER + _medianSampleCounter[index] = 0; +#endif _lastStableMovementTime[index] = 0; } #endif @@ -260,10 +267,17 @@ namespace IO // some filtering is needed for adc only #ifdef ADC_SUPPORTED - EMA _emaFilter[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)]; +#ifdef ANALOG_USE_EMA_FILTER + EMA _emaFilter[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)]; +#endif + +#ifdef ANALOG_USE_MEDIAN_FILTER uint16_t _analogSample[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)][MEDIAN_SAMPLE_COUNT] = {}; uint8_t _medianSampleCounter[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {}; - uint32_t _lastStableMovementTime[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {}; +#else + uint16_t _analogSample[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {}; +#endif + uint32_t _lastStableMovementTime[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {}; #endif uint8_t _lastStableDirection[IO::Analog::Collection::size() / 8 + 1] = {};