diff options
author | Nick Peskett <rockbox@peskett.co.uk> | 2012-01-08 12:07:17 +0000 |
---|---|---|
committer | Nick Peskett <rockbox@peskett.co.uk> | 2012-01-08 12:07:17 +0000 |
commit | 8fdef407a4b1edb774c49eb892a91512135c83db (patch) | |
tree | de358475267817c83911556bb13529eba5d32e84 | |
parent | f1fc6bae253d55ba4faf0abeeb1c8c3e125627e7 (diff) | |
download | rockbox-8fdef407a4b1edb774c49eb892a91512135c83db.tar.gz rockbox-8fdef407a4b1edb774c49eb892a91512135c83db.zip |
Simulator: Emulate a period of external power after charging.
Also a bit of optimisation of battery_status_update().
FS#12506
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31635 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | uisimulator/common/powermgmt-sim.c | 84 |
1 files changed, 55 insertions, 29 deletions
diff --git a/uisimulator/common/powermgmt-sim.c b/uisimulator/common/powermgmt-sim.c index 4445185bb9..41732c94a2 100644 --- a/uisimulator/common/powermgmt-sim.c +++ b/uisimulator/common/powermgmt-sim.c | |||
@@ -30,51 +30,76 @@ | |||
30 | #define BATT_MAXMVOLT 4300 /* maximum millivolts of battery */ | 30 | #define BATT_MAXMVOLT 4300 /* maximum millivolts of battery */ |
31 | #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in | 31 | #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in |
32 | minutes */ | 32 | minutes */ |
33 | /* Number of millivolts to discharge the battery every second */ | ||
34 | #define BATT_DISCHARGE_STEP ((BATT_MAXMVOLT - BATT_MINMVOLT) / 100) | ||
35 | /* Number of millivolts to charge the battery every second */ | ||
36 | #define BATT_CHARGE_STEP (BATT_DISCHARGE_STEP * 2) | ||
37 | #if CONFIG_CHARGING >= CHARGING_MONITOR | ||
38 | /* Number of seconds to externally power before discharging again */ | ||
39 | #define POWER_AFTER_CHARGE_TICKS (8 * HZ) | ||
40 | #endif | ||
33 | 41 | ||
34 | extern int battery_percent; | 42 | extern int battery_percent; |
35 | static bool charging = false; | 43 | static bool charging = false; |
36 | |||
37 | static unsigned int battery_millivolts = BATT_MAXMVOLT; | 44 | static unsigned int battery_millivolts = BATT_MAXMVOLT; |
38 | 45 | ||
39 | void powermgmt_init_target(void) {} | 46 | void powermgmt_init_target(void) {} |
40 | 47 | ||
41 | static void battery_status_update(void) | 48 | static void battery_status_update(void) |
42 | { | 49 | { |
43 | static long last_tick = 0; | 50 | /* Delay next battery update until tick */ |
51 | static long update_after_tick = 0; | ||
52 | #if CONFIG_CHARGING >= CHARGING_MONITOR | ||
53 | /* When greater than 0, the tick to unplug the external power at */ | ||
54 | static unsigned int ext_power_until_tick = 0; | ||
55 | #endif | ||
44 | 56 | ||
45 | if (TIME_AFTER(current_tick, (last_tick+HZ))) { | 57 | if TIME_BEFORE(current_tick, update_after_tick) |
46 | last_tick = current_tick; | 58 | return; |
47 | 59 | ||
48 | /* change the values: */ | 60 | update_after_tick = current_tick + HZ; |
49 | if (charging) { | 61 | |
50 | if (battery_millivolts >= BATT_MAXMVOLT) { | 62 | #if CONFIG_CHARGING >= CHARGING_MONITOR |
51 | #if CONFIG_CHARGING | 63 | /* Handle period of being externally powered */ |
52 | /* Pretend the charger was disconnected */ | 64 | if (ext_power_until_tick > 0) { |
53 | charger_input_state = CHARGER_UNPLUGGED; | 65 | if (TIME_AFTER(current_tick, ext_power_until_tick)) { |
54 | #endif | 66 | /* Pretend the charger was disconnected */ |
55 | charging = false; | 67 | charger_input_state = CHARGER_UNPLUGGED; |
56 | } | 68 | ext_power_until_tick = 0; |
57 | } | 69 | } |
58 | else { | 70 | return; |
59 | if (battery_millivolts <= BATT_MINMVOLT) { | 71 | } |
60 | #if CONFIG_CHARGING | ||
61 | /* Pretend the charger was connected */ | ||
62 | charger_input_state = CHARGER_PLUGGED; | ||
63 | #endif | 72 | #endif |
64 | charging = true; | ||
65 | } | ||
66 | } | ||
67 | 73 | ||
68 | if (charging) { | 74 | if (charging) { |
69 | battery_millivolts += (BATT_MAXMVOLT - BATT_MINMVOLT) / 50; | 75 | battery_millivolts += BATT_CHARGE_STEP; |
76 | if (battery_millivolts >= BATT_MAXMVOLT) { | ||
77 | charging = false; | ||
78 | battery_percent = 100; | ||
79 | #if CONFIG_CHARGING >= CHARGING_MONITOR | ||
80 | /* Keep external power until tick */ | ||
81 | ext_power_until_tick = current_tick + POWER_AFTER_CHARGE_TICKS; | ||
82 | #elif CONFIG_CHARGING | ||
83 | /* Pretend the charger was disconnected */ | ||
84 | charger_input_state = CHARGER_UNPLUGGED; | ||
85 | #endif | ||
86 | return; | ||
70 | } | 87 | } |
71 | else { | 88 | } else { |
72 | battery_millivolts -= (BATT_MAXMVOLT - BATT_MINMVOLT) / 100; | 89 | battery_millivolts -= BATT_DISCHARGE_STEP; |
90 | if (battery_millivolts <= BATT_MINMVOLT) { | ||
91 | charging = true; | ||
92 | battery_percent = 0; | ||
93 | #if CONFIG_CHARGING | ||
94 | /* Pretend the charger was connected */ | ||
95 | charger_input_state = CHARGER_PLUGGED; | ||
96 | #endif | ||
97 | return; | ||
73 | } | 98 | } |
74 | |||
75 | battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / | ||
76 | (BATT_MAXMVOLT - BATT_MINMVOLT); | ||
77 | } | 99 | } |
100 | |||
101 | battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / | ||
102 | (BATT_MAXMVOLT - BATT_MINMVOLT); | ||
78 | } | 103 | } |
79 | 104 | ||
80 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = { 3200 }; | 105 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = { 3200 }; |
@@ -96,7 +121,8 @@ int _battery_voltage(void) | |||
96 | #if CONFIG_CHARGING | 121 | #if CONFIG_CHARGING |
97 | unsigned int power_input_status(void) | 122 | unsigned int power_input_status(void) |
98 | { | 123 | { |
99 | return charging ? POWER_INPUT_CHARGER : POWER_INPUT_NONE; | 124 | return charger_input_state >= CHARGER_PLUGGED |
125 | ? POWER_INPUT_CHARGER : POWER_INPUT_NONE; | ||
100 | } | 126 | } |
101 | 127 | ||
102 | bool charging_state(void) | 128 | bool charging_state(void) |