]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
d9d412b086d35f9dcb7eb6c1cb6e334488b3dc02
[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 #ifdef FAUXCLICKY_ENABLE
37 float fauxclicky_pressed_note[2]  = MUSICAL_NOTE(_A6, 2);  // (_D4, 0.25);
38 float fauxclicky_released_note[2] = MUSICAL_NOTE(_A6, 2); // (_C4, 0.125);
39 #else // FAUXCLICKY_ENABLE
40 float fauxclicky_pressed[][2]             = SONG(S__NOTE(_A6)); // change to your tastes
41 float fauxclicky_released[][2]             = SONG(S__NOTE(_A6)); // change to your tastes
42 #endif // FAUXCLICKY_ENABLE
43
44 float tone_copy[][2]            = SONG(SCROLL_LOCK_ON_SOUND);
45 float tone_paste[][2]           = SONG(SCROLL_LOCK_OFF_SOUND);
46
47
48 bool faux_click_enabled = false;
49 bool is_overwatch = false;
50 static uint16_t copy_paste_timer;
51 #ifdef RGBLIGHT_ENABLE
52 bool rgb_layer_change = true;
53 #endif
54
55 #ifdef TAP_DANCE_ENABLE
56 //define diablo macro timer variables
57 static uint16_t diablo_timer[4];
58 static uint8_t diablo_times[] = { 0, 1, 3, 5, 10, 30 };
59 static uint8_t diablo_key_time[4];
60
61 bool check_dtimer(uint8_t dtimer) {
62   // has the correct number of seconds elapsed (as defined by diablo_times)
63   return (timer_elapsed(diablo_timer[dtimer]) < (diablo_key_time[dtimer] * 1000)) ? false : true;
64 };
65
66 // Cycle through the times for the macro, starting at 0, for disabled.
67 // Max of six values, so don't exceed
68 void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data, uint8_t diablo_key) {
69   if (state->count >= 7) {
70     diablo_key_time[diablo_key] = diablo_times[0];
71     reset_tap_dance(state);
72   }
73   else {
74     diablo_key_time[diablo_key] = diablo_times[state->count - 1];
75   }
76 }
77
78 // Would rather have one function for all of this, but no idea how to do that...
79 void diablo_tapdance1(qk_tap_dance_state_t *state, void *user_data) {
80   diablo_tapdance_master(state, user_data, 0);
81 }
82 void diablo_tapdance2(qk_tap_dance_state_t *state, void *user_data) {
83   diablo_tapdance_master(state, user_data, 1);
84 }
85 void diablo_tapdance3(qk_tap_dance_state_t *state, void *user_data) {
86   diablo_tapdance_master(state, user_data, 2);
87 }
88 void diablo_tapdance4(qk_tap_dance_state_t *state, void *user_data) {
89   diablo_tapdance_master(state, user_data, 3);
90 }
91
92 //Tap Dance Definitions
93 qk_tap_dance_action_t tap_dance_actions[] = {
94   // tap once to disable, and more to enable timed micros
95   [TD_D3_1] = ACTION_TAP_DANCE_FN(diablo_tapdance1),
96   [TD_D3_2] = ACTION_TAP_DANCE_FN(diablo_tapdance2),
97   [TD_D3_3] = ACTION_TAP_DANCE_FN(diablo_tapdance3),
98   [TD_D3_4] = ACTION_TAP_DANCE_FN(diablo_tapdance4),
99
100 };
101
102 // Sends the key press to system, but only if on the Diablo layer
103 void send_diablo_keystroke(uint8_t diablo_key) {
104   if (biton32(layer_state) == _DIABLO) {
105     switch (diablo_key) {
106       case 0:
107         SEND_STRING("1");
108         break;
109       case 1:
110         SEND_STRING("2");
111         break;
112       case 2:
113         SEND_STRING("3");
114         break;
115       case 3:
116         SEND_STRING("4");
117         break;
118     }
119   }
120 }
121
122 // Checks each of the 4 timers/keys to see if enough time has elapsed
123 // Runs the "send string" command if enough time has passed, and resets the timer.
124 void run_diablo_macro_check(void) {
125   uint8_t dtime;
126
127   for (dtime = 0; dtime < 4; dtime++) {
128     if (check_dtimer(dtime) && diablo_key_time[dtime]) {
129       diablo_timer[dtime] = timer_read();
130       send_diablo_keystroke(dtime);
131     }
132   }
133 }
134
135 #endif // TAP_DANCE_ENABLE
136
137
138 // Add reconfigurable functions here, for keymap customization
139 // This allows for a global, userspace functions, and continued
140 // customization of the keymap.  Use _keymap instead of _user
141 // functions in the keymaps
142 __attribute__ ((weak))
143 void matrix_init_keymap(void) {}
144
145 __attribute__ ((weak))
146 void matrix_scan_keymap(void) {}
147
148 __attribute__ ((weak))
149 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
150   return true;
151 }
152
153 __attribute__ ((weak))
154 uint32_t layer_state_set_keymap (uint32_t state) {
155   return state;
156 }
157
158 __attribute__ ((weak))
159 void led_set_keymap(uint8_t usb_led) {}
160
161
162 // Call user matrix init, set default RGB colors and then
163 // call the keymap's init function
164 void matrix_init_user(void) {
165 #ifdef RGBLIGHT_ENABLE
166   uint8_t default_layer = eeconfig_read_default_layer();
167
168   rgblight_enable();
169
170   if (true) {
171     if (default_layer & (1UL << _COLEMAK)) {
172       rgblight_sethsv_magenta();
173     }
174     else if (default_layer & (1UL << _DVORAK)) {
175       rgblight_sethsv_green();
176     }
177     else if (default_layer & (1UL << _WORKMAN)) {
178       rgblight_sethsv_goldenrod();
179     }
180     else {
181       rgblight_sethsv_teal();
182     }
183   }
184   else
185   {
186     rgblight_setrgb_red();
187     rgblight_mode(5);
188   }
189 #endif // RGBLIGHT_ENABLE
190
191 #if ( defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE) )
192         set_unicode_input_mode(UC_WINC);
193 #endif //UNICODE_ENABLE
194
195   matrix_init_keymap();
196 }
197 // No global matrix scan code, so just run keymap's matrix
198 // scan function
199 void matrix_scan_user(void) {
200 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
201   run_diablo_macro_check();
202 #endif // TAP_DANCE_ENABLE
203   matrix_scan_keymap();
204 }
205
206 void tap(uint16_t keycode){
207     register_code(keycode);
208     unregister_code(keycode);
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(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 LOWER:
265     if (record->event.pressed) {
266       layer_on(_LOWER);
267       update_tri_layer(_LOWER, _RAISE, _ADJUST);
268     }
269     else {
270       layer_off(_LOWER);
271       update_tri_layer(_LOWER, _RAISE, _ADJUST);
272     }
273     return false;
274     break;
275   case RAISE:
276     if (record->event.pressed) {
277       layer_on(_RAISE);
278       update_tri_layer(_LOWER, _RAISE, _ADJUST);
279     }
280     else {
281       layer_off(_RAISE);
282       update_tri_layer(_LOWER, _RAISE, _ADJUST);
283     }
284     return false;
285     break;
286   case ADJUST:
287     if (record->event.pressed) {
288       layer_on(_ADJUST);
289     }
290     else {
291       layer_off(_ADJUST);
292     }
293     return false;
294     break;
295
296
297   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
298     if (!record->event.pressed) {
299       SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
300 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
301                    ":dfu"
302 #elif defined(BOOTLOADER_HALFKAY)
303                    ":teensy"
304 #elif defined(BOOTLOADER_CATERINA)
305                    ":avrdude"
306 #endif // bootloader options
307                    SS_TAP(X_ENTER));
308     }
309     return false;
310     break;
311
312
313   case KC_RESET: // Custom RESET code that sets RGBLights to RED
314     if (!record->event.pressed) {
315 #ifdef RGBLIGHT_ENABLE
316       rgblight_enable();
317       rgblight_mode(1);
318       rgblight_setrgb_red();
319 #endif // RGBLIGHT_ENABLE
320       reset_keyboard();
321     }
322     return false;
323     break;
324
325
326   case EPRM: // Resets EEPROM
327     if (record->event.pressed) {
328       eeconfig_init();
329     }
330     return false;
331     break;
332   case VRSN: // Prints firmware version
333     if (record->event.pressed) {
334       SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE);
335     }
336     return false;
337     break;
338
339
340   case KC_SECRET_1 ... KC_SECRET_5: // Secrets!  Externally defined strings, not stored in repo
341     if (!record->event.pressed) {
342       clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
343       send_string_P(secret[keycode - KC_SECRET_1]);
344     }
345     return false;
346     break;
347
348
349 // These are a serious of gaming macros.
350 // Only enables for the viterbi, basically,
351 // to save on firmware space, since it's limited.
352 #if !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
353   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
354     if (record->event.pressed) { is_overwatch = !is_overwatch; }
355 #ifdef RGBLIGHT_ENABLE
356     is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
357 #endif //RGBLIGHT_ENABLE
358     return false; break;
359   case KC_SALT:
360     return send_game_macro("Salt, salt, salt...", record, false);
361   case KC_MORESALT:
362     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
363   case KC_SALTHARD:
364     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
365   case KC_GOODGAME:
366     return send_game_macro("Good game, everyone!", record, false);
367   case KC_GLHF:
368     return send_game_macro("Good luck, have fun!!!", record, false);
369   case KC_SYMM:
370     return send_game_macro("Left click to win!", record, false);
371   case KC_JUSTGAME:
372     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);
373   case KC_TORB:
374     return send_game_macro("That was positively riveting!", record, false);
375   case KC_AIM:
376     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
377     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
378   case KC_C9:
379     return send_game_macro("OMG!!!  C9!!!", record, false);
380   case KC_GGEZ:
381     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
382 #endif // !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
383
384
385 #ifdef TAP_DANCE_ENABLE
386   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
387     if (record->event.pressed) {
388       uint8_t dtime;
389       for (dtime = 0; dtime < 4; dtime++) {
390         diablo_key_time[dtime] = diablo_times[0];
391       }
392     }
393     return false; break;
394 #endif // TAP_DANCE_ENABLE
395
396
397   case KC_RGB_T:  // This allows me to use underglow as layer indication, or as normal
398 #ifdef RGBLIGHT_ENABLE
399     if (record->event.pressed) {
400       rgb_layer_change = !rgb_layer_change;
401       if (rgb_layer_change) {
402         layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
403       }
404     }
405 #endif // RGBLIGHT_ENABLE
406     return false; break;
407 #ifdef RGBLIGHT_ENABLE
408   case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
409     if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
410       rgb_layer_change = false;
411     }
412     return true; break;
413 #endif // RGBLIGHT_ENABLE
414
415
416   case KC_CCCV:                                    // One key copy/paste
417     if(record->event.pressed){
418       copy_paste_timer = timer_read();
419     } else {
420       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
421         register_code(KC_LCTL);
422         tap(KC_C);
423         unregister_code(KC_LCTL);
424 #ifdef AUDIO_ENABLE
425         PLAY_SONG(tone_copy);
426 #endif
427       } else {                                // Tap, paste
428         register_code(KC_LCTL);
429         tap(KC_V);
430         unregister_code(KC_LCTL);
431 #ifdef AUDIO_ENABLE
432         PLAY_SONG(tone_paste);
433 #endif
434       }
435     }
436     return false;
437     break;
438
439 #ifdef UNICODE_ENABLE
440   case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
441     if (record->event.pressed) {
442       register_code(KC_RSFT);
443       tap(KC_9);
444       unregister_code(KC_RSFT);
445       process_unicode((0x256F | QK_UNICODE), record); // Arm
446       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
447       process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
448       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
449       register_code(KC_RSFT);
450       tap(KC_0);
451       unregister_code(KC_RSFT);
452       process_unicode((0x256F | QK_UNICODE), record); // Arm
453       tap(KC_SPC);
454       process_unicode((0x0361 | QK_UNICODE), record); // Flippy
455       tap(KC_SPC);
456       process_unicode((0x253B | QK_UNICODE), record); // Table
457       process_unicode((0x2501 | QK_UNICODE), record); // Table
458       process_unicode((0x253B | QK_UNICODE), record); // Table
459     }
460     return false;
461     break;
462 #endif // UNICODE_ENABLE
463
464   }
465   return process_record_keymap(keycode, record);
466 }
467
468
469 // Runs state check and changes underglow color and animation
470 // on layer change, no matter where the change was initiated
471 // Then runs keymap's layer change check
472 uint32_t layer_state_set_user(uint32_t state) {
473 #ifdef RGBLIGHT_ENABLE
474   uint8_t default_layer = eeconfig_read_default_layer();
475
476   if (rgb_layer_change) {
477     switch (biton32(state)) {
478     case _NAV:
479       rgblight_sethsv_blue();
480       rgblight_mode(1);
481       break;
482     case _SYMB:
483       rgblight_sethsv_blue();
484       rgblight_mode(2);
485       break;
486     case _MOUS:
487       rgblight_sethsv_yellow();
488       rgblight_mode(1);
489       break;
490     case _MACROS:
491       rgblight_sethsv_orange();
492       is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
493       break;
494     case _MEDIA:
495       rgblight_sethsv_chartreuse();
496       rgblight_mode(22);
497       break;
498     case _GAMEPAD:
499       rgblight_sethsv_orange();
500       rgblight_mode(17);
501       break;
502     case _DIABLO:
503       rgblight_sethsv_red();
504       rgblight_mode(5);
505       break;
506     case _RAISE:
507       rgblight_sethsv_yellow();
508       rgblight_mode(5);
509       break;
510     case _LOWER:
511       rgblight_sethsv_orange();
512       rgblight_mode(5);
513       break;
514     case _ADJUST:
515       rgblight_sethsv_red();
516       rgblight_mode(23);
517       break;
518     case _COVECUBE:
519       rgblight_sethsv_green();
520       rgblight_mode(2);
521       break;
522     default: //  for any other layers, or the default layer
523       if (default_layer & (1UL << _COLEMAK)) {
524         rgblight_sethsv_magenta();
525       }
526       else if (default_layer & (1UL << _DVORAK)) {
527         rgblight_sethsv_green();
528       }
529       else if (default_layer & (1UL << _WORKMAN)) {
530         rgblight_sethsv_goldenrod();
531       }
532       else {
533         rgblight_sethsv_teal();
534       }
535       if (biton32(state) == _MODS) { // If the non-OSM layer is enabled, then breathe
536         rgblight_mode(2);
537       } else {                       // otherwise, stay solid
538         rgblight_mode(1);
539       }
540       break;
541     }
542   }
543 #endif // RGBLIGHT_ENABLE
544   return layer_state_set_keymap (state);
545 }
546
547
548 // Any custom LED code goes here.
549 // So far, I only have keyboard specific code,
550 // So nothing goes here.
551 void led_set_user(uint8_t usb_led) {
552   led_set_keymap(usb_led);
553 }