forked from adafruit/Adafruit_Keypad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Adafruit_Keypad.cpp
240 lines (212 loc) · 7.85 KB
/
Adafruit_Keypad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "Adafruit_Keypad.h"
#define _KEY_PRESSED_POS (1)
#define _KEY_PRESSED (1UL << _KEY_PRESSED_POS)
#define _JUST_PRESSED_POS (2)
#define _JUST_PRESSED (1UL << _JUST_PRESSED_POS)
#define _JUST_RELEASED_POS (3)
#define _JUST_RELEASED (1UL << _JUST_RELEASED_POS)
#define _KEYPAD_SETTLING_DELAY 20
/**************************************************************************/
/*!
@brief default constructor
@param userKeymap a multidimensional array of key characters
@param row an array of GPIO pins that are connected to each row of the
keypad
@param col an array of GPIO pins that are connected to each column of the
keypad
@param numRows the number of rows on the keypad
@param numCols the number of columns on the keypad
*/
/**************************************************************************/
Adafruit_Keypad::Adafruit_Keypad(byte *userKeymap, byte *row, byte *col,
int numRows, int numCols, Adafruit_MCP23017 *exp) {
_userKeymap = userKeymap;
_row = row;
_col = col;
_numRows = numRows;
_numCols = numCols;
_keystates = NULL;
_exp = exp; //adding a gpio expander of typeMCP23017 - read for pins above 100 translate them by subtract 100 and use expander
}
/**************************************************************************/
/*!
@brief default destructor
*/
/**************************************************************************/
Adafruit_Keypad::~Adafruit_Keypad() {
if (_keystates != NULL) {
free((void *)_keystates);
}
}
/**************************************************************************/
/*!
@brief get the state of a key with the given name
@param key the name of the key to be checked
*/
/**************************************************************************/
volatile byte *Adafruit_Keypad::getKeyState(byte key) {
for (int i = 0; i < _numRows * _numCols; i++) {
if (_userKeymap[i] == key) {
return _keystates + i;
}
}
return NULL;
}
/**************************************************************************/
/*!
@brief read the array of switches and place any events in the buffer.
*/
/**************************************************************************/
void Adafruit_Keypad::tick() {
uint8_t evt;
for (int i = 0; i < _numCols; i++){
if(_col[i]>99) //read an expander gpio (>>99 the subtract 100 and use mcp.digitalWrite( ...
_exp->digitalWrite(_col[i]-100, HIGH);
else //read a normal gpio
digitalWrite(_col[i], HIGH);
}
int i = 0;
for (int c = 0; c < _numCols; c++) {
digitalWrite(_col[c], LOW);
delayMicroseconds(_KEYPAD_SETTLING_DELAY);
for (int r = 0; r < _numRows; r++) {
i = r * _numCols + c;
bool pressed;
if(_row[r]>99)
pressed = !_exp->digitalRead(_row[r]-100);
else
pressed = !digitalRead(_row[r]);
// Serial.print((int)pressed);
volatile byte *state = _keystates + i;
byte currentState = *state;
if (pressed && !(currentState & _KEY_PRESSED)) {
currentState |= (_JUST_PRESSED | _KEY_PRESSED);
evt = KEY_JUST_PRESSED;
_eventbuf.store_char(evt);
_eventbuf.store_char(*(_userKeymap + i));
_eventbuf.store_char(r);
_eventbuf.store_char(c);
} else if (!pressed && (currentState & _KEY_PRESSED)) {
currentState |= _JUST_RELEASED;
currentState &= ~(_KEY_PRESSED);
evt = KEY_JUST_RELEASED;
_eventbuf.store_char(evt);
_eventbuf.store_char(*(_userKeymap + i));
_eventbuf.store_char(r);
_eventbuf.store_char(c);
}
*state = currentState;
}
// Serial.println("");
if(_col[c]>99) //read an expander gpio (>>99 the subtract 100 and use mcp.digitalWrite( ...
_exp->digitalWrite(_col[c]-100, HIGH);
else //read a normal gpio
digitalWrite(_col[c], HIGH);
}
}
/**************************************************************************/
/*!
@brief set all the pin modes and set up variables.
*/
/**************************************************************************/
void Adafruit_Keypad::begin() {
_keystates = (volatile byte *)malloc(_numRows * _numCols);
memset((void *)_keystates, 0, _numRows * _numCols);
_exp->begin();
for (int i = 0; i < _numCols; i++) {
if(_col[i]>99){ //read an expander gpio (>>99 the subtract 100 and use mcp.digitalWrite( ...
_exp->pinMode(_col[i]-100, OUTPUT);
_exp->digitalWrite(_col[i]-100, HIGH);
} else {
pinMode(_col[i], OUTPUT);
digitalWrite(_col[i], HIGH);
}
}
for (int i = 0; i < _numRows; i++) {
if(_row[i]>99)
_exp->pinMode(_row[i]-100, INPUT_PULLUP);
else
pinMode(_row[i], INPUT_PULLUP);
}
}
/**************************************************************************/
/*!
@brief check if the given key has just been pressed since the last tick.
@param key the name of the key to be checked
@param clear whether to reset the state (default yes) post-check
@returns true if it has been pressed, false otherwise.
*/
/**************************************************************************/
bool Adafruit_Keypad::justPressed(byte key, bool clear) {
volatile byte *state = getKeyState(key);
bool val = (*state & _JUST_PRESSED) != 0;
if (clear)
*state &= ~(_JUST_PRESSED);
return val;
}
/**************************************************************************/
/*!
@brief check if the given key has just been released since the last tick.
@param key the name of the key to be checked
@returns true if it has been released, false otherwise.
*/
/**************************************************************************/
bool Adafruit_Keypad::justReleased(byte key) {
volatile byte *state = getKeyState(key);
bool val = (*state & _JUST_RELEASED) != 0;
*state &= ~(_JUST_RELEASED);
return val;
}
/**************************************************************************/
/*!
@brief check if the given key is currently pressed
@param key the name of the key to be checked
@returns true if it is currently pressed, false otherwise.
*/
/**************************************************************************/
bool Adafruit_Keypad::isPressed(byte key) {
return (*getKeyState(key) & _KEY_PRESSED) != 0;
}
/**************************************************************************/
/*!
@brief check if the given key is currently released
@param key the name of the key to be checked
@returns true if it is currently released, false otherwise.
*/
/**************************************************************************/
bool Adafruit_Keypad::isReleased(byte key) {
return (*getKeyState(key) & _KEY_PRESSED) == 0;
}
/**************************************************************************/
/*!
@brief check how many events are in the keypads buffer
@returns the number of events currently in the buffer
*/
/**************************************************************************/
int Adafruit_Keypad::available() {
return (_eventbuf.available() / sizeof(keypadEvent));
}
/**************************************************************************/
/*!
@brief pop the next event off of the FIFO
@returns the next event in the FIFO
*/
/**************************************************************************/
keypadEvent Adafruit_Keypad::read() {
keypadEvent k;
k.bit.EVENT = _eventbuf.read_char();
k.bit.KEY = _eventbuf.read_char();
k.bit.ROW = _eventbuf.read_char();
k.bit.COL = _eventbuf.read_char();
return k;
}
/**************************************************************************/
/*!
@brief Clear out the event buffer and all the key states
*/
/**************************************************************************/
void Adafruit_Keypad::clear() {
_eventbuf.clear();
for (int i = 0; i < _numRows * _numCols; i++)
*(_keystates + i) = 0;
}