Skip to content

Commit

Permalink
smoother p1
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Aug 29, 2023
1 parent 43439d8 commit 9366190
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/driver/drv_adcSmoother.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 9366190

Please sign in to comment.