#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"

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 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 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};

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
    {
    }
}
void Key_Operation_SW2(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        memset(&All_Flag, 0, sizeof(All_Flag));
        All_Flag.u8flag[2] = 1;

        Display_TFT_Clear();
        Display_Chaka_Model();//查卡模式
    }
    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 )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
void Key_Operation_SW4(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
        
    }
    else
    {
    }
}
void Key_Operation_SW5(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {

    }
    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;

        Display_TFT_Clear();
        //General_Number_Disp(Flag_SW6_Dis, 200, 20);

    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else
    {
    }
}
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;
        Display_TFT_Clear();
        //界面学卡：
        Display_interface_learning_card(20,80);
    }
    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));

        Diagnosis_banbenhao_flag=1;/*CAN诊断起始信号*/
        Display_TFT_Clear();
        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 )
    {
        if(Check_Control_Mode == 1)
        {
            Check_Control_Mode = 2;
        }
        else if (Check_Control_Mode == 2)
        {
            Check_Control_Mode = 1;
        }
        else
        {
            Check_Control_Mode = 2;
        }
        
    }
    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;
                }
            }
           
        }

    }
    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_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;
        Gear = 0;
        Ready = 0;
        Cruiste = 0;
        Fault = 0;
        Motor_hert = 0;
        Battery_temp = 0;
        Left_Led = 0;
        Right_Led = 0;
        Headlights_Led = 0;
        NFC_C_card = 0;
        
        break;

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


};
