Sense keypresses from a 4x4 keypad A derivative ot he Hotboard_keypad library

Dependents:   26_Hotboards_MultiKey 26_Hotboards_EventKeypad

Fork of Hotboards_keypad by Hotboards MX

Revision:
2:e870110f753b
Parent:
1:975a5c527e8e
Child:
3:c88c922efd74
--- a/Hotboards_keypad.cpp	Wed Feb 17 01:58:30 2016 +0000
+++ b/Hotboards_keypad.cpp	Fri Mar 04 02:37:55 2016 +0000
@@ -36,6 +36,9 @@
 */
 #include "Hotboards_keypad.h"
 
+#define bitRead( var, bit )           (((var) >> (bit)) & 0x01)
+#define bitWrite( var, bit, val )     (val) ? (var) |= (1<<(bit)) : (var) &= ~(1<<(bit))
+
 // <<constructor>> Allows custom keymap, pin configuration, and keypad sizes.
 Keypad::Keypad(char *userKeymap, DigitalInOut *row, DigitalInOut *col, uint8_t numRows, uint8_t numCols) {
     rowPins = row;
@@ -88,20 +91,19 @@
 // Private : Hardware scan
 void Keypad::scanKeys() {
     // Re-intialize the row pins. Allows sharing these pins with other hardware.
-    for (uint8_t r=0; r<sizeKpd.columns; r++) {
-        columnPins[r].input();
+    for (uint8_t c=0; c<sizeKpd.columns; c++) {
+        columnPins[c].input();
     }
 
     // bitMap stores ALL the keys that are being pressed.
     for (uint8_t r=0; r<sizeKpd.rows; r++) {
         rowPins[r].output();
-        rowPins[r] = 0;  // Begin column pulse output.
+        rowPins[r].write( 0 );  // Begin column pulse output.
         for (uint8_t c=0; c<sizeKpd.columns; c++) {
-            if(!columnPins[c]) bitMap[c] |= ( 1<< r );
-            //bitWrite(bitMap[c], r, !digitalRead(columnPins[c]));  // keypress is active low so invert to high.
+            bitWrite(bitMap[c], r, !columnPins[c].read());  // keypress is active low so invert to high.
         }
         // Set pin to high impedance input. Effectively ends column pulse.
-        rowPins[r] = 1;
+        rowPins[r].write( 1 );
         rowPins[r].input();
     }
 }
@@ -123,7 +125,7 @@
     // Add new keys to empty slots in the key list.
     for (uint8_t c=0; c<sizeKpd.columns; c++) {
         for (uint8_t r=0; r<sizeKpd.rows; r++) {
-            bool button = (bitMap[r] >> c) & 0x01;  //bitRead(bitMap[r],c);
+            bool button = bitRead(bitMap[r],c);
             char keyChar = keymap[c * sizeKpd.rows + r];
             int keyCode = r * sizeKpd.columns + c;
             int idx = findInList (keyCode);