-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path4x4keyboard.ino
298 lines (281 loc) · 7.55 KB
/
4x4keyboard.ino
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
#include <HID-Settings.h>
#include <HID-Project.h>
/* 4x4 macro keyboard
* Copyright 2016 by Pedro Nariyoshi <[email protected]>
* Licensed under GNU General Public License 3.0 or later.
* @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
*
* Code derived from Button/LED Matrix Scanning Example - 3x3 Keypad
* by Jimbo @ SparkFun Electronics
* https://learn.sparkfun.com/tutorials/cherry-mx-switch-breakout-hookup-guide
*/
//// Hardware definitions
#define NUM_COLS 4 // Number of switch columns
#define NUM_ROWS 4 // Number of switch rows
//// Software defined debounce
#define MAX_DEBOUNCE 3
//// Apple keyboard macros
#define KEY_COMMAND KEY_LEFT_GUI
#define KEY_OPTION KEY_LEFT_ALT
#define KEY_EJECT HID_CONSUMER_EJECT
//// Keyboard matrix pin assignments
// Pins connected to rows
static const uint8_t RowPins[NUM_ROWS] = {2,3,4,5};
// Pins connected to columns
static const uint8_t ColPins[NUM_COLS] = {6,7,8,9};
//// Global variables
// Counter for how long a button has been pressed
static uint8_t debounce_count[NUM_ROWS][NUM_COLS];
void setup()
{
// Counter to set up pins
uint8_t i;
// Set row scan pins to output and then to HIGH (not active)
for (i = 0; i < NUM_ROWS; i++)
{
pinMode(RowPins[i], OUTPUT);
digitalWrite(RowPins[i], HIGH);
}
// Set column pins to input with pull-up resistors (no need for external pull-up resistors)
for (i = 0; i < NUM_COLS; i++)
{
pinMode(ColPins[i], INPUT_PULLUP);
}
// Initialize USB keyboard
Keyboard.begin();
// Initialize the debounce counter array
memset(debounce_count,0,sizeof(debounce_count));
// for (uint8_t i = 0; i < NUM_COLS; i++)
// {
// for (uint8_t j = 0; j < NUM_ROWS; j++)
// {
// debounce_count[i][j] = 0;
// }
// }
}
void loop()
{
// Each run through the scan function operates on a single row
// of the matrix, kept track of using the currentRow variable.
static uint8_t currentRow = 0;
static uint8_t currentCol; // for loop counters
// Select current row
digitalWrite(RowPins[currentRow], LOW);
// Scan through switches on this row:
for (currentCol = 0; currentCol < NUM_COLS; currentCol++)
{
// Read the button. If it's pressed, it should be LOW.
if (digitalRead(ColPins[currentCol]) == LOW)
{
if (debounce_count[currentRow][currentCol] < MAX_DEBOUNCE)
{
// Increment a debounce counter
debounce_count[currentRow][currentCol]++;
// If debounce counter hits MAX_DEBOUNCE
// Trigger key press
if ( debounce_count[currentRow][currentCol] == MAX_DEBOUNCE )
{
pressMacro(currentRow,currentCol);
}
}
}
else // Otherwise, button is released
{
if ( debounce_count[currentRow][currentCol] > 0)
{
// Decrement debounce counter
debounce_count[currentRow][currentCol]--;
if ( debounce_count[currentRow][currentCol] == 0 )
{ // If debounce counter hits 0
releaseMacro(currentRow,currentCol);
}
}
}
}
// Once done scanning, de-select the row pin
digitalWrite(RowPins[currentRow], HIGH);
// Increment currentRow, so next time we scan the next row
currentRow = (currentRow > NUM_ROWS-2) ? 0 : currentRow+1;
// currentRow++;
// if (currentRow >= NUM_ROWS)
// {
// currentRow = 0;
// }
}
void pressMacro(uint8_t row, uint8_t col)
{
switch(row)
{
case 0:
switch(col)
{
case 0: // constipated face
Keyboard.print(">_<");
break;
case 1: // Switch between tabs inside adobe programs
Keyboard.press(KEY_COMMAND);
Keyboard.press('~');
break;
case 2: // Command key
Keyboard.press(KEY_COMMAND);
break;
case 3: // OSX Sleep
Keyboard.press(KEY_COMMAND);
Keyboard.press(KEY_OPTION);
Keyboard.press(KEY_EJECT);
break;
}
break;
case 1:
switch(col)
{
case 0: // Adobe Illustrator: Lock selected
Keyboard.press(KEY_COMMAND);
Keyboard.press(KEY_2);
break;
case 1: // Adobe Illustrator: Unlock selected
Keyboard.press(KEY_COMMAND);
Keyboard.press(KEY_OPTION);
Keyboard.press(KEY_2);
break;
case 2: // Shift+Tab (to switch to previous app)
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_TAB);
break;
case 3: // Tab (to switch to next app)
Keyboard.press(KEY_TAB);
break;
}
break;
case 2:
switch(col)
{
case 0: // Adobe Photoshop: Decrease brush size
Keyboard.press('[');
break;
case 1: // Adobe Photoshop: Increase brush size
Keyboard.press(']');
break;
case 2: // Adobe Illustrator: Blob brush
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('b');
break;
case 3: // Adobe Illustrator: Merge shape
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('m');
break;
}
break;
case 3:
switch(col)
{
case 0: // Undo
Keyboard.press(KEY_COMMAND);
Keyboard.press('z');
break;
case 1: // Redo
Keyboard.press(KEY_COMMAND);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('z');
break;
case 2: // Export
Keyboard.press(KEY_COMMAND);
Keyboard.press(KEY_OPTION);
Keyboard.press('e');
break;
case 3: // Save
Keyboard.press(KEY_COMMAND);
Keyboard.press('s');
break;
}
break;
}
}
void releaseMacro(uint8_t row, uint8_t col)
{
switch(row)
{
case 0:
switch(col)
{
case 0:
break;
case 1:
Keyboard.release(KEY_COMMAND);
Keyboard.release('~');
break;
case 2:
Keyboard.release(KEY_COMMAND);
break;
case 3:
Keyboard.release(KEY_COMMAND);
Keyboard.release(KEY_OPTION);
Keyboard.release(KEY_EJECT);
break;
}
break;
case 1:
switch(col)
{
case 0:
Keyboard.release(KEY_COMMAND);
Keyboard.release(KEY_2);
break;
case 1:
Keyboard.release(KEY_COMMAND);
Keyboard.release(KEY_OPTION);
Keyboard.release(KEY_2);
break;
case 2:
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release(KEY_TAB);
break;
case 3:
Keyboard.release(KEY_TAB);
break;
}
break;
case 2:
switch(col)
{
case 0:
Keyboard.release('[');
break;
case 1:
Keyboard.release(']');
break;
case 2:
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release('b');
break;
case 3:
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release('m');
break;
}
break;
case 3:
switch(col)
{
case 0:
Keyboard.release(KEY_COMMAND);
Keyboard.release('z');
break;
case 1:
Keyboard.release(KEY_COMMAND);
Keyboard.release(KEY_LEFT_SHIFT);
Keyboard.release('z');
break;
case 2:
Keyboard.release(KEY_COMMAND);
Keyboard.release(KEY_OPTION);
Keyboard.release('e');
break;
case 3:
Keyboard.release(KEY_COMMAND);
Keyboard.release('s');
break;
}
break;
}
}