You are viewing an older revision! See the latest version
Keypad
Library for keypad¶
Please refer this link for more information on the library. The library provides an interrupt-based method to scan the index out of a 4x4 keypad.
How it works¶
After each key press, a user defined callback function could be called. Inside the callback function, any data processing with input key index can be done. User can write additional code within the callback to block subsequent interrupt.
Sample Code¶
#include "mbed.h"
#include "keypad.h"
/* In this example, the program waits for user's input.
* The input keys is up to KEYLEN characters or
* terminated earlier when key at index ENDKEY is entered
*/
#define KEYLEN 4
#define ENDKEY 15
char Buffer[KEYLEN];
int Index = 0;
// Define your own keypad values
char Keytable[] = { '1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};
uint32_t cbAfterInput(uint32_t key) {
bool finish = false;
// Not recommended to use printf() within an interrupt handler.
// It is use for debugging only
printf("Index:%d => Key:%c\n", Index, Keytable[key]);
if (Index < KEYLEN - 1)
{
if (key != ENDKEY) // Terminating key
Buffer[Index] = Keytable[key];
else // Terminating key is entered
finish = true;
Index++;
}
if (finish || (Index == KEYLEN - 1)) {
printf("Complete string = %s\n", Buffer);
memset(&Buffer, 0, KEYLEN);
Index = 0;
}
return 0;
}
void Sleep(void) {
__WFI(); // Wait for interrupt, ie wake up if there is interrupt
}
int main() {
memset(&Buffer, 0, KEYLEN);
Index = 0;
Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24);
keypad.CallAfterInput(&cbAfterInput);
keypad.Start();
while (1) {
Sleep();
}
}