⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 powermgmt.c

📁 编译后直接运行的MP3播放器全部C语言源代码 一个包含FAT文件系统、系统引导 Boot、FLASH Driver等内容的
💻 C
📖 第 1 页 / 共 3 页
字号:
{    if(sleeptimer_active)        return (sleeptimer_endtick - current_tick) / HZ;    else        return 0;}/* We shut off in the following cases:   1) The unit is idle, not playing music   2) The unit is playing music, but is paused   We do not shut off in the following cases:   1) The USB is connected   2) The charger is connected   3) We are recording, or recording with pause*/static void handle_auto_poweroff(void){    long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ;    int mpeg_stat = mpeg_status();    bool charger_is_inserted = charger_inserted();    static bool charger_was_inserted = false;    /* The was_inserted thing prevents the unit to shut down immediately       when the charger is extracted */    if(charger_is_inserted || charger_was_inserted)    {        last_charge_time = current_tick;    }    charger_was_inserted = charger_is_inserted;        if(timeout &&#ifdef HAVE_FMRADIO       !fmradio_get_status() &&#endif       !usb_inserted() &&       (mpeg_stat == 0 ||        ((mpeg_stat == (MPEG_STATUS_PLAY | MPEG_STATUS_PAUSE)) &&         !sleeptimer_active)))    {        if(TIME_AFTER(current_tick, last_keypress + timeout) &&           TIME_AFTER(current_tick, last_disk_activity + timeout) &&           TIME_AFTER(current_tick, last_charge_time + timeout))        {            power_off();        }    }    else    {        /* Handle sleeptimer */        if(sleeptimer_active && !usb_inserted())        {            if(TIME_AFTER(current_tick, sleeptimer_endtick))            {                mpeg_stop();                if(charger_is_inserted)                {                    DEBUGF("Sleep timer timeout. Stopping...\n");                    set_sleep_timer(0);                }                else                {                    DEBUGF("Sleep timer timeout. Shutting off...\n");                    power_off();                }            }        }    }}void set_car_adapter_mode(bool setting){    car_adapter_mode_enabled = setting;}static void car_adapter_mode_processing(void){    static bool charger_power_is_on = false;    static bool waiting_to_resume_play = false;    static long play_resume_time;        if (car_adapter_mode_enabled) {        if (waiting_to_resume_play) {            if (TIME_AFTER(current_tick, play_resume_time)) {                if (mpeg_status() & MPEG_STATUS_PAUSE) {                    mpeg_resume();                 }                waiting_to_resume_play = false;            }        }        else {            if (charger_power_is_on) {                /* if external power was turned off */                if (!charger_inserted()) {                     charger_power_is_on = false;                    /* if playing */                    if ((mpeg_status() & MPEG_STATUS_PLAY) &&                        !(mpeg_status() & MPEG_STATUS_PAUSE)) {                        mpeg_pause();                     }                }            }            else {                 /* if external power was turned on */                if (charger_inserted()) {                     charger_power_is_on = true;                    /* if paused */                    if (mpeg_status() & MPEG_STATUS_PAUSE) {                        /* delay resume a bit while the engine is cranking */                        play_resume_time = current_tick + HZ*5;                        waiting_to_resume_play = true;                    }                }            }        }    }}/* * This function is called to do the relativly long sleep waits from within the * main power_thread loop while at the same time servicing any other periodic * functions in the power thread which need to be called at a faster periodic * rate than the slow periodic rate of the main power_thread loop */static void power_thread_sleep(int ticks){    while (ticks > 0) {        int small_ticks = MIN(HZ/2, ticks);        sleep(small_ticks);        ticks -= small_ticks;        car_adapter_mode_processing();    }}/* * This power thread maintains a history of battery voltage * and implements a charging algorithm. * For a complete description of the charging algorithm read * docs/CHARGING_ALGORITHM. */static void power_thread(void){    int i;    int avg, ok_samples, spin_samples;    int current = 0;#ifdef HAVE_LIION    int charging_current;#endif#ifdef HAVE_CHARGE_CTRL    int delta;    int charged_time = 0;    int charge_max_time_now = 0;  /* max. charging duration, calculated at                                     beginning of charging */    int charge_pause = 0;         /* no charging pause at the beginning */    int trickle_time = 0;         /* how many minutes trickle charging already? */#endif        while (1)    {        /* never read power while disk is spinning, unless in USB mode */        if (ata_disk_is_active() && !usb_inserted()) {            sleep(HZ * 2);            continue;        }                /* Make POWER_AVG measurements and calculate an average of that to         * reduce the effect of backlights/disk spinning/other variation.         */        ok_samples = spin_samples = avg = 0;        for (i = 0; i < POWER_AVG_N; i++) {            if (ata_disk_is_active()) {                if (!ok_samples) {                    /* if we don't have any good non-disk-spinning samples,                     * we take a sample anyway in case the disk is going                     * to spin all the time.                     */                    avg += adc_read(ADC_UNREG_POWER);                    spin_samples++;                }            } else {                if (spin_samples) /* throw away disk-spinning samples */                    spin_samples = avg = 0;                avg += adc_read(ADC_UNREG_POWER);                ok_samples++;            }            power_thread_sleep(HZ*POWER_AVG_SLEEP);        }        avg = avg / ((ok_samples) ? ok_samples : spin_samples);        /* rotate the power history */        for (i = 0; i < POWER_HISTORY_LEN-1; i++)            power_history[i] = power_history[i+1];                /* insert new value in the end, in centivolts 8-) */        power_history[POWER_HISTORY_LEN-1] =            (avg * BATTERY_SCALE_FACTOR) / 10000;                /* update battery level every minute, ignoring first 15 minutes after           start charge/decharge */#ifdef HAVE_CHARGE_CTRL        if ((powermgmt_last_cycle_startstop_min > 25) || (charge_state > 1))#endif            battery_level_update();        /* calculate estimated remaining running time */        /* decharging: remaining running time */        /* charging:   remaining charging time */#ifdef HAVE_CHARGE_CTRL        if (charge_state == 1)            powermgmt_est_runningtime_min = (100 - battery_level()) *                battery_capacity / 100 * 60 / CURRENT_CHARGING;        else {            current = usb_inserted() ? CURRENT_USB : CURRENT_NORMAL;            if ((backlight_get_timeout() == 1) ||                (charger_inserted() && backlight_get_on_when_charging()))                /* LED always on or LED on when charger connected */                current += CURRENT_BACKLIGHT;            powermgmt_est_runningtime_min = battery_level() *                battery_capacity / 100 * 60 / current;#if MEM == 8 /* assuming 192 kbps, the running time is 22% longer with 8MB */            powermgmt_est_runningtime_min =                powermgmt_est_runningtime_min * 122 / 100;#endif /* MEM == 8 */        }#else        current = usb_inserted() ? CURRENT_USB : CURRENT_NORMAL;        if (backlight_get_timeout() == 1) /* LED always on */            current += CURRENT_BACKLIGHT;        powermgmt_est_runningtime_min = battery_level() *            battery_capacity / 100 * 60 / current;#if MEM == 8 /* assuming 192 kbps, the running time is 22% longer with 8MB */        powermgmt_est_runningtime_min =            powermgmt_est_runningtime_min * 122 / 100;#endif /* MEM == 8 */#endif /* HAVE_CHARGE_CONTROL */#ifdef HAVE_LIION        /* We use the information from the ADC_EXT_POWER ADC channel, which           tells us the charging current from the LTC1734. When DC is           connected (either via the external adapter, or via USB), we try           to determine if it is actively charging or only maintaining the           charge. My tests show that ADC readings is below about 0x80 means           that the LTC1734 is only maintaining the charge. */        if(charger_inserted()) {            charging_current = adc_read(ADC_EXT_POWER);            if(charging_current < 0x80) {                charge_state = 2; /* Trickle */            } else {                charge_state = 1; /* Charging */            }        } else {            charge_state = 0; /* Not charging */        }#else#ifdef HAVE_CHARGE_CTRL        if (charge_pause > 0)            charge_pause--;                if (charger_inserted()) {            if (charge_state == 1) {                /* charger inserted and enabled */                charged_time++;                snprintf(power_message, POWER_MESSAGE_LEN,                         "Chg %dm max %dm", charged_time, charge_max_time_now);                if (charged_time > charge_max_time_now) {                    DEBUGF("power: charged_time > charge_max_time_now, "

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -