Andrew Neville / KeyPad

Dependents:   Input_Keypad MARISOL Final_Project

Files at this revision

API Documentation at this revision

Comitter:
anevil14
Date:
Thu Mar 12 17:11:01 2015 +0000
Child:
1:518bf8b4f3b3
Commit message:
Numeric Keypad with some symbols and is built like a 4*3 Matrix

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KeyPad.cpp	Thu Mar 12 17:11:01 2015 +0000
@@ -0,0 +1,154 @@
+#include "KeyPad.h"
+//#include "mbed.h"
+
+
+KeyPad2::KeyPad2(PinName pin3, PinName pin1 ,PinName pin5 ,PinName pin2, PinName pin7,PinName pin6, PinName pin4){
+    // KeyPad2 keypad(p25, p27, p23, p26, p21, p22, p24);
+    switchrate = 0.001;
+ 
+    
+    columnoneout = new DigitalOut   ( pin3 );      // pin 3 on keypad 
+    columntwoout = new DigitalOut   ( pin1 );      // pin 1 on keypad
+    columnthreeout = new DigitalOut ( pin5 );      // pin 5 on keypad
+
+    rowonein = new DigitalIn        ( pin2 );      // pin 2 on keypad 
+    rowtwoin = new DigitalIn        ( pin7 );      // pin 7 on keypad
+    rowthreein = new DigitalIn      ( pin6 );      // pin 6 on keypad
+    rowfourin = new DigitalIn       ( pin4 );      // pin 4 on keypad
+    
+}
+
+
+KeyPad2::~KeyPad2(){
+    delete[] columnoneout;
+    delete[] columntwoout;
+    delete[] columnthreeout;
+    delete[] rowonein;
+    delete[] rowtwoin;
+    delete[] rowthreein;
+    delete[] rowfourin;   
+}
+
+void KeyPad2::setswitchrate(double switchrate){ // sets the rate which you switch between powering each column
+    this->switchrate = switchrate;   
+}
+
+
+
+
+std::vector<int> KeyPad2::getkey(){ // powers each column of the keypad once based upon the time divisions of switchrate, returns keys in sorted order
+    std::vector<int> keyspressed;
+    
+    bool addedone, addedtwo, addedthree, addedfour;
+    clock_t start;
+    //check 2nd column
+    *columnoneout = 0;
+    *columntwoout = 1;
+    *columnthreeout = 0;
+    
+    start = clock();
+   
+    addedone = false;
+    addedtwo =false;
+    addedthree = false;
+    addedfour = false;
+     
+    while (clock() - start < switchrate*CLOCKS_PER_SEC){
+        if ( *rowonein == 1 && !addedone ){
+             keyspressed.push_back(2);
+             addedone = true;
+        }
+    
+        if ( *rowtwoin == 1 && !addedtwo){
+            keyspressed.push_back(5);  
+            addedtwo = true; 
+        }
+    
+        if ( *rowthreein == 1 && !addedthree ){
+            keyspressed.push_back(8);  
+            addedthree = true; 
+        }
+    
+        if ( *rowfourin == 1 && !addedfour){
+            keyspressed.push_back(11);   
+            addedfour = true;
+        }
+    }
+    
+    
+    //check first column
+    *columnoneout = 1;
+    *columntwoout = 0;
+    *columnthreeout = 0;
+    
+    start = clock();
+   
+    addedone = false;
+    addedtwo =false;
+    addedthree = false;
+    addedfour = false;
+     
+     
+    // check first column   
+    while (clock() - start < switchrate*CLOCKS_PER_SEC){
+        if ( *rowonein == 1 && !addedone ){
+             keyspressed.push_back(1);
+             addedone = true;
+        }
+    
+        if ( *rowtwoin == 1 && !addedtwo){
+            keyspressed.push_back(4);  
+            addedtwo = true; 
+        }
+    
+        if ( *rowthreein == 1 && !addedthree ){
+            keyspressed.push_back(7);  
+            addedthree = true; 
+        }
+    
+        if ( *rowfourin == 1 && !addedfour){
+            keyspressed.push_back(10);   
+            addedfour = true;
+        }
+    }
+    
+    
+    
+    //check 3rd column
+    
+    *columnoneout = 0;
+    *columntwoout = 0;
+    *columnthreeout = 1;
+    
+    start = clock();
+   
+    addedone = false;
+    addedtwo =false;
+    addedthree = false;
+    addedfour = false;
+     
+    while (clock() - start < switchrate*CLOCKS_PER_SEC){
+        if ( *rowonein == 1 && !addedone ){
+             keyspressed.push_back(3);
+             addedone = true;
+        }
+    
+        if ( *rowtwoin == 1 && !addedtwo){
+            keyspressed.push_back(6);  
+            addedtwo = true; 
+        }
+    
+        if ( *rowthreein == 1 && !addedthree ){
+            keyspressed.push_back(9);  
+            addedthree = true; 
+        }
+    
+        if ( *rowfourin == 1 && !addedfour){
+            keyspressed.push_back(12);   
+            addedfour = true;
+        }
+    }
+    
+    std::sort(keyspressed.begin(), keyspressed.end());          
+    return keyspressed; 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KeyPad.h	Thu Mar 12 17:11:01 2015 +0000
@@ -0,0 +1,60 @@
+#ifndef MBED_KEYPAD_H
+#define MBED_KEYPAD_H
+
+#include "mbed.h"
+#include <vector>
+#include <algorithm>
+#include <time.h>
+
+//Keypad class.  A 4x3 keypad class.  Documentation is available on wiki @ ___________
+//Usage:  Instantiate object, and use getkey method to retrieve keys pressed.  Might want to play around with switchrate to get better responsiveness (decides how fast we switch powering columns)
+//Important note:  Can only reliably detect a keypress in a single row at any given time, although multiple key detection in different rows is supported (eg 1 and 4 is detected simulataneously, 1 and 3 will just detect one of those)
+
+/*  Keypad      Representations
+    1 2 3       1   2  3 
+    4 5 6   ->  4   5  6 
+    7 8 9       7   8  9
+    * 0 #       10  11 12
+    
+*/
+class KeyPad2{
+    public:
+      
+        KeyPad2(PinName pin3, PinName pin1 ,PinName pin5 ,PinName pin2, PinName pin7,PinName pin6, PinName pin4);
+        
+        void setswitchrate(double switchrate);
+        
+        // sets function to be called when any button is pressed 
+        // function should take in one parameter which will represent the button pressed (int)
+        void setinterrupthandler(const void* func);     
+        
+        ~KeyPad2();
+        
+        
+        // takes roughly 3x switchrate + processing time
+        std::vector<int> getkey();
+        
+    private:
+    
+        // the time in milliseconds which each power will be powered for before switching to the next 
+        // default = 10 milliseconds           
+        double switchrate;   
+       
+          
+        DigitalOut* columnoneout;      // pin 3 on keypad 
+        DigitalOut* columntwoout;      // pin 1 on keypad
+        DigitalOut* columnthreeout;    // pin 5 on keypad
+
+    
+        DigitalIn* rowonein;           // pin 2 on keypad 
+        DigitalIn* rowtwoin;           // pin 7 on keypad
+        DigitalIn* rowthreein;         // pin 6 on keypad
+        DigitalIn* rowfourin;          // pin 4 on keypard  
+        
+       
+        
+        
+        
+    
+};
+#endif
\ No newline at end of file