
#include "hwCtrl.h"
#include "r_cg_port.h"
#include "iodefine.h"
#include "PINdef.h"
#include "r_cg_adc.h"
static uint8_t keystate[KEY_NUM] = {0,0,0,0,0,};
static uint8_t keyPressFlag[KEY_NUM] = {0,0,0,0,0,};
static uint8_t keyReleaseFlag[KEY_NUM] = {0,0,0,0,0,};
static uint16_t keydelay[KEY_NUM] = {0,0,0,0,0,};
uint16_t g_adval[12];

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

void KeyScan(void)//1ms
{
    uint8_t i,key;
    for (i = 0; i < KEY_NUM; i++)
    {
        key = GetIOState(i+1);
        if (key == KEY_PRESSED)
        {
            keydelay[i]++;
            if (keydelay[i] >= KEY_DELAY_TIMES)
            {
                keystate[i] = KEY_PRESSED;
                
            }
            if (keydelay[i] >= KEY_LONG_TIMES && keystate[i] != KEY_Long_PRESSED)
            {
                keydelay[i] = KEY_LONG_TIMES;
                setKeyPressFlag(i);//long press
                keystate[i] = KEY_Long_PRESSED;
            }
            
        }
        else if(key == KEY_NOPRESSED)
        {
            if(keydelay[i] > 5)keydelay[i] = 5;
            if (keydelay[i] > 0)
            {
                keydelay[i]--;
            }
            else
            {
                if (keystate[i] == KEY_PRESSED)
                {
                    setKeyReleaseFlag(i);
                }
                keystate[i] = KEY_NOPRESSED;
            }  
        }
    }
    
}


uint8_t GetIOState(uint8_t keyno)
{
    switch (keyno)
    {
    //KEY
    case KEYID_UP:
        return KEY_UP==0?1:0;
    case KEYID_DOWN:
        return KEY_DOWN==0?1:0;
    case KEYID_LEFT:
        return KEY_LEFT==0?1:0;
    case KEYID_RIGHT:
        return KEY_RIGHT==0?1:0;
    case KEYID_MIDDLE:
        return KEY_MIDDLE==0?1:0;
    case KEYID_XNP:
        return KEY_XNP;
    case KEYID_XNM:
        return KEY_XNM;

    default:
        return 0;
    }
}

uint8_t GetKeyState(uint8_t keyno)
{
    if (keyno > 0 && keyno < 6)
    {
        return keystate[keyno-1];
    }
    return KEY_NOPRESSED;
}


uint16_t getAdval(uint8_t ch)
{
    if (ch < 12)
    {
        return g_adval[ch];
    }
    
    return 0;
}

void ad_handle(void)
{
    //uint8_t adch=0;
    //adch = ADS - 2;
    R_ADC_Get_Result(&g_adval[0]);//adch
    /*
    adch++;
    if (adch > 6)
    {
        adch = 0;
    }
    
    ADS = adch + 2;
    */
}


/*****************diagnostic******************/


