#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 Confirmkeycount;
void Key_Operation_SW5(Key_Event_en_t enKeyEvent)       //下一步
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(firstpowerflag == 2)
        {
            if(saomafinish == 3)
            { 
                zhenduansendStep = 0;
                zhenduanflag = Data_Mode_Dot;
                memset(erweima, 0, 64);
                memset(barcode_Msg, 0, 64);
                MENU_CHECK_STEP_ADD( ); 
            }
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        
        if(firstpowerflag == 2)
        {
            if(AutoFlag == 1)
            {
                AutoFlag = 0;
            }
            else
            {
                AutoFlag = 1;
            }
        }
    }
    else
    {
    }
}
void Key_Operation_SW4(Key_Event_en_t enKeyEvent)       //上一步
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(firstpowerflag == 2)
        {
            MENU_CHECK_STEP_SUB();
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
        if ( (LED_Waring == 1) || (UDS_Warning_Voltage == 1))
        {
            ;
        }
        else
        {
            Display_Sub( );
            Display_TFT_Clear( );  
        }  
             
        
    }
    else
    {
    }
}
void Key_Operation_SW7(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(firstpowerflag == 2)
        {
            if(zhenduanflag == Data_Mode_Write)
            {
                if(saomafinish == 1)
                {
                    zhenduansendStep = 5;
                }
            }
            else if(((zhenduansendStep == 4) && (zhenduanflag == Data_Mode_Read)))
            {
                zhenduansendStep = 5;
            }
        }
    }
    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 )
    {
        zhenduanflag = Data_Mode_Write;
        zhenduansendStep = 0;
        saomafinish = 0;
    }
    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)       //下移位
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        downkeyflag = 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 )
    {
        if(LINE_OUT_NEG_04 == 1U)       //上下ACC电
        {
            LINE_OUT_NEG_04 = 0U;
            Auto_ONOFF = 3;
        }
        else
        {
            LINE_OUT_NEG_04 = 1U;
            Auto_ONOFF = 0;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        Auto_ONOFF = 1;     //自动断电
        LINE_OUT_NEG_04 = 0U;
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
void Key_Operation_SW3(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(firstpowerflag == 2)
        {
            if(clearOdoFlag == 0)
            {
                clearOdoFlag = 1;
            }
            else
            {
                queren = 1;
            }
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
        if(Get_Display_Type( ) == 2)
        {
            MENU_CHECK_STEP_SUB( ); 
        }
    }
    else
    {
    }
}
void Key_Operation_SW1(Key_Event_en_t enKeyEvent)           //上移位
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        upkeyflag = 1;
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {

    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {

    }
    else
    {

    }
}

void Key_Operation_SW6(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        Confirmkeyflag = 1;
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        if(menumondel == 0)
        {
            menumondel = 1;
            Display_TFT_Clear();
        }
        else
        {
            menumondel = 0;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {

    }
    else
    {

    }
}
uint8_t upkeytime;
uint8_t downkeytime;
uint8_t Confirmkeytime;
uint8_t upkeyflag;
uint8_t downkeyflag;
uint8_t Confirmkeyflag;
void upkeyserve(void)
{
    if(upkeyflag == 1)
    {
        if(upkeytime<=10)
        {
            upkeytime++;
            Press_to_up = 1;
        }
        else
        {
            upkeytime = 0;
            Press_to_up = 0;
            upkeyflag = 0;
        }
    }
    else
    {
        upkeytime = 0;
        Press_to_up = 0;
    }
}

void downkeyserve(void)
{
    if(downkeyflag == 1)
    {
        if(downkeytime<=10)
        {
            downkeytime++;
            Press_to_down = 1;
        }
        else
        {
            downkeytime = 0;
            Press_to_down = 0;
            downkeyflag = 0;
        }
    }
    else
    {
        downkeytime = 0;
        Press_to_down = 0;
    }
}

void confirmkeyserve(void)
{
    if(Confirmkeyflag == 1)
    {
        if(Confirmkeytime<=10)
        {
            Confirmkeytime++;
            queren = 1;
        }
        else
        {
            Confirmkeytime = 0;
            Confirmkeyflag = 0;
            queren = 0;
        }
    }
    else
    {
        Confirmkeytime = 0;
        queren = 0;
    }
}
uint8_t KeyStep = 0;
void PasswordUnlock(void)
{
    if(MENU_CHECK_STEP == 6)
    {
        if(KeyStep == 0)
        {
            if(upkeyflag)
            {
                KeyStep = 1;
            }
        }
        else if(KeyStep == 1)
        {
            if(downkeyflag)
            {
                KeyStep = 2;
            }
        }
        else if(KeyStep == 2)
        {
            if(Confirmkeyflag)
            {
                KeyStep = 3;
                Display_TFT_Clear();
            }
        }
        else
        {
            ;
        }
    }
    else
    {
        KeyStep = 0;
    }
}
/*-------------------------------------------------*/
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_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_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_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_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_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_sw10(void)
{

    Key_Real_Status_en_t enKeyRealStatus = KEY_LINE_LOOSEN;

    if ( SW10 == 0U )
    {
        enKeyRealStatus = KEY_LINE_PRESS;
    }
    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;
    }
    return enKeyRealStatus;
}

void Key_Operation_SW10(Key_Event_en_t enKeyEvent)
{
        /*清零ODO*/
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(BlueTooth == 0)
        {
            BlueTooth = 1;
            name1 = (devicename) & 0xFF;
            name2 = (devicename >> 8) & 0xFF;
        }
        else
        {
            BlueTooth = 0;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
        connectbleFlag = 0;
    }
    else
    {
    }
}
/*---------------------------------------------------------------------------------------*/
const Key_Attribute_st_t stKeyAttribute [ KEY_NUM_MAX ] = {
    {KEY_TYPE_LINE, Key_Real_sw5, Key_Operation_SW5},    //SW5
    {KEY_TYPE_LINE, Key_Real_sw4, Key_Operation_SW4},    //SW4
    {KEY_TYPE_LINE, Key_Real_sw7, Key_Operation_SW7},
    {KEY_TYPE_LINE, Key_Real_sw8, Key_Operation_SW8},
    {KEY_TYPE_LINE, Key_Real_sw2, Key_Operation_SW2},      //SW2-
    {KEY_TYPE_LINE, Key_Real_sw1, Key_Operation_SW1},    //SW1+
    {KEY_TYPE_LINE, Key_Real_sw10, Key_Operation_SW10},
    {KEY_TYPE_LINE, Key_Real_sw3, Key_Operation_SW3},    //SW1+
    {KEY_TYPE_LINE, Key_Real_sw6, Key_Operation_SW6},    //SW1+
    {KEY_TYPE_LINE, Key_Real_sw9, Key_Operation_SW9},    //SW1+
};
