mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
fix potential prev_val == 0 init issue
This commit is contained in:
parent
ee806390f3
commit
a3dca29b43
@ -10,8 +10,8 @@ void BatteryPercentageRingBuffer::init(uint16_t v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BatteryPercentageRingBuffer::update(uint16_t v) {
|
void BatteryPercentageRingBuffer::update(uint16_t v) {
|
||||||
// Previous percentage is set to 161 only if buffer was constructed but not initialized yet
|
// Previous percentage is set > 100 only if buffer was constructed but not initialized yet
|
||||||
if (!prev_val) {
|
if (prev_val > 100) {
|
||||||
init(v);
|
init(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ uint16_t BatteryPercentageRingBuffer::evaluate() {
|
|||||||
float avg = (sum / MAX_SAMPLES) + 0.5;
|
float avg = (sum / MAX_SAMPLES) + 0.5;
|
||||||
uint16_t new_val = (int)avg;
|
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) {
|
if (new_val < prev_val) {
|
||||||
prev_val = new_val;
|
prev_val = new_val;
|
||||||
return new_val;
|
return new_val;
|
||||||
|
|||||||
@ -13,7 +13,7 @@ struct BatteryPercentageRingBuffer {
|
|||||||
uint16_t buf[MAX_SAMPLES];
|
uint16_t buf[MAX_SAMPLES];
|
||||||
uint16_t head = 0;
|
uint16_t head = 0;
|
||||||
uint16_t sum = 0;
|
uint16_t sum = 0;
|
||||||
uint16_t prev_val = 0;
|
uint16_t prev_val = 161;
|
||||||
|
|
||||||
void init(uint16_t value);
|
void init(uint16_t value);
|
||||||
void update(uint16_t value);
|
void update(uint16_t value);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user