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