1 | #ifndef KEYPAD_H
|
2 | #define KEYPAD_H
|
3 |
|
4 | #include "Key.h"
|
5 | #include "PCF8574.h"
|
6 | PCF8574 PCF(0x20);
|
7 |
|
8 | // bperrybap - Thanks for a well reasoned argument and the following macro(s).
|
9 | // See http://arduino.cc/forum/index.php/topic,142041.msg1069480.html#msg1069480
|
10 | #ifndef INPUT_PULLUP
|
11 | #warning "Using pinMode() INPUT_PULLUP AVR emulation"
|
12 | #define INPUT_PULLUP 0x2
|
13 | #define pinMode(_pin, _mode) _mypinMode(_pin, _mode)
|
14 | #define _mypinMode(_pin, _mode) \
|
15 | do { \
|
16 | if(_mode == INPUT_PULLUP) \
|
17 | pinMode(_pin, INPUT); \
|
18 | PCF.write(_pin, 1); \
|
19 | if(_mode != INPUT_PULLUP) \
|
20 | pinMode(_pin, _mode); \
|
21 | }while(0)
|
22 | #endif
|
23 |
|
24 |
|
25 | #define OPEN LOW
|
26 | #define CLOSED HIGH
|
27 |
|
28 | typedef char KeypadEvent;
|
29 | typedef unsigned int uint;
|
30 | typedef unsigned long ulong;
|
31 |
|
32 | // Made changes according to this post http://arduino.cc/forum/index.php?topic=58337.0
|
33 | // by Nick Gammon. Thanks for the input Nick. It actually saved 78 bytes for me. :)
|
34 | typedef struct {
|
35 | byte rows;
|
36 | byte columns;
|
37 | } KeypadSize;
|
38 |
|
39 | #define LIST_MAX 10 // Max number of keys on the active list.
|
40 | #define MAPSIZE 10 // MAPSIZE is the number of rows (times 16 columns)
|
41 | #define makeKeymap(x) ((char*)x)
|
42 |
|
43 |
|
44 | //class Keypad : public Key, public HAL_obj {
|
45 | class Keypad : public Key {
|
46 | public:
|
47 |
|
48 | Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
|
49 |
|
50 | virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }
|
51 | virtual void pin_write(byte pinNum, boolean level) { PCF.write(pinNum, level); }
|
52 | virtual int pin_read(byte pinNum) { return PCF.read(pinNum); }
|
53 |
|
54 | uint bitMap[MAPSIZE]; // 10 row x 16 column array of bits. Except Due which has 32 columns.
|
55 | Key key[LIST_MAX];
|
56 | unsigned long holdTimer;
|
57 |
|
58 | char getKey();
|
59 | bool getKeys();
|
60 | KeyState getState();
|
61 | void begin(char *userKeymap);
|
62 | bool isPressed(char keyChar);
|
63 | void setDebounceTime(uint);
|
64 | void setHoldTime(uint);
|
65 | void addEventListener(void (*listener)(char));
|
66 | int findInList(char keyChar);
|
67 | int findInList(int keyCode);
|
68 | char waitForKey();
|
69 | bool keyStateChanged();
|
70 | byte numKeys();
|
71 |
|
72 | private:
|
73 | unsigned long startTime;
|
74 | char *keymap;
|
75 | byte *rowPins;
|
76 | byte *columnPins;
|
77 | KeypadSize sizeKpd;
|
78 | uint debounceTime;
|
79 | uint holdTime;
|
80 | bool single_key;
|
81 |
|
82 | void scanKeys();
|
83 | bool updateList();
|
84 | void nextKeyState(byte n, boolean button);
|
85 | void transitionTo(byte n, KeyState nextState);
|
86 | void (*keypadEventListener)(char);
|
87 | };
|
88 |
|
89 | #endif
|