/*****************************************************************************
 * Module    : TaskSub
 * File      : tasksub_mic_recplay.c
 * Author    : Hanny
 * Function  : mic任务 - mic录音播放
 *****************************************************************************/
#ifdef TASK_SUB


void task_music_cal_time(void);
void task_music_display(void);
void tasksub_mic_recplay_event(void);
extern FATFS *pfs;
void task_recplay_event(void);


bool play_record_file(void)
{
    memcpy(&fat_file_opt,&fat_file,sizeof(fat_file));
    pfs = &fat_fs;
  //  printf("f:%08lx\n",fat_file_opt.fsize);
    if (f_open_exist(&fat_file_opt)) {
        return false;
    }
   // printf_array(&fat_file_opt,sizeof(fat_file));
    if (TYPE_NONE == music_init()) {
        return false;
    }
   // printf_array(&fat_file_opt,sizeof(fat_file));
    return true;
}

//任务初始化
void tasksub_mic_recplay_enter(void)
{
    mic_adc_exit();
    ocx_clear();
    draw_wait();
    led_music_play();
   // dac_enable();
    user_change_volume(sys_ctl.volume);     //数字音量

    dac_enable();

    t_msc.disp_music_time.sec = 0xff;
    t_msc.disp_flag = 1;
    t_msc.music_type = TYPE_WAV;
    t_msc.music_total = 1;
    t_msc.music_num = 1;
    //播放信息初始化
    t_msc.pause = 0;
    if (play_record_file()) {
        music_play();
    } else {
        //printf("file Not valid!\n");
        music_stop();
    }
    ocx_init(tasksub_mic_recplay_event, NULL);       //设置OCX需要响应的消息及事件
}

//任务退出
void tasksub_mic_recplay_exit(void)
{
    music_stop();
    dac_disable();
    ocx_clear();
    mic_init(SEL_RECMIC);
}

#pragma location="TASK_MUSIC_SEG"
void tasksub_mic_recplay_event(void)
{
    WATCHDOG_CLR();
    music_event();
    comm_event();                           //调用公共事件
    if (!device_is_actived(fs_cur_dev())) {
        music_stop();
    }

    task_music_cal_time();
    if (t_msc.disp_music_time.sec == 0xff) {
        u16 all_time = music_get_alltime();
        if (all_time != 0xffff) {
            t_msc.disp_music_time.min = all_time / 60;      //总时间
            t_msc.disp_music_time.sec = all_time % 60;
            t_msc.disp_status_flag = 1;                     //更新状态显示
        }
    }
}
#pragma location="TASK_MUSIC_SEG"
void tasksub_mic_recplay_loop(void)
{
    u8 msg;
    while (task_ctl.work_sta == TASK_MIC && music_get_status() > STATUS_PLAY_STOPPING) {
        tasksub_mic_recplay_event();
        while (msg = get_msg()) {
            tasksub_mic_recplay_deal_msg(msg);
        }
        task_music_display();
    }
}
//任务主流程

void tasksub_mic_recplay(void)
{
    tasksub_mic_recplay_enter();
    tasksub_mic_recplay_loop();
    tasksub_mic_recplay_exit();
}

#endif
