Interrupt-driven keypad interface library. Compatible with RTOS. Able to handle various keypad size below 4x4.

Dependencies:   keypad mbed

Committer:
yoonghm
Date:
Mon Jan 30 09:40:01 2012 +0000
Revision:
0:8209bcf62e0a
Child:
1:1ae4a77af85b
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoonghm 0:8209bcf62e0a 1 #include "mbed.h"
yoonghm 0:8209bcf62e0a 2 #include "keypad.h"
yoonghm 0:8209bcf62e0a 3
yoonghm 0:8209bcf62e0a 4 #define KEYLEN 4
yoonghm 0:8209bcf62e0a 5 #define ENDKEY 15
yoonghm 0:8209bcf62e0a 6 char Buffer[KEYLEN];
yoonghm 0:8209bcf62e0a 7 int Index = 0;
yoonghm 0:8209bcf62e0a 8
yoonghm 0:8209bcf62e0a 9 // Define your own keypad values
yoonghm 0:8209bcf62e0a 10 char Keytable[] = { '1', '2', '3', 'A',
yoonghm 0:8209bcf62e0a 11 '4', '5', '6', 'B',
yoonghm 0:8209bcf62e0a 12 '7', '8', '9', 'C',
yoonghm 0:8209bcf62e0a 13 '*', '0', '#', 'D'
yoonghm 0:8209bcf62e0a 14 };
yoonghm 0:8209bcf62e0a 15
yoonghm 0:8209bcf62e0a 16 uint32_t cbAfterInput(uint32_t key) {
yoonghm 0:8209bcf62e0a 17 bool finish = false;
yoonghm 0:8209bcf62e0a 18
yoonghm 0:8209bcf62e0a 19 printf("Index:%d => Key:%c\n", Index, Keytable[key]);
yoonghm 0:8209bcf62e0a 20
yoonghm 0:8209bcf62e0a 21 if (Index < KEYLEN - 1)
yoonghm 0:8209bcf62e0a 22 {
yoonghm 0:8209bcf62e0a 23 if (key != ENDKEY) // Terminating key
yoonghm 0:8209bcf62e0a 24 Buffer[Index] = Keytable[key];
yoonghm 0:8209bcf62e0a 25 else // Terminating key is entered
yoonghm 0:8209bcf62e0a 26 finish = true;
yoonghm 0:8209bcf62e0a 27 Index++;
yoonghm 0:8209bcf62e0a 28 }
yoonghm 0:8209bcf62e0a 29
yoonghm 0:8209bcf62e0a 30 if (finish || (Index == KEYLEN - 1)) {
yoonghm 0:8209bcf62e0a 31 printf("Complete string = %s\n", Buffer);
yoonghm 0:8209bcf62e0a 32 memset(&Buffer, 0, KEYLEN);
yoonghm 0:8209bcf62e0a 33 Index = 0;
yoonghm 0:8209bcf62e0a 34 }
yoonghm 0:8209bcf62e0a 35
yoonghm 0:8209bcf62e0a 36 return 0;
yoonghm 0:8209bcf62e0a 37 }
yoonghm 0:8209bcf62e0a 38
yoonghm 0:8209bcf62e0a 39 void Sleep(void) {
yoonghm 0:8209bcf62e0a 40 __WFI();
yoonghm 0:8209bcf62e0a 41 }
yoonghm 0:8209bcf62e0a 42
yoonghm 0:8209bcf62e0a 43 int main() {
yoonghm 0:8209bcf62e0a 44 memset(&Buffer, 0, KEYLEN);
yoonghm 0:8209bcf62e0a 45 Index = 0;
yoonghm 0:8209bcf62e0a 46 Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24);
yoonghm 0:8209bcf62e0a 47 keypad.CallAfterInput(&cbAfterInput);
yoonghm 0:8209bcf62e0a 48
yoonghm 0:8209bcf62e0a 49 keypad.Start();
yoonghm 0:8209bcf62e0a 50 while (1) {
yoonghm 0:8209bcf62e0a 51 Sleep();
yoonghm 0:8209bcf62e0a 52 }
yoonghm 0:8209bcf62e0a 53 }