Keypad that has 12 keys for input

Dependents:   Input_Keypad MARISOL Final_Project

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KeyPad.cpp Source File

KeyPad.cpp

00001 #include "KeyPad.h"
00002 //#include "mbed.h"
00003 
00004 
00005 KeyPad2::KeyPad2(PinName pin3, PinName pin1 ,PinName pin5 ,PinName pin2, PinName pin7,PinName pin6, PinName pin4){
00006     // KeyPad2 keypad(p25, p27, p23, p26, p21, p22, p24);
00007     switchrate = 0.001;
00008  
00009     
00010     columnoneout = new DigitalOut   ( pin3 );      // pin 3 on keypad 
00011     columntwoout = new DigitalOut   ( pin1 );      // pin 1 on keypad
00012     columnthreeout = new DigitalOut ( pin5 );      // pin 5 on keypad
00013 
00014     rowonein = new DigitalIn        ( pin2 );      // pin 2 on keypad 
00015     rowtwoin = new DigitalIn        ( pin7 );      // pin 7 on keypad
00016     rowthreein = new DigitalIn      ( pin6 );      // pin 6 on keypad
00017     rowfourin = new DigitalIn       ( pin4 );      // pin 4 on keypad
00018     
00019 }
00020 
00021 
00022 KeyPad2::~KeyPad2(){
00023     delete[] columnoneout;
00024     delete[] columntwoout;
00025     delete[] columnthreeout;
00026     delete[] rowonein;
00027     delete[] rowtwoin;
00028     delete[] rowthreein;
00029     delete[] rowfourin;   
00030 }
00031 
00032 void KeyPad2::setswitchrate(double switchrate){ // sets the rate which you switch between powering each column
00033     this->switchrate = switchrate;   
00034 }
00035 
00036 
00037 
00038 
00039 std::vector<int> KeyPad2::getkey(){ // powers each column of the keypad once based upon the time divisions of switchrate, returns keys in sorted order
00040     std::vector<int> keyspressed;
00041     
00042     bool addedone, addedtwo, addedthree, addedfour;
00043     clock_t start;
00044     //check 2nd column
00045     *columnoneout = 0;
00046     *columntwoout = 1;
00047     *columnthreeout = 0;
00048     
00049     start = clock();
00050    
00051     addedone = false;
00052     addedtwo =false;
00053     addedthree = false;
00054     addedfour = false;
00055      
00056     while (clock() - start < switchrate*CLOCKS_PER_SEC){
00057         if ( *rowonein == 1 && !addedone ){
00058              keyspressed.push_back(2);
00059              addedone = true;
00060         }
00061     
00062         if ( *rowtwoin == 1 && !addedtwo){
00063             keyspressed.push_back(5);  
00064             addedtwo = true; 
00065         }
00066     
00067         if ( *rowthreein == 1 && !addedthree ){
00068             keyspressed.push_back(8);  
00069             addedthree = true; 
00070         }
00071     
00072         if ( *rowfourin == 1 && !addedfour){
00073             keyspressed.push_back(11);   
00074             addedfour = true;
00075         }
00076     }
00077     
00078     
00079     //check first column
00080     *columnoneout = 1;
00081     *columntwoout = 0;
00082     *columnthreeout = 0;
00083     
00084     start = clock();
00085    
00086     addedone = false;
00087     addedtwo =false;
00088     addedthree = false;
00089     addedfour = false;
00090      
00091      
00092     // check first column   
00093     while (clock() - start < switchrate*CLOCKS_PER_SEC){
00094         if ( *rowonein == 1 && !addedone ){
00095              keyspressed.push_back(1);
00096              addedone = true;
00097         }
00098     
00099         if ( *rowtwoin == 1 && !addedtwo){
00100             keyspressed.push_back(4);  
00101             addedtwo = true; 
00102         }
00103     
00104         if ( *rowthreein == 1 && !addedthree ){
00105             keyspressed.push_back(7);  
00106             addedthree = true; 
00107         }
00108     
00109         if ( *rowfourin == 1 && !addedfour){
00110             keyspressed.push_back(10);   
00111             addedfour = true;
00112         }
00113     }
00114     
00115     
00116     
00117     //check 3rd column
00118     
00119     *columnoneout = 0;
00120     *columntwoout = 0;
00121     *columnthreeout = 1;
00122     
00123     start = clock();
00124    
00125     addedone = false;
00126     addedtwo =false;
00127     addedthree = false;
00128     addedfour = false;
00129      
00130     while (clock() - start < switchrate*CLOCKS_PER_SEC){
00131         if ( *rowonein == 1 && !addedone ){
00132              keyspressed.push_back(3);
00133              addedone = true;
00134         }
00135     
00136         if ( *rowtwoin == 1 && !addedtwo){
00137             keyspressed.push_back(6);  
00138             addedtwo = true; 
00139         }
00140     
00141         if ( *rowthreein == 1 && !addedthree ){
00142             keyspressed.push_back(9);  
00143             addedthree = true; 
00144         }
00145     
00146         if ( *rowfourin == 1 && !addedfour){
00147             keyspressed.push_back(12);   
00148             addedfour = true;
00149         }
00150     }
00151     
00152     std::sort(keyspressed.begin(), keyspressed.end());          
00153     return keyspressed; 
00154 }