arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

Keypad.h (5612B)


      1 /*
      2 ||
      3 || @file Keypad.h
      4 || @version 3.1
      5 || @author Mark Stanley, Alexander Brevig
      6 || @contact mstanley@technologist.com, alexanderbrevig@gmail.com
      7 ||
      8 || @description
      9 || | This library provides a simple interface for using matrix
     10 || | keypads. It supports multiple keypresses while maintaining
     11 || | backwards compatibility with the old single key library.
     12 || | It also supports user selectable pins and definable keymaps.
     13 || #
     14 ||
     15 || @license
     16 || | This library is free software; you can redistribute it and/or
     17 || | modify it under the terms of the GNU Lesser General Public
     18 || | License as published by the Free Software Foundation; version
     19 || | 2.1 of the License.
     20 || |
     21 || | This library is distributed in the hope that it will be useful,
     22 || | but WITHOUT ANY WARRANTY; without even the implied warranty of
     23 || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     24 || | Lesser General Public License for more details.
     25 || |
     26 || | You should have received a copy of the GNU Lesser General Public
     27 || | License along with this library; if not, write to the Free Software
     28 || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     29 || #
     30 ||
     31 */
     32 
     33 #ifndef KEYPAD_H
     34 #define KEYPAD_H
     35 
     36 #include "Key.h"
     37 
     38 // bperrybap - Thanks for a well reasoned argument and the following macro(s).
     39 // See http://arduino.cc/forum/index.php/topic,142041.msg1069480.html#msg1069480
     40 #ifndef INPUT_PULLUP
     41 #warning "Using  pinMode() INPUT_PULLUP AVR emulation"
     42 #define INPUT_PULLUP 0x2
     43 #define pinMode(_pin, _mode) _mypinMode(_pin, _mode)
     44 #define _mypinMode(_pin, _mode)  \
     45 do {							 \
     46 	if(_mode == INPUT_PULLUP)	 \
     47 		pinMode(_pin, INPUT);	 \
     48 		digitalWrite(_pin, 1);	 \
     49 	if(_mode != INPUT_PULLUP)	 \
     50 		pinMode(_pin, _mode);	 \
     51 }while(0)
     52 #endif
     53 
     54 
     55 #define OPEN LOW
     56 #define CLOSED HIGH
     57 
     58 typedef char KeypadEvent;
     59 typedef unsigned int uint;
     60 typedef unsigned long ulong;
     61 
     62 // Made changes according to this post http://arduino.cc/forum/index.php?topic=58337.0
     63 // by Nick Gammon. Thanks for the input Nick. It actually saved 78 bytes for me. :)
     64 typedef struct {
     65     byte rows;
     66     byte columns;
     67 } KeypadSize;
     68 
     69 #define LIST_MAX 10		// Max number of keys on the active list.
     70 #define MAPSIZE 10		// MAPSIZE is the number of rows (times 16 columns)
     71 #define makeKeymap(x) ((char*)x)
     72 
     73 
     74 //class Keypad : public Key, public HAL_obj {
     75 class Keypad : public Key {
     76 public:
     77 
     78 	Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
     79 
     80 	virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }
     81 	virtual void pin_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }
     82 	virtual int  pin_read(byte pinNum) { return digitalRead(pinNum); }
     83 
     84 	uint bitMap[MAPSIZE];	// 10 row x 16 column array of bits. Except Due which has 32 columns.
     85 	Key key[LIST_MAX];
     86 	unsigned long holdTimer;
     87 
     88 	char getKey();
     89 	bool getKeys();
     90 	KeyState getState();
     91 	void begin(char *userKeymap);
     92 	bool isPressed(char keyChar);
     93 	void setDebounceTime(uint);
     94 	void setHoldTime(uint);
     95 	void addEventListener(void (*listener)(char));
     96 	int findInList(char keyChar);
     97 	int findInList(int keyCode);
     98 	char waitForKey();
     99 	bool keyStateChanged();
    100 	byte numKeys();
    101 
    102 private:
    103 	unsigned long startTime;
    104 	char *keymap;
    105     byte *rowPins;
    106     byte *columnPins;
    107 	KeypadSize sizeKpd;
    108 	uint debounceTime;
    109 	uint holdTime;
    110 	bool single_key;
    111 
    112 	void scanKeys();
    113 	bool updateList();
    114 	void nextKeyState(byte n, boolean button);
    115 	void transitionTo(byte n, KeyState nextState);
    116 	void (*keypadEventListener)(char);
    117 };
    118 
    119 #endif
    120 
    121 /*
    122 || @changelog
    123 || | 3.1 2013-01-15 - Mark Stanley     : Fixed missing RELEASED & IDLE status when using a single key.
    124 || | 3.0 2012-07-12 - Mark Stanley     : Made library multi-keypress by default. (Backwards compatible)
    125 || | 3.0 2012-07-12 - Mark Stanley     : Modified pin functions to support Keypad_I2C
    126 || | 3.0 2012-07-12 - Stanley & Young  : Removed static variables. Fix for multiple keypad objects.
    127 || | 3.0 2012-07-12 - Mark Stanley     : Fixed bug that caused shorted pins when pressing multiple keys.
    128 || | 2.0 2011-12-29 - Mark Stanley     : Added waitForKey().
    129 || | 2.0 2011-12-23 - Mark Stanley     : Added the public function keyStateChanged().
    130 || | 2.0 2011-12-23 - Mark Stanley     : Added the private function scanKeys().
    131 || | 2.0 2011-12-23 - Mark Stanley     : Moved the Finite State Machine into the function getKeyState().
    132 || | 2.0 2011-12-23 - Mark Stanley     : Removed the member variable lastUdate. Not needed after rewrite.
    133 || | 1.8 2011-11-21 - Mark Stanley     : Added test to determine which header file to compile,
    134 || |                                          WProgram.h or Arduino.h.
    135 || | 1.8 2009-07-08 - Alexander Brevig : No longer uses arrays
    136 || | 1.7 2009-06-18 - Alexander Brevig : This library is a Finite State Machine every time a state changes
    137 || |                                          the keypadEventListener will trigger, if set
    138 || | 1.7 2009-06-18 - Alexander Brevig : Added setDebounceTime setHoldTime specifies the amount of
    139 || |                                          microseconds before a HOLD state triggers
    140 || | 1.7 2009-06-18 - Alexander Brevig : Added transitionTo
    141 || | 1.6 2009-06-15 - Alexander Brevig : Added getState() and state variable
    142 || | 1.5 2009-05-19 - Alexander Brevig : Added setHoldTime()
    143 || | 1.4 2009-05-15 - Alexander Brevig : Added addEventListener
    144 || | 1.3 2009-05-12 - Alexander Brevig : Added lastUdate, in order to do simple debouncing
    145 || | 1.2 2009-05-09 - Alexander Brevig : Changed getKey()
    146 || | 1.1 2009-04-28 - Alexander Brevig : Modified API, and made variables private
    147 || | 1.0 2007-XX-XX - Mark Stanley : Initial Release
    148 || #
    149 */