Here is a little keyboard decoding demo that should get you well on your way. You'll need to rewrite the key scanning routine for your hardware. In addition, there will be some kind of timeout needed to select the same digit twice in a row. You should also have little trouble figuring out how to add support for capitalization, punctuation, and meta keys...
#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;
}