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