]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
a failed attempt at hot-plugging
[qmk_firmware.git] / users / drashna / drashna.c
1 /*
2 Copyright 2017 Christopher Courtney <drashna@live.com> @drashna
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "drashna.h"
19 #include "version.h"
20
21 #if (__has_include("secrets.h") && !defined(NO_SECRETS))
22 #include "secrets.h"
23 #else
24 // `PROGMEM const char secret[][x]` may work better, but it takes up more space in the firmware
25 // And I'm not familiar enough to know which is better or why...
26 PROGMEM const char secret[][64] = {
27   "test1",
28   "test2",
29   "test3",
30   "test4",
31   "test5"
32 };
33 #endif
34
35
36 float tone_copy[][2]            = SONG(SCROLL_LOCK_ON_SOUND);
37 float tone_paste[][2]           = SONG(SCROLL_LOCK_OFF_SOUND);
38
39
40 static uint16_t copy_paste_timer;
41 #ifdef RGBLIGHT_ENABLE
42 bool rgb_layer_change = true;
43 #endif
44
45 userspace_config_t userspace_config;
46
47 //  Helper Functions
48 void tap(uint16_t keycode){ register_code(keycode); unregister_code(keycode); };
49
50 #ifdef RGBLIGHT_ENABLE
51 void rgblight_sethsv_default_helper(uint8_t index) {
52   uint8_t default_layer = eeconfig_read_default_layer();
53   if (default_layer & (1UL << _COLEMAK)) {
54     rgblight_sethsv_at(300, 255, 255, index);
55     rgblight_sethsv_at(300, 255, 255, index);
56   }
57   else if (default_layer & (1UL << _DVORAK)) {
58     rgblight_sethsv_at(120, 255, 255, index);
59     rgblight_sethsv_at(120, 255, 255, index);
60   }
61   else if (default_layer & (1UL << _WORKMAN)) {
62     rgblight_sethsv_at(43, 255, 255, index);
63     rgblight_sethsv_at(43, 255, 255, index);
64   }
65   else {
66     rgblight_sethsv_at(180, 255, 255, index);
67     rgblight_sethsv_at(180, 255, 255, index);
68   }
69 }
70 #endif // RGBLIGHT_ENABLE
71
72
73 // =========================================  TAP DANCE  =========================================
74 #ifdef TAP_DANCE_ENABLE
75 //define diablo macro timer variables
76 static uint16_t diablo_timer[4];
77 static uint8_t diablo_times[] = { 0, 1, 3, 5, 10, 30 };
78 static uint8_t diablo_key_time[4];
79
80 // has the correct number of seconds elapsed (as defined by diablo_times)
81 bool check_dtimer(uint8_t dtimer) { return (timer_elapsed(diablo_timer[dtimer]) < (diablo_key_time[dtimer] * 1000)) ? false : true; };
82
83 // Cycle through the times for the macro, starting at 0, for disabled.
84 // Max of six values, so don't exceed
85 void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data, uint8_t diablo_key) {
86   if (state->count >= 7) {
87     diablo_key_time[diablo_key] = diablo_times[0];
88     reset_tap_dance(state);
89   }  else {
90     diablo_key_time[diablo_key] = diablo_times[state->count - 1];
91   }
92 }
93
94 // Would rather have one function for all of this, but no idea how to do that...
95 void diablo_tapdance1(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 0); }
96 void diablo_tapdance2(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 1); }
97 void diablo_tapdance3(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 2); }
98 void diablo_tapdance4(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 3); }
99
100 //Tap Dance Definitions
101 qk_tap_dance_action_t tap_dance_actions[] = {
102   // tap once to disable, and more to enable timed micros
103   [TD_D3_1] = ACTION_TAP_DANCE_FN(diablo_tapdance1),
104   [TD_D3_2] = ACTION_TAP_DANCE_FN(diablo_tapdance2),
105   [TD_D3_3] = ACTION_TAP_DANCE_FN(diablo_tapdance3),
106   [TD_D3_4] = ACTION_TAP_DANCE_FN(diablo_tapdance4),
107 };
108
109 // Sends the key press to system, but only if on the Diablo layer
110 void send_diablo_keystroke(uint8_t diablo_key) {
111   if (biton32(layer_state) == _DIABLO) {
112     switch (diablo_key) {
113       case 0:
114         tap(KC_1); break;
115       case 1:
116         tap(KC_2); break;
117       case 2:
118         tap(KC_3); break;
119       case 3:
120         tap(KC_4); break;
121     }
122   }
123 }
124
125 // Checks each of the 4 timers/keys to see if enough time has elapsed
126 // Runs the "send string" command if enough time has passed, and resets the timer.
127 void run_diablo_macro_check(void) {
128   uint8_t dtime;
129   for (dtime = 0; dtime < 4; dtime++) {
130     if (check_dtimer(dtime) && diablo_key_time[dtime]) {
131       diablo_timer[dtime] = timer_read();
132       send_diablo_keystroke(dtime);
133     }
134   }
135 }
136 #endif // TAP_DANCE_ENABLE
137
138
139 // Add reconfigurable functions here, for keymap customization
140 // This allows for a global, userspace functions, and continued
141 // customization of the keymap.  Use _keymap instead of _user
142 // functions in the keymaps
143 __attribute__ ((weak))
144 void matrix_init_keymap(void) {}
145
146 __attribute__ ((weak))
147 void matrix_scan_keymap(void) {}
148
149 __attribute__ ((weak))
150 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
151   return true;
152 }
153
154 __attribute__ ((weak))
155 uint32_t layer_state_set_keymap (uint32_t state) {
156   return state;
157 }
158
159 __attribute__ ((weak))
160 void led_set_keymap(uint8_t usb_led) {}
161
162
163 // Call user matrix init, set default RGB colors and then
164 // call the keymap's init function
165 void matrix_init_user(void) {
166   uint8_t default_layer = eeconfig_read_default_layer();
167
168 #ifdef RGBLIGHT_ENABLE
169   rgblight_enable();
170 #endif // RGBLIGHT_ENABLE
171
172   if (default_layer & (1UL << _COLEMAK)) {
173 #ifdef RGBLIGHT_ENABLE
174     rgblight_sethsv_magenta();
175 #endif // RGBLIGHT_ENABLE
176   } else if (default_layer & (1UL << _DVORAK)) {
177 #ifdef RGBLIGHT_ENABLE
178     rgblight_sethsv_green();
179 #endif // RGBLIGHT_ENABLE
180   } else if (default_layer & (1UL << _WORKMAN)) {
181 #ifdef RGBLIGHT_ENABLE
182     rgblight_sethsv_goldenrod();
183 #endif // RGBLIGHT_ENABLE
184   } else {
185 #ifdef RGBLIGHT_ENABLE
186     rgblight_sethsv_teal();
187 #endif // RGBLIGHT_ENABLE
188   }
189
190   userspace_config.raw = eeprom_read_byte(EECONFIG_USERSPACE);
191   clicky_enable = userspace_config.clicky_enable;
192
193 #if ( defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE) )
194         set_unicode_input_mode(UC_WINC);
195 #endif //UNICODE_ENABLE
196
197   matrix_init_keymap();
198 }
199 // No global matrix scan code, so just run keymap's matrix
200 // scan function
201 void matrix_scan_user(void) {
202
203 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
204   run_diablo_macro_check();
205 #endif // TAP_DANCE_ENABLE
206
207   matrix_scan_keymap();
208 }
209
210
211 // This block is for all of the gaming macros, as they were all doing
212 // the same thing, but with differring text sent.
213 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
214   if (!record->event.pressed || override) {
215     clear_keyboard();
216     tap(userspace_config.is_overwatch ? KC_BSPC : KC_ENTER);
217     wait_ms(50);
218     send_string(str);
219     wait_ms(50);
220     tap(KC_ENTER);
221   }
222   if (override) wait_ms(3000);
223   return false;
224 }
225
226
227 // Defines actions tor my global custom keycodes. Defined in drashna.h file
228 // Then runs the _keymap's record handier if not processed here
229 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
230
231   // If console is enabled, it will print the matrix position and status of each key pressed
232 #ifdef CONSOLE_ENABLE
233   xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
234 #endif //CONSOLE_ENABLE
235
236
237   switch (keycode) {
238   case KC_QWERTY:
239     if (record->event.pressed) {
240       set_single_persistent_default_layer(_QWERTY);
241     }
242     return false;
243     break;
244   case KC_COLEMAK:
245     if (record->event.pressed) {
246       set_single_persistent_default_layer(_COLEMAK);
247     }
248     return false;
249     break;
250   case KC_DVORAK:
251     if (record->event.pressed) {
252       set_single_persistent_default_layer(_DVORAK);
253     }
254     return false;
255     break;
256   case KC_WORKMAN:
257     if (record->event.pressed) {
258       set_single_persistent_default_layer(_WORKMAN);
259     }
260     return false;
261     break;
262
263
264   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
265     if (!record->event.pressed) {
266       SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
267 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
268                    ":dfu"
269 #elif defined(BOOTLOADER_HALFKAY)
270                    ":teensy"
271 #elif defined(BOOTLOADER_CATERINA)
272                    ":avrdude"
273 #endif // bootloader options
274                    SS_TAP(X_ENTER));
275     }
276     return false;
277     break;
278
279
280   case KC_RESET: // Custom RESET code that sets RGBLights to RED
281     if (!record->event.pressed) {
282 #ifdef RGBLIGHT_ENABLE
283       rgblight_enable();
284       rgblight_mode(1);
285       rgblight_setrgb_red();
286 #endif // RGBLIGHT_ENABLE
287       reset_keyboard();
288     }
289     return false;
290     break;
291
292
293   case EPRM: // Resets EEPROM
294     if (record->event.pressed) {
295       eeconfig_init();
296     }
297     return false;
298     break;
299   case VRSN: // Prints firmware version
300     if (record->event.pressed) {
301       SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE);
302     }
303     return false;
304     break;
305
306
307   case KC_SECRET_1 ... KC_SECRET_5: // Secrets!  Externally defined strings, not stored in repo
308     if (!record->event.pressed) {
309       clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
310       send_string_P(secret[keycode - KC_SECRET_1]);
311     }
312     return false;
313     break;
314
315
316 // These are a serious of gaming macros.
317 // Only enables for the viterbi, basically,
318 // to save on firmware space, since it's limited.
319 #if !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2))
320   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
321     if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
322 #ifdef RGBLIGHT_ENABLE
323     userspace_config.is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
324 #endif //RGBLIGHT_ENABLE
325     return false; break;
326   case KC_SALT:
327     return send_game_macro("Salt, salt, salt...", record, false);
328   case KC_MORESALT:
329     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
330   case KC_SALTHARD:
331     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
332   case KC_GOODGAME:
333     return send_game_macro("Good game, everyone!", record, false);
334   case KC_GLHF:
335     return send_game_macro("Good luck, have fun!!!", record, false);
336   case KC_SYMM:
337     return send_game_macro("Left click to win!", record, false);
338   case KC_JUSTGAME:
339     return send_game_macro("It may be a game, but if you don't want to actually try, please go play AI, so that people that actually want to take the game seriously and \"get good\" have a place to do so without trolls like you throwing games.", record, false);
340   case KC_TORB:
341     return send_game_macro("That was positively riveting!", record, false);
342   case KC_AIM:
343     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
344     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
345   case KC_C9:
346     return send_game_macro("OMG!!!  C9!!!", record, false);
347   case KC_GGEZ:
348     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
349 #endif // !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
350
351
352 #ifdef TAP_DANCE_ENABLE
353   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
354     if (record->event.pressed) {
355       uint8_t dtime;
356       for (dtime = 0; dtime < 4; dtime++) {
357         diablo_key_time[dtime] = diablo_times[0];
358       }
359     }
360     return false; break;
361 #endif // TAP_DANCE_ENABLE
362
363
364   case KC_RGB_T:  // This allows me to use underglow as layer indication, or as normal
365 #ifdef RGBLIGHT_ENABLE
366     if (record->event.pressed) {
367       rgb_layer_change = !rgb_layer_change;
368       if (rgb_layer_change) {
369         layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
370       }
371     }
372 #endif // RGBLIGHT_ENABLE
373     return false; break;
374 #ifdef RGBLIGHT_ENABLE
375   case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
376     if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
377       rgb_layer_change = false;
378     }
379     return true; break;
380 #endif // RGBLIGHT_ENABLE
381
382
383   case KC_CCCV:                                    // One key copy/paste
384     if(record->event.pressed){
385       copy_paste_timer = timer_read();
386     } else {
387       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
388         register_code(KC_LCTL);
389         tap(KC_C);
390         unregister_code(KC_LCTL);
391 #ifdef AUDIO_ENABLE
392         PLAY_SONG(tone_copy);
393 #endif
394       } else {                                // Tap, paste
395         register_code(KC_LCTL);
396         tap(KC_V);
397         unregister_code(KC_LCTL);
398 #ifdef AUDIO_ENABLE
399         PLAY_SONG(tone_paste);
400 #endif
401       }
402     }
403     return false;
404     break;
405   case CLICKY_TOGGLE:
406     userspace_config.clicky_enable = clicky_enable;
407     eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
408     break;
409 #ifdef UNICODE_ENABLE
410   case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
411     if (record->event.pressed) {
412       register_code(KC_RSFT);
413       tap(KC_9);
414       unregister_code(KC_RSFT);
415       process_unicode((0x256F | QK_UNICODE), record); // Arm
416       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
417       process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
418       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
419       register_code(KC_RSFT);
420       tap(KC_0);
421       unregister_code(KC_RSFT);
422       process_unicode((0x256F | QK_UNICODE), record); // Arm
423       tap(KC_SPC);
424       process_unicode((0x0361 | QK_UNICODE), record); // Flippy
425       tap(KC_SPC);
426       process_unicode((0x253B | QK_UNICODE), record); // Table
427       process_unicode((0x2501 | QK_UNICODE), record); // Table
428       process_unicode((0x253B | QK_UNICODE), record); // Table
429     }
430     return false;
431     break;
432 #endif // UNICODE_ENABLE
433
434   }
435   return process_record_keymap(keycode, record);
436 }
437
438
439
440 // Runs state check and changes underglow color and animation
441 // on layer change, no matter where the change was initiated
442 // Then runs keymap's layer change check
443 uint32_t layer_state_set_user(uint32_t state) {
444   uint8_t default_layer = eeconfig_read_default_layer();
445   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
446
447   switch (biton32(state)) {
448   case _NAV:
449 #ifdef RGBLIGHT_ENABLE
450     if (rgb_layer_change) {
451       rgblight_sethsv_blue();
452       rgblight_mode(1);
453     }
454 #endif // RGBLIGHT_ENABLE
455     break;
456   case _SYMB:
457 #ifdef RGBLIGHT_ENABLE
458     if (rgb_layer_change) {
459       rgblight_sethsv_blue();
460       rgblight_mode(2);
461     }
462 #endif // RGBLIGHT_ENABLE
463     break;
464   case _MOUS:
465 #ifdef RGBLIGHT_ENABLE
466     if (rgb_layer_change) {
467       rgblight_sethsv_yellow();
468       rgblight_mode(1);
469     }
470 #endif // RGBLIGHT_ENABLE
471     break;
472   case _MACROS:
473 #ifdef RGBLIGHT_ENABLE
474     if (rgb_layer_change) {
475       rgblight_sethsv_orange();
476       userspace_config.is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
477     }
478 #endif // RGBLIGHT_ENABLE
479     break;
480   case _MEDIA:
481 #ifdef RGBLIGHT_ENABLE
482     if (rgb_layer_change) {
483       rgblight_sethsv_chartreuse();
484       rgblight_mode(22);
485     }
486 #endif // RGBLIGHT_ENABLE
487     break;
488   case _GAMEPAD:
489 #ifdef RGBLIGHT_ENABLE
490     if (rgb_layer_change) {
491       rgblight_sethsv_orange();
492       rgblight_mode(17);
493     }
494 #endif // RGBLIGHT_ENABLE
495     break;
496   case _DIABLO:
497 #ifdef RGBLIGHT_ENABLE
498     if (rgb_layer_change) {
499       rgblight_sethsv_red();
500       rgblight_mode(5);
501     }
502 #endif // RGBLIGHT_ENABLE
503     break;
504   case _RAISE:
505 #ifdef RGBLIGHT_ENABLE
506     if (rgb_layer_change) {
507       rgblight_sethsv_yellow();
508       rgblight_mode(5);
509     }
510 #endif // RGBLIGHT_ENABLE
511     break;
512   case _LOWER:
513 #ifdef RGBLIGHT_ENABLE
514     if (rgb_layer_change) {
515       rgblight_sethsv_orange();
516       rgblight_mode(5);
517     }
518 #endif // RGBLIGHT_ENABLE
519     break;
520   case _ADJUST:
521 #ifdef RGBLIGHT_ENABLE
522     if (rgb_layer_change) {
523       rgblight_sethsv_red();
524       rgblight_mode(23);
525     }
526 #endif // RGBLIGHT_ENABLE
527     break;
528   case _COVECUBE:
529 #ifdef RGBLIGHT_ENABLE
530     if (rgb_layer_change) {
531       rgblight_sethsv_green();
532       rgblight_mode(2);
533     }
534 #endif // RGBLIGHT_ENABLE
535     break;
536   default: //  for any other layers, or the default layer
537     if (default_layer & (1UL << _COLEMAK)) {
538 #ifdef RGBLIGHT_ENABLE
539       if (rgb_layer_change) { rgblight_sethsv_magenta(); }
540 #endif // RGBLIGHT_ENABLE
541     }
542     else if (default_layer & (1UL << _DVORAK)) {
543 #ifdef RGBLIGHT_ENABLE
544       if (rgb_layer_change) { rgblight_sethsv_green(); }
545 #endif // RGBLIGHT_ENABLE
546     }
547     else if (default_layer & (1UL << _WORKMAN)) {
548 #ifdef RGBLIGHT_ENABLE
549       if (rgb_layer_change) { rgblight_sethsv_goldenrod(); }
550 #endif // RGBLIGHT_ENABLE
551     }
552     else {
553 #ifdef RGBLIGHT_ENABLE
554       if (rgb_layer_change) { rgblight_sethsv_teal(); }
555 #endif // RGBLIGHT_ENABLE
556     }
557     if (biton32(state) == _MODS) { // If the non-OSM layer is enabled, then breathe
558 #ifdef RGBLIGHT_ENABLE
559       if (rgb_layer_change) { rgblight_mode(2); }
560 #endif // RGBLIGHT_ENABLE
561     } else {                       // otherwise, stay solid
562 #ifdef RGBLIGHT_ENABLE
563       if (rgb_layer_change) { rgblight_mode(1); }
564 #endif // RGBLIGHT_ENABLE
565     }
566     break;
567   }
568   return layer_state_set_keymap (state);
569 }
570
571
572 // Any custom LED code goes here.
573 // So far, I only have keyboard specific code,
574 // So nothing goes here.
575 void led_set_user(uint8_t usb_led) {
576   led_set_keymap(usb_led);
577 }