Index: uisimulator/common/stubs.c =================================================================== --- uisimulator/common/stubs.c (revision 31536) +++ uisimulator/common/stubs.c (working copy) @@ -164,11 +164,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 31536) +++ uisimulator/common/powermgmt-sim.c (working copy) @@ -29,7 +29,17 @@ #define BATT_MAXMVOLT 4500 /* 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) +#if CONFIG_CHARGING >= CHARGING_MONITOR +/* number of seconds to trickle charge before discharing again */ +#define TRICKLE_CHARGE_SECONDS 6 +#endif + extern void send_battery_level_event(void); extern int last_sent_battery_level; extern int battery_percent; @@ -38,49 +48,73 @@ /* estimated remaining time in minutes */ static int powermgmt_est_runningtime_min = BATT_MAXRUNTIME; -static void battery_status_update(void) +static long mock_battery_stack[DEFAULT_STACK_SIZE/sizeof(long)]; +static const char mock_battery_thread_name[] = "mock_battery"; + +static void mock_battery_thread(void) { - static time_t last_change = 0; - static bool charging = false; - time_t now; + while(1) + { + while(battery_millivolts > BATT_MINMVOLT) + { + battery_millivolts -= BATT_DISCHARGE_STEP; + sleep(HZ); + } - time(&now); + last_sent_battery_level = 0; +#ifdef CONFIG_CHARGING + /* Pretend the charger was connected */ + queue_broadcast(SYS_CHARGER_CONNECTED, 0); + charger_input_state = CHARGER; +#if CONFIG_CHARGING >= CHARGING_MONITOR + charge_state = CHARGING; +#endif +#endif - if (last_change < now) { - last_change = now; - - /* 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(battery_millivolts < BATT_MAXMVOLT) + { + battery_millivolts += BATT_CHARGE_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; - } - 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); - - powermgmt_est_runningtime_min = - battery_percent * BATT_MAXRUNTIME / 100; + last_sent_battery_level = 100; +#ifdef CONFIG_CHARGING + /* Pretend the charger was disconnected */ + queue_broadcast(SYS_CHARGER_DISCONNECTED, 0); + charger_input_state = NO_CHARGER; +#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. + */ + 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; + powermgmt_est_runningtime_min = + battery_percent * BATT_MAXRUNTIME / 100; send_battery_level_event(); }