pt aliff

Fork of keypad by Dimiter K

Files at this revision

API Documentation at this revision

Comitter:
aliffhilmie93
Date:
Mon Jun 04 16:01:47 2018 +0000
Parent:
0:1fa357ea3fcc
Commit message:
pt aliff

Changed in this revision

keypad.cpp Show annotated file Show diff for this revision Revisions of this file
keypad.h Show annotated file Show diff for this revision Revisions of this file
diff -r 1fa357ea3fcc -r ced7b1445bc4 keypad.cpp
--- a/keypad.cpp	Sat Nov 06 14:36:22 2010 +0000
+++ b/keypad.cpp	Mon Jun 04 16:01:47 2018 +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;
-    }    
+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 = 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);            
-        }
-    }    
+        _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;
 }
diff -r 1fa357ea3fcc -r ced7b1445bc4 keypad.h
--- a/keypad.h	Sat Nov 06 14:36:22 2010 +0000
+++ b/keypad.h	Mon Jun 04 16:01:47 2018 +0000
@@ -1,4 +1,6 @@
-/*	 Copyright (c) 2010 Dimiter Kentri
+/*mbed simple 4x4 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,111 @@
 #include "DigitalIn.h"
 #include "BusOut.h"
 
-
-namespace mbed{
+namespace mbed
+{
 
 const char NO_KEY = '\0';
 #define KEY_RELEASED NO_KEY
-          
+
+/** @brief table showing the printed layout of the keypad \n
+ */
+const char keys[16] = {'1','2','3','F',
+                       '4','5','6','E',
+                       '7','8','9','D',
+                       'A','0','B','C'
+                      };
+/**
+* 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:
+* @code
+* #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\r\n");
+*  while(1)
+*  {
+*	key = keypad.getKey();
+* 	if(key != KEY_RELEASED)
+*	{
+*	   pc.printf("%c\r\n",key);
+*	   wait(0.2);
+*	 }
+*  }
+* }
+* @endcode
+
+*/
 
-       
-const int keys[16] = {'1','2','3','A',
-                      '4','5','6','B',
-                      '7','8','9','C',
-                      '*','0','#','D'};
-                      
-class Keypad{    
-    public:
-        Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1,PinName row2, PinName row3, PinName row4);
-        int getKeyIndex();
-        char getKey();
+class Keypad
+{
+public:
+    /**  @brief Create a 4x4 (col, row) or 4x4 keypad interface\n
+     *  <pre>
+     *          | Col0 | Col1 | Col2 | Col3   \n
+     *   -------+------+------+------+-----   \n
+     *   Row 0  |   x  |   x  |   x  |  x     \n
+     *   Row 1  |   x  |   x  |   x  |  x     \n
+     *   Row 2  |   x  |   x  |   x  |  x     \n
+     *   Row 3  |   x  |   x  |   x  |  x     \n
+     *  </pre>
+     *
+     *  @param col<0..3>     Row data lines
+     *  @param row<0..3>     Column data lines
+     */
+    Keypad(PinName col0, PinName col1, PinName col2, PinName col3, PinName row0,PinName row1, PinName row2, PinName row3);
+
+    /** @brief  Returns the letter of the pressed key \n
+     *
+     *  @return char
+     *  @returns
+     *     The pressed character\n
+     *     '\0' or NO_KEY if no keys was pressed
+     */
+    char getKey();
 
-    protected:
-        DigitalIn _col1,_col2,_col3,_col4;    
-        BusOut _rows;
+    /** @brief Detects if any key was pressed
+    *
+    *   @return bool
+    *   @retval true   a key is pressed
+    *   @retval false  no keys was not pressed
+    */
+    bool getKeyPressed();
+    /**  Enables internal PullUp resistors on the coloums pins
+    *
+    *  @return void
+    */
+    void enablePullUp();
+
+protected:
+    BusIn  _cols;
+    BusOut _rows;
+    /** @brief return the index value
+    *      representating the pressed key \n
+    *
+    *  @return int
+    *  @returns
+    *    The index representing the pushed key used in table keys\n
+    *    -1 if no key was pressed
+    */
+    int getKeyIndex();
+
 };
 
 }