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