use*_*939 2 c embedded keyboard
我有一个设备,有一个像普通手机一样的9键.我想使用这9个键来打印像ABCD这样的普通字母,就像手机让你这样做一样.
这是一个嵌入式系统编程项目.我无法弄清楚实现此功能的逻辑.
我通过轮询而不是通过中断来检测密钥.
有人可以帮忙吗?如果你能指出相关资源,我将不胜感激.
这是一个小键盘解码演示,可以帮助您顺利完成.您需要重写硬件的密钥扫描例程.此外,连续两次选择相同的数字需要某种超时.您还应该在确定如何添加对大小写,标点符号和元键的支持方面遇到一些麻烦......
#include <stdio.h>
#define NUM_KEYS 10
#define NUM_PHASES 6
char KeyMap[NUM_KEYS][NUM_PHASES] =
{ { '0', 0, 0, 0, 0, 0 },
{ '1', 0, 0, 0, 0, 0 },
{ '2', 'A', 'B', 'C', 0, 0 },
{ '3', 'D', 'E', 'F', 0, 0 },
{ '4', 'G', 'H', 'I', 0, 0 },
{ '5', 'J', 'K', 'L', 0, 0 },
{ '6', 'M', 'N', 'O', 0, 0 },
{ '7', 'P', 'Q', 'R', 'S', 0 },
{ '8', 'T', 'U', 'V', 0, 0 },
{ '9', 'W', 'X', 'Y', 'Z', 0 } };
char KeyGet()
{
char key;
/* do whatever it takes to scan your
keyboard and return the _numeric_ digit. */
/* for this test simulate with console input */
key = getc(stdin);
if ((key >= '0') && (key <= '9'))
{
key -= 0x30;
}
else
{
key = 0;
}
return key;
}
char DecodeKey(char NewKey, char *pOldKey, int *pPhase)
{
char ch = 0;
/* Validate Phase */
if ((*pPhase < 0) || (*pPhase >= NUM_PHASES))
{
*pPhase = 0;
}
/* see if a different key was pressed than last time */
/* if it was then restart the phase counter */
if (NewKey != *pOldKey)
{
*pPhase = 0;
*pOldKey = NewKey;
}
/* Validate Key */
if ((NewKey >= 0) && (NewKey < NUM_KEYS))
{
ch = KeyMap[(int)NewKey][*pPhase];
/* if the phase position is NULL, just get the numeric digit */
if (ch == 0)
{
*pPhase = 0;
ch = KeyMap[(int)NewKey][*pPhase];
}
/* bump the phase */
++(*pPhase);
if (*pPhase >= NUM_PHASES)
{
*pPhase = 0;
}
}
return ch;
}
int main()
{
char nk; /* new key */
char ok = 0; /* old key */
char c; /* resulting character */
int phase = 0; /* tracks the key presses */
while (1)
{
/* get a key */
nk = KeyGet();
/* convert it to a character */
c = DecodeKey(nk, &ok, &phase);
if (c != 0)
{
printf("%c", c);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
770 次 |
| 最近记录: |