From c1bd9b0361ba92c29ceef68d74093e70a1a3e481 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Tue, 3 Jan 2012 23:44:38 +0000 Subject: Rework powermgmt to enable code re-use on appliation and sims. * Introduce CONFIG_BATTERY_MEASURE define, to allow targets (application) to break powermgmt.c's assumption about the ability to read battery voltage. There's now additionally percentage (android) and remaining time measure (maemo). No measure at all also works (sdl app). If voltage can't be measured, then battery_level() is king and it'll be used for power_history and runtime estimation. * Implement target's API in the simulator, i.e. _battery_voltage(), so it doesn't need to implement it's own powermgmt.c and other stubs. Now the sim behaves much more like a native target, although it still changes the simulated battery voltage quickly, * Other changes include include renaming battery_adc_voltage() to _battery_voltage(), for consistency with the new target functions and making some of the apps code aware that voltage and runtime estimation is not always available. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31548 a1c6a512-1295-4272-9138-f99709370657 --- firmware/target/hosted/android/powermgmt-android.c | 28 +++-------- firmware/target/hosted/android/system-android.c | 2 +- firmware/target/hosted/maemo/maemo-thread.c | 30 +----------- firmware/target/hosted/powermgmt.c | 56 ---------------------- firmware/target/hosted/sdl/system-sdl.c | 2 +- firmware/target/hosted/ypr0/powermgmt-ypr0.c | 4 +- 6 files changed, 13 insertions(+), 109 deletions(-) delete mode 100644 firmware/target/hosted/powermgmt.c (limited to 'firmware/target/hosted') diff --git a/firmware/target/hosted/android/powermgmt-android.c b/firmware/target/hosted/android/powermgmt-android.c index dcbd7a6214..1e70a81753 100644 --- a/firmware/target/hosted/android/powermgmt-android.c +++ b/firmware/target/hosted/android/powermgmt-android.c @@ -28,10 +28,10 @@ extern JNIEnv *env_ptr; extern jclass RockboxService_class; extern jobject RockboxService_instance; -static jfieldID _battery_level; +static jfieldID __battery_level; static jobject BatteryMonitor_instance; -void powermgmt_init_target(void) +static void new_battery_monitor(void) { JNIEnv e = *env_ptr; jclass class = e->FindClass(env_ptr, "org/rockbox/monitors/BatteryMonitor"); @@ -43,29 +43,15 @@ void powermgmt_init_target(void) RockboxService_instance); /* cache the battery level field id */ - _battery_level = (*env_ptr)->GetFieldID(env_ptr, + __battery_level = (*env_ptr)->GetFieldID(env_ptr, class, "mBattLevel", "I"); } -int battery_level(void) +int _battery_level(void) { - return (*env_ptr)->GetIntField(env_ptr, BatteryMonitor_instance, _battery_level); + if (!BatteryMonitor_instance) + new_battery_monitor(); + return (*env_ptr)->GetIntField(env_ptr, BatteryMonitor_instance, __battery_level); } -int battery_time(void) -{ /* cannot calculate yet */ - return 0; -} - -/* should always be safe on android targets, the host shuts us down before */ -bool battery_level_safe(void) -{ - return true; -} - -/* TODO */ -unsigned battery_voltage(void) -{ - return 0; -} diff --git a/firmware/target/hosted/android/system-android.c b/firmware/target/hosted/android/system-android.c index fba7ff4e2c..e48e910c0c 100644 --- a/firmware/target/hosted/android/system-android.c +++ b/firmware/target/hosted/android/system-android.c @@ -45,7 +45,7 @@ void system_reboot(void) { } /* this is used to return from the entry point of the native library. */ static jmp_buf poweroff_buf; -void shutdown_hw(void) +void power_off(void) { longjmp(poweroff_buf, 1); } diff --git a/firmware/target/hosted/maemo/maemo-thread.c b/firmware/target/hosted/maemo/maemo-thread.c index a32a563459..2f1d033baa 100644 --- a/firmware/target/hosted/maemo/maemo-thread.c +++ b/firmware/target/hosted/maemo/maemo-thread.c @@ -163,40 +163,14 @@ int maemo_thread_func (void *wait_for_osso_startup) return 0; } -/** Rockbox battery related functions */ -void battery_status_update(void) -{ - battery_percent = maemo_battery_level; - send_battery_level_event(); -} - -/* Returns true if any power input is connected - charging-capable - * or not. */ -bool power_input_present(void) -{ - return false; -} - -unsigned battery_voltage(void) -{ - return 0; -} - /* Returns battery level in percent */ -int battery_level(void) +int _battery_level(void) { - battery_status_update(); return maemo_battery_level; } /* Return remaining battery time in minutes */ -int battery_time(void) +int _battery_time(void) { - battery_status_update(); return maemo_remaining_time_sec / 60; } - -bool battery_level_safe(void) -{ - return battery_level() >= 5; -} diff --git a/firmware/target/hosted/powermgmt.c b/firmware/target/hosted/powermgmt.c deleted file mode 100644 index bf05e5ff19..0000000000 --- a/firmware/target/hosted/powermgmt.c +++ /dev/null @@ -1,56 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2011 by Thomas Jarosch - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ -#include "config.h" -#include "powermgmt.h" -#include "thread.h" -#include "kernel.h" - -static char power_stack[DEFAULT_STACK_SIZE]; -static const char power_thread_name[] = "power"; - -void powermgmt_init_target(void); - -#if !(CONFIG_PLATFORM & PLATFORM_ANDROID) -void powermgmt_init_target(void) -{ - /* Nothing to do */ -} -#endif - -static void power_thread(void) -{ - powermgmt_init_target(); - - while (1) - { - /* Sleep two seconds */ - sleep(HZ*2); - - handle_auto_poweroff(); - } -} /* power_thread */ - -void powermgmt_init(void) -{ - create_thread(power_thread, power_stack, sizeof(power_stack), 0, - power_thread_name IF_PRIO(, PRIORITY_SYSTEM) - IF_COP(, CPU)); -} diff --git a/firmware/target/hosted/sdl/system-sdl.c b/firmware/target/hosted/sdl/system-sdl.c index 93a8a7cb66..87b2e0ccd1 100644 --- a/firmware/target/hosted/sdl/system-sdl.c +++ b/firmware/target/hosted/sdl/system-sdl.c @@ -196,7 +196,7 @@ static int sdl_event_thread(void * param) return 0; } -void shutdown_hw(void) +void power_off(void) { /* Shut down SDL event loop */ SDL_Event event; diff --git a/firmware/target/hosted/ypr0/powermgmt-ypr0.c b/firmware/target/hosted/ypr0/powermgmt-ypr0.c index 0c162a619d..9c90f7ea02 100644 --- a/firmware/target/hosted/ypr0/powermgmt-ypr0.c +++ b/firmware/target/hosted/ypr0/powermgmt-ypr0.c @@ -70,7 +70,7 @@ unsigned int power_input_status(void) /* Returns battery voltage from ADC [millivolts], * adc returns voltage in 5mV steps */ -unsigned int battery_adc_voltage(void) +int _battery_voltage(void) { return adc_read(3) * 5; } @@ -80,5 +80,5 @@ bool charging_state(void) const unsigned short charged_thres = 4170; bool ret = (power_input_status() == POWER_INPUT_MAIN_CHARGER); /* dont indicate for > ~95% */ - return ret && (battery_adc_voltage() <= charged_thres); + return ret && (_battery_voltage() <= charged_thres); } -- cgit v1.2.3