summaryrefslogtreecommitdiff
path: root/firmware/powermgmt.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/powermgmt.c')
-rw-r--r--firmware/powermgmt.c31
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;