From 805d1a88e49dd974456f2f29d1a421cd33d04791 Mon Sep 17 00:00:00 2001 From: mooiweertje Date: Tue, 11 Nov 2025 15:41:07 +0100 Subject: [PATCH] ADCOptimization --- main/bat.cpp | 28 +++++++++++++--------------- sdkconfig.supermini | 4 ++-- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/main/bat.cpp b/main/bat.cpp index ebc61a9..e262a30 100644 --- a/main/bat.cpp +++ b/main/bat.cpp @@ -20,9 +20,11 @@ static adc_cali_handle_t adc1_cali_handle = NULL; static uint32_t batMv = 27600; // We try to measure every 100ms, so 100 points gives us 10 seconds history. -static uint8_t history[100]; -static size_t historyIndex = 0; -static size_t historySize = 0; +static uint32_t history; +static uint8_t batPercentage; + +// static size_t historyIndex = 0; +// static size_t historySize = 0; static void adc_calibration_init(adc_unit_t unit, adc_atten_t atten) { esp_err_t ret = ESP_FAIL; @@ -130,11 +132,12 @@ static uint8_t batMvToPercentage(uint32_t batMv) { void measureBat() { batMv = measureBatMv(); - history[historyIndex] = batMvToPercentage(batMv); - historyIndex = (historyIndex + 1) % sizeof(history); - if(historySize < sizeof(history)) { - historySize++; - } + + history += batMv; + uint32_t avg = history >> 7; + history -= avg; + + batPercentage = batMvToPercentage(avg); } uint32_t getBatMv() { @@ -142,17 +145,12 @@ uint32_t getBatMv() { } uint8_t getBatPercentage() { - if(historySize == 0) { + if(batPercentage == 0) { // Use a fake value of 50% when we don't have ADC. return 50; } - uint32_t historyTotal = 0; - for(int index = 0; index < historySize; index++) { - historyTotal += history[index]; - } - - return historyTotal / historySize; + return batPercentage; } void adc_teardown() { diff --git a/sdkconfig.supermini b/sdkconfig.supermini index 7b1ba63..d4c77a6 100644 --- a/sdkconfig.supermini +++ b/sdkconfig.supermini @@ -24,7 +24,7 @@ CONFIG_ION_LIGHT=n CONFIG_ION_LIGHT_PIN=1 CONFIG_ION_LIGHT_PIN_INVERTED=n -CONFIG_ION_RELAY=n +CONFIG_ION_RELAY=y CONFIG_ION_RELAY_PIN=0 CONFIG_ION_RELAY_PIN_INVERTED=n @@ -32,7 +32,7 @@ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_160=y CONFIG_ION_ADC=y # ADC channel 1 is pin 4 -CONFIG_ION_ADC_CHAN=4 +CONFIG_ION_ADC_CHAN=2 # The scale of the divider, times 1000, from APM power module CONFIG_ION_DIVIDER_SCALE=10829