/*****************************************************************************
 * Module    : Task
 * File      : task_aux.c
 * Author    : Hanny
 * Function  : 扩音器任务流程
 *****************************************************************************/
#include "include.h"

#if TASK_AUX_EN
#include "task.h"

struct _TYPE_TASK_AUX {
    u8 mute;            //播放与MUTE状态
#if IS_LCD_DISPLAY
    u8 updata_flag;     // 显示更新标志
    u8 sta;             // 现在播放状态
    u8 last_sta;        // 之前播放状态
#endif
} t_aux;
extern void aux_play_vol_music(void);
#include SET_USER_PATH(CFG_UI_AUX_DIR, /ui/ui_aux.c)

//任务事务处理
#pragma location="TASK_AUX_SEG"
void task_aux_event(void)
{
    comm_event();                           //调用公共事件
#if LINEIN_DETECT_EN
    if (!device_activate(DEVICE_LINEIN)) {
        task_ctl.work_sta = TASK_EXIT;
    }
#endif
#if AUX_AUTO_STANDBY_EN || IR_HARD_POWERDOWN_EN
    if((((!sys_ctl.sleep_cnt) && BT_IS_SLEEP) || sys_ctl.ir_power_flag) && !sys_ctl.lowpower_flag){
        task_hold();
    }
#endif
#if WARNING_LOWPOWER
    if(sys_ctl.lowpower_warning_flag == 1){
        sys_ctl.lowpower_warning_flag = 2;
        amux_stop();
        dac_enable();
        mp3_res_play_wait(RES_MP3_PLEASE_CHARGE);
        dac_disable();
        amux_play();
    }
#endif
}

#pragma location="TASK_AUX_SEG"
void aux_play_vol_music(void)
{
    //printf("flag = %d\n",sys_ctl.vol_maxmin_music_flag);
    if(sys_ctl.vol_maxmin_music_flag != 2){
        sys_ctl.vol_maxmin_music_flag = 2;   //dac 只打开或关闭一次
        amux_stop();
        dac_enable();
    }
    mp3_res_play_wait(RES_MP3_VOL_MAXMIN);
    flush_msg();
}

//任务初始化
#pragma location="TASK_AUX_SEG"
void task_aux_enter(void)
{
#if LINEIN_DETECT_EN
    if (!device_activate(DEVICE_LINEIN)) {      //检查linein
#if IS_LCD_DISPLAY
        msgbox(STR_NO_CONNECTED_AUX, NULL, MSGBOX_TIME);
#endif
        task_ctl.work_sta = TASK_EXIT;          //设备无效，返回主菜单
        return;
    }
#endif

    memset(&t_aux, 0, sizeof(t_aux));

    led_aux_play();
    draw_wait();
    set_sys_clk(SYS_24M);
#if (DEBUG_UART_SEL && ((AUX_CHANNEL_SEL & 0x07) == CHANNEL_01))
    uart_disable();
    P0DIR |= BIT(1);
#endif

/*#if MP3BT_DECT_EN
    work_sta_interrupt = TASK_NULL;
#endif*/

#if WARNING_TASK_AUX
    if (work_sta_interrupt == TASK_NULL) {
        dac_enable();
        mp3_res_play_wait(RES_MP3_AUX_MODE);
        dac_disable();
    }
#endif
    work_sta_interrupt = TASK_NULL;
    amux_init(AUX_CHANNEL_SEL);
#if MIC_DIRECT_OUT_EN
    mic_direct_out_enter(11);
#endif
    amux_play();
#if IS_LCD_DISPLAY
    set_lcd_buf(LCD_FULL_MODE);
#endif
}

//任务退出
#pragma location="TASK_AUX_SEG"
void task_aux_exit(void)
{
#if MODE_CHANGE_BLUE_ON
    led_mode_on();
#endif
    amux_exit();
#if MIC_DIRECT_OUT_EN
    mic_direct_out_exit();
#endif
#if (DEBUG_UART_SEL && ((AUX_CHANNEL_SEL & 0x07) == CHANNEL_01))
    P0DIR &= ~BIT(1);
    uart_enable();
#endif
    if(sys_ctl.mute_flag) {
        sys_unmute();
    }
}

//aux任务
#pragma location="TASK_AUX_SEG"
void task_aux(void)
{
    //printf("\ntask_aux\n");
    task_aux_enter();
    while (task_ctl.work_sta == TASK_AUX) {
        u8 msg = get_msg();
        task_aux_event();
        task_aux_deal_msg(msg);
        task_aux_display();
    }
    task_aux_exit();
}
#endif
