aboutsummaryrefslogtreecommitdiffstats
path: root/Model01-Firmware.ino
blob: f22a713749b7557d0a533a7ad1d12c7dd4c9c168 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// -*- mode: c++ -*-

#ifndef BUILD_INFORMATION
#define BUILD_INFORMATION "locally built"
#endif

#include <Kaleidoscope.h>
#include <Kaleidoscope-Macros.h>
#include <Kaleidoscope-LEDControl.h>
#include <Kaleidoscope-NumPad.h>
#include <Kaleidoscope-HardwareTestMode.h>
#include <Kaleidoscope-HostPowerManagement.h>
#include <Kaleidoscope-MagicCombo.h>
#include <Kaleidoscope-Qukeys.h>
#include <Kaleidoscope-USB-Quirks.h>
#include <Kaleidoscope-EEPROM-Settings.h>
#include <Kaleidoscope-EEPROM-Keymap.h>
#include <Kaleidoscope-HostOS.h>
#include <Kaleidoscope-Syster.h>
#include <Kaleidoscope-Unicode.h>
#include <Kaleidoscope-LEDEffect-BootGreeting.h>
#include <Kaleidoscope-LEDEffect-Breathe.h>
#include <Kaleidoscope-IdleLEDs.h>
#include <Kaleidoscope-LayerHighlighter.h>
#include <Kaleidoscope-SpaceCadet.h>
#include <Kaleidoscope-Heatmap.h>
#include <Kaleidoscope-Leader.h>

enum {
  MACRO_VERSION_INFO,
  MACRO_ANY,
  WOW,
  SAUCY,
  LUL,
  ZZZ,
  GIVE_PLZ,
  TAKE_NRG,
  SEEMS_GOOD,
  GASM
};

enum { ENGRAM, WORKMAN, DVORAK, QWERTY, FUNCTION, NUMPAD, MACROS };

typedef struct {
  uint8_t index;
  const char *string;
} emote;

const char wow[] PROGMEM = "eeriedWow";
const char saucy[] PROGMEM = "eeriedSaucy";
const char lul[] PROGMEM = "supert25LUL";
const char zzz[] PROGMEM = "plasmoWife";
const char givePLZ[] PROGMEM = "GivePLZ";
const char takeNRG[] PROGMEM = "TakeNRG";
const char seemsGood[] PROGMEM = "SeemsGood";
const char gasm[] PROGMEM = "kdubGasm";

static const emote macros[] = {
  { WOW, wow },
  { SAUCY, saucy },
  { LUL, lul },
  { ZZZ, zzz },
  { GIVE_PLZ, givePLZ },
  { TAKE_NRG, takeNRG },
  { SEEMS_GOOD, seemsGood },
  { GASM, gasm },
  { 255, NULL }
};

void leaderMacro(uint8_t i) {
  // auto event = kaleidoscope::KeyEvent(KeyAddr::none(), IS_PRESSED, ShiftToLayer(MACROS));
  // OneShot.setPending(event);
  Layer.activate(MACROS);
}

void leaderNumpad(uint8_t i) {
  Layer.activate(NUMPAD);
}

void leaderWorkmanLayout(uint8_t i) {
  Layer.move(WORKMAN);
}

void leaderEngramLayout(uint8_t i) {
  Layer.move(ENGRAM);
}

void leaderDvorakLayout(uint8_t i) {
  Layer.move(DVORAK);
}

void leaderQwertyLayout(uint8_t i) {
  Layer.move(QWERTY);
}

constexpr kaleidoscope::plugin::Leader::dictionary_t leaderDictionary[] =
  LEADER_DICT({LEADER_SEQ(LEAD(0), Key_M), leaderMacro},
              {LEADER_SEQ(LEAD(0), Key_N), leaderNumpad},
              {LEADER_SEQ(LEAD(0), Key_L, Key_1), leaderWorkmanLayout},
              {LEADER_SEQ(LEAD(0), Key_L, Key_2), leaderEngramLayout},
              {LEADER_SEQ(LEAD(0), Key_L, Key_3), leaderDvorakLayout},
              {LEADER_SEQ(LEAD(0), Key_L, Key_4), leaderQwertyLayout});

KEYMAPS([ENGRAM] = KEYMAP_STACKED
        (Key_Escape,   Key_1, Key_2, Key_3, Key_4, Key_5,     XXX,
         Key_Backtick, Key_B, Key_Y, Key_O, Key_U, Key_Quote, Key_Tab,
         XXX,          Key_C, Key_I, Key_E, Key_A, Key_Comma,
         Key_RightGui, Key_G, Key_X, Key_J, Key_K, Key_Minus, Key_LeftGui,
         Key_LeftBracket, Key_Backspace, LSHIFT(Key_LeftBracket), LSHIFT(Key_9),
         ShiftToLayer(FUNCTION),

         XXX,               Key_6,         Key_7, Key_8, Key_9, Key_0, LEAD(0),
         Key_Enter,         Key_Semicolon, Key_L, Key_D, Key_W, Key_V, Key_Z,
                            Key_Period,    Key_H, Key_T, Key_S, Key_N, Key_Q,
         Key_PcApplication, Key_Slash,     Key_R, Key_M, Key_F, Key_P, Key_Equals,
         LSHIFT(Key_0), LSHIFT(Key_RightBracket), Key_Spacebar, Key_RightBracket,
         ShiftToLayer(FUNCTION)),

        [WORKMAN] = KEYMAP_STACKED
        (Key_Escape,   Key_1, Key_2, Key_3, Key_4, Key_5, XXX,
         Key_Backtick, Key_Q, Key_D, Key_R, Key_W, Key_B, Key_Tab,
         XXX,          Key_A, Key_S, Key_H, Key_T, Key_G,
         Key_RightGui, Key_Z, Key_X, Key_M, Key_C, Key_V, Key_LeftGui,
         Key_LeftBracket, Key_Backspace, LSHIFT(Key_LeftBracket), LSHIFT(Key_9),
         ShiftToLayer(FUNCTION),

         XXX,               Key_6, Key_7, Key_8,     Key_9,      Key_0,         LEAD(0),
         Key_Enter,         Key_J, Key_F, Key_U,     Key_P,      Key_Semicolon, Key_Minus,
                            Key_Y, Key_N, Key_E,     Key_O,      Key_I,         Key_Quote,
         Key_PcApplication, Key_K, Key_L, Key_Comma, Key_Period, Key_Slash,     Key_Equals,
         LSHIFT(Key_0), LSHIFT(Key_RightBracket), Key_Spacebar, Key_RightBracket,
         ShiftToLayer(FUNCTION)),

        [DVORAK] = KEYMAP_STACKED
        (Key_Escape,   Key_1,         Key_2,       Key_3,      Key_4, Key_5, XXX,
         Key_Backtick, Key_Quote,     Key_Comma,   Key_Period, Key_P, Key_Y, Key_Tab,
         XXX,          Key_A,         Key_O,       Key_E,      Key_U, Key_I,
         Key_RightGui, Key_Semicolon, Key_Q,       Key_J,      Key_K, Key_X, Key_LeftGui,
         Key_LeftBracket, Key_Backspace, LSHIFT(Key_LeftBracket), LSHIFT(Key_9),
         ShiftToLayer(FUNCTION),

         XXX,               Key_6, Key_7, Key_8, Key_9, Key_0, LEAD(0),
         Key_Enter,         Key_F, Key_G, Key_C, Key_R, Key_L, Key_Slash,
                            Key_D, Key_H, Key_T, Key_N, Key_S, Key_Minus,
         Key_PcApplication, Key_B, Key_M, Key_W, Key_V, Key_Z, Key_Equals,
         LSHIFT(Key_0), LSHIFT(Key_RightBracket), Key_Spacebar, Key_RightBracket,
         ShiftToLayer(FUNCTION)),

        [QWERTY] = KEYMAP_STACKED
        (Key_Escape,      Key_1, Key_2, Key_3, Key_4, Key_5, XXX,
         Key_Backtick,    Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
         XXX,             Key_A, Key_S, Key_D, Key_F, Key_G,
         Key_RightGui,    Key_Z, Key_X, Key_C, Key_V, Key_B, Key_LeftGui,
         Key_LeftBracket, Key_Backspace, LSHIFT(Key_LeftBracket), LSHIFT(Key_9),
         ShiftToLayer(FUNCTION),

         XXX,               Key_6, Key_7, Key_8,     Key_9,      Key_0,         LEAD(0),
         Key_Enter,         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_PcApplication, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash,     Key_Minus,
         LSHIFT(Key_0), LSHIFT(Key_RightBracket), Key_Spacebar, Key_RightBracket,
         ShiftToLayer(FUNCTION)),

        [FUNCTION] = KEYMAP_STACKED
        (Key_LEDEffectNext, Key_F1,          Key_F2,        Key_F3,                     Key_F4,                 Key_F5,                  LCTRL(Key_ScrollLock),
         ___,               ___,             ___,           ___,                        ___,                    ___,                     Key_CapsLock,
         Key_Copy,          ___,             ___,           ___,                        ___,                    ___,
         Key_Paste,         Key_PrintScreen, Key_Insert,    Consumer_ScanPreviousTrack, Consumer_ScanNextTrack, Consumer_PlaySlashPause, ___,
         ___, Key_Delete, ___, ___,
         ___,

         Key_CapsLock, Key_F6,        Key_F7,                   Key_F8,                   Key_F9,                  Key_F10,       Key_F11,
         ___,          Key_PageUp,    LCTRL(Key_LeftArrow),     Key_UpArrow,              LCTRL(Key_RightArrow),   Key_End,       Key_F12,
                       Key_PageDown,  Key_LeftArrow,            Key_DownArrow,            Key_RightArrow,          Key_Home,      ___,
         ___,          Consumer_Mute, Consumer_VolumeDecrement, Consumer_VolumeIncrement, ___,                     Key_Backslash, Key_Pipe,
         ___, ___, ___, ___,
         ___),

        [NUMPAD] = KEYMAP_STACKED
        (___, ___, ___, ___, ___, ___, ___,
         ___, ___, ___, ___, ___, ___, ___,
         ___, ___, ___, ___, ___, ___,
         ___, ___, ___, ___, ___, ___, ___,
         ___, ___, ___, ___,
         ___,

         ___, ___, Key_7, Key_8,      Key_9,              Key_KeypadSubtract, UnlockLayer(NUMPAD),
         ___, ___, Key_4, Key_5,      Key_6,              Key_KeypadAdd,      ___,
              ___, Key_1, Key_2,      Key_3,              Key_Equals,         ___,
         ___, ___, Key_0, Key_Period, Key_KeypadMultiply, Key_KeypadDivide,   Key_Enter,
         ___, ___, ___, ___,
         ___),

        [MACROS] =  KEYMAP_STACKED
        (___, ___,    ___, ___,     ___, ___, ___,
         ___, ___,    ___, ___,     ___, ___, ___,
         ___, ___,    ___, ___,     ___, M(SEEMS_GOOD),
         ___, M(ZZZ), ___, M(GASM), ___, ___, ___,
         ___, ___, ___, ___,
         ___,

         M(MACRO_VERSION_INFO), ___, ___, ___, ___,         ___,      UnlockLayer(MACROS),
                                ___, ___, ___, ___,         ___,         M(WOW),   ___,
                                     ___, ___, ___,         M(LUL),      M(SAUCY), ___,
                                ___, ___, ___, M(GIVE_PLZ), M(TAKE_NRG), ___,      ___,
         ___, ___, ___, ___,
         ___)
        )

/** 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));
    Unicode.type(0x2615);
  }
}

/** 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(KeyEvent &event) {
  if (keyToggledOn(event.state)) {
    event.key.setKeyCode(Key_A.getKeyCode() + (uint8_t)(millis() % 36));
    event.key.setFlags(0);
  }
}

static void emoteMacro(uint8_t macroID, KeyEvent &event) {
  if (keyToggledOn(event.state)) {
    for (int i = 0; macros[i].index != 255; i++) {
      if (macros[i].index == macroID) {
        Macros.type(macros[i].string);
        return;
      }
    }
  }
}

/** 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 macroID, KeyEvent &event) {
  switch (macroID) {
  case MACRO_VERSION_INFO:
    versionInfoMacro(event.state);
    break;

  case MACRO_ANY:
    anyKeyMacro(event);
    break;

  default:
    emoteMacro(macroID, event);
  }
  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::plugin::HostPowerManagement::Event event) {
  switch (event) {
  case kaleidoscope::plugin::HostPowerManagement::Suspend:
    LEDControl.disable();
    break;
  case kaleidoscope::plugin::HostPowerManagement::Resume:
    LEDControl.enable();
    break;
  case kaleidoscope::plugin::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::plugin::HostPowerManagement::Event event) {
  toggleLedsOnSuspendResume(event);
}

/** This 'enum' is a list of all the magic combos 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 magic combos. They will be used by the
 * `USE_MAGIC_COMBOS` call below.
 */
enum {
  // Toggle between Boot (6-key rollover; for BIOSes and early boot) and NKRO
  // mode.
  COMBO_TOGGLE_NKRO_MODE,
  // Enter test mode
  COMBO_ENTER_TEST_MODE
};

/** Wrappers, to be used by MagicCombo. **/

/**
 * This simply toggles the keyboard protocol via USBQuirks, and wraps it within
 * a function with an unused argument, to match what MagicCombo expects.
 */
static void toggleKeyboardProtocol(uint8_t combo_index) {
  USBQuirks.toggleKeyboardProtocol();
}

/**
 *  This enters the hardware test mode
 */
static void enterHardwareTestMode(uint8_t combo_index) {
  HardwareTestMode.runTests();
}


/** Magic combo list, a list of key combo and action pairs the firmware should
 * recognise.
 */
USE_MAGIC_COMBOS({.action = toggleKeyboardProtocol,
    // Left Fn + Esc + Shift
    .keys = { R3C6, R2C6, R3C7 }},
  {.action = enterHardwareTestMode,
   // Left Fn + Prog + LED
   .keys = { R3C6, R0C0, R0C6 }});

static kaleidoscope::plugin::LayerHighlighter emoteHighlighter(MACROS);

// 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(EEPROMSettings,
                          //HostOS,
                          Unicode,
                          Qukeys,
                          Leader,
                          //SpaceCadet,
                          //MacrosOnTheFly,
                          //Syster,
                          // LEDControl provides support for other LED
                          // modes
                          LEDControl,
                          IdleLEDs,
                          BootGreetingEffect,
                          LEDBreatheEffect,
                          HeatmapEffect,

                          // The numpad plugin is responsible for
                          // lighting up the 'numpad' mode with a
                          // custom LED effect
                          NumPad,
                          emoteHighlighter,

                          // The macros plugin adds support for macros
                          Macros,

                          // The HostPowerManagement plugin allows us
                          // to turn LEDs off when then host goes to
                          // sleep, and resume them when it wakes up.
                          //HostPowerManagement,

                          // The MagicCombo plugin lets you use key
                          // combinations to trigger custom actions -
                          // a bit like Macros, but triggered by
                          // pressing multiple keys at the same time.
                          MagicCombo,

                          // The USBQuirks plugin lets you do some
                          // things with USB that we aren't
                          // comfortable - or able - to do
                          // automatically, but can be useful
                          // nevertheless. Such as toggling the key
                          // report protocol between Boot (used by
                          // BIOSes) and Report (NKRO).
                          USBQuirks);

void setup() {
  Serial.begin(9600);

  // Necessary for FreeBSD, as it doesn't support NKRO.
  //BootKeyboard.default_protocol = HID_BOOT_PROTOCOL;

  // First, call Kaleidoscope's internal setup function
  Kaleidoscope.setup();

  // 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;

  emoteHighlighter.lockHue = 100;
  emoteHighlighter.color = CRGB(255, 255, 0);
  LEDBreatheEffect.hue = 212;

  QUKEYS(kaleidoscope::plugin::Qukey(0, KeyAddr(3, 7), Key_LeftShift),
         kaleidoscope::plugin::Qukey(0, KeyAddr(3, 8), Key_RightShift),
         kaleidoscope::plugin::Qukey(0, KeyAddr(0, 7), Key_LeftControl),
         kaleidoscope::plugin::Qukey(0, KeyAddr(0, 8), Key_RightControl),
         kaleidoscope::plugin::Qukey(0, KeyAddr(2, 7), Key_LeftAlt),
         kaleidoscope::plugin::Qukey(0, KeyAddr(2, 8), Key_RightAlt),
         kaleidoscope::plugin::Qukey(0, KeyAddr(2, 9), Key_LeftGui));
  Qukeys.setOverlapThreshold(25);

  IdleLEDs.setIdleTimeoutSeconds(300);
}

void loop() {
  Kaleidoscope.loop();
}

/*
void systerAction(kaleidoscope::plugin::Syster::action_t action, const char *symbol) {
  switch (action) {
  case kaleidoscope::plugin::Syster::StartAction:
    Unicode.type(0x2328);
    break;

  case kaleidoscope::plugin::Syster::EndAction:
    handleKeyswitchEvent(Key_Backspace, UnknownKeyswitchLocation, IS_PRESSED | INJECTED);
    Kaleidoscope.hid().keyboard().sendReport();
    handleKeyswitchEvent(Key_Backspace, UnknownKeyswitchLocation, WAS_PRESSED | INJECTED);
    Kaleidoscope.hid().keyboard().sendReport();
    break;

  case kaleidoscope::plugin::Syster::SymbolAction:
    Serial.print("systerAction = ");
    Serial.println(symbol);
    if (strcmp(symbol, "coffee") == 0) {
      Unicode.type(0x2615);
    } else if (strcmp(symbol, "=/") == 0) { // =/
      Unicode.type(0x1f615);
    } else if (strcmp(symbol, "=9") == 0) { // =(
      Unicode.type(0x1f641);
    } else if (strcmp(symbol, "=:") == 0) { // =)
      Unicode.type(0x1f642);
    } else if (strcmp(symbol, "9=") == 0) { // (=
      Unicode.type(0x1f643);
    } else if (strcmp(symbol, "=p") == 0) { // =P
      Unicode.type(0x1f61b);
    } else if (strcmp(symbol, "=x") == 0) { // =x
      Unicode.type(0x1f636);
    } else if (strcmp(symbol, "b:") == 0) { // B)
      Unicode.type(0x1f60e);
    } else if (strcmp(symbol, ";:") == 0) { // ;)
      Unicode.type(0x1f609);
    } else if (strcmp(symbol, "1::") == 0) { // 100
      Unicode.type(0x1f4af);
    } else if (strcmp(symbol, "eye") == 0) {
      Unicode.type(0x1f440);


    } else if (strcmp(symbol, "heye") == 0) {
      Unicode.type(0x1f60d);
    } else if (strcmp(symbol, "think") == 0) {
      Unicode.type(0x1f914);
    } else if (strcmp(symbol, "party") == 0) {
      Unicode.type(0x1f389);
    } else if (strcmp(symbol, "flex") == 0) {
      Unicode.type(0x1f4aa);
    } else if (strcmp(symbol, "pray") == 0) {
      Unicode.type(0x1f64f);
    } else if (strcmp(symbol, "kiss") == 0) {
      Unicode.type(0x1f618);
    } else if (strcmp(symbol, "rip") == 0) {
      Unicode.type(0x26b0);
    } else if (strcmp(symbol, "dead") == 0) {
      Unicode.type(0x1f480);
    } else if (strcmp(symbol, "ok") == 0) {
      Unicode.type(0x1f58f);
    } else if (strcmp(symbol, "yes") == 0) {
      Unicode.type(0x1f592);
    } else if (strcmp(symbol, "no") == 0) {
      Unicode.type(0x1f593);
    } else if (strcmp(symbol, "fu") == 0) {
      Unicode.type(0x1f595);
    } else if (strcmp(symbol, "spy") == 0) {
      Unicode.type(0x1f575);
    } else if (strcmp(symbol, "ooo") == 0) {
      Unicode.type(0x1f47b);
    }
    break;
  }
}
*/