4x4 Keypad easy to use library that pollis the interface width pullups

Dependents:   4x4KeyBoardExample xoxokey 4x4KeyBoardExample ProgettoCassaforte ... more

Fork of keypad by Dimiter K

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

Committer:
rlanghbv
Date:
Sat Sep 19 07:51:21 2015 +0000
Revision:
1:fa689a04c361
Parent:
0:1fa357ea3fcc
Child:
2:cf9e1e315e74
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rlanghbv 1:fa689a04c361 1 /*mbed simple keypad library, using polling
rlanghbv 1:fa689a04c361 2
rlanghbv 1:fa689a04c361 3 Copyright (c) 2015 Rune Langøy
DimiterK 0:1fa357ea3fcc 4
DimiterK 0:1fa357ea3fcc 5 Permission is hereby granted, free of charge, to any person obtaining a copy
DimiterK 0:1fa357ea3fcc 6 of this software and associated documentation files (the "Software"), to deal
DimiterK 0:1fa357ea3fcc 7 in the Software without restriction, including without limitation the rights
DimiterK 0:1fa357ea3fcc 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DimiterK 0:1fa357ea3fcc 9 copies of the Software, and to permit persons to whom the Software is
DimiterK 0:1fa357ea3fcc 10 furnished to do so, subject to the following conditions:
DimiterK 0:1fa357ea3fcc 11
DimiterK 0:1fa357ea3fcc 12 The above copyright notice and this permission notice shall be included in
DimiterK 0:1fa357ea3fcc 13 all copies or substantial portions of the Software.
DimiterK 0:1fa357ea3fcc 14
DimiterK 0:1fa357ea3fcc 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DimiterK 0:1fa357ea3fcc 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DimiterK 0:1fa357ea3fcc 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DimiterK 0:1fa357ea3fcc 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DimiterK 0:1fa357ea3fcc 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DimiterK 0:1fa357ea3fcc 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
DimiterK 0:1fa357ea3fcc 21 THE SOFTWARE.
DimiterK 0:1fa357ea3fcc 22 */
DimiterK 0:1fa357ea3fcc 23
DimiterK 0:1fa357ea3fcc 24 #ifndef KEYPAD_H
DimiterK 0:1fa357ea3fcc 25 #define KEYPAD_H
DimiterK 0:1fa357ea3fcc 26
DimiterK 0:1fa357ea3fcc 27 #include "DigitalIn.h"
DimiterK 0:1fa357ea3fcc 28 #include "BusOut.h"
DimiterK 0:1fa357ea3fcc 29
DimiterK 0:1fa357ea3fcc 30 namespace mbed{
DimiterK 0:1fa357ea3fcc 31
DimiterK 0:1fa357ea3fcc 32 const char NO_KEY = '\0';
DimiterK 0:1fa357ea3fcc 33 #define KEY_RELEASED NO_KEY
rlanghbv 1:fa689a04c361 34
rlanghbv 1:fa689a04c361 35 const char keys[16] = {'1','2','3','A',
DimiterK 0:1fa357ea3fcc 36 '4','5','6','B',
DimiterK 0:1fa357ea3fcc 37 '7','8','9','C',
DimiterK 0:1fa357ea3fcc 38 '*','0','#','D'};
rlanghbv 1:fa689a04c361 39 /**
rlanghbv 1:fa689a04c361 40 * An simple polling-based interface to read a 4x4 keypad.
rlanghbv 1:fa689a04c361 41 *
rlanghbv 1:fa689a04c361 42 * The function getKey() reads the index of the pressed key
rlanghbv 1:fa689a04c361 43 * and returns the letter of the pressed key
rlanghbv 1:fa689a04c361 44 *
rlanghbv 1:fa689a04c361 45 * This work is a derivative of the works done by:
rlanghbv 1:fa689a04c361 46 * Dimiter Kentri in 2010 https://developer.mbed.org/users/DimiterK/code/keypad/
rlanghbv 1:fa689a04c361 47 * and
rlanghbv 1:fa689a04c361 48 * Yoong Hor Meng in 2012 https://developer.mbed.org/users/yoonghm/code/keypad/
rlanghbv 1:fa689a04c361 49 *
rlanghbv 1:fa689a04c361 50 *Example
rlanghbv 1:fa689a04c361 51 *
rlanghbv 1:fa689a04c361 52 * #include "mbed.h"
rlanghbv 1:fa689a04c361 53 * #include "keypad.h"
rlanghbv 1:fa689a04c361 54 *
rlanghbv 1:fa689a04c361 55 * Serial pc(USBTX, USBRX);
rlanghbv 1:fa689a04c361 56 * Keypad keypad(D3,D4,D5,D6,D7,D8,D9,D10);
rlanghbv 1:fa689a04c361 57 * // Keypad keypad( PC_3,PC_2,PA_0,PA_1,PA_4,PB_0,PC_1,PC_0 ); // Tested on Nucleo303RE card
rlanghbv 1:fa689a04c361 58 *
rlanghbv 1:fa689a04c361 59 * int main(void)
rlanghbv 1:fa689a04c361 60 * {
rlanghbv 1:fa689a04c361 61 * keypad.enablePullUp();
rlanghbv 1:fa689a04c361 62 * char key;
rlanghbv 1:fa689a04c361 63 * pc.printf("Please touch a key on the keypad\n\r");
rlanghbv 1:fa689a04c361 64 * while(1)
rlanghbv 1:fa689a04c361 65 * {
rlanghbv 1:fa689a04c361 66 * key = keypad.getKey();
rlanghbv 1:fa689a04c361 67 * if(key != KEY_RELEASED)
rlanghbv 1:fa689a04c361 68 * {
rlanghbv 1:fa689a04c361 69 * pc.printf("%c\n",key);
rlanghbv 1:fa689a04c361 70 * wait(0.2);
rlanghbv 1:fa689a04c361 71 * }
rlanghbv 1:fa689a04c361 72 * }
rlanghbv 1:fa689a04c361 73 * }
rlanghbv 1:fa689a04c361 74 * @endcode
rlanghbv 1:fa689a04c361 75
rlanghbv 1:fa689a04c361 76 */
rlanghbv 1:fa689a04c361 77
DimiterK 0:1fa357ea3fcc 78 class Keypad{
DimiterK 0:1fa357ea3fcc 79 public:
rlanghbv 1:fa689a04c361 80 /** Create a 4x4 (col, row) or 4x4 keypad interface:
rlanghbv 1:fa689a04c361 81 *
rlanghbv 1:fa689a04c361 82 * | Col0 | Col1 | Col2 | Col3
rlanghbv 1:fa689a04c361 83 * -------+------+------+------+-----
rlanghbv 1:fa689a04c361 84 * Row 0 | x | x | x | x
rlanghbv 1:fa689a04c361 85 * Row 1 | x | x | x | x
rlanghbv 1:fa689a04c361 86 * Row 2 | x | x | x | x
rlanghbv 1:fa689a04c361 87 * Row 3 | x | x | x | x
rlanghbv 1:fa689a04c361 88 *
rlanghbv 1:fa689a04c361 89 * @param col<0..3> Row data lines
rlanghbv 1:fa689a04c361 90 * @param row<0..3> Column data lines
rlanghbv 1:fa689a04c361 91 */
DimiterK 0:1fa357ea3fcc 92 Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1,PinName row2, PinName row3, PinName row4);
rlanghbv 1:fa689a04c361 93
rlanghbv 1:fa689a04c361 94 /** User-defined function that returns the letter of the pressed key
rlanghbv 1:fa689a04c361 95 * if no walid key was pressed
rlanghbv 1:fa689a04c361 96 * the value -1 is returned
rlanghbv 1:fa689a04c361 97 *
rlanghbv 1:fa689a04c361 98 * returns char
rlanghbv 1:fa689a04c361 99 */
DimiterK 0:1fa357ea3fcc 100 char getKey();
rlanghbv 1:fa689a04c361 101
rlanghbv 1:fa689a04c361 102 /** User-defined function that returns true if any key is pressed
rlanghbv 1:fa689a04c361 103 *
rlanghbv 1:fa689a04c361 104 * returns bool
rlanghbv 1:fa689a04c361 105 */
rlanghbv 1:fa689a04c361 106 bool getKeyPressed();
rlanghbv 1:fa689a04c361 107 /** User-defined function enables PullUp resistors on the
rlanghbv 1:fa689a04c361 108 *
rlanghbv 1:fa689a04c361 109 * returns bool
rlanghbv 1:fa689a04c361 110 */
rlanghbv 1:fa689a04c361 111 void enablePullUp();
DimiterK 0:1fa357ea3fcc 112
DimiterK 0:1fa357ea3fcc 113 protected:
rlanghbv 1:fa689a04c361 114 BusIn _cols;
DimiterK 0:1fa357ea3fcc 115 BusOut _rows;
rlanghbv 1:fa689a04c361 116 /** User-defined function that return the index value
rlanghbv 1:fa689a04c361 117 * representating the pressed key if no walid key was pressed
rlanghbv 1:fa689a04c361 118 * the value 0 is returned
rlanghbv 1:fa689a04c361 119 *
rlanghbv 1:fa689a04c361 120 * returns int
rlanghbv 1:fa689a04c361 121 */
rlanghbv 1:fa689a04c361 122 int getKeyIndex();
rlanghbv 1:fa689a04c361 123
DimiterK 0:1fa357ea3fcc 124 };
DimiterK 0:1fa357ea3fcc 125
DimiterK 0:1fa357ea3fcc 126 }
DimiterK 0:1fa357ea3fcc 127 #endif