-
Notifications
You must be signed in to change notification settings - Fork 64
/
Adafruit_NeoKey_1x4.cpp
336 lines (298 loc) · 12.5 KB
/
Adafruit_NeoKey_1x4.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "Adafruit_NeoKey_1x4.h"
/**************************************************************************/
/*!
@brief Class constructor
@param addr the I2C address this neotrellis object uses
@param i2c_bus the I2C bus connected to this neokey, defaults to "Wire"
*/
/**************************************************************************/
Adafruit_NeoKey_1x4::Adafruit_NeoKey_1x4(uint8_t addr, TwoWire *i2c_bus)
: Adafruit_seesaw(i2c_bus), pixels(NEOKEY_1X4_KEYS, NEOKEY_1X4_NEOPIN,
NEO_GRB + NEO_KHZ800, i2c_bus) {
for (int i = 0; i < NEOKEY_1X4_KEYS; i++) {
_callbacks[i] = NULL;
}
this->_addr = addr;
}
/**************************************************************************/
/*!
@brief Begin communication with the RGB trellis.
@param addr optional i2c address where the device can be found. Defaults to
NEOKEY_1X4_ADDR
@param flow optional flow control pin
@returns true on success, false on error.
*/
/**************************************************************************/
bool Adafruit_NeoKey_1x4::begin(uint8_t addr, int8_t flow) {
_addr = addr;
bool ret = pixels.begin(addr, flow);
if (!ret)
return ret;
ret = Adafruit_seesaw::begin(addr, flow, false);
if (!ret)
return ret;
pixels.setBrightness(40);
pixels.show(); // Initialize all pixels to 'off'
delay(5);
pinModeBulk(NEOKEY_1X4_BUTTONMASK, INPUT_PULLUP);
setGPIOInterrupts(NEOKEY_1X4_BUTTONMASK, 1);
return ret;
}
/**************************************************************************/
/*!
@brief register a callback function on the passed key.
@param key the key number to register the callback on
@param cb the callback function that should be called when an event on that
key happens
*/
/**************************************************************************/
void Adafruit_NeoKey_1x4::registerCallback(uint8_t key,
NeoKey1x4Callback (*cb)(keyEvent)) {
_callbacks[key] = cb;
}
/**************************************************************************/
/*!
@brief unregister a callback on a given key
@param key the key number the callback is currently mapped to.
*/
/**************************************************************************/
void Adafruit_NeoKey_1x4::unregisterCallback(uint8_t key) {
_callbacks[key] = NULL;
}
/**************************************************************************/
/*!
@brief Read key GPIO pins, possibly generating callback events
@returns Byte with the bottom 4 bits corresponding to each keypress status
*/
/**************************************************************************/
uint8_t Adafruit_NeoKey_1x4::read(void) {
uint32_t buttons = digitalReadBulk(NEOKEY_1X4_BUTTONMASK);
buttons ^= NEOKEY_1X4_BUTTONMASK;
buttons &= NEOKEY_1X4_BUTTONMASK;
buttons >>= NEOKEY_1X4_BUTTONA;
uint8_t just_pressed = (buttons ^ last_buttons) & buttons;
uint8_t just_released = (buttons ^ last_buttons) & ~buttons;
if (just_pressed | just_released) {
// Serial.print("pressed 0x"); Serial.println(just_pressed, HEX);
// Serial.print("released 0x"); Serial.println(just_released, HEX);
for (int b = 0; b < 4; b++) {
if (just_pressed & (1 << b)) { // if button b is pressed
if (_callbacks[b] != NULL) {
keyEvent evt = {SEESAW_KEYPAD_EDGE_RISING, (uint16_t)b};
_callbacks[b](evt);
}
}
if (just_released & (1 << b)) { // if button b is released
if (_callbacks[b] != NULL) {
keyEvent evt = {SEESAW_KEYPAD_EDGE_FALLING, (uint16_t)b};
_callbacks[b](evt);
}
}
}
}
last_buttons = buttons;
return buttons;
}
/**************************************************************************/
/*!
@brief class constructor
@param neokeys pointer to a multidimensional array of Adafruit_NeoKey_1x4
objects. these object must have their I2C addresses specified in the class
constructors.
@param rows the number of individual neokey boards in the Y direction
of your matrix.
@param cols the number of individual neokey boards in the X direction
of your matrix.
*/
/**************************************************************************/
Adafruit_MultiNeoKey1x4::Adafruit_MultiNeoKey1x4(Adafruit_NeoKey_1x4 *neokeys,
uint8_t rows, uint8_t cols) {
this->_rows = rows;
this->_cols = cols;
this->_neokeys = neokeys;
}
/**************************************************************************/
/*!
@brief begin communication with the matrix of neotrellis boards.
@returns true on success, false otherwise.
*/
/**************************************************************************/
bool Adafruit_MultiNeoKey1x4::begin() {
Adafruit_NeoKey_1x4 *t;
for (int n = 0; n < _rows; n++) {
for (int m = 0; m < _cols; m++) {
t = (_neokeys + n * _cols) + m;
if (!t->begin(t->_addr))
return false;
}
}
return true;
}
/**************************************************************************/
/*!
@brief register a callback for a key addressed by key index.
@param x the column index of the key. column 0 is on the lefthand side of
the matix.
@param y the row index of the key. row 0 is at the top of the matrix and
the numbers increase downwards.
@param cb the function to be called when an event from the specified key is
detected.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::registerCallback(
uint8_t x, uint8_t y, NeoKey1x4Callback (*cb)(keyEvent)) {
Adafruit_NeoKey_1x4 *t =
(_neokeys + y / NEOKEY_1X4_ROWS * _cols) + x / NEOKEY_1X4_COLS;
int xkey = NEOKEY_1X4_X(x);
int ykey = NEOKEY_1X4_Y(y % NEOKEY_1X4_ROWS * NEOKEY_1X4_COLS);
t->registerCallback(NEOKEY_1X4_XY(xkey, ykey), cb);
}
/**************************************************************************/
/*!
@brief register a callback for a key addressed by key number.
@param num the keynumber to set the color of. Key 0 is in the top left
corner of the trellis matrix, key 1 is directly to the right of it,
and the last key number is in the bottom righthand corner.
@param cb the function to be called when an event from the specified key is
detected.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::registerCallback(
uint16_t num, NeoKey1x4Callback (*cb)(keyEvent)) {
uint8_t x = num % (NEOKEY_1X4_COLS * _cols);
uint8_t y = num / (NEOKEY_1X4_COLS * _cols);
registerCallback(x, y, cb);
}
/**************************************************************************/
/*!
@brief Unregister a callback for a key addressed by key index.
@param x the column index of the key. column 0 is on the lefthand side of
the matix.
@param y the row index of the key. row 0 is at the top of the matrix and
the numbers increase downwards.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::unregisterCallback(uint8_t x, uint8_t y) {
Adafruit_NeoKey_1x4 *t =
(_neokeys + y / NEOKEY_1X4_ROWS * _cols) + x / NEOKEY_1X4_COLS;
int xkey = NEOKEY_1X4_X(x);
int ykey = NEOKEY_1X4_Y(y % NEOKEY_1X4_ROWS * NEOKEY_1X4_COLS);
t->unregisterCallback(NEOKEY_1X4_XY(xkey, ykey));
}
/**************************************************************************/
/*!
@brief Unregister a callback for a key addressed by key number.
@param num the keynumber to set the color of. Key 0 is in the top left
corner of the trellis matrix, key 1 is directly to the right of it,
and the last key number is in the bottom righthand corner.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::unregisterCallback(uint16_t num) {
uint8_t x = num % (NEOKEY_1X4_COLS * _cols);
uint8_t y = num / (NEOKEY_1X4_COLS * _cols);
unregisterCallback(x, y);
}
/**************************************************************************/
/*!
@brief set the color of a neopixel at a key index.
@param x the column index of the key. column 0 is on the lefthand side of
the matix.
@param y the row index of the key. row 0 is at the top of the matrix and
the numbers increase downwards.
@param color the color to set the pixel to. This is a 24 bit RGB value.
for example, full brightness red would be 0xFF0000, and full
brightness blue would be 0x0000FF.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::setPixelColor(uint8_t x, uint8_t y,
uint32_t color) {
Adafruit_NeoKey_1x4 *t =
(_neokeys + y / NEOKEY_1X4_ROWS * _cols) + x / NEOKEY_1X4_COLS;
int xkey = NEOKEY_1X4_X(x);
int ykey = NEOKEY_1X4_Y(y % NEOKEY_1X4_ROWS * NEOKEY_1X4_COLS);
t->pixels.setPixelColor(NEOKEY_1X4_XY(xkey, ykey), color);
}
/**************************************************************************/
/*!
@brief set the color of a neopixel at a key number.
@param num the keynumber to set the color of. Key 0 is in the top left
corner of the trellis matrix, key 1 is directly to the right of it,
and the last key number is in the bottom righthand corner.
@param color the color to set the pixel to. This is a 24 bit RGB value.
for example, full brightness red would be 0xFF0000, and full
brightness blue would be 0x0000FF.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::setPixelColor(uint16_t num, uint32_t color) {
uint8_t x = num % (NEOKEY_1X4_COLS * _cols);
uint8_t y = num / (NEOKEY_1X4_COLS * _cols);
setPixelColor(x, y, color);
}
/**************************************************************************/
/*!
@brief call show for all connected neotrellis boards to show all neopixels
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::show() {
Adafruit_NeoKey_1x4 *t;
for (int n = 0; n < _rows; n++) {
for (int m = 0; m < _cols; m++) {
t = (_neokeys + n * _cols) + m;
t->pixels.show();
}
}
}
/**************************************************************************/
/*!
@brief read all events currently stored in the seesaw fifo and call any
callbacks.
*/
/**************************************************************************/
void Adafruit_MultiNeoKey1x4::read() {
Adafruit_NeoKey_1x4 *nk;
for (int n = 0; n < _rows; n++) {
for (int m = 0; m < _cols; m++) {
// get the individual breakout
nk = (_neokeys + n * _cols) + m;
// query what buttons are pressed
nk->digitalReadBulk(
NEOKEY_1X4_BUTTONMASK); // not sure why we have to do it 2ce
uint32_t buttons = nk->digitalReadBulk(NEOKEY_1X4_BUTTONMASK);
// Serial.print("Neokey number "); Serial.print(n * _cols + m);
// Serial.print(" buttons: 0x"); Serial.println(buttons, HEX);
buttons ^= NEOKEY_1X4_BUTTONMASK;
buttons &= NEOKEY_1X4_BUTTONMASK;
buttons >>= NEOKEY_1X4_BUTTONA;
// compared to last time
uint8_t just_pressed = (buttons ^ nk->last_buttons) & buttons;
uint8_t just_released = (buttons ^ nk->last_buttons) & ~buttons;
// stash for next run
nk->last_buttons = buttons;
if (just_pressed | just_released) {
// Serial.print("pressed 0x"); Serial.println(just_pressed, HEX);
// Serial.print("released 0x"); Serial.println(just_released, HEX);
// for each key, process the event
for (int b = 0; b < 4; b++) {
int x = b; // column is the button
int y = 0; // a 1x4 neokey only has one row
// extend into whole grid
x = x + m * NEOKEY_1X4_COLS;
y = y + n * NEOKEY_1X4_ROWS;
int event_key = y * NEOKEY_1X4_COLS * _cols + x;
if (just_pressed & (1 << b)) { // if button b is pressed
if (nk->_callbacks[b] != NULL) {
keyEvent evt = {SEESAW_KEYPAD_EDGE_RISING, (uint16_t)event_key};
nk->_callbacks[b](evt);
}
}
if (just_released & (1 << b)) { // if button b is pressed
if (nk->_callbacks[b] != NULL) {
keyEvent evt = {SEESAW_KEYPAD_EDGE_FALLING, (uint16_t)event_key};
nk->_callbacks[b](evt);
}
}
}
}
}
}
}