Tomislav Grgić / Keypad
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     Keypad::Keypad(PinName row1, PinName row2, PinName row3, PinName row4,PinName col1, PinName col2, PinName col3, PinName col4)
00005         : _col(col1,col2,col3,col4), _row(row1,row2,row3,row4){}
00006         
00007     Timer t;
00008     
00009     int x,y;
00010     char keypad_matrica[4][4] = {{'1','2','3'},
00011                                  {'4','5','6'},
00012                                  {'7','8','9'},
00013                                  {'a','0','b'}};
00014                                  
00015         
00016     void Keypad::visokoStanje()
00017     {
00018         _col = 0b1111;
00019     }
00020 
00021     void Keypad::prviStupac()
00022     {
00023         _col = 0b1110;
00024     }
00025 
00026     void Keypad::drugiStupac()
00027     {
00028         _col = 0b1101;
00029     }
00030 
00031     void Keypad::treciStupac()
00032     {
00033         _col = 0b1011;
00034     }
00035     
00036      void Keypad::cetvrtiStupac()
00037     {
00038         _col = 0b0111;
00039     }
00040 
00041 void Keypad::colActivate(int state){
00042     switch(state){
00043         case 0:
00044         prviStupac();
00045         break;
00046         
00047         case 1:
00048         drugiStupac();
00049         break;
00050         
00051         case 2:
00052         treciStupac();
00053         break;
00054         }
00055     }    
00056     
00057 char Keypad::keyscan()
00058     {
00059         t.stop();
00060         t.reset();
00061         char out = 'a';
00062         bool state = true;
00063         visokoStanje();
00064         wait_ms(5);
00065         t.start();
00066 
00067         while(state == true && t.read()<30) {
00068             
00069         for(x = 0; x <4; x++){    
00070             colActivate(x);
00071             wait_ms(5);
00072             if(_row == 0b1110) {
00073                 y = 0;
00074                 state = false;
00075             } else if (_row == 0b1101) {
00076                 y = 1;
00077                 state = false;
00078             } else if (_row == 0b1011) {
00079                 y = 2;
00080                 state = false;
00081             } else if (_row == 0b0111) {
00082                 y = 3;
00083                 state = false;
00084             }
00085         }
00086             
00087     }
00088         out = keypad_matrica[x][y];
00089         
00090         if(t.read()> 30){
00091             t.stop();
00092             t.reset();
00093             out ='c';
00094             return out;
00095             }
00096 
00097         visokoStanje();
00098         return out;
00099     }