#include "RTE_GPIO.h"
#include "Key.h"
#include "Display_Info.h"
#include "Check_Ctrl.h"
#include "Barcode_Scanner.h"
#include "Task.h"
#include "string.h"
#include "TFT_LCD.h"
#include "CAN_APP.h"

extern const unsigned char gImage_titlex0_y0[];

uint8_t Check_Steps = 0;
uint8_t Check_Control_Mode = 2;// 1 自走模式 2 打点模式 0  默认都发0
uint16_t Check_Control_Time = 0;
uint8_t Vspeed = 0;
uint8_t Signal_4G = 0;
uint8_t Signal_GPS = 0;
uint16_t Eletricity = 0;
uint8_t Hour = 0;
uint8_t Tcs = 0;
uint8_t Energy_recovery = 0;
uint8_t motor_hot = 0;
uint8_t Gear = 0;
uint8_t Ready = 0;
uint8_t Cruiste = 0;
uint8_t Fault = 0;
uint8_t Motor_hert = 0;
uint8_t Battery_temp = 0;
uint8_t NFC_C_card = 0;
uint8_t Side_bracket=0;
uint8_t Hill_Descent_control= 0;
uint8_t bluetooth_phone = 0;
uint8_t Student_Card = 0;
uint8_t Student_Card_can_command=0;
uint8_t Query_Card = 0;
uint8_t Query_Card_can_command = 0;

uint8_t Diagnosis_banbenhao_flag = 0;
uint8_t Diagnosis_banbenhao_flag_ = 0;
uint8_t key_flag = 0;


u8_All_Flag_t All_Flag = {0};


// All_Flag.KEY.Key1 = 0;




uint8_t Flag_SW6_Dis[] = {0x00,0xff};
uint8_t UIDMondel;
void Key_Operation_SW1(Key_Event_en_t enKeyEvent)
{
        /*清零ODO*/
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(POWER_CTRL_KL15 == 1)
        {
            POWER_CTRL_KL15 = 0;
        }
        else
        {
            POWER_CTRL_KL15 = 1;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
uint8_t Study_Flag = 0;
void Key_Operation_SW2(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
       // memset(can_car_Data, 0, sizeof(can_car_Data));
        memset(&All_Flag, 0, sizeof(All_Flag));
        All_Flag.u8flag[2] = 1;
        Student_Card_can_command = 1;
        Display_TFT_Clear();
        Study_Flag = 1;
        UIDMondel = 0;
        
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
void Key_Operation_SW3(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        Display_SUCCESS_Clear();
        if(UIDMondel == 0)
        {
            memset(&All_Flag, 0, sizeof(All_Flag));  
            All_Flag.u8flag[2] = 1;
            Display_TFT_Clear();
            UIDMondel = 1;
        }
        else
        {
            UIDStep = 0;
            uidWrite = 1;
            UIDGetstep = 0;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
uint8_t change = 17;
uint8_t writeflag = 0;
void Key_Operation_SW4(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(UIDMondel)
        {
            Display_TFT_Clear();
            if(UIDNumber2[change] < 9)
            {
                UIDNumber2[change]++;
            }
            else
            {
                UIDNumber2[change] = 0;
            }
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        Display_TFT_Clear();
        writeflag = 1;
        uidwriteStep = 0;
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
        
    }
    else
    {
    }
}
void Key_Operation_SW5(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(UIDMondel)
        {
            Display_TFT_Clear();
            if(change > 0)
            {
                change--;
                UUIDX -= 10;
            }
            else
            {
                change = 17;
                UUIDX = 170;
            }
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else
    {
    }
}
void Key_Operation_SW6(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        memset(&All_Flag, 0, sizeof(All_Flag));  
        All_Flag.u8flag[6] = 1;

        Check_Steps = 0;
        Study_Flag = 0;
        UIDMondel = 0;
        
        Display_TFT_Clear();

        if(Check_Control_Mode == 1)
        {
            Check_Control_Mode = 2;
        }
        else if (Check_Control_Mode == 2)
        {
            Check_Control_Mode = 1;
        }
        else
        {
            Check_Control_Mode = 2;
        }
        //General_Number_Disp(Flag_SW6_Dis, 200, 20);
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else
    {
    }
}
uint8_t Require_card_flag = 0;
void Key_Operation_SW7(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        memset(&All_Flag, 0, sizeof(All_Flag));  
        All_Flag.u8flag[7] = 1;
        Check_Steps = 0;
        Study_Flag = 0;
        Query_Card_can_command = 1;
        Require_card_flag = 1;
        Display_TFT_Clear();
        UIDMondel = 0;
        //Display_Chaka_Model();//查卡模式：
        
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}

void Key_Operation_SW8(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        memset(&All_Flag, 0, sizeof(All_Flag));  
        All_Flag.u8flag[8] = 1;

        memset(canData1, 0, sizeof(canData1));/*清除上次CAN诊断接收的数据*/
        memset(canData4, 0, sizeof(canData4));
        memset(canData2, 0, sizeof(canData2));
        memset(canData3, 0, sizeof(canData3));
        Study_Flag = 0;
        UIDMondel = 0;
        Diagnosis_banbenhao_flag=1;/*CAN诊断起始信号*/

        Display_TFT_Clear();
        TFT_LCD_Draw_Bmp(5, 50,   ( uint8_t * )gImage_Dyy_words_1X6_Y16 );      //软件版本号
        TFT_LCD_Draw_Bmp(5, 100,   ( uint8_t * )gImage_Dyy_words_2X6_Y46 );    //硬件版本号 
        TFT_LCD_Draw_Bmp(5, 200,   ( uint8_t * )gImage_Dyy_words_5X6_Y137 );    //静态电流
        TFT_LCD_Draw_Bmp(0, 220, ( uint8_t * )gImage_titlex0_y0 ); 
        General_Number_Disp(Detecting_Set_Version, 90, 230);/*显示检测台版本号*/
        //Display_Can_Receive_Data(5,50);


    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}

void Key_Operation_SW9(Key_Event_en_t enKeyEvent)
{
        /*清零ODO*/
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        UIDMondel = 0;
        
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}

void Key_Operation_SW10(Key_Event_en_t enKeyEvent)
{
        /*清零ODO*/
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(Check_Control_Mode == 2)
        { 
            //All_Flag.u8flag[10] == 1;
            if((( All_Flag.u8flag[8] == 0) && ( All_Flag.u8flag[2] == 0)&& ( All_Flag.u8flag[7] == 0) && (( All_Flag.u8flag[6] == 1))))
            {
                Check_Steps++;
                Display_flag(Check_Steps);
                if(Check_Steps > Check_Couont)
                {
                    Check_Steps = 0;
                }
            }
           
        }
        UIDMondel = 0;
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
/*-------------------------------------------------*/
Key_Real_Status_en_t Key_Real_sw1(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW1 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw2(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;
    if ( SW2 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw3(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW3 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw4(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW4 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw5(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;
    if ( SW5 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    else
    {
        enKeyRealStatus = KEY_LINE_LOOSEN;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw6(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;
    if ( SW6 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    else
    {
        enKeyRealStatus = KEY_LINE_LOOSEN;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw7(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW7 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    else
    {
        enKeyRealStatus = KEY_LINE_LOOSEN;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw8(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW8 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw9(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW9 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}
Key_Real_Status_en_t Key_Real_sw10(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW10 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    return enKeyRealStatus;
}




void User_CheckMode_Service(uint8_t CbkCycle)
{
    if(Check_Control_Mode == 1)
    {
        Check_Control_Time += CbkCycle;

        if(Check_Control_Time > Check_KeepTime)
        {
            Check_Control_Time = 0;
            Check_Steps++;

            if(Check_Control_Mode == 1)
            {
                Display_flag(Check_Steps);
            }
        }
        if(Check_Steps > Check_Couont)
        {
            Check_Steps = 0;
        }
    }



    switch(Check_Steps)
    {   
        case 0:
        Vspeed = 0;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 0;
        Hour = 0;
        Tcs = 0;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        NFC_C_card = 0;
        Side_bracket = 0;
        
        break;

        case 1:
        Vspeed = 10;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 0;
        Hour = 2;
        Tcs = 0;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 1;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 1;
        Right_Led = 1;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        NFC_C_card = 1;
        Side_bracket = 0;
        Hill_Descent_control = 1;
        break;
        case 2:
        Vspeed = 19;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 150;
        Hour = 4;
        Tcs = 1;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 1;
        LINE_OUT_NEG_05 = 1;
        NFC_C_card=2;
        Side_bracket = 0;
        Hill_Descent_control = 0;
        break;
        case 3:
        Vspeed = 29;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 350;
        Hour = 6;
        Tcs = 2;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 2;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 1;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        Side_bracket = 0;
        NFC_C_card=0;
        Charging_connection_led = 0;
        break;
        case 4:
        Vspeed = 38;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 550;
        Hour = 8;
        Tcs = 0;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 0;
        Side_bracket = 0;
        Charging_connection_led=1;
        break;
        case 5:
        Vspeed = 47;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 750;
        Hour = 10;
        Tcs = 0;
        Energy_recovery = 2;
        motor_hot = 0;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        Side_bracket=1;
        Charging_connection_led = 0;
        break;
        case 6:
        Vspeed = 57;
        Signal_4G = 0;
        Signal_GPS = 0;
         Eletricity = 1000;
        Hour = 12;
        Tcs = 0;
        Energy_recovery = 1;
        motor_hot = 0;
        Gear = 0;
        Ready = 1;
        Cruiste = 1;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        Side_bracket=0;
        break;
        case 7:
        Vspeed = 75;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 750;
        Hour = 14;
        Tcs = 0;
        Energy_recovery = 3;
        motor_hot = 0;
        Gear = 0;
        Ready = 3;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        Side_bracket = 0;
        break;
        case 8:
        Vspeed = 88;
        Signal_4G = 0;
        Signal_GPS = 0;
        Eletricity = 550;
        Hour = 16;
        Tcs = 0;
        Energy_recovery = 0;
        motor_hot = 1;
        Gear = 0;
        Ready = 2;
        Cruiste = 0;
        Fault = 1;
        Motor_hert = 1;
        Battery_temp = 0;        
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        Side_bracket = 0;
        break;
        case 9:
        Vspeed = 88;
        Signal_4G = 0;
        Signal_GPS = 65;
         Eletricity = 350;
        Hour = 18;
        Tcs = 0;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        LINE_OUT_NEG_05 = 1;
        Side_bracket = 0;
        break;
        case 10:
        Vspeed = 88;
        Signal_4G = 65;
        Signal_GPS = 0;
        Eletricity = 150;
        Hour = 20;
        Tcs = 0;
        Energy_recovery = 0;
        motor_hot = 0;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        bluetooth_phone = 1;
        LINE_OUT_NEG_05 = 1;
        Side_bracket = 0;
        break;
        case 11:
        Vspeed = 88;
        Signal_4G = 65;
        Signal_GPS = 65;
        Eletricity = 1000;
        Hour = 0;
        Tcs = 3;
        Energy_recovery = 1;
        motor_hot = 1;
        Gear = 1;
        Ready = 3;
        Cruiste = 1;
        Fault = 1;
        Motor_hert = 1;
        Battery_temp = 1;
        Left_Led = 1;
        Right_Led = 1;
        Headlights_Led = 1;
        LINE_OUT_NEG_05 = 0;
        Side_bracket = 1;
        Hill_Descent_control = 1;
        bluetooth_phone = 0 ;
        break;
    }
}

void User_CanSend(uint8_t steps)
{
 
}



/*---------------------------------------------------------------------------------------*/
const Key_Attribute_st_t stKeyAttribute [ KEY_NUM_MAX ] = {



    {KEY_TYPE_LINE, Key_Real_sw1, Key_Operation_SW1},    //SW1+
    {KEY_TYPE_LINE, Key_Real_sw2, Key_Operation_SW2},      //SW2-
    {KEY_TYPE_LINE, Key_Real_sw3, Key_Operation_SW3},    //SW1+
    {KEY_TYPE_LINE, Key_Real_sw4, Key_Operation_SW4},    //SW4
    {KEY_TYPE_LINE, Key_Real_sw5, Key_Operation_SW5},    //SW5
    {KEY_TYPE_LINE, Key_Real_sw6, Key_Operation_SW6},    //SW5    
    {KEY_TYPE_LINE, Key_Real_sw7, Key_Operation_SW7},
    {KEY_TYPE_LINE, Key_Real_sw8, Key_Operation_SW8},
    {KEY_TYPE_LINE, Key_Real_sw9, Key_Operation_SW9},
    {KEY_TYPE_LINE, Key_Real_sw10, Key_Operation_SW10},


};
