From 9366190698506990f7e84b0c08cb535c7a53fe5c Mon Sep 17 00:00:00 2001 From: Tester23 Date: Tue, 29 Aug 2023 08:08:47 +0200 Subject: [PATCH] smoother p1 --- src/driver/drv_adcSmoother.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/driver/drv_adcSmoother.c diff --git a/src/driver/drv_adcSmoother.c b/src/driver/drv_adcSmoother.c new file mode 100644 index 000000000..d1193f488 --- /dev/null +++ b/src/driver/drv_adcSmoother.c @@ -0,0 +1,35 @@ +#include "../new_common.h" +#include "../new_pins.h" +#include "../quicktick.h" +#include "../cmnds/cmd_public.h" +#include "drv_local.h" +#include "drv_public.h" +#include "../logging/logging.h" +#include "../hal/hal_pins.h" +#include "../hal/hal_adc.h" +#include "../mqtt/new_mqtt.h" + +static int *g_samples = NULL; +static int g_samplesCount = 0; +static int g_nextSample = 0; + +void ADCSmoother_SetupWindow(int size) { + g_samples = realloc(g_samples, sizeof(int) * size); + memset(g_samples,0, sizeof(int) * size); + g_samplesCount = size; +} +void ADCSmoother_AppendSample(int val) { + if (g_samples == 0) + return; + g_samples[g_nextSample] = val; + g_nextSample++; + g_nextSample %= g_samplesCount; +} +float ADCSmoother_Sample() { + float s = 0; + for (int i = 0; i < g_samplesCount; i++) { + s += g_samples[i]; + } + s /= g_samplesCount; + return s; +}