4x4 Keypad easy to use library that pollis the interface width pullups
Dependents: 4x4KeyBoardExample xoxokey 4x4KeyBoardExample ProgettoCassaforte ... more
Fork of keypad by
Simple library for reading a 4x4 keypad width at ability to use internal pullups
Import program
00001 #include "mbed.h" 00002 #include "keypad.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 00006 00007 int main() { 00008 // c0 c1 c2 c3 r0 r1 r2 r3 00009 Keypad keypad( PC_3,PC_2,PA_0,PA_1,PA_4,PB_0,PC_1,PC_0 ); 00010 00011 keypad.enablePullUp(); 00012 char key; 00013 pc.printf("Please touch a key on the keypad\n\r"); 00014 while (1) 00015 { 00016 key = keypad.getKey(); 00017 if(key != KEY_RELEASED) 00018 { 00019 pc.printf("%c\r\n",key); 00020 wait(0.6); 00021 } 00022 } 00023 }
Import library
Public Member Functions |
|
Keypad (PinName col0, PinName col1, PinName col2, PinName col3, PinName row0, PinName row1, PinName row2, PinName row3) | |
Create a 4x4 (col, row) or 4x4 keypad interface
. |
|
char | getKey () |
Returns the letter of the pressed key
. |
|
bool | getKeyPressed () |
Detects if any key was pressed.
|
|
void | enablePullUp () |
Enables internal PullUp resistors on the coloums pins.
|
|
Protected Member Functions |
|
int | getKeyIndex () |
return the index value representating the pressed key
|
Diff: keypad.cpp
- Revision:
- 1:fa689a04c361
- Parent:
- 0:1fa357ea3fcc
- Child:
- 14:d6592dac4365
diff -r 1fa357ea3fcc -r fa689a04c361 keypad.cpp --- a/keypad.cpp Sat Nov 06 14:36:22 2010 +0000 +++ b/keypad.cpp Sat Sep 19 07:51:21 2015 +0000 @@ -1,4 +1,4 @@ -/* Copyright (c) 2010 Dimiter Kentri +/* Copyright (c) 2015 Rune Langøy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,60 +19,54 @@ THE SOFTWARE. */ -/*Simple example +/* + Example is available in keypad.h -#include "mbed.h" -#include "keypad.h" - -Serial pc(USBTX, USBRX); -Keypad telepad(p13,p14,p15,p16,p17,p18,p19,p20); - -int main(void){ - char key; - pc.printf("Enter codes\n\r"); - while(1){ - key = telepad.getKey(); - if(key != KEY_RELEASED){ - pc.printf("%c\n",key); - wait(0.6); - } - } -} */ +*/ #include "mbed.h" #include "keypad.h" using namespace mbed; -Keypad::Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1,PinName row2, PinName row3, PinName row4): _col1(col1), - _col2(col2),_col3(col3),_col4(col4),_rows(row1,row2,row3,row4) { - -} +Keypad::Keypad(PinName col0, PinName col1, PinName col2, PinName col3, + PinName row0,PinName row1, PinName row2, PinName row3): + _cols(col0,col1,col2,col3) ,_rows(row0,row1,row2,row3) { } + + void Keypad::enablePullUp() +{ + _cols.mode(PullUp); +} -int Keypad::getKeyIndex(){ - for(int k=0; k<4; k++) { - _rows = 0; - } - for(int i=0; i<4; i++) { - _rows = 1 << i; - for(int j=0; j<4;j++){ - if (_col1) - return 1+(i*4); - else if (_col2) - return 2+(i*4); - else if (_col3) - return 3+(i*4); - else if (_col4) - return 4+(i*4); - } +bool Keypad::getKeyPressed() +{ + _rows = 0; // Ground all keys + if(_cols.read()==0xff) //Chk if key is pressed + return false; + + return true; +} + +int Keypad::getKeyIndex() +{ + if (!getKeyPressed()) + return -1; + + //Scan rows and cols and return switch index + for(int i=0; i<4; i++) + { _rows = ~(0x01 << i); + for(int j=0; j<4;j++) + if ( !( (_cols.read()>> j )& 0x1 )) + return j + (i*4); } return -1; } -char Keypad::getKey(){ -int k = getKeyIndex(); -if(k != -1) - return keys[k-1]; - else - return 0; +char Keypad::getKey() +{ + int k = getKeyIndex(); + if(k != -1) + return keys[k]; + + return 0; }