/*****************************************************************************
 * Module    : Task
 * File      : task_mic.c
 * Author    : Hanny
 * Function  : 录音任务流程
 *****************************************************************************/
#include "include.h"
#include "task.h"
#include "task_mic.h"
#include "task_music.h"

#include "user_key.h"

#if TASK_MIC_EN

void tasksub_mic_record(void);
void tasksub_mic_recplay(void);
bool wav_sync_file(void *buf);
void wav_record_abort(void);
void task_record_pause(void);
#define WAV_OUT_BUF_SIZE  0x1C00
IAR_XDATA_A u8 wav_en_out_buf[WAV_OUT_BUF_SIZE] @ "WAV_ENCODE_BUF";
IAR_XDATA_A u8 wav_en_header_buf[512] @ "WAV_EN_HEADER_BUF";
IAR_XDATA_A u8 rec_write_buf[512] @ "WAV_EN_HEADER_BUF";
#define TASK_SUB
#include SET_USER_PATH(CFG_UI_MIC_DIR, /ui/ui_mic.c)
#include "sub/tasksub_mic_record.c"                            //录音子任务
#include "sub/tasksub_mic_recplay.c"                           //录音子任务

#pragma location="TASK_MIC_SEG"
void mp3res_play_micmode(u8 music_name)
{
    dac_enable();
    mp3_res_play_wait(music_name);
    dac_disable();
    mic_init(SEL_RECMIC);
    wav_load_code(0x60);
}

//录音任务初始化
#pragma location="TASK_MIC_SEG"
void task_mic_enter(void)
{
    led_mode_off();
    led_idle();
    set_sys_clk(SYS_48M);
    draw_wait();
    device_change();
    if(!record_mount_device()) {
        task_ctl.work_sta = TASK_EXIT;      //设备无效，返回主菜单
#if IS_LCD_DISPLAY
        msgbox(STR_NO_DEVICE, NULL, MSGBOX_TIME);   //未插入存储设备
#endif
        return;
    }
#if WARNING_TASK_MIC
    if (work_sta_interrupt == TASK_NULL) {
        dac_enable();
        mp3_res_play_wait(RES_MP3_RECORDING_MODE);
        dac_disable();
    }
#endif
    work_sta_interrupt = TASK_NULL;
    //关闭预分配
    fat_fs.limit_clust = 0;
    memset(&t_rec,0,sizeof(t_rec));
    t_rec.rec_sta = REC_STA_STOP;
    mic_init(SEL_RECMIC);  //选择MIC录音
    wav_load_code(0x60);

#if IS_LCD_DISPLAY
    draw_clear();                                       //清空屏显
    t_rec.updata_flag = 1;
    // 清除"请稍候"字样
    line_clear();
    line_update(2);
    line_update(4);
#if THEME_SELECT==THEME_LCD_DEFAULT
    lcd_buf = lcd_buf2;   // lcd使用小buf，否则会花屏
#endif
#endif
}

//退出录音任务
#pragma location="TASK_MIC_SEG"
void task_mic_exit(void)
{
    if((t_rec.rec_sta == REC_STA_PAUSE) && t_rec.file_open) {
        task_record_stop();
    }
    mic_adc_exit();
    amux_exit();
#if MODE_CHANGE_BLUE_ON
    led_mode_on();
#endif
#if IS_LCD_DISPLAY
    lcd_buf = lcd_buf1;   // 退出时，lcd恢复使用大buf
#endif
}

//录音任务事务处理
#pragma location="TASK_MIC_SEG"
void task_mic_event(void)
{
    WATCHDOG_CLR();
    comm_event();
    //检查是否有设备需要初始化
    if (device_change()) {
        if(t_rec.rec_sta == REC_STA_PAUSE) {
            t_rec.play_flag = 0;
            task_record_stop();
        }
    }

    if (!device_is_actived(DEVICE_SDMMC) && !device_is_actived(DEVICE_UDISK)) {
        task_ctl.work_sta = TASK_EXIT;          //设备无效，退出
#if IS_LCD_DISPLAY
        msgbox(STR_NO_DEVICE, NULL, MSGBOX_TIME);   //未插入存储设备
#endif
    }

#if WARNING_LOWPOWER
    if(sys_ctl.lowpower_warning_flag == 1){
        sys_ctl.lowpower_warning_flag = 2;
        mp3res_play_micmode(RES_MP3_PLEASE_CHARGE);
    }
#endif
}


//录音任务
#pragma location="TASK_RECORD_SEG"
void task_mic(void)
{
    //printf("mic\n");
    task_mic_enter();
    while (task_ctl.work_sta == TASK_MIC) {
        u8 msg = get_msg();
        task_mic_event();
        task_mic_deal_msg(msg);
        task_mic_display();
    }
    task_mic_exit();
}

#endif
