Keypad library prototype

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 PinName rowPins[4] = { PA_13, PA_14, PC_2, PC_3 };
00005 PinName colPins[4] = { PA_0, PA_1, PA_4, PB_0 };
00006 
00007 DigitalOut* _rows[4];
00008 DigitalIn* _cols[4];
00009 
00010 TextLCD lcd(D2,D3,D4,D5,D6,D7);
00011 
00012 // Define your own keypad values
00013 char Keytable[] = {
00014     '1', '2', '3', 'A', // r0
00015     '4', '5', '6', 'B', // r1
00016     '7', '8', '9', 'C', // r2
00017     '*', '0', '#', 'D'  // r3
00018   // c0   c1  c2   c3
00019 };
00020 
00021 int getKeyIndex() {
00022     int result = -1;
00023     for (int r = 0; r < 4 && result == -1; r++) {
00024         _rows[r]->write(1);
00025         for(int c = 0; c < 4 && result == -1; c++){
00026             DigitalIn *col = _cols[c];
00027             if(col->read() == 1) {
00028                 result = r*4+c;
00029             }
00030         }
00031         _rows[r]->write(0);
00032     }
00033     return result;
00034 }
00035 
00036 char getKey() {
00037     int idx = getKeyIndex();
00038     return idx != -1 ? Keytable[idx] : '\0';
00039 }
00040 
00041 void init() {
00042     for(int i = 0;i < 4; i++){
00043         _rows[i] = new DigitalOut(rowPins[i]);
00044         _rows[i]->write(0);
00045     }
00046     for(int i = 0;i < 4; i++){
00047         _cols[i] = new DigitalIn(colPins[i],PullDown);
00048     }
00049 }
00050 
00051 int main() {
00052     char key;
00053     lcd.locate(0,0);
00054     lcd.printf("Clean");
00055 
00056     init();
00057 
00058     while(1) {
00059         key = getKey();
00060         lcd.locate(0,1);
00061         if (key != '\0') {
00062             lcd.printf("%c", key);
00063         } else  {
00064             lcd.printf("%c", ' ');
00065         }
00066     }
00067 }