/* || || @file Keypad.cpp || @version 3.1 || @author Mark Stanley, Alexander Brevig || @contact mstanley@technologist.com, alexanderbrevig@gmail.com || || @description || | This library provides a simple interface for using matrix || | keypads. It supports multiple keypresses while maintaining || | backwards compatibility with the old single key library. || | It also supports user selectable pins and definable keymaps. || # || || @license || | This library is free software; you can redistribute it and/or || | modify it under the terms of the GNU Lesser General Public || | License as published by the Free Software Foundation; version || | 2.1 of the License. || | || | This library is distributed in the hope that it will be useful, || | but WITHOUT ANY WARRANTY; without even the implied warranty of || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU || | Lesser General Public License for more details. || | || | You should have received a copy of the GNU Lesser General Public || | License along with this library; if not, write to the Free Software || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA || # || */ #include // <> Allows custom keymap, pin configuration, and keypad sizes. Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols) { rowPins = row; columnPins = col; sizeKpd.rows = numRows; sizeKpd.columns = numCols; begin(userKeymap); setDebounceTime(10); setHoldTime(500); keypadEventListener = 0; startTime = 0; single_key = false; } // Let the user define a keymap - assume the same row/column count as defined in constructor void Keypad::begin(char *userKeymap) { keymap = userKeymap; } // Returns a single key only. Retained for backwards compatibility. char Keypad::getKey() { single_key = true; if (getKeys() && key[0].stateChanged && (key[0].kstate==PRESSED)) return key[0].kchar; single_key = false; return NO_KEY; } // Populate the key list. bool Keypad::getKeys() { bool keyActivity = false; // Limit how often the keypad is scanned. This makes the loop() run 10 times as fast. if ( (millis()-startTime)>debounceTime ) { scanKeys(); keyActivity = updateList(); startTime = millis(); } return keyActivity; } // Private : Hardware scan void Keypad::scanKeys() { // Re-intialize the row pins. Allows sharing these pins with other hardware. for (byte r=0; r -1) { nextKeyState(idx, button); } // Key is NOT on the list so add it. if ((idx == -1) && button) { for (byte i=0; iholdTime) // Waiting for a key HOLD... transitionTo (idx, HOLD); else if (button==OPEN) // or for a key to be RELEASED. transitionTo (idx, RELEASED); break; case HOLD: if (button==OPEN) transitionTo (idx, RELEASED); break; case RELEASED: transitionTo (idx, IDLE); break; } } // New in 2.1 bool Keypad::isPressed(char keyChar) { for (byte i=0; i