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