]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/planck/keymaps/zach/zach_common_functions.c
Removed some common functions from my keymap
[qmk_firmware.git] / keyboards / planck / keymaps / zach / zach_common_functions.c
1 #ifndef ZACH_COMMON_FUNCTIONS
2 #define ZACH_COMMON_FUNCTIONS
3 #include "eeconfig.h"
4 #include "action_layer.h"
5 #include "keymap_colemak.h"
6 extern keymap_config_t keymap_config;
7
8 // Fillers to make layering more clear
9 #define _______ KC_TRNS
10 #define XXXXXXX KC_NO
11 #define C(n)    RCTL(n)
12 #define CADKEY  RCTL(RALT(KC_DEL))
13
14 void tap(uint16_t keycode){
15     register_code(keycode);
16     unregister_code(keycode);
17 };
18
19 void persistant_default_layer_set(uint16_t default_layer){
20     eeconfig_update_default_layer(default_layer);
21     default_layer_set(default_layer);
22 };
23
24 // Automatic number generation of important keywords
25 enum my_keycodes{
26     // Layer numbers
27     _COLEMAK = 0,
28     _SWCOLE,
29     _RAISE,
30     _LOWER,
31     _ADJUST,
32     _UNICODES,
33     // These use process_record_user()
34     COLEMAK = SAFE_RANGE,
35     SWCOLE,
36     LOWER,
37     RAISE,
38     SHFT_CAP,
39     CTRLB,
40     CPYPST,
41     FACE,
42     UNIWIN,
43     UNILIN,
44     DISFACE,
45     TFLIP,
46     TPUT,
47     SHRUG,
48     RANDIG,
49     // Tap_Dance nums
50     RAI = 0,
51     LOW,
52     SUP
53 };
54
55 #ifdef AUDIO_ENABLE
56 #include "audio.h"
57 float tone_startup[][2]         = SONG(STARTUP_SOUND);
58 float tone_goodbye[][2]         = SONG(GOODBYE_SOUND);
59 float tone_colemak[][2]         = SONG(COLEMAK_SOUND);
60 float tone_swcole[][2]          = SONG(QWERTY_SOUND);
61 float tone_capslock_on[][2]     = SONG(CAPS_LOCK_ON_SOUND);
62 float tone_capslock_off[][2]    = SONG(CAPS_LOCK_OFF_SOUND);
63 float tone_ctrl_mod[][2]        = SONG(COIN_SOUND);
64 float tone_copy[][2]            = SONG(SCROLL_LOCK_ON_SOUND);
65 float tone_paste[][2]           = SONG(SCROLL_LOCK_OFF_SOUND);
66 float uniwin[][2]               = SONG(UNICODE_WINDOWS);
67 float unilin[][2]               = SONG(UNICODE_LINUX);
68 #endif
69
70 #ifdef TAP_DANCE_ENABLE
71 #define TAPPING_TERM 200
72
73 uint8_t Lstate = 0, Rstate = 0;
74 uint32_t Ltimer = 0, Rtimer = 0;
75 uint32_t Ltimes[3], Rtimes[4];   // Ratio of tap times should be about 1.335 (L/R)
76 void rhythm_parse(void){
77     int L = Ltimes[0] + Ltimes[1] + Ltimes[2];  // Start to end time
78     int R = Rtimes[0] + Rtimes[1] + Rtimes[2] + Rtimes[3];
79     if(abs(R-L) > 10){
80         tap(KC_N); tap(KC_O);
81         return;
82     } else {
83         L = (L / 3)*100;                    // Average time per tap * 100
84         R = (R / 4);
85         if(abs(abs(L/R)-133) > 1){
86             tap(KC_N); tap(KC_O);
87             tap(KC_P); tap(KC_E);
88             return;
89         } else {
90             tap(KC_O); tap(KC_K);
91             return;
92         }
93     }
94 };
95
96 void dance_raise_press(qk_tap_dance_state_t *state, void *user_data){// Called on each tap
97   switch(state->count){      // Only turn the layer on once
98     case 1:
99         layer_off(_UNICODES);
100         layer_on(_RAISE);
101         update_tri_layer(_LOWER, _RAISE, _ADJUST);
102         break;
103   }
104 };
105 void dance_raise_lift(qk_tap_dance_state_t *state, void *user_data){ // Called on release
106   switch(state->count){
107     case 1:         // Normal action. Turn off layers
108         layer_off(_RAISE);
109         update_tri_layer(_LOWER, _RAISE, _ADJUST);
110         layer_off(_UNICODES);
111         break;
112   }
113 };
114 /////////////////////////////////////////////////////////////////////
115 void dance_lower_press(qk_tap_dance_state_t *state, void *user_data){// Called on tap
116   switch(state->count){
117     case 1:         // Turn on lower
118         layer_off(_UNICODES);
119         layer_on(_LOWER);
120         update_tri_layer(_LOWER, _RAISE, _ADJUST);
121         break;
122   }
123 };
124 void dance_lower_lift(qk_tap_dance_state_t *state, void *user_data){ // Called on release
125   switch(state->count){
126     case 1:         // Normal action. Turn off layers
127         layer_off(_LOWER);
128         update_tri_layer(_LOWER, _RAISE, _ADJUST);
129         layer_off(_UNICODES);
130         break;
131     case 2:         // Turn on _UNICODES layer
132         layer_off(_LOWER);
133         update_tri_layer(_LOWER, _RAISE, _ADJUST);
134         layer_on(_UNICODES);
135         #ifdef AUDIO_ENABLE
136             PLAY_NOTE_ARRAY(tone_ctrl_mod, false, 0);
137         #endif
138         break;
139   }
140 };
141 /////////////////////////////////////////////////////////////////////
142 void dance_super_press(qk_tap_dance_state_t *state, void *user_data){   // Called on down
143     if(state->count == 1){
144         register_code(KC_LGUI);
145     }
146 }
147 void dance_super_done(qk_tap_dance_state_t *state, void *user_data){    // Called on timeout
148   switch(state->count){
149     case 2:
150         register_code(KC_LGUI);
151         tap(KC_L);
152         unregister_code(KC_LGUI);
153         break;
154   }
155 }
156 void dance_super_lift(qk_tap_dance_state_t *state, void *user_data){        // Called on up
157     unregister_code(KC_LGUI);
158 }
159
160 qk_tap_dance_action_t tap_dance_actions[] = {
161     [RAI] = ACTION_TAP_DANCE_FN_ADVANCED(dance_raise_press, NULL, dance_raise_lift),
162     [LOW] = ACTION_TAP_DANCE_FN_ADVANCED(dance_lower_press, NULL, dance_lower_lift),
163     [SUP] = ACTION_TAP_DANCE_FN_ADVANCED(dance_super_press, dance_super_done, dance_super_lift)
164 };
165 #endif
166
167 #ifdef UNICODE_ENABLE
168 // Unicode shortcuts
169 #define IBANG   UC(0x203D)
170 #define RAROW   UC(0x2192)
171 #define LAROW   UC(0x2190)
172 #define DEGREE  UC(0x00B0)
173 #define OMEGA   UC(0x03A9)
174 #define WOMEGA  UC(0x03C9)
175 #define MICRO   UC(0x00B5)
176 #define PLUMIN  UC(0x00B1)
177 #define SUPA2   UC(0x00B2)
178 #define ROMAN1  UC(0x2160)
179 #define ROMAN2  UC(0x2161)
180 #define ROMAN3  UC(0x2162)
181 #define ROMAN4  UC(0x2163)
182 #define ROMAN5  UC(0x2164)
183 #define ROMAN6  UC(0x2165)
184 #define ROMAN7  UC(0x2166)
185 #define roman1  UC(0x2170)
186 #define roman2  UC(0x2171)
187 #define roman3  UC(0x2172)
188 #define roman4  UC(0x2173)
189 #define roman5  UC(0x2174)
190 #define roman6  UC(0x2175)
191 #define roman7  UC(0x2176)
192
193 #ifdef UNICODEMAP_ENABLE        // For Unicode characters larger than 0x8000. Send with X(<unicode>)
194 enum Ext_Unicode{
195     PENGUIN = 0,
196     BOAR,
197     MONKEY,
198     DRAGON,
199     CHICK,
200     TUMBLER
201 };
202 const uint32_t PROGMEM unicode_map[] = {
203     [PENGUIN]   = 0x1F427,
204     [BOAR]      = 0x1F417,
205     [MONKEY]    = 0x1F412,
206     [DRAGON]    = 0x1F409,
207     [CHICK]     = 0x1F425,
208     [TUMBLER]   = 0x1F943
209 };
210 #define PENGY   X(PENGUIN)
211 #define BOARY   X(BOAR)
212 #define MNKY    X(MONKEY)
213 #define DRGN    X(DRAGON)
214 #define DUCK    X(CHICK)
215 #define TMBL    X(TUMBLER)
216 #endif
217
218 #endif
219
220 static uint16_t key_timer;
221 static uint8_t  caps_status = 0;
222 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
223   switch (keycode) {
224     case COLEMAK:
225         if(record->event.pressed){
226             persistant_default_layer_set(1UL<<_COLEMAK);
227             #ifdef AUDIO_ENABLE
228               PLAY_NOTE_ARRAY(tone_colemak, false, 0);
229             #endif
230         }
231         return false;
232         break;
233     case SWCOLE:
234         if(record->event.pressed){
235             persistant_default_layer_set(1UL<<_SWCOLE);
236             #ifdef AUDIO_ENABLE
237               PLAY_NOTE_ARRAY(tone_swcole, false, 0);
238             #endif
239         }
240         return false;
241         break;
242     case RAISE:
243         if(record->event.pressed){
244             layer_on(_RAISE);
245             update_tri_layer(_LOWER, _RAISE, _ADJUST);
246         } else {
247             layer_off(_RAISE);
248             update_tri_layer(_LOWER, _RAISE, _ADJUST);
249         }
250         return false;
251         break;
252     case LOWER:
253         if(record->event.pressed){
254             layer_on(_LOWER);
255             update_tri_layer(_LOWER, _RAISE, _ADJUST);
256         } else {
257             layer_off(_LOWER);
258             update_tri_layer(_LOWER, _RAISE, _ADJUST);
259         }
260         return false;
261         break;
262     case SHFT_CAP: 
263         if(record->event.pressed){
264             key_timer = timer_read();               // if the key is being pressed, we start the timer.
265             register_code(KC_LSHIFT);
266         } else {                                    // this means the key was just released (tap or "held down")
267             if(timer_elapsed(key_timer) < 152){     // Time in ms, the threshold we pick for counting something as a tap.
268                 tap(KC_CAPS);
269                 if(caps_status == 0){
270                     caps_status = 1;
271                     #ifdef AUDIO_ENABLE
272                         PLAY_NOTE_ARRAY(tone_capslock_on, false, 0);
273                     #endif
274                 } else {
275                     caps_status = 0;
276                     #ifdef AUDIO_ENABLE
277                         PLAY_NOTE_ARRAY(tone_capslock_off, false, 0);
278                     #endif
279                 }
280             }
281             unregister_code(KC_LSHIFT);
282         }
283         return false;
284         break;
285     case CTRLB:                                     // Control-B on tap (bold)
286         if(record->event.pressed){
287             key_timer = timer_read();               // if the key is being pressed, we start the timer.
288             register_code(KC_LCTL);
289         } else {                                    // this means the key was just released (tap or "held down")
290             if (timer_elapsed(key_timer) < 152) {   // Time in ms, the threshold we pick for counting something as a tap.
291                 tap(KC_B);
292                 #ifdef AUDIO_ENABLE
293                     PLAY_NOTE_ARRAY(tone_ctrl_mod, false, 0);
294                 #endif
295                 #ifdef BACKLIGHT_BREATHING
296                     breathing_speed_set(2);
297                     breathing_pulse();
298                 #endif
299             }
300             unregister_code(KC_LCTL);
301         }
302         return false;
303         break;
304     case CPYPST:                                    // One key copy/paste
305         if(record->event.pressed){
306             key_timer = timer_read();
307         } else {
308             if (timer_elapsed(key_timer) > 152) {   // Hold, copy
309                 register_code(KC_LCTL);
310                 tap(KC_C);
311                 unregister_code(KC_LCTL);
312                 #ifdef AUDIO_ENABLE
313                     PLAY_NOTE_ARRAY(tone_copy, false, 0);
314                 #endif
315             } else {                                // Tap, paste
316                 register_code(KC_LCTL);
317                 tap(KC_V);
318                 unregister_code(KC_LCTL);
319                 #ifdef AUDIO_ENABLE
320                     PLAY_NOTE_ARRAY(tone_paste, false, 0);
321                 #endif
322             }
323         }
324         return false;
325         break;
326     #ifdef UNICODE_ENABLE
327     case UNIWIN:
328         if(record->event.pressed){
329             set_unicode_input_mode(UC_WIN);
330             #ifdef AUDIO_ENABLE
331               PLAY_NOTE_ARRAY(uniwin, false, 0);
332             #endif
333         }
334         return false;
335         break;
336     case UNILIN:
337         if(record->event.pressed){
338             set_unicode_input_mode(UC_LNX);
339             #ifdef AUDIO_ENABLE
340               PLAY_NOTE_ARRAY(unilin, false, 0);
341             #endif
342         }
343         return false;
344         break;
345     case DISFACE:       // ಠ_ಠ
346         if(record->event.pressed){
347             process_unicode((0x0CA0|QK_UNICODE), record);   // Eye
348             register_code(KC_RSFT);
349             tap(KC_MINS);
350             unregister_code(KC_RSFT);
351             process_unicode((0x0CA0|QK_UNICODE), record);   // Eye
352         }
353         return false;
354         break;
355     case TFLIP:         // (╯°□°)╯ ︵ ┻━┻
356         if(record->event.pressed){
357             register_code(KC_RSFT);
358             tap(KC_9);
359             unregister_code(KC_RSFT);
360             process_unicode((0x256F|QK_UNICODE), record);   // Arm
361             process_unicode((0x00B0|QK_UNICODE), record);   // Eye
362             process_unicode((0x25A1|QK_UNICODE), record);   // Mouth
363             process_unicode((0x00B0|QK_UNICODE), record);   // Eye
364             register_code(KC_RSFT);
365             tap(KC_0);
366             unregister_code(KC_RSFT);
367             process_unicode((0x256F|QK_UNICODE), record);   // Arm
368             tap(KC_SPC);
369             process_unicode((0x0361|QK_UNICODE), record);   // Flippy
370             tap(KC_SPC);
371             process_unicode((0x253B|QK_UNICODE), record);   // Table
372             process_unicode((0x2501|QK_UNICODE), record);   // Table
373             process_unicode((0x253B|QK_UNICODE), record);   // Table
374         }
375         return false;
376         break;
377     case TPUT:          // ┬──┬ ノ( ゜-゜ノ)
378         if(record->event.pressed){
379             process_unicode((0x252C|QK_UNICODE), record);   // Table
380             process_unicode((0x2500|QK_UNICODE), record);   // Table
381             process_unicode((0x2500|QK_UNICODE), record);   // Table
382             process_unicode((0x252C|QK_UNICODE), record);   // Table
383             tap(KC_SPC);
384             process_unicode((0x30CE|QK_UNICODE), record);   // Arm
385             register_code(KC_RSFT);
386             tap(KC_9);
387             unregister_code(KC_RSFT);
388             tap(KC_SPC);
389             process_unicode((0x309C|QK_UNICODE), record);   // Eye
390             tap(KC_MINS);
391             process_unicode((0x309C|QK_UNICODE), record);   // Eye
392             process_unicode((0x30CE|QK_UNICODE), record);   // Arm
393             register_code(KC_RSFT);
394             tap(KC_0);
395             unregister_code(KC_RSFT);
396         }
397         return false;
398         break;
399     case SHRUG:         // ¯\_(ツ)_/¯
400         if(record->event.pressed){
401             process_unicode((0x00AF|QK_UNICODE), record);   // Hand
402             tap(KC_BSLS);                                   // Arm
403             register_code(KC_RSFT);
404             tap(KC_UNDS);                                   // Arm
405             tap(KC_LPRN);                                   // Head
406             unregister_code(KC_RSFT);
407             process_unicode((0x30C4|QK_UNICODE), record);   // Face
408             register_code(KC_RSFT);
409             tap(KC_RPRN);                                   // Head
410             tap(KC_UNDS);                                   // Arm
411             unregister_code(KC_RSFT);
412             tap(KC_SLSH);                                   // Arm
413             process_unicode((0x00AF|QK_UNICODE), record);   // Hand
414         }
415         return false;
416         break;
417     #endif
418     case FACE:          // (o_O)
419         if(record->event.pressed){
420             register_code(KC_RSFT);
421             tap(KC_LPRN);
422             unregister_code(KC_RSFT);
423             tap(KC_O);
424             register_code(KC_RSFT);
425             tap(KC_UNDS);
426             tap(KC_O);
427             tap(KC_RPRN);
428             unregister_code(KC_RSFT);
429         }
430         return false;
431         break;
432     case RANDIG:
433         if (record->event.pressed) {
434             tap_random_base64();
435         }
436         return false;
437         break;
438   }
439   return true;
440 };
441
442 void matrix_init_user(void){        // Run once at startup
443     #ifdef AUDIO_ENABLE
444         _delay_ms(50); // gets rid of tick
445         PLAY_NOTE_ARRAY(tone_startup, false, 0);
446     #endif
447 }
448
449 #ifdef AUDIO_ENABLE
450 void play_goodbye_tone(void){
451   PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
452   _delay_ms(150);
453 }
454
455 void shutdown_user(){
456     PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
457     _delay_ms(150);
458     stop_all_notes();
459 }
460
461 void music_on_user(void){           // Run when the music layer is turned on
462     PLAY_NOTE_ARRAY(tone_startup, false, 0);
463 }
464
465 void music_off_user(void){          // Run when music is turned off
466         PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
467 }
468 #endif
469
470 #endif