#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 "app_lsdn.h"

uint8_t Key_Mode = 0;
//ABC输出
void Key_Operation_SW5(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        // RotationAlarmFlag = 0;
        // if(ABC_OUT == 1)
        // {
        //     RotationAlarmMode = 0;
        //     ABC_OUT = 0;
        // }
        // else
        // {
        //     RotationAlarmMode = 1;
        //     ABC_OUT = 1;
        // }
    }
}

//上一步(右侧第四个)（原边撑上下电与检验Flash屏幕自扫）
void Key_Operation_SW4(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        MENU_CHECK_STEP_SUB();
        // BCDD_MCU_Flag = 1;
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        Key_Mode = 4;       //检验Flash屏幕自扫
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {

    }
    else
    {
    }
}

void Key_Operation_SW7(Key_Event_en_t enKeyEvent)       //上下ACC电
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(RELAY_KEY_MCU == 1)
        {
            RELAY_KEY_MCU = 0;
        }
        else
        {
            RELAY_KEY_MCU = 1;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        // Key_Mode = 3;
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {
    }
    else
    {
    }
}
//一键启动控制
void Key_Operation_SW8(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )      //长按一键启动
    {
        // ONEKEYFlag = 1;
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )      //长按2秒
    {
        // Display_TFT_Clear();
        // ONEKEYFlag = 2;     //双击一键启动
        // ZTSVOK_A = 0;
        // ZTSVOK_B = 0;
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )        //长按5秒
    {

    }
    else
    {

    }
}

void Key_Operation_SW2(Key_Event_en_t enKeyEvent)
{
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(Scroll_Wheel < 100)
        {
            Scroll_Wheel++;
        }
        else
        {
            Scroll_Wheel = 0;
        } 
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        // Key_Mode = 1;    //左键
        backflag = 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 )
    {
        MENU_CHECK_STEP_ADD();
        // if(BCMLED.Sig.AutoLamp)
        // {
        //     BCMLED.Sig.AutoLamp = 0x00;
        // }
        // else
        // {
        //     BCMLED.Sig.AutoLamp = 0x01;     //自动大灯开启
        // }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        // backflag = 1;
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {

    }
    else
    {
    }
}
//下一步
void Key_Operation_SW1(Key_Event_en_t enKeyEvent)
{    
    if ( enKeyEvent == KEY_EVENT_SHORT_PRESS )
    {
        if(Scroll_Wheel > 0)
        {
            Scroll_Wheel--;
        }
        else
        {
            Scroll_Wheel = 100;
        }
    }
    else if ( enKeyEvent == KEY_EVENT_LONG_PRESS )
    {
        Key_Mode = 2;    //右键
    }
    else if ( enKeyEvent == KEY_EVENT_SUPER_LONG_PRESS )
    {

    }
    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(connectbleFlag == 0)
        {
            connectbleFlag = 1;
            name1= (devicename) & 0xFF;
            name2 = (devicename >> 8) & 0xFF;
        }
        else
        {
            Display_TFT_Clear();
            name1= (devicename) & 0xFF;
            name2 = (devicename >> 8) & 0xFF;
            startONfalg = 0;
            send0x42time = 0;
            bleOFFtoONtime = 0;
            startONfalg = 0;
            startIGONtime = 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+

};
