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