
#include "key.h"
#include "scm_canmatrix-binutil.h"
#include "MotorCtrl.h"

static uint8_t GetSigState(KEY_ID_type key_id);

static uint8_t keystate[KEY_NUM];
static uint8_t keyPressFlag[KEY_NUM];
static uint8_t keyReleaseFlag[KEY_NUM];
static uint16_t keydelay[KEY_NUM];

#define KEY_DELAY_TIMES 4 //20Ms
void ClearAllKeyState(void)
{
    uint8_t i;
    for (i = 0; i < KEY_NUM; i++)
    {
        keystate[i] = 0;
    }
}
static void setKeyPressFlag(KEY_ID_type id)
{
    if (id < KEY_NUM)
    {
        keyPressFlag[id] = KEY_PRESSED;
    }
}
static void setKeyReleaseFlag(KEY_ID_type id)
{
    if (id < KEY_NUM)
    {
        keyReleaseFlag[id] = KEY_PRESSED;
    }
}
uint8_t getKeyPressFlag(KEY_ID_type id)
{
    uint8_t retVal = KEY_NOPRESSED;
    if (id < KEY_NUM)
    {
        retVal = keyPressFlag[id];
        keyPressFlag[id] = KEY_NOPRESSED;
    }
    return retVal;
}
uint8_t getKeyReleaseFlag(KEY_ID_type id)
{
    uint8_t retVal = KEY_NOPRESSED;
    if (id < KEY_NUM)
    {
        retVal = keyReleaseFlag[id];
        keyReleaseFlag[id] = KEY_NOPRESSED;
    }
    return retVal;
}

void KeyScanTask(void)//5ms
{
    uint8_t i,key;

    for (i = 0; i < KEY_NUM; i++)
    {
        key = GetSigState(i);
        if (key == KEY_PRESSED && keystate[i] == KEY_NOPRESSED)
        {
            keydelay[i]++;
            if (keydelay[i] >= KEY_DELAY_TIMES)
            {
                keystate[i] = KEY_PRESSED;
                setKeyPressFlag(i);
            }
        }
        else if(key == KEY_NOPRESSED)
        {
            if (keydelay[i] > 0)
            {
                keydelay[i]--;
            }
            else
            {
                if (keystate[i] == KEY_PRESSED)
                {
                    setKeyReleaseFlag(i);
                }
                keystate[i] = KEY_NOPRESSED;
            }  
        }
    }
    
}



static uint8_t GetSigState(KEY_ID_type key_id)
{
    switch (key_id)
    {
    case KEY_HG_F:
        return scm_canmatrix_rx.Panel_Key.KEY_HG_F;
    case KEY_HG_R:
        return scm_canmatrix_rx.Panel_Key.KEY_HG_R;
    case KEY_KB_F:
        return scm_canmatrix_rx.Panel_Key.KEY_KB_F;
    case KEY_KB_R:
        return scm_canmatrix_rx.Panel_Key.KEY_KB_R;
    case KEY_TT_F:
        return scm_canmatrix_rx.Panel_Key.KEY_TT_F;
    case KEY_TT_R:
        return scm_canmatrix_rx.Panel_Key.KEY_TT_R;
    case KEY_ZY_U:
        return scm_canmatrix_rx.Panel_Key.KEY_ZY_U;
    case KEY_ZY_D:
        return scm_canmatrix_rx.Panel_Key.KEY_ZY_D;
    default:
        return 0;
    }
}

void KeyProTask(void)
{
    //HG
    if (getKeyPressFlag(KEY_HG_F) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CW);
    }
    if (getKeyReleaseFlag(KEY_HG_F) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }
    if (getKeyPressFlag(KEY_HG_R) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CCW);
    }
    if (getKeyReleaseFlag(KEY_HG_R) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }
    
    //KB
    if (getKeyPressFlag(KEY_KB_F) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CW);
    }
    if (getKeyReleaseFlag(KEY_KB_F) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }
    if (getKeyPressFlag(KEY_KB_R) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CCW);
    }
    if (getKeyReleaseFlag(KEY_KB_R) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }

    //TT
    if (getKeyPressFlag(KEY_TT_F) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CW);
    }
    if (getKeyReleaseFlag(KEY_TT_F) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }
    if (getKeyPressFlag(KEY_TT_R) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CCW);
    }
    if (getKeyReleaseFlag(KEY_TT_R) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }

    //ZY
    if (getKeyPressFlag(KEY_ZY_U) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CW);
    }
    if (getKeyReleaseFlag(KEY_ZY_U) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }
    if (getKeyPressFlag(KEY_ZY_D) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_CCW);
    }
    if (getKeyReleaseFlag(KEY_ZY_D) == KEY_PRESSED)
    {
        setMotorState(MotorHG,Motor_ACT_NOACT);
    }
}







