2023-05-05 11:53:54 +08:00
|
|
|
|
|
|
|
#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
|
2023-05-24 15:39:59 +08:00
|
|
|
#define KEY_LONG_TIMES 500 //500Ms
|
2023-05-05 11:53:54 +08:00
|
|
|
void ClearKeyState(void)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
for (i = 0; i < KEY_NUM; i++)
|
|
|
|
{
|
|
|
|
keystate[i] = 0;
|
2023-05-24 15:39:59 +08:00
|
|
|
keyPressFlag[i] = 0;
|
|
|
|
keyReleaseFlag[i] = 0;
|
2023-05-05 11:53:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:39:59 +08:00
|
|
|
void KeyScan(void)//1ms
|
2023-05-05 11:53:54 +08:00
|
|
|
{
|
2023-05-24 15:39:59 +08:00
|
|
|
uint8_t i,key;
|
2023-05-05 11:53:54 +08:00
|
|
|
for (i = 0; i < KEY_NUM; i++)
|
|
|
|
{
|
|
|
|
key = GetIOState(i+1);
|
2023-05-24 15:39:59 +08:00
|
|
|
if (key == KEY_PRESSED)
|
2023-05-05 11:53:54 +08:00
|
|
|
{
|
|
|
|
keydelay[i]++;
|
|
|
|
if (keydelay[i] >= KEY_DELAY_TIMES)
|
|
|
|
{
|
|
|
|
keystate[i] = KEY_PRESSED;
|
2023-05-24 15:39:59 +08:00
|
|
|
|
2023-05-05 11:53:54 +08:00
|
|
|
}
|
2023-05-24 15:39:59 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-05 11:53:54 +08:00
|
|
|
}
|
|
|
|
else if(key == KEY_NOPRESSED)
|
|
|
|
{
|
2023-05-24 15:39:59 +08:00
|
|
|
if(keydelay[i] > 5)keydelay[i] = 5;
|
2023-05-05 11:53:54 +08:00
|
|
|
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:
|
2024-11-28 22:55:34 +08:00
|
|
|
return 0;//KEY_UP==0?1:0;
|
2023-05-05 11:53:54 +08:00
|
|
|
case KEYID_DOWN:
|
2024-11-28 22:55:34 +08:00
|
|
|
return 0;//KEY_DOWN==0?1:0;
|
2023-05-05 11:53:54 +08:00
|
|
|
case KEYID_LEFT:
|
2024-11-28 22:55:34 +08:00
|
|
|
return 0;//KEY_LEFT==0?1:0;
|
2023-05-05 11:53:54 +08:00
|
|
|
case KEYID_RIGHT:
|
2024-11-28 22:55:34 +08:00
|
|
|
return 0;//KEY_RIGHT==0?1:0;
|
2023-05-05 11:53:54 +08:00
|
|
|
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******************/
|
|
|
|
|
|
|
|
|