forked from keyboardio/Model01-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel01-Firmware.ino
316 lines (254 loc) · 11.4 KB
/
Model01-Firmware.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// -*- mode: c++ -*-
// Copyright 2016 Keyboardio, inc. <[email protected]>
// See "LICENSE" for license details
#ifndef BUILD_INFORMATION
#define BUILD_INFORMATION "locally built"
#endif
/**
* These #include directives pull in the Kaleidoscope firmware core,
* as well as the Kaleidoscope plugins we use in the Model 01's firmware
*/
// The Kaleidoscope core
#include "Kaleidoscope.h"
#include <Kaleidoscope-Focus.h>
// Support for keys that move the mouse
#include "Kaleidoscope-MouseKeys.h"
// Support for macros
#include "Kaleidoscope-Macros.h"
// Support for controlling the keyboard's LEDs
#include "Kaleidoscope-LEDControl.h"
// Support for "Numpad" mode, which is mostly just the Numpad specific LED mode
#include "Kaleidoscope-NumPad.h"
// Support for an "LED off mode"
#include "LED-Off.h"
// Support for the "Boot greeting" effect, which pulses the 'LED' button for 10s
// when the keyboard is connected to a computer (or that computer is powered on)
#include "Kaleidoscope-LEDEffect-BootGreeting.h"
// Support for LED modes that set all LEDs to a single color
#include "Kaleidoscope-LEDEffect-SolidColor.h"
// Support for an LED mode that makes all the LEDs 'breathe'
#include "Kaleidoscope-LEDEffect-Breathe.h"
// Support for an LED mode that makes a red pixel chase a blue pixel across the keyboard
#include "Kaleidoscope-LEDEffect-Chase.h"
// Support for an LED mode that lights up the keys as you press them
#include "Kaleidoscope-LED-Stalker.h"
// Support for an LED mode that prints the keys you press in letters 4px high
#include "Kaleidoscope-LED-AlphaSquare.h"
// Support for Keyboardio's internal keyboard testing mode
#include "Kaleidoscope-Model01-TestMode.h"
// Support for host power management (suspend & wakeup)
#include "Kaleidoscope-HostPowerManagement.h"
#include <Kaleidoscope-Qukeys.h>
#include <Kaleidoscope-DualUse.h>
#include <Kaleidoscope-OneShot.h>
#include <kaleidoscope/hid.h>
/** This 'enum' is a list of all the macros used by the Model 01's firmware
* The names aren't particularly important. What is important is that each
* is unique.
*
* These are the names of your macros. They'll be used in two places.
* The first is in your keymap definitions. There, you'll use the syntax
* `M(MACRO_NAME)` to mark a specific keymap position as triggering `MACRO_NAME`
*
* The second usage is in the 'switch' statement in the `macroAction` function.
* That switch statement actually runs the code associated with a macro when
* a macro key is pressed.
*/
enum { MACRO_VERSION_INFO,
MACRO_HYPER,
MACRO_TOGGLE_QUKEYS
};
/** versionInfoMacro handles the 'firmware version info' macro
* When a key bound to the macro is pressed, this macro
* prints out the firmware build information as virtual keystrokes
*/
static void versionInfoMacro(uint8_t keyState) {
if (keyToggledOn(keyState)) {
Macros.type(PSTR("Keyboardio Model 01 - Kaleidoscope "));
Macros.type(PSTR(BUILD_INFORMATION));
}
}
/** anyKeyMacro is used to provide the functionality of the 'Any' key.
*
* When the 'any key' macro is toggled on, a random alphanumeric key is
* selected. While the key is held, the function generates a synthetic
* keypress event repeating that randomly selected key.
*
*/
static void anyKeyMacro(uint8_t keyState) {
static Key lastKey;
if (keyToggledOn(keyState))
lastKey.keyCode = Key_A.keyCode + (uint8_t)(millis() % 36);
if (keyIsPressed(keyState))
kaleidoscope::hid::pressKey(lastKey);
}
static void oneShotHyper(uint8_t keyState) {
handleKeyswitchEvent(OSM(LeftControl), UNKNOWN_KEYSWITCH_LOCATION, keyState);
handleKeyswitchEvent(OSM(LeftAlt), UNKNOWN_KEYSWITCH_LOCATION, keyState);
handleKeyswitchEvent(OSM(LeftGui), UNKNOWN_KEYSWITCH_LOCATION, keyState);
}
/** macroAction dispatches keymap events that are tied to a macro
to that macro. It takes two uint8_t parameters.
The first is the macro being called (the entry in the 'enum' earlier in this file).
The second is the state of the keyswitch. You can use the keyswitch state to figure out
if the key has just been toggled on, is currently pressed or if it's just been released.
The 'switch' statement should have a 'case' for each entry of the macro enum.
Each 'case' statement should call out to a function to handle the macro in question.
*/
const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
switch (macroIndex) {
case MACRO_HYPER:
oneShotHyper(keyState);
break;
case MACRO_VERSION_INFO:
versionInfoMacro(keyState);
break;
}
return MACRO_NONE;
}
/** toggleLedsOnSuspendResume toggles the LEDs off when the host goes to sleep,
* and turns them back on when it wakes up.
*/
void toggleLedsOnSuspendResume(kaleidoscope::HostPowerManagement::Event event) {
switch (event) {
case kaleidoscope::HostPowerManagement::Suspend:
LEDControl.paused = true;
LEDControl.set_all_leds_to({0, 0, 0});
LEDControl.syncLeds();
break;
case kaleidoscope::HostPowerManagement::Resume:
LEDControl.paused = false;
LEDControl.refreshAll();
break;
case kaleidoscope::HostPowerManagement::Sleep:
break;
}
}
/** hostPowerManagementEventHandler dispatches power management events (suspend,
* resume, and sleep) to other functions that perform action based on these
* events.
*/
void hostPowerManagementEventHandler(kaleidoscope::HostPowerManagement::Event event) {
toggleLedsOnSuspendResume(event);
}
// First, tell Kaleidoscope which plugins you want to use.
// The order can be important. For example, LED effects are
// added in the order they're listed here.
KALEIDOSCOPE_INIT_PLUGINS(
Qukeys,
BootGreetingEffect,
TestMode,
LEDControl,
LEDOff,
NumPad,
Macros,
OneShot,
MouseKeys,
Focus,
HostPowerManagement
);
enum { QWERTY, NUMPAD, FUNCTION }; // layers
// *INDENT-OFF*
KEYMAPS(
[QWERTY] = KEYMAP_STACKED
(___, Key_1, Key_2, Key_3, Key_4, Key_5, Key_Backtick,
LockLayer(NUMPAD), Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G,
Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_LeftAlt,
Key_Backspace, Key_Delete, Key_Tab, Key_Escape,
ShiftToLayer(FUNCTION),
Key_Slash, Key_6, Key_7, Key_8, Key_9, Key_0, Key_Minus,
Key_Escape, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals,
Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote,
Key_RightAlt, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, M(MACRO_HYPER),
Key_Escape, Key_Tab, Key_Enter, Key_Spacebar,
ShiftToLayer(FUNCTION)),
[NUMPAD] = KEYMAP_STACKED
(___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___,
___,
M(MACRO_VERSION_INFO), ___, Key_Keypad7, Key_Keypad8, Key_Keypad9, Key_KeypadSubtract, ___,
___, ___, Key_Keypad4, Key_Keypad5, Key_Keypad6, Key_KeypadAdd, ___,
___, Key_Keypad1, Key_Keypad2, Key_Keypad3, Key_Equals, ___,
___, ___, Key_Keypad0, Key_KeypadDot, Key_KeypadMultiply, Key_KeypadDivide, Key_Enter,
___, ___, ___, Key_Keypad0,
___),
[FUNCTION] = KEYMAP_STACKED
(___, Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, XXX,
Key_Tab, ___, Key_mouseUp, ___, Key_mouseBtnR, Key_mouseWarpEnd, Key_mouseWarpNE,
Key_Home, Key_mouseL, Key_mouseDn, Key_mouseR, Key_mouseBtnL, Key_mouseWarpNW,
Key_End, Key_PrintScreen, Key_Insert, ___, Key_mouseBtnM, Key_mouseWarpSW, Key_mouseWarpSE,
___, ___, ___, ___,
___,
Key_Backslash, Key_F6, Key_F7, Key_F8, Key_F9, Key_F10, Key_F11,
Consumer_PlaySlashPause, Consumer_ScanNextTrack, Key_LeftCurlyBracket, Key_RightCurlyBracket, Key_LeftBracket, Key_RightBracket, Key_F12,
Key_LeftArrow, Key_DownArrow, Key_UpArrow, Key_RightArrow, ___, ___,
Key_PcApplication, Consumer_Mute, Consumer_VolumeDecrement, Consumer_VolumeIncrement, ___, Key_Backslash, Key_Pipe,
___, ___, ___, ___,
___)
) // KEYMAPS(
/* Re-enable astyle's indent enforcement */
// *INDENT-ON*
/** The 'setup' function is one of the two standard Arduino sketch functions.
* It's called when your keyboard first powers up. This is where you set up
* Kaleidoscope and any plugins.
*/
void setup() {
Serial.begin(9600);
// Refer to https://github.com/keyboardio/Kaleidoscope-Hardware-Model01/blob/f469015346535cb864a340bf8eb317d268943248/src/Kaleidoscope-Hardware-Model01.h#L267-L279
// r0c0, r0c1, r0c2, r0c3, r0c4, r0c5, r0c6, \
// r1c0, r1c1, r1c2, r1c3, r1c4, r1c5, r1c6, \
// r2c0, r2c1, r2c2, r2c3, r2c4, r2c5, \
// r3c0, r3c1, r3c2, r3c3, r3c4, r3c5, r2c6, \
// r0c7, r1c7, r2c7, r3c7, \
// r3c6, \
// \
// r0c9, r0c10, r0c11, r0c12, r0c13, r0c14, r0c15, \
// r1c9, r1c10, r1c11, r1c12, r1c13, r1c14, r1c15, \
// r2c10, r2c11, r2c12, r2c13, r2c14, r2c15, \
// r2c9, r3c10, r3c11, r3c12, r3c13, r3c14, r3c15, \
// r3c8, r2c8, r1c8, r0c8, \
// r3c9, ...)
QUKEYS(
kaleidoscope::Qukey(0, 1, 6, M(MACRO_HYPER)), // (TAB)
kaleidoscope::Qukey(0, 1, 9, M(MACRO_HYPER)), // (ENTER)
kaleidoscope::Qukey(0, 2, 6, Key_LeftAlt), // (ESC)
kaleidoscope::Qukey(0, 2, 9, Key_RightAlt), // (Butterfly)
kaleidoscope::Qukey(2, 2, 6, Key_LeftAlt), // (ESC)
kaleidoscope::Qukey(2, 2, 9, Key_RightAlt), // (Butterfly)
// Left thumb
kaleidoscope::Qukey(0, 1, 7, Key_LeftShift), // (Backspace)
kaleidoscope::Qukey(0, 2, 7, Key_LeftGui), // (CMD)
kaleidoscope::Qukey(0, 3, 7, Key_LeftControl), // (SHIFT)
// Right thumb
kaleidoscope::Qukey(0, 1, 8, Key_RightShift),
kaleidoscope::Qukey(0, 2, 8, Key_RightGui), // (ALT)
kaleidoscope::Qukey(0, 3, 8, Key_RightControl), // (SHIFT)
)
Qukeys.setTimeout(140);
Qukeys.setReleaseDelay(50);
// First, call Kaleidoscope's internal setup function
Kaleidoscope.setup();
Focus.addHook(FOCUS_HOOK_HELP);
Focus.addHook(FOCUS_HOOK_LEDCONTROL);
Focus.addHook(FOCUS_HOOK_VERSION);
// While we hope to improve this in the future, the NumPad plugin
// needs to be explicitly told which keymap layer is your numpad layer
NumPad.numPadLayer = NUMPAD;
// We want to make sure that the firmware starts with LED effects off
// This avoids over-taxing devices that don't have a lot of power to share
// with USB devices
LEDOff.activate();
}
/** loop is the second of the standard Arduino sketch functions.
* As you might expect, it runs in a loop, never exiting.
*
* For Kaleidoscope-based keyboard firmware, you usually just want to
* call Kaleidoscope.loop(); and not do anything custom here.
*/
void loop() {
Kaleidoscope.loop();
}