初级会员
- 积分
- 135
- 金钱
- 135
- 注册时间
- 2018-8-5
- 在线时间
- 11 小时
|
本帖最后由 yikexiaoshu 于 2021-11-4 13:48 编辑
/* 硬件映射 */
#define PIN_KEY1 _pa1
#define PIN_KEY2 _pa2
#define KEY_PRESS 1
#define KEY_RELASE 0
#define LONG_CNT_MIN 50
typedef enum {
KEY_VALUE_NONE, /* 无按键按下*/
KEY_VALUE_SHORT,
KEY_VALUE_LONG,
KEY_VALUE_MAX,
}KeyValueEn;
typedef enum {
KEY_STATE_IDEL, /* 未按下 */
KEY_STATE_FILT, /* 滤波 */
KEY_STATE_DOWN,
KEY_STATE_CHECK_RELASE,
KEY_STATE_MAX,
}KeyStateEn;
typedef enum {
KEY_NUM1 = 1,
KEY_NUM2,
KEY_NUM_MAX,
}KeyNumEn;
typedef struct keyObjLable{
u8 keyState; /* @KeyStateEn */
u8 keyValue; /* @KeyValueEn */
u8 keyNum; /* @KeyNumEn */
u8 keepCnt;
u8 (*IsUp)(u8 keyNumValue);
void (*KeyScan)(keyObjLable* keyN);
}KeyObj;
KeyObj g_key1Obj, g_key2Obj;
static u8 IsKeyUp(u8 keyNumValue);
static void KeyScan(KeyObj* keyN);
void InitCreatKye1Obj(void)
{
g_key1Obj.keyNum = KEY_NUM1;
g_key1Obj.keyState = KEY_STATE_IDEL;
g_key1Obj.keyValue = KEY_VALUE_NONE;
g_key1Obj.keepCnt = 0;
g_key1Obj.IsUp = IsKeyUp;
g_key1Obj.KeyScan = KeyScan;
}
/* 间隔50ms 调用一次 */
static void KeyScan(KeyObj* keyN)
{
switch (keyN->keyState)
{
case KEY_STATE_IDEL :
{
if (keyN->IsUp(g_key1Obj.keyNum) == 0)
{
keyN->keepCnt = 0;
keyN->keyState = KEY_STATE_FILT;
}
}
break;
case KEY_STATE_FILT :
{
if (keyN->IsUp(keyN->keyNum) == 0)
{
keyN->keyState = KEY_STATE_DOWN;
}
}
break;
case KEY_STATE_DOWN :
{
if (keyN->IsUp(keyN->keyNum) == 0)
{
if (++keyN->keepCnt >= LONG_CNT_MIN)
{
keyN->keyValue = KEY_VALUE_LONG;
keyN->keyState = KEY_STATE_CHECK_RELASE;
}
}
else
{
keyN->keyValue = KEY_VALUE_SHORT;
keyN->keyState = KEY_STATE_IDEL;
}
}
break;
case KEY_STATE_CHECK_RELASE :
{
if (keyN->IsUp(keyN->keyNum) == 1)
{
keyN->keyState = KEY_STATE_IDEL;
}
}
break;
default :
//err print lastword
while(1);
break;
}
}
/* keyNum @KeyNumEn */
static u8 IsKeyUp(u8 keyNumValue)
{
switch(keyNumValue)
{
case 1 :
{
return (PIN_KEY1 == KEY_PRESS);
}
break;
case 2 :
{
return (PIN_KEY2 == KEY_PRESS);
}
break;
default :
//err print lastword
while(1); /* 卡住等狗咬死 */
break;
}
}
void main(void)
{
InitCreatKye1Obj();
while(1)
{
if (g_timeFlag50Ms == SET)
{
g_timeFlag50Ms = RESET;
g_key1Obj.KeyScan(&g_key1Obj);
}
}
}
|
|