]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
Add Faux Clicking as subset of Audio feature (#2748)
[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
228   switch (keycode) {
229   case KC_QWERTY:
230     if (record->event.pressed) {
231       set_single_persistent_default_layer(_QWERTY);
232     }
233     return false;
234     break;
235   case KC_COLEMAK:
236     if (record->event.pressed) {
237       set_single_persistent_default_layer(_COLEMAK);
238     }
239     return false;
240     break;
241   case KC_DVORAK:
242     if (record->event.pressed) {
243       set_single_persistent_default_layer(_DVORAK);
244     }
245     return false;
246     break;
247   case KC_WORKMAN:
248     if (record->event.pressed) {
249       set_single_persistent_default_layer(_WORKMAN);
250     }
251     return false;
252     break;
253
254
255   case LOWER:
256     if (record->event.pressed) {
257       layer_on(_LOWER);
258       update_tri_layer(_LOWER, _RAISE, _ADJUST);
259     }
260     else {
261       layer_off(_LOWER);
262       update_tri_layer(_LOWER, _RAISE, _ADJUST);
263     }
264     return false;
265     break;
266   case RAISE:
267     if (record->event.pressed) {
268       layer_on(_RAISE);
269       update_tri_layer(_LOWER, _RAISE, _ADJUST);
270     }
271     else {
272       layer_off(_RAISE);
273       update_tri_layer(_LOWER, _RAISE, _ADJUST);
274     }
275     return false;
276     break;
277   case ADJUST:
278     if (record->event.pressed) {
279       layer_on(_ADJUST);
280     }
281     else {
282       layer_off(_ADJUST);
283     }
284     return false;
285     break;
286
287
288   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
289     if (!record->event.pressed) {
290       SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
291 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
292                    ":dfu"
293 #elif defined(BOOTLOADER_HALFKAY)
294                    ":teensy"
295 #elif defined(BOOTLOADER_CATERINA)
296                    ":avrdude"
297 #endif // bootloader options
298                    SS_TAP(X_ENTER));
299     }
300     return false;
301     break;
302
303
304   case KC_RESET: // Custom RESET code that sets RGBLights to RED
305     if (!record->event.pressed) {
306 #ifdef RGBLIGHT_ENABLE
307       rgblight_enable();
308       rgblight_mode(1);
309       rgblight_setrgb_red();
310 #endif // RGBLIGHT_ENABLE
311       reset_keyboard();
312     }
313     return false;
314     break;
315
316
317   case EPRM: // Resets EEPROM
318     if (record->event.pressed) {
319       eeconfig_init();
320     }
321     return false;
322     break;
323   case VRSN: // Prints firmware version
324     if (record->event.pressed) {
325       SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE);
326     }
327     return false;
328     break;
329
330
331   case KC_SECRET_1 ... KC_SECRET_5: // Secrets!  Externally defined strings, not stored in repo
332     if (!record->event.pressed) {
333       clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
334       send_string_P(secret[keycode - KC_SECRET_1]);
335     }
336     return false;
337     break;
338
339
340 // These are a serious of gaming macros.
341 // Only enables for the viterbi, basically,
342 // to save on firmware space, since it's limited.
343 #if !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
344   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
345     if (record->event.pressed) { is_overwatch = !is_overwatch; }
346 #ifdef RGBLIGHT_ENABLE
347     is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
348 #endif //RGBLIGHT_ENABLE
349     return false; break;
350   case KC_SALT:
351     return send_game_macro("Salt, salt, salt...", record, false);
352   case KC_MORESALT:
353     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
354   case KC_SALTHARD:
355     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
356   case KC_GOODGAME:
357     return send_game_macro("Good game, everyone!", record, false);
358   case KC_GLHF:
359     return send_game_macro("Good luck, have fun!!!", record, false);
360   case KC_SYMM:
361     return send_game_macro("Left click to win!", record, false);
362   case KC_JUSTGAME:
363     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);
364   case KC_TORB:
365     return send_game_macro("That was positively riveting!", record, false);
366   case KC_AIM:
367     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
368     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
369   case KC_C9:
370     return send_game_macro("OMG!!!  C9!!!", record, false);
371   case KC_GGEZ:
372     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
373 #endif // !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
374
375
376 #ifdef TAP_DANCE_ENABLE
377   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
378     if (record->event.pressed) {
379       uint8_t dtime;
380       for (dtime = 0; dtime < 4; dtime++) {
381         diablo_key_time[dtime] = diablo_times[0];
382       }
383     }
384     return false; break;
385 #endif // TAP_DANCE_ENABLE
386
387
388   case KC_RGB_T:  // This allows me to use underglow as layer indication, or as normal
389 #ifdef RGBLIGHT_ENABLE
390     if (record->event.pressed) {
391       rgb_layer_change = !rgb_layer_change;
392       if (rgb_layer_change) {
393         layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
394       }
395     }
396 #endif // RGBLIGHT_ENABLE
397     return false; break;
398 #ifdef RGBLIGHT_ENABLE
399   case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
400     if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
401       rgb_layer_change = false;
402     }
403     return true; break;
404 #endif // RGBLIGHT_ENABLE
405   }
406   return process_record_keymap(keycode, record);
407 }
408
409
410 // Runs state check and changes underglow color and animation
411 // on layer change, no matter where the change was initiated
412 // Then runs keymap's layer change check
413 uint32_t layer_state_set_user(uint32_t state) {
414 #ifdef RGBLIGHT_ENABLE
415   uint8_t default_layer = eeconfig_read_default_layer();
416   if (rgb_layer_change) {
417     switch (biton32(state)) {
418     case _NAV:
419       rgblight_sethsv_blue();
420       rgblight_mode(1);
421       break;
422     case _SYMB:
423       rgblight_sethsv_blue();
424       rgblight_mode(2);
425       break;
426     case _MOUS:
427       rgblight_sethsv_yellow();
428       rgblight_mode(1);
429       break;
430     case _MACROS:
431       rgblight_sethsv_orange();
432       is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
433       break;
434     case _MEDIA:
435       rgblight_sethsv_chartreuse();
436       rgblight_mode(22);
437       break;
438     case _GAMEPAD:
439       rgblight_sethsv_orange();
440       rgblight_mode(17);
441       break;
442     case _DIABLO:
443       rgblight_sethsv_red();
444       rgblight_mode(5);
445       break;
446     case _RAISE:
447       rgblight_sethsv_yellow();
448       rgblight_mode(5);
449       break;
450     case _LOWER:
451       rgblight_sethsv_orange();
452       rgblight_mode(5);
453       break;
454     case _ADJUST:
455       rgblight_sethsv_red();
456       rgblight_mode(23);
457       break;
458     case _COVECUBE:
459       rgblight_sethsv_green();
460       rgblight_mode(2);
461       break;
462     default: //  for any other layers, or the default layer
463       if (default_layer & (1UL << _COLEMAK)) {
464         rgblight_sethsv_magenta();
465       }
466       else if (default_layer & (1UL << _DVORAK)) {
467         rgblight_sethsv_green();
468       }
469       else if (default_layer & (1UL << _WORKMAN)) {
470         rgblight_sethsv_goldenrod();
471       }
472       else {
473         rgblight_sethsv_teal();
474       }
475       if (biton32(state) == _MODS) { // If the non-OSM layer is enabled, then breathe
476         rgblight_mode(2);
477       } else {                       // otherwise, stay solid
478         rgblight_mode(1);
479       }
480       break;
481     }
482   }
483 #endif // RGBLIGHT_ENABLE
484   return layer_state_set_keymap (state);
485 }
486
487
488 // Any custom LED code goes here.
489 // So far, I only have keyboard specific code,
490 // So nothing goes here.
491 void led_set_user(uint8_t usb_led) {
492   led_set_keymap(usb_led);
493 }