/*****************************************************************************
* Module    : UI
* File      : ui_fm.c
* Author    : Hanny
* Function  : FM任务用户自定义UI设计，处理按键功能映射与显示
*****************************************************************************/
#ifndef CONFIG_C

//任务显示处理
#pragma location="TASK_FMREC_SEG"
void task_fm_record_display(void)
{

}

//任务显示处理
#if IS_LEDSEG_5C7S_DISPLAY
#pragma location="TASK_FMPLAY_DISP_SEG"
__near_func void task_fm_display(void)
{
    ledseg_ocx_event();

    switch (ledseg_ocx.disp_status) {
    case LEDSEG_OCX_NULL:
        ledseg_disp(MENU_FREQ);
        break;
    default:
        ledseg_ocx_display();
        break;
    }
}

//#pragma location="TASK_FMREC_SEG"
void task_fm_disp_clear(void)
{
    ledseg_ocx_clear();
}

#pragma location="TASK_FMREC_SEG"
void task_fm_ledseg_disp_init(void)
{
    ledseg_disp(MENU_FREQ);
}
#else

#pragma location="TASK_FMPLAY_DISP_SEG"
__near_func void task_fm_display(void)
{
}
//#pragma location="TASK_FMREC_SEG"
void task_fm_disp_clear(void)
{
}

#pragma location="TASK_FMREC_SEG"
void task_fm_ledseg_disp_init(void)
{
}
#endif

//任务消息处理
#pragma location="TASK_FMPLAY_SEG"
__near_func void task_fm_deal_msg(u8 msg)
{

#if WARNING_VOL_MAXMIN
    if(sys_ctl.vol_maxmin_music_flag == 1){
        sys_ctl.vol_maxmin_music_flag = 0;
        fm_play_continue();
        flush_msg();
        return;
    }
#endif

    switch(msg) {
        //自动搜台
    case KL_PLAY:
    case KU_PLAY_POWER:
        if(t_fm.seek_sta == SEEK_AUTO){
            task_fm_auto_stop();        //停止自动搜台
            task_fm_auto_play();
            led_fm_play();
        } else {
            task_fm_auto_start();       //开始自动搜台
            t_fm.seek_sta = SEEK_AUTO;
            sys_ctl.sleep_cnt = 0xffff;
            led_fm_scan();
        }
        break;

    case KU_PLAY:   //在搜台时,停止搜台. 在播放时为PP
        if(t_fm.seek_sta == SEEK_AUTO){
            task_fm_auto_stop();        //停止自动搜台
            task_fm_auto_play();
        } else {  //播放/暂停

            if(t_fm.mute){     //播放
                t_fm.mute = 0;
#if FM_VOUT_THROUGH_MCU
                amux_play();
#else
                speaker_unmute();
                fm_set_vol(sys_ctl.volume);
#endif
            }
            else{               //暂停
                printf("mu\n");
                t_fm.mute = 1;
#if FM_VOUT_THROUGH_MCU
                amux_stop();
#else
                if(sys_ctl.spk_mute){
                    speaker_unmute();
                    fm_set_vol(sys_ctl.volume);
                }else{
                    speaker_mute();
                    fm_set_vol(0);
                }
#endif
            }
        }
        break;

        //上一曲，下一曲
    case KU_PREV:
    case KU_PREV_VOL_DOWN:
        if(t_fm.seek_sta == SEEK_STOP)
        {
            task_fm_prevch();
            sys_param_write8(PARAM_FM_CHSEL, t_fm.ch_sel);              //保存当前台号
            sys_param_write16(PARAM_FM_FREQ, t_fm.freq);                //保存起始频率
        }
        //printf("channel %d: %dMHz\n",t_fm.ch_sel,t_fm.freq);
        sys_ctl.sleep_cnt = 0xffff;
        break;

    case KU_NEXT:
    case KU_NEXT_VOL_UP:
        if(t_fm.seek_sta == SEEK_STOP)
        {
            task_fm_nextch();
            sys_param_write8(PARAM_FM_CHSEL, t_fm.ch_sel);              //保存当前台号
            sys_param_write16(PARAM_FM_FREQ, t_fm.freq);                //保存起始频率
        }
        //printf("t_fm.freq: %d\n",t_fm.freq);
        //printf("channel %d: %dMHz\n",t_fm.ch_sel,t_fm.freq);
        sys_ctl.sleep_cnt = 0xffff;
        break;

        //调节音量
    case K_VOL_DOWN:
    case KL_VOL_DOWN:
    case KH_VOL_DOWN:
    case KH_PREV_VOL_DOWN:
#if FM_SET_VOL_CHANGE_BANK
        user_set_volume(user_volume_dec(sys_ctl.volume));
#else
        user_set_volume_fm(user_volume_dec_fm(sys_ctl.volume));
#endif

#if !FM_VOUT_THROUGH_MCU
        if(!sys_ctl.spk_mute){
            fm_set_vol(sys_ctl.volume);
        }
#endif
        show_volume();
#if WARNING_VOL_MAXMIN
        if(!sys_ctl.volume){
            fm_play_vol_music();
        }
#endif
        break;

    case K_VOL_UP:
    case KL_VOL_UP:
    case KH_VOL_UP:
    case KH_NEXT_VOL_UP:
#if FM_SET_VOL_CHANGE_BANK
        user_set_volume(user_volume_inc(sys_ctl.volume));
#else
        user_set_volume_fm(user_volume_inc_fm(sys_ctl.volume));
#endif

#if !FM_VOUT_THROUGH_MCU
        if(!sys_ctl.spk_mute){
            fm_set_vol(sys_ctl.volume);
        }
#endif
        show_volume();
#if WARNING_VOL_MAXMIN
        if(sys_ctl.volume == VOLUME_MAX){
            fm_play_vol_music();
        }
#endif
        break;

#if (HARD_IR_EN || SOFT_IR_EN)
    case T_KEY_NUM_0:
    case T_KEY_NUM_1:
    case T_KEY_NUM_2:
    case T_KEY_NUM_3:
    case T_KEY_NUM_4:
    case T_KEY_NUM_5:
    case T_KEY_NUM_6:
    case T_KEY_NUM_7:
    case T_KEY_NUM_8:
    case T_KEY_NUM_9:
        task_fm_select_freq_chan(msg - T_KEY_NUM_0);
        break;
    case KL_SCAN:
    case KU_SCAN:
        if(t_fm.seek_sta == SEEK_AUTO){ //短按、长按SCAN键均可停止搜台
            task_fm_auto_stop();        //停止自动搜台
            task_fm_auto_play();
            sys_ctl.sleep_cnt = 0xffff;
        }
        else if(msg == KL_SCAN){        //长按SCAN键开始搜台
            task_fm_auto_start();       //开始自动搜台
            t_fm.seek_sta = SEEK_AUTO;
        }
        break;
#endif
    default:
        deal_msg(msg);
        break;
    }
}


#endif
