diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2021-12-12 12:30:50 +0000 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2021-12-23 00:12:43 +0000 |
commit | 4506f2b58d533a5cf0055f43d234a19fa3ae2bdb (patch) | |
tree | 72cf14e1389f5b539394d0132bebc1d2aca96119 /uisimulator | |
parent | d6faef8205ef7aedaa81571c05c886150c9c581b (diff) | |
download | rockbox-4506f2b58d533a5cf0055f43d234a19fa3ae2bdb.tar.gz rockbox-4506f2b58d533a5cf0055f43d234a19fa3ae2bdb.zip |
powermgmt: Refactor battery measurement code
I've tried to clean up cruft and clarify what's being
done, prior to adding new features. The behavior should
be unchanged.
Change-Id: If8a68b7b48173d283df981a21cdd854b714e7695
Diffstat (limited to 'uisimulator')
-rw-r--r-- | uisimulator/common/powermgmt-sim.c | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/uisimulator/common/powermgmt-sim.c b/uisimulator/common/powermgmt-sim.c index 511648bc9d..5d669bcae2 100644 --- a/uisimulator/common/powermgmt-sim.c +++ b/uisimulator/common/powermgmt-sim.c | |||
@@ -39,9 +39,10 @@ | |||
39 | #define POWER_AFTER_CHARGE_TICKS (8 * HZ) | 39 | #define POWER_AFTER_CHARGE_TICKS (8 * HZ) |
40 | #endif | 40 | #endif |
41 | 41 | ||
42 | extern int battery_percent; | ||
43 | static bool charging = false; | 42 | static bool charging = false; |
44 | static unsigned int battery_millivolts = BATT_MAXMVOLT; | 43 | static unsigned int batt_millivolts = BATT_MAXMVOLT; |
44 | static unsigned int batt_percent = 100; | ||
45 | static unsigned int batt_runtime = BATT_MAXRUNTIME; | ||
45 | 46 | ||
46 | void powermgmt_init_target(void) {} | 47 | void powermgmt_init_target(void) {} |
47 | 48 | ||
@@ -54,7 +55,7 @@ static void battery_status_update(void) | |||
54 | static unsigned int ext_power_until_tick = 0; | 55 | static unsigned int ext_power_until_tick = 0; |
55 | #endif | 56 | #endif |
56 | 57 | ||
57 | if TIME_BEFORE(current_tick, update_after_tick) | 58 | if(TIME_BEFORE(current_tick, update_after_tick)) |
58 | return; | 59 | return; |
59 | 60 | ||
60 | update_after_tick = current_tick + HZ; | 61 | update_after_tick = current_tick + HZ; |
@@ -72,10 +73,9 @@ static void battery_status_update(void) | |||
72 | #endif | 73 | #endif |
73 | 74 | ||
74 | if (charging) { | 75 | if (charging) { |
75 | battery_millivolts += BATT_CHARGE_STEP; | 76 | batt_millivolts += BATT_CHARGE_STEP; |
76 | if (battery_millivolts >= BATT_MAXMVOLT) { | 77 | if (batt_millivolts >= BATT_MAXMVOLT) { |
77 | charging = false; | 78 | charging = false; |
78 | battery_percent = 100; | ||
79 | #if CONFIG_CHARGING >= CHARGING_MONITOR | 79 | #if CONFIG_CHARGING >= CHARGING_MONITOR |
80 | /* Keep external power until tick */ | 80 | /* Keep external power until tick */ |
81 | ext_power_until_tick = current_tick + POWER_AFTER_CHARGE_TICKS; | 81 | ext_power_until_tick = current_tick + POWER_AFTER_CHARGE_TICKS; |
@@ -83,23 +83,20 @@ static void battery_status_update(void) | |||
83 | /* Pretend the charger was disconnected */ | 83 | /* Pretend the charger was disconnected */ |
84 | charger_input_state = CHARGER_UNPLUGGED; | 84 | charger_input_state = CHARGER_UNPLUGGED; |
85 | #endif | 85 | #endif |
86 | return; | ||
87 | } | 86 | } |
88 | } else { | 87 | } else { |
89 | battery_millivolts -= BATT_DISCHARGE_STEP; | 88 | batt_millivolts -= BATT_DISCHARGE_STEP; |
90 | if (battery_millivolts <= BATT_MINMVOLT) { | 89 | if (batt_millivolts <= BATT_MINMVOLT) { |
91 | charging = true; | 90 | charging = true; |
92 | battery_percent = 0; | ||
93 | #if CONFIG_CHARGING | 91 | #if CONFIG_CHARGING |
94 | /* Pretend the charger was connected */ | 92 | /* Pretend the charger was connected */ |
95 | charger_input_state = CHARGER_PLUGGED; | 93 | charger_input_state = CHARGER_PLUGGED; |
96 | #endif | 94 | #endif |
97 | return; | ||
98 | } | 95 | } |
99 | } | 96 | } |
100 | 97 | ||
101 | battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / | 98 | batt_percent = (batt_millivolts - BATT_MINMVOLT) / (BATT_MAXMVOLT - BATT_MINMVOLT); |
102 | (BATT_MAXMVOLT - BATT_MINMVOLT); | 99 | batt_runtime = batt_percent * BATT_MAXRUNTIME; |
103 | } | 100 | } |
104 | 101 | ||
105 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = { 3200 }; | 102 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = { 3200 }; |
@@ -111,15 +108,28 @@ const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] = | |||
111 | const unsigned short percent_to_volt_charge[11] = | 108 | const unsigned short percent_to_volt_charge[11] = |
112 | { 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300 }; | 109 | { 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300 }; |
113 | 110 | ||
111 | #if CONFIG_BATTERY_MEASURE & VOLTAGE_MEASURE | ||
114 | int _battery_voltage(void) | 112 | int _battery_voltage(void) |
115 | { | 113 | { |
116 | battery_status_update(); | 114 | battery_status_update(); |
117 | return battery_millivolts; | 115 | return batt_millivolts; |
118 | } | 116 | } |
117 | #endif | ||
118 | |||
119 | #if CONFIG_BATTERY_MEASURE & PERCENTAGE_MEASURE | ||
120 | int _battery_level(void) | ||
121 | { | ||
122 | battery_status_update(); | ||
123 | return batt_percent; | ||
124 | } | ||
125 | #endif | ||
119 | 126 | ||
120 | #if (CONFIG_BATTERY_MEASURE & TIME_MEASURE) | 127 | #if (CONFIG_BATTERY_MEASURE & TIME_MEASURE) |
121 | static int powermgmt_est_runningtime_min; | 128 | int _battery_time(void) |
122 | int _battery_time(void) { return powermgmt_est_runningtime_min; } | 129 | { |
130 | battery_status_update(); | ||
131 | return batt_runtime; | ||
132 | } | ||
123 | #endif | 133 | #endif |
124 | 134 | ||
125 | #if CONFIG_CHARGING | 135 | #if CONFIG_CHARGING |
@@ -169,6 +179,7 @@ unsigned int input_millivolts(void) | |||
169 | /* Just return a safe value if battery isn't connected */ | 179 | /* Just return a safe value if battery isn't connected */ |
170 | return 4050; | 180 | return 4050; |
171 | } | 181 | } |
172 | return battery_voltage();; | 182 | |
183 | return battery_voltage(); | ||
173 | } | 184 | } |
174 | #endif | 185 | #endif |