HWBS/ECU_APP/user/hwCtrl.c
2023-08-22 14:04:32 +08:00

176 lines
3.3 KiB
C

#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_P16 :
return KEY_P16==0?1:0;
case KEYID_P17 :
return KEY_P17==0?1:0;
case KEYID_P30 :
return KEY_P30==0?1:0;
case KEYID_P34:
return KEY_P34==0?1:0;
case KEYID_P41 :
return KEY_P41==0?1:0;
case KEYID_P60 :
return KEY_P60==0?1:0;
case KEYID_P61 :
return KEY_P61==0?1:0;
case KEYID_P62 :
return KEY_P62==0?1:0;
case KEYID_P63 :
return KEY_P63==0?1:0;
case KEYID_P85 :
return KEY_P85==0?1:0;
case KEYID_P84 :
return KEY_P84==0?1:0;
case KEYID_P83:
return KEY_P83==0?1:0;
case KEYID_P82 :
return KEY_P82==0?1:0;
case KEYID_P81 :
return KEY_P81==0?1:0;
case KEYID_P80 :
return KEY_P80==0?1:0;
case KEYID_P120 :
return KEY_P120==0?1:0;
case KEYID_P125:
return KEY_P125==0?1:0;
case KEYID_P137:
return KEY_P137==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)
{
R_ADC_Get_Result(&g_adval[0]);//adch
}
void SetRGB(uint8_t r,uint8_t g,uint8_t b)
{
TDR04 = r<<5;
TDR05 = g<<5;
TDR06 = b<<5;
}
/*****************diagnostic******************/