fix potential prev_val == 0 init issue

This commit is contained in:
Dave ID 2026-02-03 16:05:30 +01:00
parent ee806390f3
commit a3dca29b43
2 changed files with 4 additions and 4 deletions

View File

@ -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;

View File

@ -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);