A keypad library used in one of my projects.

Files at this revision

API Documentation at this revision

Comitter:
tgrgic1
Date:
Mon May 03 22:17:39 2021 +0000
Parent:
0:1ae25267abc0
Commit message:
2

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
--- a/Keypad.cpp	Mon May 03 21:40:24 2021 +0000
+++ b/Keypad.cpp	Mon May 03 22:17:39 2021 +0000
@@ -5,6 +5,13 @@
         : _col(col1,col2,col3,col4), _row(row1,row2,row3,row4){}
         
     Timer t;
+    
+    int x,y;
+    char keypad_matrica[4][4] = {{'1','2','3'},
+                                 {'4','5','6'},
+                                 {'7','8','9'},
+                                 {'a','0','b'}};
+                                 
         
     void Keypad::visokoStanje()
     {
@@ -31,7 +38,23 @@
         _col = 0b0111;
     }
 
-    char Keypad::keyscan()
+void Keypad::colActivate(int state){
+    switch(state){
+        case 0:
+        prviStupac();
+        break;
+        
+        case 1:
+        drugiStupac();
+        break;
+        
+        case 2:
+        treciStupac();
+        break;
+        }
+    }    
+    
+char Keypad::keyscan()
     {
         t.stop();
         t.reset();
@@ -42,56 +65,29 @@
         t.start();
 
         while(state == true && t.read()<30) {
-            prviStupac();
+            
+        for(x = 0; x <4; x++){    
+            colActivate(x);
             wait_ms(5);
             if(_row == 0b1110) {
-                out = '1';
+                y = 0;
                 state = false;
             } else if (_row == 0b1101) {
-                out = '4';
-                state = false;
-            } else if (_row == 0b1011) {
-                out = '7';
-                state = false;
-            } else if (_row == 0b0111) {
-                out = 'b';
-                state = false;
-            }
-
-            drugiStupac();
-            wait_ms(5);
-            if(_row == 0b1110) {
-                out = '2';
-                state = false;
-            } else if (_row == 0b1101) {
-                out = '5';
+                y = 1;
                 state = false;
             } else if (_row == 0b1011) {
-                out = '8';
+                y = 2;
                 state = false;
             } else if (_row == 0b0111) {
-                out = '0';
+                y = 3;
                 state = false;
             }
-
-            treciStupac();
-            wait_ms(5);
-            if(_row == 0b1110) {
-                out = '3';
-                state = false;
-            } else if (_row == 0b1101) {
-                out = '6';
-                state = false;
-            } else if (_row == 0b1011) {
-                out = '9';
-                state = false;
-            } else if (_row == 0b0111) {
-                state = false;
-                return out;
-            }
         }
+            
+    }
+        out = keypad_matrica[x][y];
         
-        if(t.read()>=29){
+        if(t.read()> 30){
             t.stop();
             t.reset();
             out ='c';
@@ -101,5 +97,3 @@
         visokoStanje();
         return out;
     }
-    
-
--- a/Keypad.h	Mon May 03 21:40:24 2021 +0000
+++ b/Keypad.h	Mon May 03 22:17:39 2021 +0000
@@ -19,6 +19,7 @@
     void prviStupac();
     void drugiStupac();
     void treciStupac();
+    void colActivate(int state);
     
 };