Index: uisimulator/common/stubs.c =================================================================== --- uisimulator/common/stubs.c (revision 31574) +++ uisimulator/common/stubs.c (working copy) @@ -168,11 +168,6 @@ return 0; } -int storage_init(void) -{ - return 1; -} - int storage_write_sectors(IF_MV2(int drive,) unsigned long start, int count, Index: uisimulator/common/powermgmt-sim.c =================================================================== --- uisimulator/common/powermgmt-sim.c (revision 31574) +++ uisimulator/common/powermgmt-sim.c (working copy) @@ -30,53 +30,85 @@ #define BATT_MAXMVOLT 4300 /* maximum millivolts of battery */ #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in minutes */ +#define BATT_MVOLT_RANGE (BATT_MAXMVOLT - BATT_MINMVOLT) +/* number of millivolts to charge the battery by every second */ +#define BATT_CHARGE_STEP (BATT_MVOLT_RANGE / 50) +/* number of millivolts to discharge the battery by every second */ +#define BATT_DISCHARGE_STEP (BATT_MVOLT_RANGE / 100) -extern void send_battery_level_event(void); -extern int last_sent_battery_level; +#if CONFIG_CHARGING >= CHARGING_MONITOR +/* number of seconds to trickle charge before discharging again */ +#define TRICKLE_CHARGE_SECONDS 6 +#endif + extern int battery_percent; -static bool charging = false; static unsigned int battery_millivolts = BATT_MAXMVOLT; void powermgmt_init_target(void) {} -static void battery_status_update(void) +static void mock_battery_thread(void) { - static long last_tick = 0; - - if (TIME_AFTER(current_tick, (last_tick+HZ))) { - last_tick = current_tick; - - /* change the values: */ - if (charging) { - if (battery_millivolts >= BATT_MAXMVOLT) { - /* Pretend the charger was disconnected */ - charging = false; - queue_broadcast(SYS_CHARGER_DISCONNECTED, 0); - last_sent_battery_level = 100; - } + while(1) + { + while(battery_millivolts > BATT_MINMVOLT) + { + battery_millivolts -= BATT_DISCHARGE_STEP; + sleep(HZ); } - else { - if (battery_millivolts <= BATT_MINMVOLT) { - /* Pretend the charger was connected */ - charging = true; - queue_broadcast(SYS_CHARGER_CONNECTED, 0); - last_sent_battery_level = 0; - } - } - if (charging) { - battery_millivolts += (BATT_MAXMVOLT - BATT_MINMVOLT) / 50; +#ifdef CONFIG_CHARGING + /* Pretend the charger was connected */ + charger_input_state = CHARGER_PLUGGED; +#if CONFIG_CHARGING >= CHARGING_MONITOR + charge_state = CHARGING; +#endif +#endif + + while(battery_millivolts < BATT_MAXMVOLT) + { + battery_millivolts += BATT_CHARGE_STEP; + sleep(HZ); } - else { - battery_millivolts -= (BATT_MAXMVOLT - BATT_MINMVOLT) / 100; - } + +#if CONFIG_CHARGING >= CHARGING_MONITOR + /* Pretend to trickle charge full battery */ + charge_state = TRICKLE; + sleep(TRICKLE_CHARGE_SECONDS * HZ); +#endif - battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / - (BATT_MAXMVOLT - BATT_MINMVOLT); +#ifdef CONFIG_CHARGING + /* Pretend the charger was disconnected */ + charger_input_state = CHARGER_UNPLUGGED; +#if CONFIG_CHARGING >= CHARGING_MONITOR + charge_state = DISCHARGING; +#endif +#endif } } + +int storage_init(void) +{ + /* + * OK, mock_battery_thread has nothing to do with initialising storage, + * it was just a handy place to create the thread from. + * Feel free to move it to a more appropriate place if you know of one. + */ + static long mock_battery_stack[DEFAULT_STACK_SIZE/sizeof(long)]; + static const char mock_battery_thread_name[] = "mock_battery"; + create_thread(mock_battery_thread, mock_battery_stack, + sizeof(mock_battery_stack), 0, mock_battery_thread_name + IF_PRIO(, PRIORITY_USER_INTERFACE) + IF_COP(, CPU)); + return 1; +} +static void battery_status_update(void) +{ + battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / + BATT_MVOLT_RANGE; +} + const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = { 3200 }; const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] = { 3200 }; @@ -96,14 +128,17 @@ #if CONFIG_CHARGING unsigned int power_input_status(void) { - return charging ? POWER_INPUT_NONE : POWER_INPUT_MAIN; + return charger_input_state >= CHARGER_PLUGGED + ? POWER_INPUT_CHARGER : POWER_INPUT_NONE; } +#if CONFIG_CHARGING >= CHARGING_MONITOR bool charging_state(void) { - return charging; + return charge_state == CHARGING; } #endif +#endif #ifdef HAVE_ACCESSORY_SUPPLY void accessory_supply_set(bool enable)