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

Revision:
1:fa689a04c361
Parent:
0:1fa357ea3fcc
Child:
2:cf9e1e315e74
--- a/keypad.h	Sat Nov 06 14:36:22 2010 +0000
+++ b/keypad.h	Sat Sep 19 07:51:21 2015 +0000
@@ -1,4 +1,6 @@
-/*	 Copyright (c) 2010 Dimiter Kentri
+/*mbed simple keypad library, using polling 
+
+	 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
@@ -25,28 +27,100 @@
 #include "DigitalIn.h"
 #include "BusOut.h"
 
-
 namespace mbed{
 
 const char NO_KEY = '\0';
 #define KEY_RELEASED NO_KEY
-          
-
-       
-const int keys[16] = {'1','2','3','A',
+   
+const char keys[16] = {'1','2','3','A',
                       '4','5','6','B',
                       '7','8','9','C',
                       '*','0','#','D'};
-                      
+ /**
+ * An simple polling-based interface to read a 4x4 keypad.
+ *
+ * The function getKey() reads  the index of the pressed key 
+ * and returns the letter of the pressed key 
+ *
+ *  This work is a derivative of the works done by:
+ *    Dimiter Kentri in 2010 https://developer.mbed.org/users/DimiterK/code/keypad/
+ *  and
+ *    Yoong Hor Meng in 2012 https://developer.mbed.org/users/yoonghm/code/keypad/
+ *
+ *Example
+ * 
+ * #include "mbed.h"
+ * #include "keypad.h"
+ *	  
+ * Serial pc(USBTX, USBRX); 
+ * Keypad keypad(D3,D4,D5,D6,D7,D8,D9,D10);
+ * // Keypad keypad( PC_3,PC_2,PA_0,PA_1,PA_4,PB_0,PC_1,PC_0 ); // Tested on  Nucleo303RE card
+ * 					
+ * int main(void)
+ * {
+ *  keypad.enablePullUp();
+ *  char key;
+ *  pc.printf("Please touch a key on the keypad\n\r");
+ *  while(1)
+ *  { 
+ *	key = keypad.getKey();	
+ * 	if(key != KEY_RELEASED)
+ *	{
+ *	   pc.printf("%c\n",key);
+ *	   wait(0.2);
+ *	 }
+ *  }
+ * }	
+ * @endcode
+
+ */
+                     
 class Keypad{    
     public:
+    /** Create a 4x4 (col, row) or 4x4 keypad interface:
+     *
+     *          | Col0 | Col1 | Col2 | Col3
+     *   -------+------+------+------+-----
+     *   Row 0  |   x  |   x  |   x  |  x
+     *   Row 1  |   x  |   x  |   x  |  x
+     *   Row 2  |   x  |   x  |   x  |  x
+     *   Row 3  |   x  |   x  |   x  |  x
+     *
+     *  @param col<0..3>     Row data lines
+     *  @param row<0..3>     Column data lines
+     */
         Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1,PinName row2, PinName row3, PinName row4);
-        int getKeyIndex();
+    
+       /** User-defined function that returns the letter of the pressed key
+        *      if no walid key was pressed
+        *      the value -1 is returned
+        *          
+        *  returns char
+        */
         char getKey();
+        
+        /** User-defined function that returns true if any key is pressed 
+        *          
+        *  returns bool
+        */
+        bool getKeyPressed();
+        /** User-defined function enables PullUp resistors on the 
+        *          
+        *  returns bool
+        */
+        void enablePullUp();
 
     protected:
-        DigitalIn _col1,_col2,_col3,_col4;    
+        BusIn  _cols;    
         BusOut _rows;
+         /** User-defined function that return the index value 
+        *      representating the pressed key if no walid key was pressed
+        *      the value 0 is returned
+        *          
+        *  returns int
+        */
+        int getKeyIndex();
+       
 };
 
 }