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"
27 #include <SDL_events.h>
29 #define BATT_MINMVOLT 2500 /* minimum millivolts of battery */
30 #define BATT_MAXMVOLT 4500 /* maximum millivolts of battery */
31 #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in
34 extern void send_battery_level_event(void);
35 extern int last_sent_battery_level
;
36 extern int battery_percent
;
38 static unsigned int battery_millivolts
= BATT_MAXMVOLT
;
39 /* estimated remaining time in minutes */
40 static int powermgmt_est_runningtime_min
= BATT_MAXRUNTIME
;
42 static void battery_status_update(void)
44 static time_t last_change
= 0;
45 static bool charging
= false;
50 if (last_change
< now
) {
53 /* change the values: */
55 if (battery_millivolts
>= BATT_MAXMVOLT
) {
56 /* Pretend the charger was disconnected */
58 queue_broadcast(SYS_CHARGER_DISCONNECTED
, 0);
59 last_sent_battery_level
= 100;
63 if (battery_millivolts
<= BATT_MINMVOLT
) {
64 /* Pretend the charger was connected */
66 queue_broadcast(SYS_CHARGER_CONNECTED
, 0);
67 last_sent_battery_level
= 0;
72 battery_millivolts
+= (BATT_MAXMVOLT
- BATT_MINMVOLT
) / 50;
75 battery_millivolts
-= (BATT_MAXMVOLT
- BATT_MINMVOLT
) / 100;
78 battery_percent
= 100 * (battery_millivolts
- BATT_MINMVOLT
) /
79 (BATT_MAXMVOLT
- BATT_MINMVOLT
);
81 powermgmt_est_runningtime_min
=
82 battery_percent
* BATT_MAXRUNTIME
/ 100;
85 send_battery_level_event();
88 void battery_read_info(int *voltage
, int *level
)
90 battery_status_update();
93 *voltage
= battery_millivolts
;
96 *level
= battery_percent
;
99 unsigned int battery_voltage(void)
101 battery_status_update();
102 return battery_millivolts
;
105 int battery_level(void)
107 battery_status_update();
108 return battery_percent
;
111 int battery_time(void)
113 battery_status_update();
114 return powermgmt_est_runningtime_min
;
117 bool battery_level_safe(void)
119 return battery_level() >= 10;
122 void set_poweroff_timeout(int timeout
)
127 void set_battery_capacity(int capacity
)
132 #if BATTERY_TYPES_COUNT > 1
133 void set_battery_type(int type
)
139 #ifdef HAVE_ACCESSORY_SUPPLY
140 void accessory_supply_set(bool enable
)
146 #ifdef HAVE_LINEOUT_POWEROFF
147 void lineout_set(bool enable
)
153 void reset_poweroff_timer(void)
157 void shutdown_hw(void)
159 /* Shut down SDL event loop */
161 memset(&event
, 0, sizeof(SDL_Event
));
162 event
.type
= SDL_USEREVENT
;
163 SDL_PushEvent(&event
);
166 void cancel_shutdown(void)