From a3dca29b43289a58e38cc8dc014a61808a6d216f Mon Sep 17 00:00:00 2001 From: Dave ID Date: Tue, 3 Feb 2026 16:05:30 +0100 Subject: [PATCH] fix potential prev_val == 0 init issue --- src/Battery.cpp | 6 +++--- src/Battery.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Battery.cpp b/src/Battery.cpp index ee38a895..2f16709e 100644 --- a/src/Battery.cpp +++ b/src/Battery.cpp @@ -10,8 +10,8 @@ void BatteryPercentageRingBuffer::init(uint16_t v) { } void BatteryPercentageRingBuffer::update(uint16_t v) { - // Previous percentage is set to 161 only if buffer was constructed but not initialized yet - if (!prev_val) { + // Previous percentage is set > 100 only if buffer was constructed but not initialized yet + if (prev_val > 100) { init(v); } @@ -30,7 +30,7 @@ uint16_t BatteryPercentageRingBuffer::evaluate() { float avg = (sum / MAX_SAMPLES) + 0.5; uint16_t new_val = (int)avg; - // Battery percentage should not increse when not charging so we just cap it to be lower than the last value + // Battery percentage should not increase when not charging so we just cap it to be lower than the last value if (new_val < prev_val) { prev_val = new_val; return new_val; diff --git a/src/Battery.h b/src/Battery.h index 564c5a9b..0baf56ae 100644 --- a/src/Battery.h +++ b/src/Battery.h @@ -13,7 +13,7 @@ struct BatteryPercentageRingBuffer { uint16_t buf[MAX_SAMPLES]; uint16_t head = 0; uint16_t sum = 0; - uint16_t prev_val = 0; + uint16_t prev_val = 161; void init(uint16_t value); void update(uint16_t value);