mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-13 18:17:39 -04:00
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
This commit is contained in:
parent
a7703e4926
commit
af872b54ec
1 changed files with 19 additions and 12 deletions
|
@ -392,17 +392,20 @@ static void battery_status_update(void)
|
||||||
int current = battery_current();
|
int current = battery_current();
|
||||||
int resolution = battery_capacity * 36;
|
int resolution = battery_capacity * 36;
|
||||||
|
|
||||||
int time_est;
|
int time_est = 0;
|
||||||
|
if(level >= 0 && current > 0) {
|
||||||
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
||||||
if (charging_state())
|
if (charging_state())
|
||||||
time_est = (100 - level) * battery_capacity * 36 / current;
|
time_est = (100 - level) * battery_capacity * 36 / current;
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
time_est = level * battery_capacity * 36 / current;
|
time_est = level * battery_capacity * 36 / current;
|
||||||
|
|
||||||
/* The first term nudges the counter toward the estimate.
|
/* The first term nudges the counter toward the estimate. */
|
||||||
* The second term decrements the counter due to elapsed time. */
|
time_err += current * (time_est - time_cnt);
|
||||||
time_err += current * (time_est - time_cnt);
|
}
|
||||||
|
|
||||||
|
/* The second term decrements the counter due to elapsed time. */
|
||||||
time_err -= resolution;
|
time_err -= resolution;
|
||||||
|
|
||||||
/* Arbitrary cutoff to ensure we don't get too far out
|
/* Arbitrary cutoff to ensure we don't get too far out
|
||||||
|
@ -413,13 +416,17 @@ static void battery_status_update(void)
|
||||||
time_err = 0;
|
time_err = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert the error into a time and adjust the counter. */
|
if(resolution > 0) {
|
||||||
int64_t adjustment = time_err / (2 * resolution);
|
/* Convert the error into a time and adjust the counter. */
|
||||||
time_cnt += adjustment;
|
int64_t adjustment = time_err / (2 * resolution);
|
||||||
time_err -= adjustment * (2 * resolution);
|
time_cnt += adjustment;
|
||||||
|
time_err -= adjustment * (2 * resolution);
|
||||||
|
}
|
||||||
|
|
||||||
/* Update the reported time based on the counter. */
|
/* Update the reported time based on the counter. */
|
||||||
time_now = (time_cnt + 30) / 60;
|
time_now = (time_cnt + 30) / 60;
|
||||||
|
if(time_now < 0)
|
||||||
|
time_now = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
percent_now = level;
|
percent_now = level;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue