Optimized polling keybolard reading

Dependents:   design_project

Files at this revision

API Documentation at this revision

Comitter:
fangoman91
Date:
Tue May 12 14:50:28 2015 +0000
Commit message:
Optimized Keyboard (3x4) polling based reading

Changed in this revision

Keypad3x4polling.cpp Show annotated file Show diff for this revision Revisions of this file
Keypad3x4polling.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Keypad3x4polling.cpp	Tue May 12 14:50:28 2015 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "Keypad3x4polling.h"
+
+using namespace mbed;
+
+Keypad::Keypad(PinName col1,PinName col2,PinName col3,PinName row1,PinName row2,PinName row3,PinName row4):_cols(col1,col2,col3),_rows(row1,row2,row3,row4)   
+{ 
+    _cols.mode(PullUp);
+    _rows.write(0);     
+}
+char Keypad::read()
+{
+    char key = 10;
+    _rows.write(14);//1110b
+    switch(_cols)
+    {
+        case 3: key = '3'; break;//011b
+        case 5: key = '2'; break;//101b
+        case 6: key = '1'; break;//110b
+    }
+    _rows.write(13);//1101b
+    switch(_cols)
+    {
+        case 3: key = '6'; break;
+        case 5: key = '5'; break;
+        case 6: key = '4'; break;
+    }
+    _rows.write(11);//1011b
+    switch(_cols)
+    {
+        case 3: key = '9'; break;
+        case 5: key = '8'; break;
+        case 6: key = '7'; break;
+    }
+    _rows.write(7);//0111b
+    switch(_cols)
+    {
+        case 3: key = '#'; break;
+        case 5: key = '0'; break;
+        case 6: key = '*'; break;
+    }
+    return key;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Keypad3x4polling.h	Tue May 12 14:50:28 2015 +0000
@@ -0,0 +1,45 @@
+#ifndef KEYPAD3x4POLLING_H
+#define KEYPAD3x4POLLING_H
+
+#include "BusOut.h"
+#include "BusIn.h"
+
+#define NO_PRESS 10
+
+namespace mbed
+{                  
+    class Keypad
+    {    
+        public:
+            Keypad(PinName col1,PinName col2,PinName col3,PinName row1,PinName row2,PinName row3,PinName row4);
+            char read();
+        protected:
+            BusIn _cols;    
+            BusOut _rows;
+    };
+}
+#endif
+
+/*SIMPLE EXAMPLE
+
+#include "mbed.h"
+#include "Keypad3x4polling.h"
+
+Serial pc(USBTX,USBRX);
+
+Keypad Keyboard(p17,p16,p15,p10,p9,p8,p7);
+
+int main() 
+{       
+    char button;  
+    pc.printf("Premi un tasto:");
+    while(1)
+    {
+        button = Keyboard.read();
+        if(button != NO_PRESS)
+            pc.putc(button);  
+        wait(0.2);             
+    }    
+}
+
+*/
\ No newline at end of file