]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
31f58a430ceafc12fcdeb87df330453584131c67
[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 "quantum.h"
20 #include "action.h"
21 #include "version.h"
22
23 #ifdef TAP_DANCE_ENABLE
24 //define diablo macro timer variables
25 static uint16_t diablo_timer[4];
26 static uint8_t diablo_times[] = { 0, 1, 3, 5, 10, 30 };
27 static uint8_t diablo_key_time[4];
28
29
30 bool check_dtimer(uint8_t dtimer) {
31   // has the correct number of seconds elapsed (as defined by diablo_times)
32   return (timer_elapsed(diablo_timer[dtimer]) < (diablo_key_time[dtimer] * 1000)) ? false : true;
33 };
34
35
36
37
38 // Cycle through the times for the macro, starting at 0, for disabled.
39 // Max of six values, so don't exceed
40 void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data, uint8_t diablo_key) {
41   if (state->count >= 7) {
42     diablo_key_time[diablo_key] = diablo_times[0];
43     reset_tap_dance(state);
44   }
45   else {
46     diablo_key_time[diablo_key] = diablo_times[state->count - 1];
47   }
48 }
49
50
51 // Would rather have one function for all of this, but no idea how to do that...
52 void diablo_tapdance1(qk_tap_dance_state_t *state, void *user_data) {
53   diablo_tapdance_master(state, user_data, 0);
54 }
55
56 void diablo_tapdance2(qk_tap_dance_state_t *state, void *user_data) {
57   diablo_tapdance_master(state, user_data, 1);
58 }
59
60 void diablo_tapdance3(qk_tap_dance_state_t *state, void *user_data) {
61   diablo_tapdance_master(state, user_data, 2);
62 }
63
64 void diablo_tapdance4(qk_tap_dance_state_t *state, void *user_data) {
65   diablo_tapdance_master(state, user_data, 3);
66 }
67
68
69
70 //Tap Dance Definitions
71 qk_tap_dance_action_t tap_dance_actions[] = {
72   // tap once to disable, and more to enable timed micros
73   [TD_D3_1] = ACTION_TAP_DANCE_FN(diablo_tapdance1),
74   [TD_D3_2] = ACTION_TAP_DANCE_FN(diablo_tapdance2),
75   [TD_D3_3] = ACTION_TAP_DANCE_FN(diablo_tapdance3),
76   [TD_D3_4] = ACTION_TAP_DANCE_FN(diablo_tapdance4),
77
78 };
79 #endif
80
81
82 // Add reconfigurable functions here, for keymap customization
83 // This allows for a global, userspace functions, and continued
84 // customization of the keymap.  Use _keymap instead of _user
85 // functions in the keymaps
86 __attribute__ ((weak))
87 void matrix_init_keymap(void) {}
88
89 __attribute__ ((weak))
90 void matrix_scan_keymap(void) {}
91
92 __attribute__ ((weak))
93 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
94   return true;
95 }
96
97 __attribute__ ((weak))
98 uint32_t layer_state_set_keymap (uint32_t state) {
99   return state;
100 }
101
102 __attribute__ ((weak))
103 void led_set_keymap(uint8_t usb_led) {}
104
105 bool is_overwatch = false;
106 #ifdef RGBLIGHT_ENABLE
107 bool rgb_layer_change = true;
108 #endif
109
110
111
112
113 // Call user matrix init, set default RGB colors and then
114 // call the keymap's init function
115 void matrix_init_user(void) {
116 #ifdef RGBLIGHT_ENABLE
117   uint8_t default_layer = eeconfig_read_default_layer();
118
119   rgblight_enable();
120
121   if (true) {
122     if (default_layer & (1UL << _COLEMAK)) {
123       rgblight_set_magenta;
124     }
125     else if (default_layer & (1UL << _DVORAK)) {
126       rgblight_set_green;
127     }
128     else if (default_layer & (1UL << _WORKMAN)) {
129       rgblight_set_purple;
130     }
131     else {
132       rgblight_set_teal;
133     }
134   }
135   else
136   {
137     rgblight_set_red;
138     rgblight_mode(5);
139   }
140 #endif
141   matrix_init_keymap();
142 }
143 #ifdef TAP_DANCE_ENABLE
144
145 // Sends the key press to system, but only if on the Diablo layer
146 void send_diablo_keystroke(uint8_t diablo_key) {
147   if (biton32(layer_state) == _DIABLO) {
148     switch (diablo_key) {
149     case 0:
150       SEND_STRING("1");
151       break;
152     case 1:
153       SEND_STRING("2");
154       break;
155     case 2:
156       SEND_STRING("3");
157       break;
158     case 3:
159       SEND_STRING("4");
160       break;
161     }
162   }
163 }
164
165 // Checks each of the 4 timers/keys to see if enough time has elapsed
166 // Runs the "send string" command if enough time has passed, and resets the timer.
167 void run_diablo_macro_check(void) {
168   uint8_t dtime;
169
170   for (dtime = 0; dtime < 4; dtime++) {
171     if (check_dtimer(dtime) && diablo_key_time[dtime]) {
172       diablo_timer[dtime] = timer_read();
173       send_diablo_keystroke(dtime);
174     }
175   }
176
177 }
178 #endif
179 // No global matrix scan code, so just run keymap's matix
180 // scan function
181 void matrix_scan_user(void) {
182 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
183   run_diablo_macro_check();
184 #endif
185   matrix_scan_keymap();
186 }
187
188 void led_set_user(uint8_t usb_led) {
189   led_set_keymap(usb_led);
190 }
191
192
193 #ifdef AUDIO_ENABLE
194 float tone_qwerty[][2]     = SONG(QWERTY_SOUND);
195 float tone_dvorak[][2]     = SONG(DVORAK_SOUND);
196 float tone_colemak[][2]    = SONG(COLEMAK_SOUND);
197 float tone_workman[][2]    = SONG(PLOVER_SOUND);
198 #endif
199
200
201 void persistent_default_layer_set(uint16_t default_layer) {
202   eeconfig_update_default_layer(default_layer);
203   default_layer_set(default_layer);
204 }
205
206 // Defines actions tor my global custom keycodes. Defined in drashna.h file
207 // Then runs the _keymap's recod handier if not processed here
208 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
209   
210 #ifdef CONSOLE_ENABLE
211   xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
212 #endif
213
214   switch (keycode) {
215   case KC_QWERTY:
216     if (record->event.pressed) {
217 #ifdef AUDIO_ENABLE
218       PLAY_SONG(tone_qwerty);
219 #endif
220       persistent_default_layer_set(1UL << _QWERTY);
221     }
222     return false;
223     break;
224   case KC_COLEMAK:
225     if (record->event.pressed) {
226 #ifdef AUDIO_ENABLE
227       PLAY_SONG(tone_colemak);
228 #endif
229       persistent_default_layer_set(1UL << _COLEMAK);
230     }
231     return false;
232     break;
233   case KC_DVORAK:
234     if (record->event.pressed) {
235 #ifdef AUDIO_ENABLE
236       PLAY_SONG(tone_dvorak);
237 #endif
238       persistent_default_layer_set(1UL << _DVORAK);
239     }
240     return false;
241     break;
242   case KC_WORKMAN:
243     if (record->event.pressed) {
244 #ifdef AUDIO_ENABLE
245       PLAY_SONG(tone_workman);
246 #endif
247       persistent_default_layer_set(1UL << _WORKMAN);
248     }
249     return false;
250     break;
251   case LOWER:
252     if (record->event.pressed) {
253       layer_on(_LOWER);
254       update_tri_layer(_LOWER, _RAISE, _ADJUST);
255     }
256     else {
257       layer_off(_LOWER);
258       update_tri_layer(_LOWER, _RAISE, _ADJUST);
259     }
260     return false;
261     break;
262   case RAISE:
263     if (record->event.pressed) {
264       layer_on(_RAISE);
265       update_tri_layer(_LOWER, _RAISE, _ADJUST);
266     }
267     else {
268       layer_off(_RAISE);
269       update_tri_layer(_LOWER, _RAISE, _ADJUST);
270     }
271     return false;
272     break;
273   case ADJUST:
274     if (record->event.pressed) {
275       layer_on(_ADJUST);
276     }
277     else {
278       layer_off(_ADJUST);
279     }
280     return false;
281     break;
282 #if !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_ergodox_ez))
283   case KC_OVERWATCH:
284     if (record->event.pressed) {
285       is_overwatch = !is_overwatch;
286     }
287 #ifdef RGBLIGHT_ENABLE
288     is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
289 #endif
290     return false;
291     break;
292   case KC_SALT:
293     if (!record->event.pressed) {
294       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
295       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
296       _delay_ms(50);
297       SEND_STRING("Salt, salt, salt...");
298       register_code(KC_ENTER);
299       unregister_code(KC_ENTER);
300     }
301     return false;
302     break;
303   case KC_MORESALT:
304     if (!record->event.pressed) {
305       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
306       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
307       _delay_ms(50);
308       SEND_STRING("Please sir, can I have some more salt?!");
309       register_code(KC_ENTER);
310       unregister_code(KC_ENTER);
311     }
312     return false;
313     break;
314   case KC_SALTHARD:
315     if (!record->event.pressed) {
316       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
317       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
318       _delay_ms(50);
319       SEND_STRING("Your salt only makes me harder, and even more aggressive!");
320       register_code(KC_ENTER);
321       unregister_code(KC_ENTER);
322     }
323     return false;
324     break;
325   case KC_GOODGAME:
326     if (!record->event.pressed) {
327       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
328       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
329       _delay_ms(50);
330       SEND_STRING("Good game, everyone!");
331       register_code(KC_ENTER);
332       unregister_code(KC_ENTER);
333     }
334     return false;
335     break;
336   case KC_GLHF:
337     if (!record->event.pressed) {
338       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
339       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
340       _delay_ms(50);
341       SEND_STRING("Good luck, have fun!!!");
342       register_code(KC_ENTER);
343       unregister_code(KC_ENTER);
344     }
345     return false;
346     break;
347   case KC_SYMM:
348     if (!record->event.pressed) {
349       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
350       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
351       _delay_ms(50);
352       SEND_STRING("Left click to win!");
353       register_code(KC_ENTER);
354       unregister_code(KC_ENTER);
355     }
356     return false;
357     break;
358   case KC_JUSTGAME:
359     if (!record->event.pressed) {
360       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
361       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
362       _delay_ms(50);
363       SEND_STRING("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.");
364       register_code(KC_ENTER);
365       unregister_code(KC_ENTER);
366     }
367     return false;
368     break;
369   case KC_TORB:
370     if (!record->event.pressed) {
371       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
372       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
373       _delay_ms(50);
374       SEND_STRING("That was positively riveting!");
375       register_code(KC_ENTER);
376       unregister_code(KC_ENTER);
377     }
378     return false;
379     break;
380   case KC_AIM:
381     if (!record->event.pressed) {
382       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
383       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
384       _delay_ms(50);
385       SEND_STRING("That aim is absolutely amazing. It's almost like you're a machine!" SS_TAP(X_ENTER));
386       _delay_ms(3000);
387       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
388       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
389       SEND_STRING("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!" SS_TAP(X_ENTER));
390     }
391     return false;
392     break;
393   case KC_C9:
394     if (!record->event.pressed) {
395       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
396       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
397       _delay_ms(50);
398       SEND_STRING("OMG!!!  C9!!!");
399       register_code(KC_ENTER);
400       unregister_code(KC_ENTER);
401     }
402     return false;
403     break;
404   case KC_GGEZ:
405     if (!record->event.pressed) {
406       register_code(is_overwatch ? KC_BSPC : KC_ENTER);
407       unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
408       _delay_ms(50);
409       SEND_STRING("That was a fantastic game, though it was a bit easy. Try harder next time!");
410       register_code(KC_ENTER);
411       unregister_code(KC_ENTER);
412     }
413     return false;
414     break;
415 #endif
416 #ifdef TAP_DANCE_ENABLE
417   case KC_DIABLO_CLEAR:  // reset all Diable timers, disabling them
418     if (record->event.pressed) {
419       uint8_t dtime;
420
421       for (dtime = 0; dtime < 4; dtime++) {
422         diablo_key_time[dtime] = diablo_times[0];
423       }
424     }
425     return false;
426     break;
427 #endif
428   case KC_MAKE:
429     if (!record->event.pressed) {
430       SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP);
431 #ifndef CATERINA_BOOTLOADER
432       SEND_STRING(":teensy ");
433 #else
434       SEND_STRING(" ");
435 #endif
436 #ifdef RGBLIGHT_ENABLE
437       SEND_STRING("RGBLIGHT_ENABLE=yes ");
438 #else
439       SEND_STRING("RGBLIGHT_ENABLE=no ");
440 #endif
441 #ifdef AUDIO_ENABLE
442       SEND_STRING("AUDIO_ENABLE=yes ");
443 #else
444       SEND_STRING("AUDIO_ENABLE=no ");
445 #endif
446 #ifdef FAUXCLICKY_ENABLE
447       SEND_STRING("FAUXCLICKY_ENABLE=yes ");
448 #else
449       SEND_STRING("FAUXCLICKY_ENABLE=no ");
450 #endif
451       SEND_STRING(SS_TAP(X_ENTER));
452     }
453     return false;
454     break;
455   case KC_RESET:
456     if (!record->event.pressed) {
457 #ifdef RGBLIGHT_ENABLE
458       rgblight_enable();
459       rgblight_mode(1);
460       rgblight_setrgb(0xff, 0x00, 0x00);
461 #endif
462       reset_keyboard();
463     }
464     return false;
465     break;
466   case EPRM:
467     if (record->event.pressed) {
468       eeconfig_init();
469     }
470     return false;
471     break;
472   case VRSN:
473     if (record->event.pressed) {
474       SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION);
475     }
476     return false;
477     break;
478   case KC_RGB_T:  // Because I want the option to go back to normal RGB mode rather than always layer indication
479     if (record->event.pressed) {
480       rgb_layer_change = !rgb_layer_change;
481     }
482     return false;
483     break;
484   case RGB_MOD:
485   case RGB_SMOD:
486   case RGB_HUI:
487   case RGB_HUD:
488   case RGB_SAI:
489   case RGB_SAD:
490   case RGB_VAI:
491   case RGB_VAD:
492   case RGB_MODE_PLAIN:
493   case RGB_MODE_BREATHE:
494   case RGB_MODE_RAINBOW:
495   case RGB_MODE_SWIRL:
496   case RGB_MODE_SNAKE:
497   case RGB_MODE_KNIGHT:
498   case RGB_MODE_XMAS:
499   case RGB_MODE_GRADIENT:
500     if (record->event.pressed) {
501       rgb_layer_change = false;
502     }
503     return true;
504     break;
505   }
506   return process_record_keymap(keycode, record);
507 }
508
509 // Runs state check and changes underglow color and animation
510 // on layer change, no matter where the change was initiated
511 // Then runs keymap's layer change check
512 uint32_t layer_state_set_user(uint32_t state) {
513 #ifdef RGBLIGHT_ENABLE
514   uint8_t default_layer = eeconfig_read_default_layer();
515   if (rgb_layer_change) {
516     switch (biton32(state)) {
517     case _NAV:
518       rgblight_set_blue;
519       rgblight_mode(1);
520       break;
521     case _SYMB:
522       rgblight_set_blue;
523       rgblight_mode(2);
524       break;
525     case _MOUS:
526       rgblight_set_yellow;
527       rgblight_mode(1);
528       break;
529     case _MACROS:
530       rgblight_set_orange;
531       is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
532       break;
533     case _MEDIA:
534       rgblight_set_green;
535       rgblight_mode(22);
536       break;
537     case _OVERWATCH:
538       rgblight_set_orange;
539       rgblight_mode(17);
540       break;
541     case _DIABLO:
542       rgblight_set_red;
543       rgblight_mode(5);
544       break;
545     case _RAISE:
546       rgblight_set_yellow;
547       rgblight_mode(5);
548       break;
549     case _LOWER:
550       rgblight_set_orange;
551       rgblight_mode(5);
552       break;
553     case _ADJUST:
554       rgblight_set_red;
555       rgblight_mode(23);
556       break;
557     case _COVECUBE:
558       rgblight_set_green;
559       rgblight_mode(2);
560     default:
561       if (default_layer & (1UL << _COLEMAK)) {
562         rgblight_set_magenta;
563       }
564       else if (default_layer & (1UL << _DVORAK)) {
565         rgblight_set_green;
566       }
567       else if (default_layer & (1UL << _WORKMAN)) {
568         rgblight_set_purple;
569       }
570       else {
571         rgblight_set_teal;
572       }
573       rgblight_mode(1);
574       break;
575     }
576   }
577 #endif
578   return layer_state_set_keymap (state);
579 }
580
581