#include "main.h"
#include "YX5200_main.h"

uint8_t TxBufferUart2[256] = { 0 };
uint8_t RxBufferUart2[256] = { 0 };
__IO uint8_t TxCounterUart2 = 0x00;
__IO uint8_t RxCounterUart2 = 0x00;

uint8_t Uart2SendCount = 0;//实际发送

bool YX5200HaveRecFlag = false;

/*******************wifi UART通讯配置*********************/

void Configuration_YX5200_Usart2 ( void )
{
#if xy5200_Uart2_def
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    //使能USARTz 时钟可配置
    YX5200_RCC_GPIO_CONFIG();
	YX5200_RCC_UART_CONFIG();
	YX5200_RCC_Remap_CONFIG();
    /*
    *  USART2_TX -> PD5 , USART2_RX ->	PD6
    */
    GPIO_InitStructure.GPIO_Pin = YX5200_Tx;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init ( YX5200_GPIO, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = YX5200_Rx;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init ( YX5200_GPIO, &GPIO_InitStructure );

    USART_InitStructure.USART_BaudRate = YX5200_UART_BAUD;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init ( YX5200_UART, &USART_InitStructure );

    USART_ITConfig ( YX5200_UART, USART_IT_RXNE, ENABLE );
    USART_ITConfig ( YX5200_UART, USART_IT_TXE, DISABLE );

    USART_Cmd ( YX5200_UART, ENABLE );

#endif
}

//系统中断管理
void NVIC_Configuration_YX5200_USART2 ( void )
{
#if xy5200_Uart2_def

    NVIC_InitTypeDef NVIC_InitStructure;
    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig ( YX5200_NVIC_Group );

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = YX5200_NVIC_Interrupt;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = YX5200_NVIC_Pre;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = YX5200_NVIC_Sub;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init ( &NVIC_InitStructure );
#endif
}

void USART2_Remap_Config(void)
{
#if xy5200_Uart2_def
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* config USART1 clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

	/* Enable Remap clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
	
	/* config the NVIC(USART2) */
	NVIC_Configuration_YX5200_USART2();
	
	/* USART2 Remap GPIO config */
	/* Configure USART2 Tx (PD.05) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOD, &GPIO_InitStructure);    
	/* Configure USART2 Rx (PD.06) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOD, &GPIO_InitStructure);
	
	/* USART2 mode config */
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART2, &USART_InitStructure);
	
	/*USART2中断配置*/
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    USART_ITConfig (USART2, USART_IT_TXE, DISABLE );
	
	USART_Cmd(USART2, ENABLE);
#endif	
}


void Uart2Send ( char *buffer, int nBytes )
{
    // Wait for previous xmt to complete
    while ( TxCounterUart2 < Uart2SendCount );

    // Setup next XMT
    memcpy ( TxBufferUart2, buffer, nBytes );
    TxCounterUart2 = 0;
    Uart2SendCount = nBytes;

    USART_ITConfig ( YX5200_UART, USART_IT_TXE, ENABLE );
}

/*******************************************************************************************************************/
static INT8U MSG_FIFO = NO_MSG;
static INT8U InputNumCnt = 0 ;
static INT8U FolderNum = 0 ;//指定文件夹操作时的文件夹数目
static INT8U FileNum = 0 ;//指定文件名操作时的文件名数目

INT8U _ErrorStatus = 0 ;
INT8U FEEDBACK = 0 ;//是否需要反馈[=1:要应答][=0:不要应答]
INT8U UartRecvStatus ;
INT16U UartRecvACK ;
bool UartRecvACKFlag = false;
INT8U UartRecv_100Ms ;//保证串口接收错误，也能回到初始状态
INT8U System_Status = 0 ;//系统状态

INT8U OutTimeCnt  = 0 ;//重发计时
INT8U ResendCount = 3 ;//重发次数

INT8U QueryACKTime = 0 ;//[查询参数]等待应答计时
INT8U SysReturnTime = 0 ;//系统返回初始界面计时
INT8U PlayledCnt = 0 ;//测试led点亮的时间
INT8U PlayledStatus;
INT8U CurrentEQ = 0 ;//当前的EQ
INT8U PlayMode	= 0 ;//当前的播放模式
INT8U PlayDevice = 0;//当前播放的设备
INT8U OnlineDevice = 0 ;//在线的设备

INT8U DelayTime = 0 ;

INT8U input_vol = 0 ;//当前的音量
INT8U CurrentModule = 0 ;//当前的模块型号
INT8U PointFunc = 0 ;

INT16U input_number ;//选曲输入的数字
INT16U CurDevTotal = 0 ;//当前设备的总文件数
INT16U UDiskTotal =0  ;//U盘总文件数
INT16U TFTotal = 0 ;//TF卡文件数
INT16U FlashTotal = 0 ;//_FLASH中的文件数

INT16U CurDevNUM = 0 ;//当前设备播放的曲目
INT16U UDiskCurFile =0	;//当前U盘播放的文件号
INT16U TFCurFile = 0 ;//当前TF播放的文件号
INT16U FlashCurFile = 0 ;//当前_FLASH播放的文件号


cc Status0 , Status1 ;

/*****************************************************************************************************
 - 功能描述： 将消息存入消息池
 - 隶属模块： 外部 
 - 参数说明： 无
 - 返回说明： 
 - 注：无 
*****************************************************************************************************/
void put_msg_lifo(INT8U msg)
{
    MSG_FIFO = msg ;           
}

/*****************************************************************************************************
 - 功能描述： 取消息
 - 隶属模块： 外部 
 - 参数说明： 无
 - 返回说明： 
 - 注：无 
*****************************************************************************************************/
INT8U app_get_msg(void)
{
    static INT8U msg ;

    msg = MSG_FIFO ;
    
    if(MSG_FIFO != NO_MSG)
    {
        MSG_FIFO = NO_MSG ;
        return msg ;
    }
    else
    {
        return NO_MSG ;//将消息清除
    }
}

/********************************************************************************************
 - 功能描述： 清屏
 - 隶属模块： 内部
 - 参数说明：
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void LCD1602_Clear(void)
{
    LCD1602_wcmd(0x01);
    Delay_Ms(1);  
}

/********************************************************************************************
 - 功能描述： 指定位置显示字符串
 - 隶属模块： 内部
 - 参数说明： x:代表显示的行[0:第一行][1:第二行]
              y:代表显示的列[0~15]
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void LCD1602_DispStr( INT8U x, INT8U y, INT8U *str)
{
    if(x > 1 || y > 15)
    {
          return ;
    } 
    for( ; *str != 0; )
    {
        LCD1602_DispChar( x, y,*str);
        str++;
        y++;
    }
}

/********************************************************************************************
 - 功能描述： 清除指定的行
 - 隶属模块： 内部
 - 参数说明：
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void LCD1602_ClearLine(INT8U x)
{
    LCD1602_DispStr(x, 0, "                ") ;   
}


/*****************************************************************************************************
 - 函数说明：显示模块返回的错误
 - 隶属模块：内部
 - 参数说明：无
 - 返回说明：
 - 注      ：

*****************************************************************************************************/
void DispModlueError(INT8U error)
{
    LCD1602_Clear();
    LCD1602_DispStr(0 , 0  , "RETURN ERROR    ");
    switch(error)
    {
        case ERROR_IDLE :
                        
                        break ;
        case ERROR_Busy :
                        LCD1602_DispStr(1 , 0  , "BUSY    ");
                        break ;
        case ERROR_SLEEP :
                        LCD1602_DispStr(1 , 0  , "SLEEP    ");
                        break ;
        case ERROR_RecvError :
                        LCD1602_DispStr(1 , 0  , "RECV ERROR   ");
                        break ;
        case ERROR_CheckSError :
                        LCD1602_DispStr(1 , 0  , "SUM ERROR    ");
                        break ;
        case ERROR_FolPointOver :
                        LCD1602_DispStr(1 , 0  , "FOL OVER     ");
                        break ;
        case ERROR_FolPointNo :
                        LCD1602_DispStr(1 , 0  , "NO FIND FILE ");
                        break ;
    }
}

/*****************************************************************************************************
 - 功能描述： 按键的初始化
 - 隶属模块： 外部
 - 参数说明： 无
 - 返回说明： 无
 - 注：
*****************************************************************************************************/
void RAM_Init(void)
{
	System_Flag_2Ms    = 0 ;
	System_Flag_10Ms   = 0 ;
	System_Flag_100Ms  = 0 ;
	System_Flag_500Ms	   = 0 ;
	RecvOver_Flag	   = 0 ;
	Resend_Flag_Enable = 0 ;
	MainEnable  = true ;  
    SysReturnTime = 0 ;
    PlayledCnt = 0 ;
    PlayledStatus = 0;
    PlayledStatus = SET_PLAYLED_OFF;
}

/*****************************************************************************************************
 - 功能描述：初始化
 - 参数说明：无
 - 返回参数：无
 - 注：无
*****************************************************************************************************/
void System_Init(void)
{
	RAM_Init() ;
    TrigEnable = 1 ;
    PointFunc = POINTNUM_ONE ;
    input_vol = MAX_MAIN_VOL ;//上电默认最大音量
    System_Status = SYS_STATUS_WORK ;
    SysReturnTime = 30 ;//定义3S的时候等待模块初始化完成
}



/********************************************************************************************
 - 功能描述： 返回液晶显示界面
 - 隶属模块： 内部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void ReturnLcdInit(void)
{
    DispDevice(PlayDevice) ;
    if(FEEDBACK)
    {
        LCD1602_DispStr(0 , 13  , "ACK");
    }
    else
    {
        LCD1602_DispStr(0 , 12  , "NACK");   
    } 
#if 0
    if(TrigEnable)
    {
        LCD1602_DispStr(1 , 5  , "TRIG");//触发模式          
    }
    else
    {
        LCD1602_DispStr(1 , 5  , "CONT");//连续模式    
    } 
#endif
    if(PointFunc == POINTNUM_ONE)
    {
        LCD1602_DispStr(1 , 5  , "ONE ");
    }
    else if(PointFunc == POINTNUM_LOOP)
    {
        LCD1602_DispStr(1 , 5  , "LOOP");   
    }
    else if(PointFunc == POINTNUM_FOLDER)
    {
        LCD1602_DispStr(1 , 5  , "FOLD");    
    }
    if(PointFolNum_Flag)
    {
        LCD1602_DispStr(0 , 7  , "F");//指定文件夹
    }
    else
    {
        LCD1602_DispStr(0 , 7  , "N");//正常指定曲目        
    }
}


/********************************************************************************************
 - 功能描述： 显示播放到的设备
 - 隶属模块： 内部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void Disp_PlayDevice(INT8U device)
{
    if(device == PLAYDEVICE_UDISK)
    {
        LCD1602_DispStr(1 , 0  , "PLAYD:_UDISK");
    }
    else if(device == PLAYDEVICE_TFCARD)
    {
        LCD1602_DispStr(1 , 0  , "PLAYD:_TFCARD");
    }
    else if(device == PLAYDEVICE_PC)
    {
        LCD1602_DispStr(1 , 0  , "PLAYD:_PC");
    }
    else if(device == PLAYDEVICE_FLASH)
    {
        LCD1602_DispStr(1 , 0  , "PLAYD:_FLASH");
    }
    else if(device == PLAYDEVICE_SLEEP)
    {
        LCD1602_DispStr(1 , 0  , "PLAYD:SLEEP");
    }    
}

/********************************************************************************************
 - 功能描述： 显示播放到的模式
 - 隶属模块： 内部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void Disp_PlayMode(INT8U mode)
{
    if(mode == REPEAT_ALL)
    {
        LCD1602_DispStr(1 , 0  , "PLAYM:ALL_0");
    }
    else if(mode == REPEAT_FOLDER)
    {
        LCD1602_DispStr(1 , 0  , "PLAYM:FOLDER_1");
    }  
    else if(mode == REPEAT_ONE)
    {
        LCD1602_DispStr(1 , 0  , "PLAYM:ONE_2");
    }
    else if(mode == REPEAT_RANDOM)
    {
        LCD1602_DispStr(1 , 0  , "PLAYM:RANDOM_3");
    }    
}

/********************************************************************************************
 - 功能描述： 显示播放到的EQ
 - 隶属模块： 内部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void Disp_PlayEQ(INT8U EQ)
{
    if(EQ == EQ_NORMAL)
    {
        LCD1602_DispStr(1 , 0  , "EQ:NORMAL_0");
    }
    else if(EQ == EQ_POP)
    {
        LCD1602_DispStr(1 , 0  , "EQ:POP_1");
    }
    else if(EQ == EQ_ROCK)
    {
        LCD1602_DispStr(1 , 0  , "EQ:ROCK_2");
    }
    else if(EQ == EQ_JAZZ)
    {
        LCD1602_DispStr(1 , 0  , "EQ:JAZZ_3");
    }
    else if(EQ == EQ_CLASSIC)
    {
        LCD1602_DispStr(1 , 0  , "EQ:CLASSIC_4");
    }
    else if(EQ == EQ_BASS)
    {
        LCD1602_DispStr(1 , 0  , "EQ:BASS_5");
    }    
}

/********************************************************************************************
 - 功能描述： 显示模块返回的在线设备
 - 隶属模块： 内部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void Disp_OnlineDevice(INT8U Online)
{
    INT8U temp ;
    temp = Online & 0x01 ;
    if(temp)//_UDISK
    {
        LCD1602_DispStr(1 , 12  , "U");
    }
    temp = Online & 0x02 ;
    if(temp)//TF
    {
        LCD1602_DispStr(1 , 13  , "T");
    }
    temp = Online & 0x04 ;
    if(temp)//_PC
    {
        LCD1602_DispStr(1 , 14  , "P");
    }   
    temp = Online & 0x08 ;
    if(temp)//_FLASH
    {
        LCD1602_DispStr(1 , 15  , "F");
    }  
}
/********************************************************************************************
 - 功能描述： 上电跳转至设备状态
 - 隶属模块： 外部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void GoInitDevice(INT8U Online)
{
    INT8U temp ;
    if(0 == Online)
    {
        PlayDevice = PLAYDEVICE_SLEEP ; 
        return ;
    }
    temp = Online & 0x01 ;
    if(temp)
    {
        PlayDevice = PLAYDEVICE_UDISK ; 
        return ;
    }
    temp = Online & 0x02 ;
    if(temp)//TF
    {
        PlayDevice = PLAYDEVICE_TFCARD ; 
        return ;
    }
    temp = Online & 0x04 ;
    if(temp)//_PC
    {
        PlayDevice = PLAYDEVICE_PC ; 
        return ;
    }   
    temp = Online & 0x08 ;
    if(temp)//_FLASH
    {
        PlayDevice = PLAYDEVICE_FLASH ; 
        return ;
    }
}

/********************************************************************************************
 - 功能描述： 设备拔出跳至下一个设备
 - 隶属模块： 外部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void GotoNextDevice(INT8U device)
{
    if(device == _UDISK)
    {
        if(OnlineDevice & BIT(_TFCARD))
        {
            PlayDevice = PLAYDEVICE_TFCARD ;
        }
        else
        {
            if(OnlineDevice & BIT(_FLASH))
            {
                PlayDevice = PLAYDEVICE_FLASH ;
            }
            else
            {
                PlayDevice = PLAYDEVICE_SLEEP ;
            }
        }
    }
    else if(device == _TFCARD)
    {
        if(OnlineDevice & BIT(_UDISK))
        {
            PlayDevice = PLAYDEVICE_UDISK ;
        }
        else
        {
            if(OnlineDevice & BIT(_FLASH))
            {
                PlayDevice = PLAYDEVICE_FLASH ;
            }
            else
            {
                PlayDevice = PLAYDEVICE_SLEEP ;
            }
        }  
    }
    else if(device == _PC)
    {
        if(OnlineDevice & BIT(_FLASH))
        {
            PlayDevice = PLAYDEVICE_FLASH ;
        }
        else
        {
            PlayDevice = PLAYDEVICE_SLEEP ;
        }
    }
       
}

void ChangeDevice(INT8U dev)
{
    if(dev == PLAYDEVICE_UDISK)
    {
        if(0 == (OnlineDevice & BIT(_UDISK)))
        {
            if(OnlineDevice & BIT(_TFCARD))
            {
                PlayDevice = PLAYDEVICE_TFCARD ;
            }
            else
            {
                if(OnlineDevice & BIT(_FLASH))
                {
                    PlayDevice = PLAYDEVICE_FLASH ;
                }
                else
                {
                    PlayDevice = PLAYDEVICE_SLEEP ;
                }
            }
        }
    }
    if(dev == PLAYDEVICE_TFCARD)
    {
        if(0 == (OnlineDevice & BIT(_TFCARD)))
        {
            if(OnlineDevice & BIT(_UDISK))
            {
                PlayDevice = PLAYDEVICE_UDISK ;
            }
            else
            {
                if(OnlineDevice & BIT(_FLASH))
                {
                    PlayDevice = PLAYDEVICE_FLASH ;
                }
                else
                {
                    PlayDevice = PLAYDEVICE_SLEEP ;
                }
            }
        }
    }
    else if(dev == PLAYDEVICE_PC)
    {
        if(0 == (OnlineDevice & BIT(_PC)))
        {    
            if(OnlineDevice & BIT(_FLASH))
            {
                PlayDevice = PLAYDEVICE_FLASH ;
            }
            else
            {
                PlayDevice = PLAYDEVICE_SLEEP ;
            }
        }
    }
    else if(dev == PLAYDEVICE_FLASH)
    {
        if(0 == (OnlineDevice & BIT(_FLASH)))
        {    
             PlayDevice = PLAYDEVICE_SLEEP ;
        }  
    }
}

/********************************************************************************************
 - 功能描述： LCD1602液晶初始化
 - 隶属模块： 内部
 - 参数说明： 
 - 返回说明：
 - 注：	     
*******************************************************************************************/
void LCD1602_Init()
{							//LCD初始化设定
    LCD1602_DispDat( 0,  0, 0);
    LCD1602_DispStr( 0 , 0 , "QYMxFS----------");//
    LCD1602_DispStr( 1 , 0 , "PLAY:           ");//
    ReturnLcdInit();
    Disp_OnlineDevice(OnlineDevice);
}




/*****************************************************************************************************
 - 函数说明：消息处理
 - 隶属模块：内部
 - 参数说明：无
 - 返回说明：
 - 注      ：
*****************************************************************************************************/
void MSG_Task(void)
{
    static INT8U msg ;

    msg = app_get_msg();

    switch(msg)
    {
       /*****************************************************
                          显示模块返回的错误
        *****************************************************/
        case MSG_RECV_ERROR:
                DispModlueError(_ErrorStatus);
                SysReturnTime = SYSRETURNTIME ;           
                break ;
                
       /*****************************************************
                          返回初始状态
        *****************************************************/
        case MSG_RETURN_INIT:
                RAM_Init() ;
                LCD1602_Init();
                DBG("MSG_RETURN_INIT\n") ;            
                break ;
       /*****************************************************
                          设备插入
        *****************************************************/
        case MSG_UDISK_IN:
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "_UDISK IN");
                PlayDevice = PLAYDEVICE_UDISK ;
                OnlineDevice |= BIT(_UDISK) ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
        case MSG_TF_IN:
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "TF IN");
                PlayDevice = PLAYDEVICE_TFCARD ;
                OnlineDevice |= BIT(_TFCARD) ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
        case MSG_PC_IN:
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "_PC IN");
                PlayDevice = PLAYDEVICE_PC ;
                OnlineDevice |= BIT(_PC) ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
       /*****************************************************
                          设备拔出
        *****************************************************/
        case MSG_UDISK_OUT:
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "_UDISK OUT");
                OnlineDevice &= ~ BIT(_UDISK) ;
                GotoNextDevice(_UDISK) ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
        case MSG_TF_OUT:
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "TF OUT");
                OnlineDevice &= ~ BIT(_TFCARD) ;
                GotoNextDevice(_TFCARD) ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
        case MSG_PC_OUT:
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "_PC OUT");
                OnlineDevice &= ~ BIT(_PC) ;
                GotoNextDevice(_PC) ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
       /*****************************************************
                          返回初始状态
        *****************************************************/
        case MSG_DELAY_TIME:
                DelayTime = 6 ;        
                break ;                   

       /*****************************************************
                          查询参数无应答
        *****************************************************/
        case MSG_QUERY_ERROR:
                LCD1602_Clear() ;
                LCD1602_DispStr(1 , 0  , "ERROR CMD");
                SysReturnTime = SYSRETURNTIME ;
                break ;
                
       /*****************************************************
                          查询歌曲总数
        *****************************************************/
        case MSG_QUERY_NUMTOTAL:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "QUERY TOTAL NUM ");    
#if 1
                if(PlayDevice == PLAYDEVICE_SLEEP)
                {
                    LCD1602_Clear() ;
                    LCD1602_DispStr(1 , 0  , "MOD IN SLEEP  ");    
                    QueryACKTime = QUERYACKTIME ;//等待应答计时
                    break;
                }
#endif
                if(PlayDevice == PLAYDEVICE_UDISK)
                {
                    Uart_SendCMD(UARTQUR_UDISK_TOTAL , FEEDBACK , 0);
                }
                else if(PlayDevice == PLAYDEVICE_TFCARD)
                {
                    Uart_SendCMD(UARTQUR_TF_TOTAL , FEEDBACK , 0);   
                }
                else if(PlayDevice == PLAYDEVICE_FLASH)
                {
                    Uart_SendCMD(UARTQUR_FLASH_TOTAL , FEEDBACK , 0);   
                }
                LCD1602_DispStr(1 , 0  , "WAIT RETURN ...."); 
                QueryACKTime = QUERYACKTIME ;//等待应答计时
                break ;

       /*****************************************************
                        显示查询当前播放设备曲目总数
        *****************************************************/
        case MSG_RETURN_NUMTOTAL:
                QueryACKTime = 0 ;//清除等待超时
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "DEVICE:         "); 
                LCD1602_DispStr(1 , 0  , "TOTAL NUM:      "); 
                if(PlayDevice == PLAYDEVICE_UDISK)
                {
                    CurDevTotal = UDiskTotal ;
                    LCD1602_DispStr(0 , 8  , "_UDISK");     
                }
                else if(PlayDevice == PLAYDEVICE_TFCARD)
                {
                    CurDevTotal = TFTotal ;
                    LCD1602_DispStr(0 , 8  , "TF");       
                }
                else if(PlayDevice == PLAYDEVICE_FLASH)
                {
                    CurDevTotal = FlashTotal ;
                    LCD1602_DispStr(0 , 8  , "_FLASH");      
                }
                itoa4(CurDevTotal);
                /LCD1602_DispStr(1 , 11 , (INT8U *)bcd_number);
                SysReturnTime = SYSRETURNTIME ;
                break ;

                
       /*****************************************************
                          查询当前曲目
        *****************************************************/
        case MSG_QUERY_CURNUM:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "QUERY CUR NUM   "); 
                if(PlayDevice == PLAYDEVICE_SLEEP)
                {
                    LCD1602_Clear() ;
                    LCD1602_DispStr(1 , 0  , "MOD IN SLEEP ");   
                    QueryACKTime = QUERYACKTIME ;//等待应答计时
                    break;
                }  
                if(PlayDevice == PLAYDEVICE_UDISK)
                {
                    Uart_SendCMD(UARTQUR_UDISK_FILE , FEEDBACK , 0);
                }
                else if(PlayDevice == PLAYDEVICE_TFCARD)
                {
                    Uart_SendCMD(UARTQUR_TF_FILE , FEEDBACK , 0);   
                }
                else if(PlayDevice == PLAYDEVICE_FLASH)
                {
                    Uart_SendCMD(UARTQUR_FLASH_FILE, FEEDBACK , 0);   
                }
                LCD1602_DispStr(1 , 0  , "WAIT RETURN ....");

                QueryACKTime = QUERYACKTIME ;//等待应答计时
                break ;

       /*****************************************************
                        显示查询当前播放曲目
        *****************************************************/
        case MSG_RETURN_CURNUM:
                QueryACKTime = 0 ;//清除等待超时
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "DEVICE:         "); 
                LCD1602_DispStr(1 , 0  , "CUR UM:            "); 
                if(PlayDevice == PLAYDEVICE_UDISK)
                {
                    CurDevNUM = UDiskCurFile ;
                    LCD1602_DispStr(0 , 8  , "_UDISK");     
                }
                else if(PlayDevice == PLAYDEVICE_TFCARD)
                {
                    CurDevNUM = TFCurFile ;
                    LCD1602_DispStr(0 , 8  , "TF");       
                }
                else if(PlayDevice == PLAYDEVICE_FLASH)
                {
                    CurDevNUM = FlashCurFile ;
                    LCD1602_DispStr(0 , 8  , "_FLASH");      
                }
                itoa4(CurDevNUM);
                LCD1602_DispStr(1 , 11 , (INT8U *)bcd_number);
                SysReturnTime = SYSRETURNTIME ;
                break ;
       /*****************************************************
                          查询当前音量
        *****************************************************/
        case MSG_QUERY_VOL:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "QUERY CUR VOL   ");
#if 0
                if(PlayDevice == PLAYDEVICE_SLEEP)
                {
                    LCD1602_DispStr(1 , 0  , "MOD IN SLEEP ");
                }
                else
#endif
                {
                    LCD1602_DispStr(1 , 0  , "WAIT RETURN ...."); 
                    Uart_SendCMD(UARTQUR_VOL , FEEDBACK , 0);
                }
                QueryACKTime = QUERYACKTIME ;//等待应答计时
                break ;

       /*****************************************************
                        显示查询当前音量的数据
        *****************************************************/
        case MSG_RETURN_VOL:
                QueryACKTime = 0 ;//清除等待超时
                LCD1602_Clear() ;
                LCD1602_DispStr(1 , 0  , "VOL:            "); 
                itoa2(input_vol);
                LCD1602_DispStr(1 , 11 , (INT8U *)bcd_number);
                SysReturnTime = SYSRETURNTIME ;
                break ;
                
       /*****************************************************
                          查询当前EQ
        *****************************************************/
        case MSG_QUERY_EQ:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "QUERY CUR EQ    ");
#if 1
                if(PlayDevice == PLAYDEVICE_SLEEP)
                {
                    LCD1602_DispStr(1 , 0  , "MOD IN SLEEP ");
                }
                else
#endif
                {
                    LCD1602_DispStr(1 , 0  , "WAIT RETURN ...."); 
                    Uart_SendCMD(UARTQUR_EQ , FEEDBACK , 0);
                }
                QueryACKTime = QUERYACKTIME ;//等待应答计时
                break ;

       /*****************************************************
                        显示查询当前EQ的数据
        *****************************************************/
        case MSG_RETURN_EQ:
                QueryACKTime = 0 ;//清除等待超时
                LCD1602_Clear() ;
                Disp_PlayEQ(CurrentEQ);
                SysReturnTime = SYSRETURNTIME ;
                break ;

                
       /*****************************************************
                          查询当前播放模式
        *****************************************************/
        case MSG_QUERY_PLAYMODE:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "QUERY PLAY MODE ");

                if(PlayDevice == PLAYDEVICE_SLEEP)
                {
                    LCD1602_DispStr(1 , 0  , "MOD IN SLEEP   ");
                }
                else

                {
                    LCD1602_DispStr(1 , 0  , "WAIT RETURN ...."); 
                    Uart_SendCMD(UARTQUR_PLAYMODE , FEEDBACK , 0);
                }
                QueryACKTime = QUERYACKTIME ;//等待应答计时
                break ;

       /*****************************************************
                        显示查询当前播放模式的数据
        *****************************************************/
        case MSG_RETURN_PLAYMODE:
                QueryACKTime = 0 ;//清除等待超时
                LCD1602_Clear() ;
                Disp_PlayMode(PlayMode);
                SysReturnTime = SYSRETURNTIME ;
                break ;

       /*****************************************************
                          显示模块初始化发出的数据
        *****************************************************/
        case MSG_RETURN_MINIT:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "RECV MODL OK    ");
                LCD1602_DispStr(1 , 0  , "ONLINE DEV:     ");
                Disp_OnlineDevice(OnlineDevice);
                GoInitDevice(OnlineDevice) ;
                System_Status = SYS_STATUS_WORK ;
                SysReturnTime = SYSRETURNTIME ;//定义3S的时候等待模块初始化完成
                break ;
#if 0
       /*****************************************************
                          切换工作模式
        *****************************************************/
        case MSG_TRIG_MODE:
                TrigEnable = !TrigEnable ;
                LCD1602_ClearLine(1);
                if(TrigEnable == PLAYDEVICE_UDISK)
                {
                    LCD1602_DispStr(1 , 0  , "PLAYM :  TRIG");//1:触发模式
                }
                else
                {
                    LCD1602_DispStr(1 , 0  , "PLAYM :  CONT");//0:连续模式
                }
                SysReturnTime = SYSRETURNTIME ;
                break ;
#endif                

       /*****************************************************
                          切换当前播放设备
        *****************************************************/
        case MSG_NEXT_WORKMODE: 
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "PLAY DEVICE SET");
                PlayDevice ++ ;
                if(PlayDevice == PLAYDEVICE_MAX)
                {
                    PlayDevice = PLAYDEVICE_UDISK;  
                }
                ChangeDevice(PlayDevice);
                Disp_PlayDevice(PlayDevice);
                Uart_SendCMD(UARTCMD_PLAYDEVICE , FEEDBACK , PlayDevice);
                
                SysReturnTime = SYSRETURNTIME ;
                break ;
       /*****************************************************
                          EQ
        *****************************************************/
        case MSG_MUSIC_NEXT_EQ:
                LCD1602_Clear() ;
                LCD1602_DispStr(0 , 0  , "EQ SET");
                CurrentEQ ++ ;
                if(CurrentEQ == EQ_BASS)
                {
                    CurrentEQ = EQ_NORMAL;  
                }
                Uart_SendCMD(UARTCMD_EQ_POINT , FEEDBACK , CurrentEQ);
                Disp_PlayEQ(CurrentEQ);
                SysReturnTime = SYSRETURNTIME ;
                break ;

       /*****************************************************
                          播放
        *****************************************************/
        case MSG_MUSIC_PLAY: 
                Uart_SendCMD(UARTCMD_MUSIC_PLAY , FEEDBACK , 0);
                DBG("MUSIC_PLAY\n") ;
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "MUSIC_PLAY");
                SysReturnTime = SYSRETURNTIME ;
                break ;
       /*****************************************************
                          暂停
        *****************************************************/
        case MSG_MUSIC_PAUSE: 
                Uart_SendCMD(UARTCMD_MUSIC_PAUSE , FEEDBACK , 0);
                DBG("MUSIC_PAUSE\n") ;
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "MUSIC_PAUSE");
                SysReturnTime = SYSRETURNTIME ;
                break ;                   
       /*****************************************************
                          下一曲
        *****************************************************/
        case MSG_MUSIC_NEXT_FILE:
                if(input_vol > 0)
                {
                    input_vol -- ;
                }
                Uart_SendCMD(UARTCMD_NEXT_FILE , FEEDBACK , 0);
                DBG("MSG_MUSIC_NEXT_FILE\n") ;
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "NEXT FILE PLAY");
                SysReturnTime = SYSRETURNTIME ;
                break ;

       /*****************************************************
                          上一曲
        *****************************************************/
        case MSG_MUSIC_PREV_FILE:
                Uart_SendCMD(UARTCMD_PREV_FILE , FEEDBACK , 0);
                DBG("MSG_MUSIC_PREV_FILE\n") ;
                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "PREV FILE PLAY");
                SysReturnTime = SYSRETURNTIME ;
                break ;

       /*****************************************************
                         循环播放模式设置
        *****************************************************/
        case MSG_PLAYALL:
                PlayAllFlag = !PlayAllFlag ;
                LCD1602_ClearLine(1);
                if(PlayAllFlag)
                {
                    LCD1602_DispStr(1 , 0  , "ALL PLAY ON   ");
                    put_msg_lifo(MSG_PLAYALL_ON);
                    
                }
                else
                {
                    LCD1602_DispStr(1 , 0  , "ALL PLAY OFF");
                    put_msg_lifo(MSG_PLAYALL_OFF);
                }
                break ;
       /*****************************************************
                         循环播放模式--开
        *****************************************************/
        case MSG_PLAYALL_ON:                
                Uart_SendCMD(UARTCMD_PLAYALL , FEEDBACK , 0x01);
                DBG("MSG_MUSIC_PREV_FILE\n") ;
                SysReturnTime = SYSRETURNTIME ;
                break ;

       /*****************************************************
                         循环播放模式--关
        *****************************************************/
        case MSG_PLAYALL_OFF:                
                Uart_SendCMD(UARTCMD_PLAYALL , FEEDBACK , 0);
                DBG("MSG_MUSIC_PREV_FILE\n") ;
                SysReturnTime = SYSRETURNTIME ;
                break ;
        /******************************************************
                          音量调节
         *****************************************************/
	    case MSG_VOL_UP:
        		input_vol++;
        		input_vol++;
        case MSG_VOL_DOWN:
                input_vol--;
                if ( input_vol == 255 )
                {
                    input_vol = 0 ;
                }
                else if (input_vol > MAX_MAIN_VOL)
                {
                    input_vol = MAX_MAIN_VOL ;
                }
                Uart_SendCMD(UARTCMD_VOL_POINT , FEEDBACK , input_vol);

                LCD1602_ClearLine(1);
                LCD1602_DispStr(1 , 0  , "CUR VOL[  ]");
                SysReturnTime = SYSRETURNTIME ;
                itoa2(input_vol);
                LCD1602_DispStr(1 , 8 , (INT8U *)bcd_number);
                break ;
                
       /*****************************************************
                          输入超时处理
        *****************************************************/
		case MSG_INPUT_TIMEOUT:
                DBG("MSG_INPUT_TIMEOUT\n") ;
    			put_msg_lifo(MSG_MUSIC_PLAY_NEW_FILE);
    			break;
                
       /*****************************************************
                          选择指定的曲目
        *****************************************************/		
        case MSG_MUSIC_PLAY_NEW_FILE:
			    DBG("MSG_MUSIC_PLAY_NEW_FILE\n") ;
                LCD1602_Clear() ;
                if(PlayDevice == PLAYDEVICE_SLEEP)
                {
                    LCD1602_DispStr(1 , 0  , "MOD IN SLEEP ");
                }
                else
                {
                    if(POINTNUM_ONE == PointFunc)//指定单曲播放
                    {
                        Uart_SendCMD(UARTCMD_NUM_POINT , FEEDBACK , input_number);
                        LCD1602_DispStr(1 , 0  , "SEND ONE NUM ");
                    }
                    else if(POINTNUM_LOOP == PointFunc)//指定单曲循环播放
                    {
                        Uart_SendCMD(UARTCMD_LOOP_PLAY , FEEDBACK , input_number);
                        LCD1602_DispStr(1 , 0  , "SEND LOOP NUM ");
                    }
                    else if(POINTNUM_FOLDER == PointFunc)//指定文件夹和文件
                    {
                        input_number = ((INT16U)(FolderNum)<<8)| FileNum ;
                        FileNum  = 0 ; 
                        FolderNum= 0 ;
                        Uart_SendCMD(UARTCMD_MUSIC_FOLDER , FEEDBACK , input_number);
                        LCD1602_DispStr(1 , 0  , "FOLD FILE POINT");
                    }
                }          
                input_number = 0 ;
                SysReturnTime = SYSRETURNTIME ;
			    break;

       /*****************************************************
                      指定文件夹以及指定文件名
        *****************************************************/		
                
        case MSG_SET_PLAYMODE:
                if((++PointFunc) == POINTNUM_MAX)
                {
                    PointFunc = POINTNUM_ONE ;   
                }
                SysReturnTime = SYSRETURNTIME ;
                LCD1602_ClearLine(1);
                if(PointFunc == POINTNUM_ONE)
                {
                    LCD1602_DispStr(1 , 0  , "POINT ONE      ");
                }
                else if(PointFunc == POINTNUM_LOOP)
                {
                    LCD1602_DispStr(1 , 0  , "POINT LOOP     ");   
                }
                else if(PointFunc == POINTNUM_FOLDER)
                {
                    LCD1602_DispStr(1 , 0  , "POINT FOLDER   ");    
                }
			    break;

       /*****************************************************
                          切换是否需要反馈
        *****************************************************/		
                
        case MSG_UART_ACK:
                LCD1602_ClearLine(1);
                if(FEEDBACK)
                {
                    FEEDBACK = 0 ;
                    LCD1602_DispStr(1 , 0  , "NO NEED ACK");
                }
                else
                {
                    FEEDBACK = 1 ;
                    LCD1602_DispStr(1 , 0  , "NEED ACK");
                }    
                SysReturnTime = SYSRETURNTIME ;
			    break;
       /*****************************************************
                          数字键操作，用于数字选曲
                          和文件夹指定，文件名指定
        *****************************************************/
        case MSG_0:
        case MSG_1:
        case MSG_2:
        case MSG_3:
        case MSG_4:
        case MSG_5:
        case MSG_6:
        case MSG_7:
        case MSG_8:
        case MSG_9:
                if(POINTNUM_FOLDER == PointFunc)//指定文件夹的状态
                {
                    PointPlayFolderNum(msg);                 
                }
                else
                {
                    PointPlayNum(msg) ;                 
                }
                break ;
    }
}



