diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2021-12-31 16:47:44 +0000 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2022-01-02 20:11:03 +0000 |
commit | af872b54ec3f8f7ad6ec111e4fe80dd2c7ac9946 (patch) | |
tree | 83b03175ca50757359d98d2ad965ca8dfdfc773f | |
parent | a7703e4926075ee41269ca96d1806444107e65f2 (diff) | |
download | rockbox-af872b54ec3f8f7ad6ec111e4fe80dd2c7ac9946.tar.gz rockbox-af872b54ec3f8f7ad6ec111e4fe80dd2c7ac9946.zip |
powermgmt: Bugfixes to time estimation code
Guard against division by zero and prevent the time_now
value from going negative if the counter drops below zero.
Change-Id: Ia8cadfe76086d6d0200964c1f27bab0be708b135
-rw-r--r-- | firmware/powermgmt.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c index 0f5b104d7c..2cafd4b759 100644 --- a/firmware/powermgmt.c +++ b/firmware/powermgmt.c | |||
@@ -392,17 +392,20 @@ static void battery_status_update(void) | |||
392 | int current = battery_current(); | 392 | int current = battery_current(); |
393 | int resolution = battery_capacity * 36; | 393 | int resolution = battery_capacity * 36; |
394 | 394 | ||
395 | int time_est; | 395 | int time_est = 0; |
396 | if(level >= 0 && current > 0) { | ||
396 | #if CONFIG_CHARGING >= CHARGING_MONITOR | 397 | #if CONFIG_CHARGING >= CHARGING_MONITOR |
397 | if (charging_state()) | 398 | if (charging_state()) |
398 | time_est = (100 - level) * battery_capacity * 36 / current; | 399 | time_est = (100 - level) * battery_capacity * 36 / current; |
399 | else | 400 | else |
400 | #endif | 401 | #endif |
401 | time_est = level * battery_capacity * 36 / current; | 402 | time_est = level * battery_capacity * 36 / current; |
403 | |||
404 | /* The first term nudges the counter toward the estimate. */ | ||
405 | time_err += current * (time_est - time_cnt); | ||
406 | } | ||
402 | 407 | ||
403 | /* The first term nudges the counter toward the estimate. | 408 | /* The second term decrements the counter due to elapsed time. */ |
404 | * The second term decrements the counter due to elapsed time. */ | ||
405 | time_err += current * (time_est - time_cnt); | ||
406 | time_err -= resolution; | 409 | time_err -= resolution; |
407 | 410 | ||
408 | /* Arbitrary cutoff to ensure we don't get too far out | 411 | /* Arbitrary cutoff to ensure we don't get too far out |
@@ -413,13 +416,17 @@ static void battery_status_update(void) | |||
413 | time_err = 0; | 416 | time_err = 0; |
414 | } | 417 | } |
415 | 418 | ||
416 | /* Convert the error into a time and adjust the counter. */ | 419 | if(resolution > 0) { |
417 | int64_t adjustment = time_err / (2 * resolution); | 420 | /* Convert the error into a time and adjust the counter. */ |
418 | time_cnt += adjustment; | 421 | int64_t adjustment = time_err / (2 * resolution); |
419 | time_err -= adjustment * (2 * resolution); | 422 | time_cnt += adjustment; |
423 | time_err -= adjustment * (2 * resolution); | ||
424 | } | ||
420 | 425 | ||
421 | /* Update the reported time based on the counter. */ | 426 | /* Update the reported time based on the counter. */ |
422 | time_now = (time_cnt + 30) / 60; | 427 | time_now = (time_cnt + 30) / 60; |
428 | if(time_now < 0) | ||
429 | time_now = 0; | ||
423 | #endif | 430 | #endif |
424 | 431 | ||
425 | percent_now = level; | 432 | percent_now = level; |