简单实现一下这个程序,只是一个框架,没按照严格代码来写,提供一个思路,看是否可行
#define KEY_COUNT 8
#define KEY_SCAN_PERIOD 1 //ms
#define KEY_SHORT_SHOOT_LIMIT 5 //ms
#define KEY_LONG_SHOOT_LIMIT 1000 //ms
#define KEY1 0x1
#define KEY2 0x2
#define KEY3 0x4
#define KEY4 0x8
#define KEY5 0x10
....
typedef enum {
KEY_DOWN = 0,
KEY_UP,
KEY_SHORT,
KEY_LONG
}eKEY_STATE;
INT32U ucKeyCount[KEY_COUNT];
//init your key
void keyInit (void)
{
INT32U ulLoop;
// add your init code
for (ulLoop = 0;ulLoop < KEY_COUNT;ulLoop ++) {
ucKeyCount[ulLoop] = 0;
}
}
static eKEY_STATE keyCurStateGet (INT8U ucKeyNum)
{
// add your key check manner as you need
if ("my check" = ucKeyNum) {
return KEY_DOWN;
}
return KEY_UP;
}
// add this code to the periodic INT FUN whoes period is KEY_SCAN_PERIOD ms
void keyScanPeriodic (void)
{
INT8U ucLoop;
for (ucLoop = 0;ucLoop < ;ucLoop ++) {
if (keyStateGet(ucLoop) = KEY_DOWN) {
ucKeyCount[ucLoop] ++;
} else {
ucKeyCount[ucLoop] = 0;
}
}
}
eKEY_STATE keyGet(INT32U ucKeyID)
{
INT8U ucLoop;
if (ucKeyID == 0) {
return KEY_UP;
}
for (ucLoop = 0;ucLoop < KEY_COUNT;ucLoop ++) {
if ((ucKeyID & 0x1) != 0) {
if (ucKeyCount[ucLoop] < (KEY_SHORT_SHOOT_LIMIT/KEY_SCAN_PERIOD)) {
return KEY_UP;
}
}
ucKeyID >>= 1;
if (ucKeyID = 0) {
return KEY_DOWN;
}
}
}
example:
keyGet(KEY1|KEY3);
|