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