#include "Check_Ctrl.h"
#include "TYW_stdint.h"
#include "RTE_GPIO.h"
#include "UDS_ISO14229_Server.h"
#include "string.h"
#include "Display_Info.h"
#include "Analog_Signals.h"
#include "CAN_Communication_Matrix.h"
#include "COM_CAN.h"
#include "GenDelay.h"
#include "TFT_LCD.h"
#include "Key.h"
uint32_t NO_IPK_EXIST_Status;
uint32_t NO_IPK_EXIST;
//uint32_t NO_IPK_EXIST_Back;
uint32_t NO_IPK_EXIST_TIMER;
const uint16_t ODODigitNum09PosX1 []  = {220,210,200};
const uint16_t ODODigitNum09PosX2 []  = {230,220,210};
const unsigned char gImage_cuo12[] = {24, 24,0X00,0X00,0X00,0X00,0X30,0X70,0XE0,0XC0,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0X70,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X83,0XC7,0X66,0X38,0X38,0X7C,0XE6,0XC3,0X81,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X0C,0X06,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X07,0X0E,0X0C,0X00,0X00,0X00,0X00,};
const unsigned char gImage_dui12[] = {24, 24,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XE0,0X40,0X00,0X00,0X00,0X00,0X08,0X18,0X30,0X70,0XC0,0X80,0X00,0X00,0X80,0XC0,0XE0,0X70,0X38,0X1C,0X0E,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,};
uint8_t InterNal_Version [ 128 ];
uint8_t SW_Version [ 128 ];
uint8_t HW_Version [ 128 ];
uint8_t Part_Version [ 128 ];
uint8_t Risk_Evade [ 128 ];
uint8_t Evade_ODO  [ 128 ];
uint8_t Line_IN_St [ 128 ];
uint8_t Res_St [ 128 ];
uint8_t Made_Date [ 128 ];//制造日期
uint8_t SUPPLIER [ 128 ];//供应商代码
uint8_t Voltage_UDS [ 128 ];
uint8_t BCM_2014 [ 128 ];
//uint8_t InterNal_Version [ 128 ];
uint32_t LCD_CHECN_BEGIN;
uint32_t LCD_CHECN_Retry;

uint32_t LINE_IN_CHECK_BEGIN;
uint32_t LINE_IN_CHECK_Retry;

uint32_t TFT_CHECN_BEGIN;
uint32_t TFT_CHECN_Retry;

uint32_t VOICE_BACK_BEGIN;
uint32_t VOICE_BACK_Retry;

uint32_t VOICE_CHECK_BEGIN;
uint32_t VOICE_CHECK_Retry;

uint32_t VOICE_CHECN_BEGIN;
uint32_t VOICE_CHECN_Retry;

uint32_t LINE_ODO_CLEAR_BEGIN;
uint32_t VOICE_ODO_CLEAR_Retry;

uint32_t RES_CHECK_BEGIN;
uint32_t RES_CHECK_Retry;

uint32_t UnLock_Flag = 0;
uint32_t UnLock_Retry;

uint8_t  KEYBUFFER [ 4 ];

uint32_t CAN_Msg_Tx_Enable;


uint32_t MENU_CHECK_STEP;

uint32_t Global_Session = 0;
uint32_t loc_Step;


void Function_Check_Ctrl(uint32_t cmd);

void MENU_CHECK_STEP_ADD(void)
{
    // if(CheckResult != 1)        //版本号校验，取消校验屏掉此条
    // {
    //     return ;
    // }

    // if(guangganflag != 3)       //光感锁校验，取消校验屏掉此条
    // {
    //     return ;
    // }

    // if((CurrentWrong == 1) && (MENU_CHECK_STEP == 2))       //静态电流校验，取消校验屏掉此条
    // {
    //     return ;
    // }

    if(MENU_CHECK_STEP <= 9)
    {
        MENU_CHECK_STEP++;
    }
    else        //循环
    {
        MENU_CHECK_STEP = 0;
        guangganflag = 0;
    }
}
void MENU_CHECK_STEP_SUB(void)
{
    if ( MENU_CHECK_STEP != 0 );
        MENU_CHECK_STEP--;
  
    
}

void MENU_CHECK_Init(void)
{
    MENU_CHECK_STEP = 0;
}



uint8_t Get_DID_High_Byte(uint16_t Input_DID)
{
    uint16_t Res = 0;

    Res = (Input_DID >> 8);

    return  (uint8_t)(Res);
}

uint8_t Get_DID_Low_Byte(uint16_t Input_DID)
{
    uint16_t Res = 0;

    Res = (Input_DID & 0xFF);

    return  (uint8_t)(Res);
}







void HW_CHECK_IPK_EXIST(void)
{

    uint8_t DIAG_BUFER [ 2 ];

    DIAG_BUFER [ 0 ] = 0x3E;
    DIAG_BUFER [ 1 ] = 0x00;
    UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);
    
    SET_IPK_NONE( );
}

void SET_IPK_NONE(void)
{
    NO_IPK_EXIST_TIMER = 12;
    NO_IPK_EXIST       = 12345678;
}
void Clear_IPK_NONE(void)   //CAN在线
{
    NO_IPK_EXIST        = 0;
    NO_IPK_EXIST_Status = 0x55667788;
}
uint8_t ClearFlag = 0;
uint8_t ClearFlagBack = 0;

void CHECK_IPK_COUNT(void)
{
    if ( CAN_Msg_Tx_Enable == 1u )
    {
        if ( NO_IPK_EXIST_TIMER != 0 )
        {
            NO_IPK_EXIST_TIMER--;
        }
        else
        {
            if ( NO_IPK_EXIST == 12345678 )
            {
                NO_IPK_EXIST_Status = 0x11223344;
                memset(InterNal_Version, 0xFF, sizeof(InterNal_Version));
                memset(SW_Version, 0xFF, sizeof(SW_Version));
                memset(HW_Version, 0xFF, sizeof(HW_Version));
                memset(Part_Version, 0xFF, sizeof(Part_Version));
                memset(Made_Date, 0xFF, sizeof(Made_Date));
                memset(SUPPLIER, 0xFF, sizeof(SUPPLIER));
                memset(Voltage_UDS, 0xFF, sizeof(Voltage_UDS));
                
                Display_Menu_Type = 0;
                Display_TFT_Clear( );
                Send_Init();                              
                // MENU_CHECK_Init( );
                Global_Session = 0;//掉线时，认为会话模式归零，重连CAN线后，重新将会话模式切到03扩展会话。
            }
            else
            {
                ClearFlag = 0;
            }
        }


    }
}

// 0x11223344 不在线
// 0x55667788 在线

uint32_t Get_IPK_Status(void)
{
    return NO_IPK_EXIST_Status;
}
uint8_t Datajudgment(void)
{
    for(int i = 0; i<34; i++)
    {
        if(barcode_Msg[i] != mimaread[i])
        {
            return 0;
        }
    }
    return 1;
}

void Diag_Info_Init(void)
{
    NO_IPK_EXIST_Status = 0x11223344;
    CAN_Msg_Tx_Enable   = 1u;

    memset(InterNal_Version , 0xFF, sizeof(InterNal_Version));
    memset(SW_Version , 0xFF, sizeof(SW_Version));
    memset(HW_Version , 0xFF, sizeof(HW_Version));
    memset(Part_Version , 0xFF, sizeof(Part_Version));
    memset(Risk_Evade, 0xFF, sizeof(Risk_Evade));
    memset(Evade_ODO, 0xFF, sizeof(Evade_ODO));
    memset(Res_St, 0xFF, sizeof(Res_St));
    memset(Made_Date , 0xFF, sizeof(Made_Date));
    memset(SUPPLIER , 0xFF, sizeof(SUPPLIER));
    //memset(Voltage_UDS, 0xFF, sizeof(Voltage_UDS));
    
}
uint8_t Type_Clear = 0;
extern uint8_t BCM_FLAG_2014 = 0;
uint8_t writefail[5] = {0x46,0x41,0x49,0x4c,0xff};
uint8_t erweima[64] ;
    uint32_t loc_Type = 0;
uint8_t blnamedisplay[4];
uint8_t blenamedisplay[5];
void Total_Check(void)
{

uint8_t cmperweima[34];
uint8_t yibiaoshuju[34];
    
    loc_Type          = Get_Display_Type( );
    if ( Type_Clear != loc_Type )
    {
        Type_Clear = loc_Type;
        Display_TFT_Clear();
    }

    // POWER_CTRL_KL30_Current = 0u;//静态电流电阻的远仪表端，永远接通总电源。B+~

    BCM_FLAG_2014 = BCM_2014[1] & 0X40 ;
    // loc_Type = 2;
    switch ( loc_Type )
    {
        case 0:
            if(saomafinish == 0)
            {
                for(int i = 0;i<34;i++)
                {
                    if(barcode_Msg[i] != erweima[i])
                    {
                        Display_TFT_Clear();
                        break;
                    }
                }

                memcpy(erweima,barcode_Msg,34);
                erweima[34] = 0xFF;

                
                General_Number_Disp(erweima, 3, 60); 

                if(writeflag == 1 || writeflag == 3)
                {
                    TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_dui12 );  
                }
                else
                {
                    TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_cuo12 );
                }
            }
            else if(saomafinish == 1)
            {
                for(int i = 0;i<34;i++)
                {
                    if(barcode_Msg[i] != erweima[i])
                    {
                        Display_TFT_Clear();
                        break;
                    }
                }
                
                memcpy(erweima,barcode_Msg,34);
                erweima[34] = 0xFF;
                General_Number_Disp(erweima, 3, 60);  

                if(writeflag == 1 || writeflag == 3)
                {
                    TFT_LCD_Draw_Bmp(200,   90+40-5,  ( uint8_t * )gImage_dui12 );  
                }
                else
                {
                    TFT_LCD_Draw_Bmp(200,   90+40-5,  ( uint8_t * )gImage_cuo12 );
                }
                uint8_t writesuccess[8] = {0x53,0x55,0x43,0x43,0x45,0x53,0x53,0xff};
                General_Number_Disp(writesuccess, 3, 125); 
            }
            else if(saomafinish == 2)
            {
                memcpy(erweima,barcode_Msg,34);
                erweima[34] = 0xFF;
                General_Number_Disp(erweima, 3, 60);  

                if(writeflag == 1 || writeflag == 3)
                {
                    TFT_LCD_Draw_Bmp(200,   90+40-5,  ( uint8_t * )gImage_dui12 );  
                }
                else
                {
                    TFT_LCD_Draw_Bmp(200,   90+40-5,  ( uint8_t * )gImage_cuo12 );
                }

                uint8_t writesuccess[8] = {0x53,0x55,0x43,0x43,0x45,0x53,0x53,0xff};
                General_Number_Disp(writesuccess, 3, 125);

                mimaread[34] = 0xFF;
                General_Number_Disp(mimaread, 3, 200);  

                if(Datajudgment())
                {
                    saomafinish = 3;
                    firstpowerflag = 2;
                    General_Number_Disp(writesuccess, 3, 250);
                }
                else
                {
                    General_Number_Disp(writefail, 3, 250);
                    LINE_OUT_NEG_01 = 0;
                    LINE_OUT_NEG_04 = 0;
                    LINE_OUT_NEG_02 = 0;
                    LINE_OUT_NEG_03 = 0;
                }
            }
            break;

        case 1: 
            // Buzzer_Warning();    
            memcpy(cmperweima,barcode_Msg1,34);
            
            
            //  for(int i =0;i<34;i++)
            // {
            //     cmperweima[i] = barcode_Msg1[i];
            // }
            cmperweima[34] = 0xFF;
            General_Number_Disp(cmperweima, 3, 60);  
            // memcpy(yibiaoshuju,get_num_buf,34);
            // for(int i =0;i<34;i++)
            // {
            //     yibiaoshuju[i] = get_num_buf[i];
            // }
            // yibiaoshuju[34] = 0xFF;
            // General_Number_Disp(yibiaoshuju, 3, 165);  
            // zhenduanflag = 3;
            break; 
        case 2: 
            // POWER_CTRL_KL30   = 0u;  //B+
            POWER_CTRL_KL15   = 0u;  //KL15
            CAN_Msg_Tx_Enable = 1u;  
            if(MENU_CHECK_STEP == 0)
            {
                Display_Version_Info(1);  
            }
            else
            {
                loc_Type_back = 0;
                
            }
            if((R485_ID4Dh.Sig.ESP322 == 0) && ( R485_ID4Dh.Sig.G0312 == 0))
            {
                // MENU_CHECK_STEP = 0;
            }
            Function_Check_Ctrl(1);    //Excel表格发报文。硬线信号  
            Diag_Info_Init( );       
            Display_Init( );            
            // Send_Init();
            // Send_ODO_Init();             
            // Display_Send_ODO(0); 
            //FUEL_UDS__Display(3);
            Buzzer_Warning();
                  
            break;              
            
        case 3:
            Function_Check_Ctrl(1);    //Excel表格发报文。硬线信号  
            blename[4] = 0xFF;
            GUI_Display_Version_Code_Service(3, 114, "Name   is ", Letter_Num_11, PCodeText_Space_size);
            blnamedisplay[0] = name2 / 16;
            blnamedisplay[1] = name2 % 16;
            blnamedisplay[2] = name1 / 16;
            blnamedisplay[3] = name1 % 16;

            for(int i = 0; i<4; i++)
            {
                if(blnamedisplay[i] >= 10)
                {
                    blnamedisplay[i] += 55;
                }
            }

            blenamedisplay[0] = blnamedisplay[0];
            blenamedisplay[1] = blnamedisplay[1];
            blenamedisplay[2] = blnamedisplay[2];
            blenamedisplay[3] = blnamedisplay[3];
            blenamedisplay[4] = 0xFF;
            
            if(blnamedisplay[0]+blnamedisplay[1]+blnamedisplay[2]+blnamedisplay[3] != 0)
            {
                General_Number_Disp(blenamedisplay, 100, 114);              //显示蓝牙名字
            }

            //负号
            uint8_t fuhao[2] = {0x91, 0xFF};
            /* 显示蓝牙强度 */
            GUI_Display_Version_Code_Service(3, 135, "BT   Signal   Strength   is", Letter_Num_11, PCodeText_Space_size);
            /* 显示WIFI强度 */
            GUI_Display_Version_Code_Service(3, 160, "WIFI   Signal   Strength   is", Letter_Num_11, PCodeText_Space_size);
            if(data_received_flag == 1)
            {
                if(BT_Signal_Symbol)
                {
                    General_Number_Disp(fuhao, 180, 134);          //符号
                }
                else
                {
                    Display_TFT_Clear();
                }
                GUI_General_Digit_Display(BT_Signal_Strength, Num_15, 3, 2, ODODigitNum09PosX1, 139);

                if(WIFI_Signal_Symbol)
                {
                    General_Number_Disp(fuhao, 190, 160);          //符号
                }
                else
                {
                    Display_TFT_Clear();
                }
                GUI_General_Digit_Display(WIFI_Signal_Strength, Num_15, 3, 2, ODODigitNum09PosX2, 164);
            }
            break;                   
            
        case 4:         //AP模式
            if(Auto_ONOFF == 0)
            {
                LINE_OUT_NEG_01 = 1;
                LINE_OUT_NEG_04 = 1;
                LINE_OUT_NEG_02 = 1;
                LINE_OUT_NEG_03 = 1;
            }

            RightturnSignal = 0;
            LeftturnSignal = 1;
            Bluetoothlight = 0;
            Positionlight = 1;
            highbeam = 0;
            Lowbeam = 0; 
            GUI_Display_Version_Code_Service(15, 120, "OTA ", Letter_Num_11, PCodeText_Space_size);
            GUI_Display_Version_Code_Service(15, 150, "Name    is ", Letter_Num_11, PCodeText_Space_size);
            blnamedisplay[0] = name2 / 16;
            blnamedisplay[1] = name2 % 16;
            blnamedisplay[2] = name1 / 16;
            blnamedisplay[3] = name1 % 16;

            for(int i = 0; i<4; i++)
            {
                if(blnamedisplay[i] >= 10)
                {
                    blnamedisplay[i] += 55;
                }
            }

            blenamedisplay[0] = blnamedisplay[0];
            blenamedisplay[1] = blnamedisplay[1];
            blenamedisplay[2] = blnamedisplay[2];
            blenamedisplay[3] = blnamedisplay[3];
            blenamedisplay[4] = 0xFF;
            
            if(blnamedisplay[0]+blnamedisplay[1]+blnamedisplay[2]+blnamedisplay[3] != 0)
            {
                General_Number_Disp(blenamedisplay, 90, 154); 
            }
            break;
        
        case 5:         //P2P模式
            if(Auto_ONOFF == 0)
            {
                LINE_OUT_NEG_01 = 1;
                LINE_OUT_NEG_04 = 1;
                LINE_OUT_NEG_02 = 1;
                LINE_OUT_NEG_03 = 1;
            }

            RightturnSignal = 1;
            LeftturnSignal = 0;
            Bluetoothlight = 0;
            Positionlight = 1;
            highbeam = 0;
            Lowbeam = 0; 
            GUI_Display_Version_Code_Service(15, 120, "OTA ", Letter_Num_11, PCodeText_Space_size);
            GUI_Display_Version_Code_Service(15, 150, "Name    is ", Letter_Num_11, PCodeText_Space_size);
            blnamedisplay[0] = name2 / 16;
            blnamedisplay[1] = name2 % 16;
            blnamedisplay[2] = name1 / 16;
            blnamedisplay[3] = name1 % 16;

            for(int i = 0; i<4; i++)
            {
                if(blnamedisplay[i] >= 10)
                {
                    blnamedisplay[i] += 55;
                }
            }

            blenamedisplay[0] = blnamedisplay[0];
            blenamedisplay[1] = blnamedisplay[1];
            blenamedisplay[2] = blnamedisplay[2];
            blenamedisplay[3] = blnamedisplay[3];
            blenamedisplay[4] = 0xFF;
            
            if(blnamedisplay[0]+blnamedisplay[1]+blnamedisplay[2]+blnamedisplay[3] != 0)
            {
                General_Number_Disp(blenamedisplay, 90, 154); 
            }
            break;

        default:            
            // POWER_CTRL_KL30   = 0u;
            POWER_CTRL_KL15   = 0u;
            CAN_Msg_Tx_Enable = 1u;
            Display_Menu_Type = 0;
            Diag_Info_Init( );       
            Display_Init( );  
            Display_Version_Info(0);             
            Send_Init();
            Send_ODO_Init();
            // Buzzer_Init(); 
              
            break;
    }
}

void Read_IPK_Session_Type(void)//请求仪表发送 软硬件版本号 等数据。
{
    uint8_t         DIAG_BUFER [ 10 ];

    switch ( loc_Step )
    {
        case 0:
            loc_Step = 5;    
            break;

        case 1:
            if ( Global_Session != 3 )
            {
                DIAG_BUFER [ 0 ] = 0x10;
                DIAG_BUFER [ 1 ] = 0x03;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);//让仪表进入 扩展会话 中。
            }
            break;
        case 2:
            //*******************************
            //UnLock_Flag == 0x00   //初始    阶段
            //UnLock_Flag == 0x01   //请求种子阶段
            //UnLock_Flag == 0x02   //得到种子阶段
            //UnLock_Flag == 0x03   //发送密钥阶段
            //UnLock_Flag == 0x55   //解锁成功阶段
            //*******************************
            if ( UnLock_Flag == 0 )
            {
                UnLock_Retry     = 10;
                UnLock_Flag      = 1;//请求种子阶段
                DIAG_BUFER [ 0 ] = 0x27;
                DIAG_BUFER [ 1 ] = 0x03;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);    //
            }
            if ( UnLock_Flag == 0x55 )//解锁成功阶段
            {
                loc_Step = 3;
            }
            break;
        case 3:
            if ( UnLock_Flag == 2 )//得到种子阶段
            {
                UnLock_Flag      = 3;//发送密钥阶段
                UnLock_Retry     = 10;
                DIAG_BUFER [ 0 ] = 0x27;
                DIAG_BUFER [ 1 ] = 0x04;
                DIAG_BUFER [ 2 ] = KEYBUFFER [ 0 ];
                DIAG_BUFER [ 3 ] = KEYBUFFER [ 1 ];
                DIAG_BUFER [ 4 ] = KEYBUFFER [ 2 ];
                DIAG_BUFER [ 5 ] = KEYBUFFER [ 3 ];
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 4);    //
            }
            if ( UnLock_Flag == 0x55 )//解锁成功阶段
            {
                loc_Step = 4;
            }
            break;
        case 4:

            if ( InterNal_Version [ 0 ] == 0xff )
            {

                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = 0xf0;
                DIAG_BUFER [ 2 ] = 0x86;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //（工厂版本号）
            }
            loc_Step = 5;
            break;

        case 5:
            if ( SW_Version [ 0 ] == 0xff )
            {

                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(SV_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(SV_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 软件版本号
                //Execute_Specify_Step(2);
            }
            loc_Step = 6;
            break;

        case 6:
            if ( HW_Version [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(HV_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(HV_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 硬件版本号
            }
            loc_Step = 7;
            break;

        case 7:
            if ( Part_Version [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(PV_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(PV_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 零件号
            }
            loc_Step = 9;
            break;

        case 8:
            if ( Made_Date [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(DATE_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(DATE_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 生产日期
            }
            loc_Step = 9;
            break;
        case 9:
            if ( SUPPLIER [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(CODE_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(CODE_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 制造商代码
            }
            loc_Step = 10;
            break;
        case 10:
            //if ( Voltage_UDS [ 0 ] == 0xff )
            //{
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = 0x20;
                DIAG_BUFER [ 2 ] = 0x14;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    
            //}
            loc_Step = 11;
            break;
        case 11:
            //if ( Voltage_UDS [ 0 ] == 0xff )
            //{
                DIAG_BUFER [ 0 ] = 0x19;
                DIAG_BUFER [ 1 ] = 0x0A;
                
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);    
            //}
            loc_Step = 0;
            break;
        
        

        /*case 8:  //22 5008  读取燃油阻值
            if (RES_CHECK_BEGIN == 0) //发燃油信号时置0，下一步的时候若是0则请求读取燃油阻值，回复62，则置2，为2则刷图
            {
                if ( Res_St [ 0 ] == 0xff )
                {
                    RES_CHECK_BEGIN  = 1U;
                    DIAG_BUFER [ 0 ] = 0x22;
                    DIAG_BUFER [ 1 ] = Get_DID_High_Byte(FUEL_DID);
                    DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(FUEL_DID);
                    UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 燃油阻值
                }
            }*/
            
            
            #if 0
                DIAG_BUFER [ 0 ] = 0x2e;
                DIAG_BUFER [ 1 ] = 0x20;
                DIAG_BUFER [ 2 ] = 0x00;//07 07 F7 0B 

                DIAG_BUFER [ 3 ] = 0x4b;
                DIAG_BUFER [ 4 ] = 0x47;
                DIAG_BUFER [ 5 ] = 0xf7;
                DIAG_BUFER [ 6 ] = 0x6b;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 7);    //
            #endif
            
            //loc_Step = 0;
            break;


    }

    if ( (UnLock_Flag == 1) || (UnLock_Flag == 3) )
    {
        if ( UnLock_Retry != 0 )
        {
            UnLock_Retry--;
        }
        else
        {
            if ( UnLock_Flag == 1 )
            {
                UnLock_Flag = 0u;
            }
            else if ( UnLock_Flag == 3 )
            {
                UnLock_Flag = 2;
            }
        }
    }
}
uint8_t delaytime = 0;
uint8_t delayresult = 0;
uint8_t TimeDelay_3s(void)
{
    if(MENU_CHECK_STEP == 6)        //光感步骤
    {
        if(delaytime <= 30)
        {
            delaytime++;
        }
        else
        {
            delayresult = 1;
        }
    }
    else
    {
        delaytime = 0;
        delayresult = 0;
    }
    
    return delayresult;
}
uint8_t LED_Waring = 0;
uint8_t ABStime = 0;
void Function_Check_Ctrl(uint32_t cmd)
{
    uint8_t         p [ 20 ];
    uint8_t         DIAG_BUFER [ 40 ];
    uint32_t        i;
    uint32_t        j;
    static uint32_t status    = 0;
    static uint32_t MENU_BACK = 0xff;
    static uint8_t  UDS_EunD = 0;

    if ( cmd == 0 )
    {
        ;
    }
    else if(cmd == 1)
    {
        //Voltage_UDS__Display();
        //FUEL_UDS__Display(1);

        if ( MENU_CHECK_STEP >= Get_Total_CheckStep( ) )
        {
            // MENU_CHECK_STEP = 0;
        }

        if ( MENU_BACK != MENU_CHECK_STEP )
        {
            MENU_BACK = MENU_CHECK_STEP;
            
            Execute_Specify_Step(MENU_CHECK_STEP);//Excel表格发报文。

            if (0)//数字需要3位还是2位
            {
                p [ 0 ] = (MENU_CHECK_STEP + 1) / 100;    // 65535
                p [ 1 ] = ((MENU_CHECK_STEP + 1) % 100) / 10;
                p [ 2 ] = (MENU_CHECK_STEP + 1) % 10;    // 65535
                p [ 3 ] = 0xff;
            }
            else
            {
                p [ 0 ] = ((MENU_CHECK_STEP ) % 100) / 10;
                p [ 1 ] = (MENU_CHECK_STEP ) % 10;    // 65535
                p [ 2 ] = 0xff;
            }
            General_Number_Disp(p, 190, 10);//刷图：报文检测步数
        }
        if(bt_control.BlueTooth == 0)
        {
            Display_Send_Vspead(MENU_CHECK_STEP);
        }
        switch (MENU_CHECK_STEP)
        {
            case 0:       
            {
                // if(Auto_ONOFF == 0)
                // {
                //     LINE_OUT_NEG_01 = 1;
                //     LINE_OUT_NEG_04 = 1;
                //     LINE_OUT_NEG_02 = 1;
                //     LINE_OUT_NEG_03 = 1;
                // }

                if(Version_number_begin == 1)
                {
                    RightturnSignal = 0;
                    LeftturnSignal = 0;
                    Bluetoothlight = 0;
                    Positionlight = 0;
                    highbeam = 0;
                    Lowbeam = 0; 
                }
                
                break;
            }
            
            case 1:
            {     
                if(Auto_ONOFF == 0)
                {
                    LINE_OUT_NEG_01 = 0;
                    LINE_OUT_NEG_04 = 0;
                    LINE_OUT_NEG_02 = 0;
                    LINE_OUT_NEG_03 = 1;
                }

                RightturnSignal = 0;
                LeftturnSignal = 0;
                Bluetoothlight = 0;
                Positionlight = 0;
                highbeam = 0;
                Lowbeam = 0; 
                break;
            } 
            case 2:
            {
                if(Auto_ONOFF == 0)
                {
                    LINE_OUT_NEG_01 = 1;
                    LINE_OUT_NEG_04 = 1;
                    LINE_OUT_NEG_02 = 1;
                    LINE_OUT_NEG_03 = 1;
                }
                
                RightturnSignal = 1;
                LeftturnSignal = 0;
                Bluetoothlight = 1;
                Positionlight = 0;
                highbeam = 1;
                Lowbeam = 0; 
                break;
            }             
            case 3:
            {       
                RightturnSignal = 0;
                LeftturnSignal = 1;
                Bluetoothlight = 1;
                Positionlight = 0;
                highbeam = 1;
                Lowbeam = 0; 
                break;
            }            
            case 4:
            {       
                RightturnSignal = 0;
                LeftturnSignal = 1;
                Bluetoothlight = 1;
                Positionlight = 0;
                highbeam = 1;
                Lowbeam = 0;                      
                break;
            }
            
            case 5:
            {      
                if(Auto_ONOFF == 0)
                {
                    LINE_OUT_NEG_01 = 1;
                    LINE_OUT_NEG_04 = 1;
                    LINE_OUT_NEG_02 = 1;
                    LINE_OUT_NEG_03 = 1;
                }

                RightturnSignal = 0;
                LeftturnSignal = 0;
                Bluetoothlight = 0;
                Positionlight = 1;
                highbeam = 0;
                Lowbeam = 1; 
                break;
            }
            case 6:
            {       
                //密码认证
                if(KeyStep == 3)
                {
                    if(Auto_ONOFF == 0)
                    {
                        LINE_OUT_NEG_01 = 1;
                        LINE_OUT_NEG_04 = 1;
                        LINE_OUT_NEG_02 = 1;
                        LINE_OUT_NEG_03 = 1;
                    }
                }
                else
                {
                    if(TimeDelay_3s() == 0)
                    {
                        if(Auto_ONOFF == 0)
                        {
                            LINE_OUT_NEG_01 = 0;
                            LINE_OUT_NEG_04 = 0;
                            LINE_OUT_NEG_02 = 1;
                            LINE_OUT_NEG_03 = 1;
                        }
                    }
                    else
                    {
                        if(Auto_ONOFF == 0)
                        {
                            LINE_OUT_NEG_01 = 0;
                            LINE_OUT_NEG_04 = 1;
                            LINE_OUT_NEG_02 = 1;
                            LINE_OUT_NEG_03 = 1;
                        }
                    }
                }

                RightturnSignal = 0;
                LeftturnSignal = 0;
                Bluetoothlight = 0;
                Positionlight = 1;
                highbeam = 0;
                Lowbeam = 0;      
                break;
            } 
            case 7:
            {
                if(Auto_ONOFF == 0)
                {
                    LINE_OUT_NEG_01 = 0;
                    LINE_OUT_NEG_04 = 0;
                    LINE_OUT_NEG_02 = 1;
                    LINE_OUT_NEG_03 = 1;
                }

                RightturnSignal = 0;
                LeftturnSignal = 0;
                Bluetoothlight = 1;
                Positionlight = 0;
                highbeam = 0;
                Lowbeam = 0;             
                break;
            }
            case 8:
            {  
                if(Auto_ONOFF == 0)
                {
                    LINE_OUT_NEG_01 = 1;
                    LINE_OUT_NEG_04 = 1;
                    LINE_OUT_NEG_02 = 1;
                    LINE_OUT_NEG_03 = 1;
                }

                Lowbeam = 0; 
                LeftturnSignal = 0;
                highbeam = 0;
                RightturnSignal = 0;
                Bluetoothlight = 0;  
                break;
            }
            // case 9:
            // {          
            //     //    LED3 = 1;
            //     // LED4 = 1;
            //     // LINE_OUT_NEG_05 = 0;
            //     // LINE_jinguang = 1;
            //     // POWER_CTRL_KL30_Current = 1;   
            //     // POWER_CTRL_KL30 = 1;              
            //     Lowbeam = 0; 
            //     LeftturnSignal = 0;
            //     highbeam = 0;
            //     RightturnSignal = 0;
            //     Bluetoothlight = 0;            
            //     break;
            // } 
            // case 10:
            // {
            //     // if(Auto_ONOFF == 0)
            //     // {
            //     //     LINE_OUT_NEG_01 = 1;
            //     //     LINE_OUT_NEG_04 = 1; 
            //     // }
            //     // LINE_OUT_NEG_02 = 1;
            //     // LINE_OUT_NEG_03 = 1;
                
            //     // LED3 = 1;
            //     // LED4 = 1;
            //     // LINE_OUT_NEG_05 = 0;
            //     // LINE_jinguang = 1;
            //     // POWER_CTRL_KL30 = 1;   
            //     // POWER_CTRL_KL30_Current = 1; 
            //     Lowbeam = 0; 
            //     LeftturnSignal = 0;
            //     highbeam = 0;
            //     RightturnSignal = 0;
            //     Bluetoothlight = 0;      
            //     break;
            // }         
            // case 11:        //下电蓝牙外屏
            // {
            //     // POWER_CTRL_KL30_Current = 1;  
            //     // if(Auto_ONOFF == 0)
            //     // {
            //     //     LINE_OUT_NEG_01 = 0;
            //     // }
            //     // LINE_OUT_NEG_02 = 0;
            //     // LINE_OUT_NEG_03 = 1;
            //     // LINE_OUT_NEG_04 = 0; 

            //     Lowbeam = 0; 
            //     LeftturnSignal = 0;
            //     highbeam = 0;
            //     RightturnSignal = 0;
            //     Bluetoothlight = 0;       
            //     break;
            // } 
            // case 12:
            // { 
            //     Lowbeam = 0; 
            //     LeftturnSignal = 0;
            //     highbeam = 0;
            //     RightturnSignal = 0;
            //     Bluetoothlight = 0;      
            //     break;
            // } 
            case 9:
            {
                if(Auto_ONOFF == 0)
                {
                    LINE_OUT_NEG_01 = 1;
                    LINE_OUT_NEG_04 = 1;
                    LINE_OUT_NEG_02 = 1;
                    LINE_OUT_NEG_03 = 1;
                }

                RightturnSignal = 1;
                LeftturnSignal = 1;
                Bluetoothlight = 1;
                Positionlight = 1;
                highbeam = 1;
                Lowbeam = 1; 
                break;
            } 
            // case 14:
            // {
            //     Lowbeam = 0; 
            //     LeftturnSignal = 0;
            //     highbeam = 0;
            //     RightturnSignal = 0;
            //     Bluetoothlight = 1;      
            //     break;
            // } 
            // case 15:        //全上电连蓝牙
            // {
            //     LINE_OUT_NEG_05 = 0;
            //     if(Auto_ONOFF == 0)
            //     {
            //         LINE_OUT_NEG_01 = 1;
            //         LINE_OUT_NEG_04 = 1; 
            //     }
            //     LINE_OUT_NEG_02 = 1;
            //     LINE_OUT_NEG_03 = 1;
            //     break;
            // } 
            default:
            break;
        }
    }
}


void Get_ODO_Call(void)
{
}
