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

Committer:
Hotboards
Date:
Tue Feb 09 03:25:28 2016 +0000
Revision:
0:4ca112f96484
keyoad first release, is not tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:4ca112f96484 1 /*
Hotboards 0:4ca112f96484 2 ||
Hotboards 0:4ca112f96484 3 || @file Hotboards_Keypad.h
Hotboards 0:4ca112f96484 4 || @version 3.2
Hotboards 0:4ca112f96484 5 || @ported by Diego (Hotboards)
Hotboards 0:4ca112f96484 6 || This Keypad fork library allow to work with keyboard that
Hotboards 0:4ca112f96484 7 || has physical pull-ups on columns (rather than rows)
Hotboards 0:4ca112f96484 8 ||
Hotboards 0:4ca112f96484 9 || Keypad library originaly develop by:
Hotboards 0:4ca112f96484 10 || @author Mark Stanley, Alexander Brevig
Hotboards 0:4ca112f96484 11 || @contact mstanley@technologist.com, alexanderbrevig@gmail.com
Hotboards 0:4ca112f96484 12 ||
Hotboards 0:4ca112f96484 13 || @description
Hotboards 0:4ca112f96484 14 || | This library provides a simple interface for using matrix
Hotboards 0:4ca112f96484 15 || | keypads. It supports multiple keypresses while maintaining
Hotboards 0:4ca112f96484 16 || | backwards compatibility with the old single key library.
Hotboards 0:4ca112f96484 17 || | It also supports user selectable pins and definable keymaps.
Hotboards 0:4ca112f96484 18 || #
Hotboards 0:4ca112f96484 19 ||
Hotboards 0:4ca112f96484 20 || @license
Hotboards 0:4ca112f96484 21 || | This library is free software; you can redistribute it and/or
Hotboards 0:4ca112f96484 22 || | modify it under the terms of the GNU Lesser General Public
Hotboards 0:4ca112f96484 23 || | License as published by the Free Software Foundation; version
Hotboards 0:4ca112f96484 24 || | 2.1 of the License.
Hotboards 0:4ca112f96484 25 || |
Hotboards 0:4ca112f96484 26 || | This library is distributed in the hope that it will be useful,
Hotboards 0:4ca112f96484 27 || | but WITHOUT ANY WARRANTY; without even the implied warranty of
Hotboards 0:4ca112f96484 28 || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Hotboards 0:4ca112f96484 29 || | Lesser General Public License for more details.
Hotboards 0:4ca112f96484 30 || |
Hotboards 0:4ca112f96484 31 || | You should have received a copy of the GNU Lesser General Public
Hotboards 0:4ca112f96484 32 || | License along with this library; if not, write to the Free Software
Hotboards 0:4ca112f96484 33 || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Hotboards 0:4ca112f96484 34 || #
Hotboards 0:4ca112f96484 35 ||
Hotboards 0:4ca112f96484 36 */
Hotboards 0:4ca112f96484 37 #include "Hotboards_keypad.h"
Hotboards 0:4ca112f96484 38
Hotboards 0:4ca112f96484 39 // <<constructor>> Allows custom keymap, pin configuration, and keypad sizes.
Hotboards 0:4ca112f96484 40 Keypad::Keypad(char *userKeymap, DigitalInOut *row, DigitalInOut *col, uint8_t numRows, uint8_t numCols) {
Hotboards 0:4ca112f96484 41 rowPins = row;
Hotboards 0:4ca112f96484 42 columnPins = col;
Hotboards 0:4ca112f96484 43 sizeKpd.rows = numRows;
Hotboards 0:4ca112f96484 44 sizeKpd.columns = numCols;
Hotboards 0:4ca112f96484 45
Hotboards 0:4ca112f96484 46 begin(userKeymap);
Hotboards 0:4ca112f96484 47
Hotboards 0:4ca112f96484 48 setDebounceTime(10);
Hotboards 0:4ca112f96484 49 setHoldTime(500);
Hotboards 0:4ca112f96484 50 keypadEventListener = 0;
Hotboards 0:4ca112f96484 51
Hotboards 0:4ca112f96484 52 startTime = 0;
Hotboards 0:4ca112f96484 53 debounce.start();
Hotboards 0:4ca112f96484 54 single_key = false;
Hotboards 0:4ca112f96484 55 }
Hotboards 0:4ca112f96484 56
Hotboards 0:4ca112f96484 57 // Let the user define a keymap - assume the same row/column count as defined in constructor
Hotboards 0:4ca112f96484 58 void Keypad::begin(char *userKeymap) {
Hotboards 0:4ca112f96484 59 keymap = userKeymap;
Hotboards 0:4ca112f96484 60 }
Hotboards 0:4ca112f96484 61
Hotboards 0:4ca112f96484 62 // Returns a single key only. Retained for backwards compatibility.
Hotboards 0:4ca112f96484 63 char Keypad::getKey() {
Hotboards 0:4ca112f96484 64 single_key = true;
Hotboards 0:4ca112f96484 65
Hotboards 0:4ca112f96484 66 if (getKeys() && key[0].stateChanged && (key[0].kstate==PRESSED))
Hotboards 0:4ca112f96484 67 return key[0].kchar;
Hotboards 0:4ca112f96484 68
Hotboards 0:4ca112f96484 69 single_key = false;
Hotboards 0:4ca112f96484 70
Hotboards 0:4ca112f96484 71 return NO_KEY;
Hotboards 0:4ca112f96484 72 }
Hotboards 0:4ca112f96484 73
Hotboards 0:4ca112f96484 74 // Populate the key list.
Hotboards 0:4ca112f96484 75 bool Keypad::getKeys() {
Hotboards 0:4ca112f96484 76 bool keyActivity = false;
Hotboards 0:4ca112f96484 77
Hotboards 0:4ca112f96484 78 // Limit how often the keypad is scanned. This makes the loop() run 10 times as fast.
Hotboards 0:4ca112f96484 79 if ( (debounce.read_ms()-startTime)>debounceTime ) {
Hotboards 0:4ca112f96484 80 scanKeys();
Hotboards 0:4ca112f96484 81 keyActivity = updateList();
Hotboards 0:4ca112f96484 82 startTime = debounce.read_ms();
Hotboards 0:4ca112f96484 83 }
Hotboards 0:4ca112f96484 84
Hotboards 0:4ca112f96484 85 return keyActivity;
Hotboards 0:4ca112f96484 86 }
Hotboards 0:4ca112f96484 87
Hotboards 0:4ca112f96484 88 // Private : Hardware scan
Hotboards 0:4ca112f96484 89 void Keypad::scanKeys() {
Hotboards 0:4ca112f96484 90 // Re-intialize the row pins. Allows sharing these pins with other hardware.
Hotboards 0:4ca112f96484 91 for (uint8_t r=0; r<sizeKpd.columns; r++) {
Hotboards 0:4ca112f96484 92 columnPins[r].input();
Hotboards 0:4ca112f96484 93 }
Hotboards 0:4ca112f96484 94
Hotboards 0:4ca112f96484 95 // bitMap stores ALL the keys that are being pressed.
Hotboards 0:4ca112f96484 96 for (uint8_t r=0; r<sizeKpd.rows; r++) {
Hotboards 0:4ca112f96484 97 rowPins[r].output();
Hotboards 0:4ca112f96484 98 rowPins[r] = 0; // Begin column pulse output.
Hotboards 0:4ca112f96484 99 for (uint8_t c=0; c<sizeKpd.columns; c++) {
Hotboards 0:4ca112f96484 100 if(!columnPins[c]) bitMap[c] |= ( 1<< r );
Hotboards 0:4ca112f96484 101 //bitWrite(bitMap[c], r, !digitalRead(columnPins[c])); // keypress is active low so invert to high.
Hotboards 0:4ca112f96484 102 }
Hotboards 0:4ca112f96484 103 // Set pin to high impedance input. Effectively ends column pulse.
Hotboards 0:4ca112f96484 104 rowPins[r] = 1;
Hotboards 0:4ca112f96484 105 rowPins[r].input();
Hotboards 0:4ca112f96484 106 }
Hotboards 0:4ca112f96484 107 }
Hotboards 0:4ca112f96484 108
Hotboards 0:4ca112f96484 109 // Manage the list without rearranging the keys. Returns true if any keys on the list changed state.
Hotboards 0:4ca112f96484 110 bool Keypad::updateList() {
Hotboards 0:4ca112f96484 111
Hotboards 0:4ca112f96484 112 bool anyActivity = false;
Hotboards 0:4ca112f96484 113
Hotboards 0:4ca112f96484 114 // Delete any IDLE keys
Hotboards 0:4ca112f96484 115 for (uint8_t i=0; i<LIST_MAX; i++) {
Hotboards 0:4ca112f96484 116 if (key[i].kstate==IDLE) {
Hotboards 0:4ca112f96484 117 key[i].kchar = NO_KEY;
Hotboards 0:4ca112f96484 118 key[i].kcode = -1;
Hotboards 0:4ca112f96484 119 key[i].stateChanged = false;
Hotboards 0:4ca112f96484 120 }
Hotboards 0:4ca112f96484 121 }
Hotboards 0:4ca112f96484 122
Hotboards 0:4ca112f96484 123 // Add new keys to empty slots in the key list.
Hotboards 0:4ca112f96484 124 for (uint8_t c=0; c<sizeKpd.columns; c++) {
Hotboards 0:4ca112f96484 125 for (uint8_t r=0; r<sizeKpd.rows; r++) {
Hotboards 0:4ca112f96484 126 bool button = (bitMap[r] >> c) & 0x01; //bitRead(bitMap[r],c);
Hotboards 0:4ca112f96484 127 char keyChar = keymap[c * sizeKpd.rows + r];
Hotboards 0:4ca112f96484 128 int keyCode = r * sizeKpd.columns + c;
Hotboards 0:4ca112f96484 129 int idx = findInList (keyCode);
Hotboards 0:4ca112f96484 130 // Key is already on the list so set its next state.
Hotboards 0:4ca112f96484 131 if (idx > -1) {
Hotboards 0:4ca112f96484 132 nextKeyState(idx, button);
Hotboards 0:4ca112f96484 133 }
Hotboards 0:4ca112f96484 134 // Key is NOT on the list so add it.
Hotboards 0:4ca112f96484 135 if ((idx == -1) && button) {
Hotboards 0:4ca112f96484 136 for (uint8_t i=0; i<LIST_MAX; i++) {
Hotboards 0:4ca112f96484 137 if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list.
Hotboards 0:4ca112f96484 138 key[i].kchar = keyChar;
Hotboards 0:4ca112f96484 139 key[i].kcode = keyCode;
Hotboards 0:4ca112f96484 140 key[i].kstate = IDLE; // Keys NOT on the list have an initial state of IDLE.
Hotboards 0:4ca112f96484 141 nextKeyState (i, button);
Hotboards 0:4ca112f96484 142 break; // Don't fill all the empty slots with the same key.
Hotboards 0:4ca112f96484 143 }
Hotboards 0:4ca112f96484 144 }
Hotboards 0:4ca112f96484 145 }
Hotboards 0:4ca112f96484 146 }
Hotboards 0:4ca112f96484 147 }
Hotboards 0:4ca112f96484 148
Hotboards 0:4ca112f96484 149 // Report if the user changed the state of any key.
Hotboards 0:4ca112f96484 150 for (uint8_t i=0; i<LIST_MAX; i++) {
Hotboards 0:4ca112f96484 151 if (key[i].stateChanged) anyActivity = true;
Hotboards 0:4ca112f96484 152 }
Hotboards 0:4ca112f96484 153
Hotboards 0:4ca112f96484 154 return anyActivity;
Hotboards 0:4ca112f96484 155 }
Hotboards 0:4ca112f96484 156
Hotboards 0:4ca112f96484 157 // Private
Hotboards 0:4ca112f96484 158 // This function is a state machine but is also used for debouncing the keys.
Hotboards 0:4ca112f96484 159 void Keypad::nextKeyState(uint8_t idx, bool button) {
Hotboards 0:4ca112f96484 160 key[idx].stateChanged = false;
Hotboards 0:4ca112f96484 161
Hotboards 0:4ca112f96484 162 switch (key[idx].kstate) {
Hotboards 0:4ca112f96484 163 case IDLE:
Hotboards 0:4ca112f96484 164 if (button==CLOSED) {
Hotboards 0:4ca112f96484 165 transitionTo (idx, PRESSED);
Hotboards 0:4ca112f96484 166 holdTimer = debounce.read_ms(); } // Get ready for next HOLD state.
Hotboards 0:4ca112f96484 167 break;
Hotboards 0:4ca112f96484 168 case PRESSED:
Hotboards 0:4ca112f96484 169 if ((debounce.read_ms()-holdTimer)>holdTime) // Waiting for a key HOLD...
Hotboards 0:4ca112f96484 170 transitionTo (idx, HOLD);
Hotboards 0:4ca112f96484 171 else if (button==OPEN) // or for a key to be RELEASED.
Hotboards 0:4ca112f96484 172 transitionTo (idx, RELEASED);
Hotboards 0:4ca112f96484 173 break;
Hotboards 0:4ca112f96484 174 case HOLD:
Hotboards 0:4ca112f96484 175 if (button==OPEN)
Hotboards 0:4ca112f96484 176 transitionTo (idx, RELEASED);
Hotboards 0:4ca112f96484 177 break;
Hotboards 0:4ca112f96484 178 case RELEASED:
Hotboards 0:4ca112f96484 179 transitionTo (idx, IDLE);
Hotboards 0:4ca112f96484 180 break;
Hotboards 0:4ca112f96484 181 }
Hotboards 0:4ca112f96484 182 }
Hotboards 0:4ca112f96484 183
Hotboards 0:4ca112f96484 184 // New in 2.1
Hotboards 0:4ca112f96484 185 bool Keypad::isPressed(char keyChar) {
Hotboards 0:4ca112f96484 186 for (uint8_t i=0; i<LIST_MAX; i++) {
Hotboards 0:4ca112f96484 187 if ( key[i].kchar == keyChar ) {
Hotboards 0:4ca112f96484 188 if ( (key[i].kstate == PRESSED) && key[i].stateChanged )
Hotboards 0:4ca112f96484 189 return true;
Hotboards 0:4ca112f96484 190 }
Hotboards 0:4ca112f96484 191 }
Hotboards 0:4ca112f96484 192 return false; // Not pressed.
Hotboards 0:4ca112f96484 193 }
Hotboards 0:4ca112f96484 194
Hotboards 0:4ca112f96484 195 // Search by character for a key in the list of active keys.
Hotboards 0:4ca112f96484 196 // Returns -1 if not found or the index into the list of active keys.
Hotboards 0:4ca112f96484 197 int Keypad::findInList (char keyChar) {
Hotboards 0:4ca112f96484 198 for (uint8_t i=0; i<LIST_MAX; i++) {
Hotboards 0:4ca112f96484 199 if (key[i].kchar == keyChar) {
Hotboards 0:4ca112f96484 200 return i;
Hotboards 0:4ca112f96484 201 }
Hotboards 0:4ca112f96484 202 }
Hotboards 0:4ca112f96484 203 return -1;
Hotboards 0:4ca112f96484 204 }
Hotboards 0:4ca112f96484 205
Hotboards 0:4ca112f96484 206 // Search by code for a key in the list of active keys.
Hotboards 0:4ca112f96484 207 // Returns -1 if not found or the index into the list of active keys.
Hotboards 0:4ca112f96484 208 int Keypad::findInList (int keyCode) {
Hotboards 0:4ca112f96484 209 for (uint8_t i=0; i<LIST_MAX; i++) {
Hotboards 0:4ca112f96484 210 if (key[i].kcode == keyCode) {
Hotboards 0:4ca112f96484 211 return i;
Hotboards 0:4ca112f96484 212 }
Hotboards 0:4ca112f96484 213 }
Hotboards 0:4ca112f96484 214 return -1;
Hotboards 0:4ca112f96484 215 }
Hotboards 0:4ca112f96484 216
Hotboards 0:4ca112f96484 217 // New in 2.0
Hotboards 0:4ca112f96484 218 char Keypad::waitForKey() {
Hotboards 0:4ca112f96484 219 char waitKey = NO_KEY;
Hotboards 0:4ca112f96484 220 while( (waitKey = getKey()) == NO_KEY ); // Block everything while waiting for a keypress.
Hotboards 0:4ca112f96484 221 return waitKey;
Hotboards 0:4ca112f96484 222 }
Hotboards 0:4ca112f96484 223
Hotboards 0:4ca112f96484 224 // Backwards compatibility function.
Hotboards 0:4ca112f96484 225 KeyState Keypad::getState() {
Hotboards 0:4ca112f96484 226 return key[0].kstate;
Hotboards 0:4ca112f96484 227 }
Hotboards 0:4ca112f96484 228
Hotboards 0:4ca112f96484 229 // The end user can test for any changes in state before deciding
Hotboards 0:4ca112f96484 230 // if any variables, etc. needs to be updated in their code.
Hotboards 0:4ca112f96484 231 bool Keypad::keyStateChanged() {
Hotboards 0:4ca112f96484 232 return key[0].stateChanged;
Hotboards 0:4ca112f96484 233 }
Hotboards 0:4ca112f96484 234
Hotboards 0:4ca112f96484 235 // The number of keys on the key list, key[LIST_MAX], equals the number
Hotboards 0:4ca112f96484 236 // of bytes in the key list divided by the number of bytes in a Key object.
Hotboards 0:4ca112f96484 237 uint8_t Keypad::numKeys() {
Hotboards 0:4ca112f96484 238 return sizeof(key)/sizeof(Key);
Hotboards 0:4ca112f96484 239 }
Hotboards 0:4ca112f96484 240
Hotboards 0:4ca112f96484 241 // Minimum debounceTime is 1 mS. Any lower *will* slow down the loop().
Hotboards 0:4ca112f96484 242 void Keypad::setDebounceTime(uint debounce) {
Hotboards 0:4ca112f96484 243 debounce<1 ? debounceTime=1 : debounceTime=debounce;
Hotboards 0:4ca112f96484 244 }
Hotboards 0:4ca112f96484 245
Hotboards 0:4ca112f96484 246 void Keypad::setHoldTime(uint hold) {
Hotboards 0:4ca112f96484 247 holdTime = hold;
Hotboards 0:4ca112f96484 248 }
Hotboards 0:4ca112f96484 249
Hotboards 0:4ca112f96484 250 void Keypad::addEventListener(void (*listener)(char)){
Hotboards 0:4ca112f96484 251 keypadEventListener = listener;
Hotboards 0:4ca112f96484 252 }
Hotboards 0:4ca112f96484 253
Hotboards 0:4ca112f96484 254 void Keypad::transitionTo(uint8_t idx, KeyState nextState) {
Hotboards 0:4ca112f96484 255 key[idx].kstate = nextState;
Hotboards 0:4ca112f96484 256 key[idx].stateChanged = true;
Hotboards 0:4ca112f96484 257
Hotboards 0:4ca112f96484 258 // Sketch used the getKey() function.
Hotboards 0:4ca112f96484 259 // Calls keypadEventListener only when the first key in slot 0 changes state.
Hotboards 0:4ca112f96484 260 if (single_key) {
Hotboards 0:4ca112f96484 261 if ( (keypadEventListener!=NULL) && (idx==0) ) {
Hotboards 0:4ca112f96484 262 keypadEventListener(key[0].kchar);
Hotboards 0:4ca112f96484 263 }
Hotboards 0:4ca112f96484 264 }
Hotboards 0:4ca112f96484 265 // Sketch used the getKeys() function.
Hotboards 0:4ca112f96484 266 // Calls keypadEventListener on any key that changes state.
Hotboards 0:4ca112f96484 267 else {
Hotboards 0:4ca112f96484 268 if (keypadEventListener!=NULL) {
Hotboards 0:4ca112f96484 269 keypadEventListener(key[idx].kchar);
Hotboards 0:4ca112f96484 270 }
Hotboards 0:4ca112f96484 271 }
Hotboards 0:4ca112f96484 272 }
Hotboards 0:4ca112f96484 273
Hotboards 0:4ca112f96484 274 /*
Hotboards 0:4ca112f96484 275 || @changelog
Hotboards 0:4ca112f96484 276 || | 3.1 2013-01-15 - Mark Stanley : Fixed missing RELEASED & IDLE status when using a single key.
Hotboards 0:4ca112f96484 277 || | 3.0 2012-07-12 - Mark Stanley : Made library multi-keypress by default. (Backwards compatible)
Hotboards 0:4ca112f96484 278 || | 3.0 2012-07-12 - Mark Stanley : Modified pin functions to support Keypad_I2C
Hotboards 0:4ca112f96484 279 || | 3.0 2012-07-12 - Stanley & Young : Removed static variables. Fix for multiple keypad objects.
Hotboards 0:4ca112f96484 280 || | 3.0 2012-07-12 - Mark Stanley : Fixed bug that caused shorted pins when pressing multiple keys.
Hotboards 0:4ca112f96484 281 || | 2.0 2011-12-29 - Mark Stanley : Added waitForKey().
Hotboards 0:4ca112f96484 282 || | 2.0 2011-12-23 - Mark Stanley : Added the public function keyStateChanged().
Hotboards 0:4ca112f96484 283 || | 2.0 2011-12-23 - Mark Stanley : Added the private function scanKeys().
Hotboards 0:4ca112f96484 284 || | 2.0 2011-12-23 - Mark Stanley : Moved the Finite State Machine into the function getKeyState().
Hotboards 0:4ca112f96484 285 || | 2.0 2011-12-23 - Mark Stanley : Removed the member variable lastUdate. Not needed after rewrite.
Hotboards 0:4ca112f96484 286 || | 1.8 2011-11-21 - Mark Stanley : Added decision logic to compile WProgram.h or Arduino.h
Hotboards 0:4ca112f96484 287 || | 1.8 2009-07-08 - Alexander Brevig : No longer uses arrays
Hotboards 0:4ca112f96484 288 || | 1.7 2009-06-18 - Alexander Brevig : Every time a state changes the keypadEventListener will trigger, if set.
Hotboards 0:4ca112f96484 289 || | 1.7 2009-06-18 - Alexander Brevig : Added setDebounceTime. setHoldTime specifies the amount of
Hotboards 0:4ca112f96484 290 || | microseconds before a HOLD state triggers
Hotboards 0:4ca112f96484 291 || | 1.7 2009-06-18 - Alexander Brevig : Added transitionTo
Hotboards 0:4ca112f96484 292 || | 1.6 2009-06-15 - Alexander Brevig : Added getState() and state variable
Hotboards 0:4ca112f96484 293 || | 1.5 2009-05-19 - Alexander Brevig : Added setHoldTime()
Hotboards 0:4ca112f96484 294 || | 1.4 2009-05-15 - Alexander Brevig : Added addEventListener
Hotboards 0:4ca112f96484 295 || | 1.3 2009-05-12 - Alexander Brevig : Added lastUdate, in order to do simple debouncing
Hotboards 0:4ca112f96484 296 || | 1.2 2009-05-09 - Alexander Brevig : Changed getKey()
Hotboards 0:4ca112f96484 297 || | 1.1 2009-04-28 - Alexander Brevig : Modified API, and made variables private
Hotboards 0:4ca112f96484 298 || | 1.0 2007-XX-XX - Mark Stanley : Initial Release
Hotboards 0:4ca112f96484 299 || #
Hotboards 0:4ca112f96484 300 */