1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
11 * Revisions copyright (C) 2005 by Gerald Van Baren
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
26 #include "powermgmt.h"
28 #define BATT_MINMVOLT 2500 /* minimum millivolts of battery */
29 #define BATT_MAXMVOLT 4500 /* maximum millivolts of battery */
30 #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in
33 extern void send_battery_level_event(void);
34 extern int last_sent_battery_level
;
35 extern int battery_percent
;
37 static unsigned int battery_millivolts
= BATT_MAXMVOLT
;
38 /* estimated remaining time in minutes */
39 static int powermgmt_est_runningtime_min
= BATT_MAXRUNTIME
;
41 static void battery_status_update(void)
43 static time_t last_change
= 0;
44 static bool charging
= false;
49 if (last_change
< now
) {
52 /* change the values: */
54 if (battery_millivolts
>= BATT_MAXMVOLT
) {
55 /* Pretend the charger was disconnected */
57 queue_broadcast(SYS_CHARGER_DISCONNECTED
, 0);
58 last_sent_battery_level
= 100;
62 if (battery_millivolts
<= BATT_MINMVOLT
) {
63 /* Pretend the charger was connected */
65 queue_broadcast(SYS_CHARGER_CONNECTED
, 0);
66 last_sent_battery_level
= 0;
71 battery_millivolts
+= (BATT_MAXMVOLT
- BATT_MINMVOLT
) / 50;
74 battery_millivolts
-= (BATT_MAXMVOLT
- BATT_MINMVOLT
) / 100;
77 battery_percent
= 100 * (battery_millivolts
- BATT_MINMVOLT
) /
78 (BATT_MAXMVOLT
- BATT_MINMVOLT
);
80 powermgmt_est_runningtime_min
=
81 battery_percent
* BATT_MAXRUNTIME
/ 100;
84 send_battery_level_event();
87 void battery_read_info(int *voltage
, int *level
)
89 battery_status_update();
92 *voltage
= battery_millivolts
;
95 *level
= battery_percent
;
98 unsigned int battery_voltage(void)
100 battery_status_update();
101 return battery_millivolts
;
104 int battery_level(void)
106 battery_status_update();
107 return battery_percent
;
110 int battery_time(void)
112 battery_status_update();
113 return powermgmt_est_runningtime_min
;
116 bool battery_level_safe(void)
118 return battery_level() >= 10;
121 void set_poweroff_timeout(int timeout
)
126 void set_battery_capacity(int capacity
)
131 #if BATTERY_TYPES_COUNT > 1
132 void set_battery_type(int type
)
138 #ifdef HAVE_ACCESSORY_SUPPLY
139 void accessory_supply_set(bool enable
)
145 void reset_poweroff_timer(void)
149 void shutdown_hw(void)
153 void sys_poweroff(void)
157 void cancel_shutdown(void)