]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
f72902f0b0319fb25bb6e4aaa49bfa822f000861
[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 #include "eeprom.h"
21 #include "tap_dances.h"
22 #include "rgb_stuff.h"
23
24
25 float tone_copy[][2]            = SONG(SCROLL_LOCK_ON_SOUND);
26 float tone_paste[][2]           = SONG(SCROLL_LOCK_OFF_SOUND);
27
28 static uint16_t copy_paste_timer;
29 userspace_config_t userspace_config;
30
31 //  Helper Functions
32
33
34 // This block is for all of the gaming macros, as they were all doing
35 // the same thing, but with differring text sent.
36 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
37   if (!record->event.pressed || override) {
38     clear_keyboard();
39     tap(userspace_config.is_overwatch ? KC_BSPC : KC_ENTER);
40     wait_ms(50);
41     send_string_with_delay(str, MACRO_TIMER);
42     wait_ms(50);
43     tap(KC_ENTER);
44   }
45   if (override) wait_ms(3000);
46   return false;
47 }
48
49 void tap(uint16_t keycode){ register_code(keycode); unregister_code(keycode); };
50
51 bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
52   static uint16_t this_timer;
53   if(pressed) {
54       this_timer= timer_read();
55   } else {
56       if (timer_elapsed(this_timer) < TAPPING_TERM){
57           register_code(code);
58           unregister_code(code);
59       } else {
60           register_code(mod_code);
61           register_code(code);
62           unregister_code(code);
63           unregister_code(mod_code);
64       }
65   }
66   return false;
67 }
68
69 bool mod_key_press (uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
70   if(pressed) {
71       this_timer= timer_read();
72   } else {
73       if (timer_elapsed(this_timer) < TAPPING_TERM){
74           register_code(code);
75           unregister_code(code);
76       } else {
77           register_code(mod_code);
78           register_code(code);
79           unregister_code(code);
80           unregister_code(mod_code);
81       }
82   }
83   return false;
84 }
85
86 // Add reconfigurable functions here, for keymap customization
87 // This allows for a global, userspace functions, and continued
88 // customization of the keymap.  Use _keymap instead of _user
89 // functions in the keymaps
90 __attribute__ ((weak))
91 void matrix_init_keymap(void) {}
92
93 __attribute__ ((weak))
94 void startup_keymap(void) {}
95
96 __attribute__ ((weak))
97 void suspend_power_down_keymap(void) {}
98
99 __attribute__ ((weak))
100 void suspend_wakeup_init_keymap(void) {}
101
102 __attribute__ ((weak))
103 void matrix_scan_keymap(void) {}
104
105 __attribute__ ((weak))
106 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
107   return true;
108 }
109
110 __attribute__ ((weak))
111 bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
112   return true;
113 }
114
115
116 __attribute__ ((weak))
117 uint32_t layer_state_set_keymap (uint32_t state) {
118   return state;
119 }
120
121 __attribute__ ((weak))
122 uint32_t default_layer_state_set_keymap (uint32_t state) {
123   return state;
124 }
125
126 __attribute__ ((weak))
127 void led_set_keymap(uint8_t usb_led) {}
128
129
130 // Call user matrix init, set default RGB colors and then
131 // call the keymap's init function
132 void matrix_init_user(void) {
133   userspace_config.raw = eeprom_read_byte(EECONFIG_USERSPACE);
134
135 #ifdef AUDIO_CLICKY
136   clicky_enable = userspace_config.clicky_enable;
137 #endif
138
139 #ifdef BOOTLOADER_CATERINA
140   DDRD &= ~(1<<5);
141   PORTD &= ~(1<<5);
142
143   DDRB &= ~(1<<0);
144   PORTB &= ~(1<<0);
145 #endif
146
147
148 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
149         set_unicode_input_mode(UC_WINC);
150 #endif //UNICODE_ENABLE
151   matrix_init_keymap();
152 }
153
154 void startup_user (void) {
155   #ifdef RGBLIGHT_ENABLE
156     matrix_init_rgb();
157   #endif //RGBLIGHT_ENABLE
158   startup_keymap();
159 }
160
161 void suspend_power_down_user(void)
162 {
163     suspend_power_down_keymap();
164 }
165
166 void suspend_wakeup_init_user(void)
167 {
168   suspend_wakeup_init_keymap();
169   #ifdef KEYBOARD_ergodox_ez
170   wait_ms(10);
171   #endif
172 }
173
174
175 // No global matrix scan code, so just run keymap's matrix
176 // scan function
177 void matrix_scan_user(void) {
178   static bool has_ran_yet;
179   if (!has_ran_yet) {
180     has_ran_yet = true;
181     startup_user();
182   }
183
184 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
185   run_diablo_macro_check();
186 #endif // TAP_DANCE_ENABLE
187
188 #ifdef RGBLIGHT_ENABLE
189   matrix_scan_rgb();
190 #endif // RGBLIGHT_ENABLE
191
192   matrix_scan_keymap();
193 }
194
195
196
197
198 // Defines actions tor my global custom keycodes. Defined in drashna.h file
199 // Then runs the _keymap's record handier if not processed here
200 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
201
202   // If console is enabled, it will print the matrix position and status of each key pressed
203 #ifdef KEYLOGGER_ENABLE
204   xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
205 #endif //KEYLOGGER_ENABLE
206
207   switch (keycode) {
208   case KC_QWERTY:
209     if (record->event.pressed) {
210       set_single_persistent_default_layer(_QWERTY);
211     }
212     return false;
213     break;
214   case KC_COLEMAK:
215     if (record->event.pressed) {
216       set_single_persistent_default_layer(_COLEMAK);
217     }
218     return false;
219     break;
220   case KC_DVORAK:
221     if (record->event.pressed) {
222       set_single_persistent_default_layer(_DVORAK);
223     }
224     return false;
225     break;
226   case KC_WORKMAN:
227     if (record->event.pressed) {
228       set_single_persistent_default_layer(_WORKMAN);
229     }
230     return false;
231     break;
232
233
234   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
235     if (!record->event.pressed) {
236       send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
237 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
238                    ":dfu"
239 #elif defined(BOOTLOADER_HALFKAY)
240                    ":teensy"
241 #elif defined(BOOTLOADER_CATERINA)
242                    ":avrdude"
243 #endif // bootloader options
244                    SS_TAP(X_ENTER)), 10);
245     }
246     return false;
247     break;
248
249
250   case KC_RESET: // Custom RESET code that sets RGBLights to RED
251     if (!record->event.pressed) {
252 #ifdef RGBLIGHT_ENABLE
253       rgblight_enable_noeeprom();
254       rgblight_mode_noeeprom(1);
255       rgblight_setrgb_red();
256 #endif // RGBLIGHT_ENABLE
257       reset_keyboard();
258     }
259     return false;
260     break;
261
262
263   case EPRM: // Resets EEPROM
264     if (record->event.pressed) {
265       eeconfig_init();
266       default_layer_set(1UL<<eeconfig_read_default_layer());
267       layer_state_set(layer_state);
268     }
269     return false;
270     break;
271   case VRSN: // Prints firmware version
272     if (record->event.pressed) {
273       send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
274     }
275     return false;
276     break;
277
278 /*  Code has been depreciated
279     case KC_SECRET_1 ... KC_SECRET_5: // Secrets!  Externally defined strings, not stored in repo
280       if (!record->event.pressed) {
281         clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
282         send_string(decoy_secret[keycode - KC_SECRET_1]);
283       }
284       return false;
285       break;
286 */
287
288 // These are a serious of gaming macros.
289 // Only enables for the viterbi, basically,
290 // to save on firmware space, since it's limited.
291 #ifdef MACROS_ENABLED
292   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
293     if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
294 #ifdef RGBLIGHT_ENABLE
295     userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
296 #endif //RGBLIGHT_ENABLE
297     return false; break;
298   case KC_SALT:
299     return send_game_macro("Salt, salt, salt...", record, false);
300   case KC_MORESALT:
301     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
302   case KC_SALTHARD:
303     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
304   case KC_GOODGAME:
305     return send_game_macro("Good game, everyone!", record, false);
306   case KC_GLHF:
307     return send_game_macro("Good luck, have fun!!!", record, false);
308   case KC_SYMM:
309     return send_game_macro("Left click to win!", record, false);
310   case KC_JUSTGAME:
311     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);
312   case KC_TORB:
313     return send_game_macro("That was positively riveting!", record, false);
314   case KC_AIM:
315     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
316     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
317   case KC_C9:
318     return send_game_macro("OMG!!!  C9!!!", record, false);
319   case KC_GGEZ:
320     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
321 #endif // MACROS_ENABLED
322
323
324   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
325 #ifdef TAP_DANCE_ENABLE
326     if (record->event.pressed) {
327       uint8_t dtime;
328       for (dtime = 0; dtime < 4; dtime++) {
329         diablo_key_time[dtime] = diablo_times[0];
330       }
331     }
332 #endif // TAP_DANCE_ENABLE
333     return false; break;
334
335
336   case KC_CCCV:                                    // One key copy/paste
337     if(record->event.pressed){
338       copy_paste_timer = timer_read();
339     } else {
340       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
341         register_code(KC_LCTL);
342         tap(KC_C);
343         unregister_code(KC_LCTL);
344 #ifdef AUDIO_ENABLE
345         PLAY_SONG(tone_copy);
346 #endif
347       } else {                                // Tap, paste
348         register_code(KC_LCTL);
349         tap(KC_V);
350         unregister_code(KC_LCTL);
351 #ifdef AUDIO_ENABLE
352         PLAY_SONG(tone_paste);
353 #endif
354       }
355     }
356     return false;
357     break;
358   case CLICKY_TOGGLE:
359 #ifdef AUDIO_CLICKY
360     userspace_config.clicky_enable = clicky_enable;
361     eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
362 #endif
363     break;
364 #ifdef UNICODE_ENABLE
365   case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
366     if (record->event.pressed) {
367       register_code(KC_RSFT);
368       tap(KC_9);
369       unregister_code(KC_RSFT);
370       process_unicode((0x256F | QK_UNICODE), record); // Arm
371       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
372       process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
373       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
374       register_code(KC_RSFT);
375       tap(KC_0);
376       unregister_code(KC_RSFT);
377       process_unicode((0x256F | QK_UNICODE), record); // Arm
378       tap(KC_SPC);
379       process_unicode((0x0361 | QK_UNICODE), record); // Flippy
380       tap(KC_SPC);
381       process_unicode((0x253B | QK_UNICODE), record); // Table
382       process_unicode((0x2501 | QK_UNICODE), record); // Table
383       process_unicode((0x253B | QK_UNICODE), record); // Table
384     }
385     return false;
386     break;
387 #endif // UNICODE_ENABLE
388
389   }
390   return process_record_keymap(keycode, record) &&
391 #ifdef RGBLIGHT_ENABLE
392     process_record_user_rgb(keycode, record) &&
393 #endif // RGBLIGHT_ENABLE
394     process_record_secrets(keycode, record);
395 }
396
397
398
399 // Runs state check and changes underglow color and animation
400 // on layer change, no matter where the change was initiated
401 // Then runs keymap's layer change check
402 uint32_t layer_state_set_user(uint32_t state) {
403   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
404 #ifdef RGBLIGHT_ENABLE
405   state = layer_state_set_rgb(state);
406 #endif // RGBLIGHT_ENABLE
407   return layer_state_set_keymap (state);
408 }
409
410
411 uint32_t default_layer_state_set_kb(uint32_t state) {
412   return default_layer_state_set_keymap (state);
413 }
414
415
416 // Any custom LED code goes here.
417 // So far, I only have keyboard specific code,
418 // So nothing goes here.
419 void led_set_user(uint8_t usb_led) {
420   led_set_keymap(usb_led);
421 }