HDG/ECU_APP/user/hwCtrl.c

173 lines
2.9 KiB
C
Raw Normal View History

2024-05-26 08:08:32 +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};
static uint8_t keyPressFlag[KEY_NUM] = {0};
static uint8_t keyReleaseFlag[KEY_NUM] = {0};
static uint16_t keydelay[KEY_NUM] = {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;
if (id < KEY_NUM)
{
retVal = keyPressFlag[id];
keyPressFlag[id] = KEY_NOPRESSED;
}
return retVal;
}
uint8_t getKeyReleaseFlag(uint8_t id)
{
uint8_t retVal = KEY_NOPRESSED;
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);
if (key == KEY_PRESSED)
{
keydelay[i]++;
if (keydelay[i] >= KEY_DELAY_TIMES)
{
keystate[i] = KEY_PRESSED;
}
}
else if(key == KEY_NOPRESSED)
{
if(keydelay[i] > 5)keydelay[i] = 5;
if (keydelay[i] > 0)
{
keydelay[i]--;
}
else
{
keystate[i] = KEY_NOPRESSED;
}
}
}
}
uint8_t GetIOState(uint8_t keyno)
{
switch (keyno)
{
//KEY
case KEYID_1_P125 :
return KEY_1==0?1:0;
case KEYID_2_P34 :
return KEY_2==0?1:0;
case KEYID_3_P81 :
return KEY_3==0?1:0;
case KEYID_4_P82 :
return KEY_4==0?1:0;
case KEYID_5_P83 :
return KEY_5==0?1:0;
case KEYID_6_P80 :
return KEY_6==0?1:0;
case KEYID_7_P63 :
return KEY_7==0?1:0;
case KEYID_8_P17 :
return KEY_8==0?1:0;
case KEYID_9_P30 :
return KEY_9==0?1:0;
default:
return 0;
}
}
uint8_t GetKeyState(uint8_t keyno)
{
if (keyno < KEY_NUM)
{
return keystate[keyno];
}
return KEY_NOPRESSED;
}
uint16_t getAdval(uint8_t ch)
{
if (ch < 1)
{
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******************/