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

#define R485_CLEAR_START 11    // 起始索引
#define R485_CLEAR_LEN   33    // 清零长度（元素个数）

uint8_t delayflag;
uint8_t CursorPosition = 2;
void Key_Operation_SW5(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(uuidmodel == 0)
        {
            if(firstpowerflag == 2)
            {
                MENU_CHECK_STEP_ADD( );   
            }
        }
        else
        {
            if(CursorPosition == 2)
            {
                if(uuidnumber2[change] < 0x5A)
                {
                    if(uuidnumber2[change] == 0x39)
                    {
                        uuidnumber2[change] = 0x41;
                        Display_TFT_Clear_UID3();
                    }
                    else
                    {
                        uuidnumber2[change]++;
                        if((uuidnumber2[change] > 0x39) || (uuidnumber2[change] == 0x30))
                        {
                            Display_TFT_Clear_UID3();
                        }
                    }
                }
                else
                {
                    uuidnumber2[change] = 0x30;
                }
            }
            else if(CursorPosition == 1)
            {
                if(uuidnumber1[change] < 0x5A)
                {
                    if(uuidnumber1[change] == 0x39)
                    {
                        uuidnumber1[change] = 0x41;
                    }
                    else
                    {
                        uuidnumber1[change]++;
                    }
                }
                else
                {
                    uuidnumber1[change] = 0x30;
                }
            }
        }  
    }
    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(uuidmodel == 0)
        {
            if(firstpowerflag == 2)
            {
                MENU_CHECK_STEP_SUB();
            }
        }
        else
        {
            Display_TFT_Clear_UID();
            Display_TFT_Clear_UID2();
            if(change > 0)
            {
                UUIDX -= 10;
                change--;
            }
            else
            {
                if(CursorPosition == 2)
                {
                    change = 11;        //第一行最后一位
                    UUIDX = 113;        //第一行坐标
                    CursorPosition = 1;         //配置到第一行
                }
                else if(CursorPosition == 1)
                {
                    change = 19;
                    UUIDX = 193;        //第一行坐标
                    CursorPosition = 2;
                }
            }
        }
    }
    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 != 2 && zhenduanflag != 3)
            {
                    zhenduanflag = 2;
            }
            else
            {
                    zhenduanflag = 0;
            }
        }
       
    }
    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 )
    {
        if(firstpowerflag == 2)
        {
             if(zhenduanflag  != 1)
            {
                zhenduanflag = 1;
            }
            else
            {
                zhenduanflag = 0;
                LINE_OUT_NEG_01 = 1;
                LINE_OUT_NEG_02 = 1;
                LINE_OUT_NEG_03 = 1;
                LINE_OUT_NEG_04 = 1;
                zhenduansendStep = 0;
                // memset(barcode_Msg,0,sizeof(barcode_Msg));

            }
            RS485_TX_finish = 0;
        }
        
    }
    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(firstpowerflag == 2)
        {
            if(LINE_OUT_NEG_01 == 1U)
            {
                LINE_OUT_NEG_01 = 0U;
                Auto_ONOFF = 3;
            }
            else
            {
                LINE_OUT_NEG_01 = 1U;
                Auto_ONOFF == 0;
            }
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        if(firstpowerflag == 2)
        {
            if(Auto_ONOFF != 0)
            {
                Auto_ONOFF = 0;
            }
            else
            {
                Auto_ONOFF = 1;
            }
        }
    }
    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(Get_Display_Type( ) == 2)
        // {
        //     MENU_CHECK_STEP_SUB( ); 
        // }
        if(firstpowerflag == 2)
        {
            if(clearOdoFlag == 0)
            {
                clearOdoFlag = 1;
                
            }
            RS485_TX_finish = 0;
            // else
            // {
            //     clearOdoFlag = 0;
            // }
            // queren = 0;
        }
    }
    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)
{
        /*清零ODO*/
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(uuidmodel == 1)
        {
            Display_TFT_Clear_UID4();
            UUID_Write();
            delayflag = 1;
        }
        uuidmodel = 1;
        Display_TFT_Clear();
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        uuidmodel = 0;
        Display_TFT_Clear();
        memset(&R485_0x80_msg[R485_CLEAR_START], 0, R485_CLEAR_LEN);
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
        if(Get_Display_Type( ) == 2)
        {
            MENU_CHECK_STEP_ADD( );
        }
    }
    else
    {
    }
}

/*-------------------------------------------------*/
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_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;
}

void Key_Operation_SW10(Key_Event_en_t enKeyEvent)
{
        /*清零ODO*/
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        
        if(firstpowerflag == 2)
        {
            Display_TFT_Clear();
            if(connectbleFlag == 0)
            {
                connectbleFlag = 1;
                name1= (devicename) & 0xFF;
                name2 = (devicename >> 8) & 0xFF;
            }
            else
            {
                name1= (devicename) & 0xFF;
                name2 = (devicename >> 8) & 0xFF;
                startONfalg = 0;
                send0x42time = 0;
                bleOFFtoONtime = 0;
                startONfalg = 0;
                startIGONtime = 0;
                recvflag111 = 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+

};
