]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/arkag/arkag.c
fresh commit for a new fork for PR to upstream/master (#5406)
[qmk_firmware.git] / users / arkag / arkag.c
1 #include "arkag.h"
2
3 /*
4  Current Layout and Keeb:
5  https://github.com/arkag/qmk_firmware/blob/master/keyboards/mechmini/v2/keymaps/arkag/keymap.c
6 */
7
8 #include <stdbool.h>
9
10 // Start: Written by Chris Lewis
11 #ifndef MIN
12 #define MIN(a,b) (((a)<(b))?(a):(b))
13 #endif
14 #ifndef MAX
15 #define MAX(a,b) (((a)>(b))?(a):(b))
16 #endif
17
18 #define TYPING_SPEED_MAX_VALUE 200
19 uint8_t typing_speed = 0;
20
21 void velocikey_accelerate() {
22     if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += (TYPING_SPEED_MAX_VALUE / 50);
23 }
24
25 void velocikey_decelerate() {
26   static uint16_t decay_timer = 0;
27
28   if (timer_elapsed(decay_timer) > 500 || decay_timer == 0) {
29     if (typing_speed > 0) typing_speed -= 1;
30     //Decay a little faster at half of max speed
31     if (typing_speed > TYPING_SPEED_MAX_VALUE / 2) typing_speed -= 1;
32     //Decay even faster at 3/4 of max speed
33     if (typing_speed > TYPING_SPEED_MAX_VALUE / 4 * 3) typing_speed -= 3;
34     decay_timer = timer_read();
35   }
36 }
37
38 uint8_t velocikey_match_speed(uint8_t minValue, uint8_t maxValue) {
39   return MAX(minValue, maxValue - (maxValue - minValue) * ((float)typing_speed / TYPING_SPEED_MAX_VALUE));
40 }
41 // End: Written by Chris Lewis
42
43 uint8_t       current_os,
44               mod_primary_mask,
45               fade_interval,
46               num_extra_flashes_off = 0;
47 Color         underglow,
48               flash_color,
49               saved_color,
50               hsv_none      = {0,0,0};
51 flashState    flash_state   = no_flash;
52 fadeState     fade_state    = add_fade;
53 activityState state         = boot;
54 bool          aesthetic     = false,
55               shifty        = false;
56
57 void set_color (Color new, bool update) {
58   rgblight_sethsv_eeprom_helper(new.h, new.s, new.v, update);
59 }
60
61 void save_color(Color to_save) {
62   saved_color = to_save;
63 }
64
65 void reset_color(void) {
66   underglow = saved_color;
67 }
68
69 Color mod_color(Color current_color, bool should_add, uint8_t change_amount) {
70   save_color(underglow);
71   int addlim = 359 - change_amount;
72   int sublim = change_amount;
73   int leftovers;
74   if (should_add) {
75     if (current_color.h <= addlim) {
76       current_color.h += change_amount;
77     } else {
78       leftovers = (359 + change_amount) % 359;
79       current_color.h  = 0 + leftovers;
80     }
81   } else {
82     if (current_color.h >= sublim) {
83       current_color.h -= change_amount;
84     } else {
85       leftovers = change_amount - current_color.h;
86       current_color.h  = 359 - leftovers;
87     }
88   }
89   return current_color;
90 }
91
92 void check_state (void) {
93   static uint16_t active_timer;
94   if (!active_timer) {active_timer = timer_read();}
95   static bool activated, deactivated, slept;
96   switch (state) {
97   case active:
98     if (!activated) {
99       if (slept) {rgblight_mode_noeeprom(1);}
100       activated = true;
101       deactivated = false;
102       slept = false;
103     }
104     fade_interval = velocikey_match_speed(1, 25);
105     if (timer_elapsed(active_timer) < INACTIVE_DELAY) {return;}
106     active_timer = timer_read();
107     state = inactive;
108     return;
109
110   case inactive:
111     if (!deactivated) {
112       deactivated = true;
113       activated = false;
114       slept = false;
115     }
116     velocikey_decelerate();
117     fade_interval = velocikey_match_speed(1, 25);
118     if (timer_elapsed(active_timer) < SLEEP_DELAY) {return;}
119     state = sleeping;
120     return;
121
122   case sleeping:
123     if (!slept) {
124       rgblight_mode_noeeprom(2);
125       slept = true;
126       activated = false;
127       deactivated = false;
128     }
129     return;
130
131   case boot:
132     return;
133   }
134 }
135
136 void fade_rgb (void) {
137   static uint16_t fade_timer;
138   if (state == boot) {return;}
139   if (!fade_timer) {fade_timer = timer_read();}
140   if (timer_elapsed(fade_timer) < fade_interval) {return;}
141   switch (fade_state) {
142   case add_fade:
143     if (underglow.h == 359) {
144       fade_state = sub_fade;
145       return;
146     }
147     underglow.h = underglow.h + 1;
148     break;
149
150   case sub_fade:
151     if (underglow.h == 0) {
152       fade_state = add_fade;
153       return;
154     }
155     underglow.h = underglow.h - 1;
156     break;
157   }
158   fade_timer = timer_read();
159   if (flash_state == no_flash) {
160     set_color(underglow, false);
161   }
162 }
163
164 void flash_rgb (void) {
165   static uint16_t flash_timer;
166   switch(flash_state) {
167   case no_flash:
168     return;
169
170   case flash_off:
171     if (!flash_timer) {flash_timer = timer_read();}
172     if (timer_elapsed(flash_timer) >= LED_FLASH_DELAY) {
173       set_color(hsv_none, false);
174       flash_timer = timer_read();
175       flash_state = flash_on;
176     }
177     return;
178
179   case flash_on:
180     if (timer_elapsed(flash_timer) >= LED_FLASH_DELAY) {
181       set_color(flash_color, false);
182       flash_timer = timer_read();
183       if (num_extra_flashes_off > 0) {
184         flash_state = flash_off;
185         num_extra_flashes_off--;
186       } else {
187         set_color(underglow, false);
188         flash_state = no_flash;
189       }
190     }
191     return;
192   }
193 }
194
195 void set_os (uint8_t os, bool update) {
196   current_os = os;
197   if (update) {
198     eeprom_update_byte(EECONFIG_USERSPACE, current_os);
199   }
200   switch (os) {
201   case OS_MAC:
202     set_unicode_input_mode(UC_OSX);
203     underglow = (Color){ 300, 255, 255 };
204     mod_primary_mask = MOD_GUI_MASK;
205     break;
206   case OS_WIN:
207     set_unicode_input_mode(UC_WINC);
208     underglow = (Color){ 180, 255, 255 };
209     mod_primary_mask = MOD_CTL_MASK;
210     break;
211   case OS_NIX:
212     set_unicode_input_mode(UC_LNX);
213     underglow = (Color){ 60, 255, 255 };
214     mod_primary_mask = MOD_CTL_MASK;
215     break;
216   default:
217     underglow = (Color){ 0, 0, 255 };
218     mod_primary_mask = MOD_CTL_MASK;
219   }
220   set_color(underglow, update);
221   flash_color           = underglow;
222   flash_state           = flash_off;
223   state                 = boot;
224   num_extra_flashes_off = 1;
225 }
226
227 // register GUI if Mac or Ctrl if other
228 void pri_mod(bool press) {
229   if (press) {
230     if (current_os == OS_MAC) {
231       register_code(KC_LGUI);
232     } else {
233       register_code(KC_LCTL);
234     }
235   } else {
236     if (current_os == OS_MAC) {
237       unregister_code(KC_LGUI);
238     } else {
239       unregister_code(KC_LCTL);
240     }
241   }
242 }
243
244 // register Ctrl if Mac or GUI if other
245 void sec_mod(bool press) {
246   if (press) {
247     if (current_os == OS_MAC) {
248       register_code(KC_LCTL);
249     } else {
250       register_code(KC_LGUI);
251     }
252   } else {
253     if (current_os == OS_MAC) {
254       unregister_code(KC_LCTL);
255     } else {
256       unregister_code(KC_LGUI);
257     }
258   }
259 }
260
261 void surround_type(uint8_t num_of_chars, uint16_t keycode, bool use_shift) {
262   if (use_shift) {
263     register_code(KC_LSFT);
264   }
265   for (int i = 0; i < num_of_chars; i++) {
266     tap_code(keycode);
267   }
268   if (use_shift) {
269     unregister_code(KC_LSFT);
270   }
271   for (int i = 0; i < (num_of_chars/2); i++) {
272     tap_code(KC_LEFT);
273   }
274 }
275
276 void long_keystroke(size_t num_of_keys, uint16_t keys[]) {
277   for (int i = 0; i < num_of_keys-1; i++) {
278     register_code(keys[i]);
279   }
280   tap_code(keys[num_of_keys-1]);
281   for (int i = 0; i < num_of_keys-1; i++) {
282     unregister_code(keys[i]);
283   }
284 }
285
286 void dance_grv (qk_tap_dance_state_t *state, void *user_data) {
287   if (state->count == 1) {
288     tap_code(KC_GRV);
289     if (aesthetic) {
290       tap_code(KC_SPACE);
291     }
292   } else if (state->count == 2) {
293     surround_type(2, KC_GRAVE, false);
294   } else {
295     surround_type(6, KC_GRAVE, false);
296   }
297 }
298
299 void dance_quot (qk_tap_dance_state_t *state, void *user_data) {
300   if (state->count == 1) {
301     tap_code(KC_QUOT);
302     if (aesthetic) {
303       tap_code(KC_SPACE);
304     }
305   } else if (state->count == 2) {
306     surround_type(2, KC_QUOTE, false);
307   } else if (state->count == 3) {
308     surround_type(2, KC_QUOTE, true);
309   }
310 }
311
312 void dance_hyph (qk_tap_dance_state_t *state, void *user_data) {
313   if (state->count == 1) {
314     tap_code(KC_MINS);
315     if (aesthetic) {
316       tap_code(KC_SPACE);
317     }
318   } else if (state->count == 2) {
319     register_code(KC_LSFT);
320     tap_code(KC_MINS);
321     if (aesthetic) {
322       tap_code(KC_SPACE);
323     }
324     unregister_code(KC_LSFT);
325   } else if (state->count == 3) {
326     send_unicode_hex_string("2014");
327   }
328 }
329
330 void dance_obrck (qk_tap_dance_state_t *state, void *user_data) {
331   if (state->count == 1) {
332     tap_code(KC_LBRC);
333     if (aesthetic) {
334       tap_code(KC_SPACE);
335     }
336   } else if (state->count == 2) {
337     register_code(KC_LSFT);
338     tap_code(KC_9);
339     if (aesthetic) {
340       tap_code(KC_SPACE);
341     }
342     unregister_code(KC_LSFT);
343   }
344 }
345
346 void dance_cbrck (qk_tap_dance_state_t *state, void *user_data) {
347   if (state->count == 1) {
348     tap_code(KC_RBRC);
349     if (aesthetic) {
350       tap_code(KC_SPACE);
351     }
352   } else if (state->count == 2) {
353     register_code(KC_LSFT);
354     tap_code(KC_0);
355     if (aesthetic) {
356       tap_code(KC_SPACE);
357     }
358     unregister_code(KC_LSFT);
359   }
360 }
361
362 void matrix_init_user(void) {
363   current_os = eeprom_read_byte(EECONFIG_USERSPACE);
364   set_os(current_os, false);
365 }
366
367 LEADER_EXTERNS();
368
369 void matrix_scan_user(void) {
370   check_state();
371   flash_rgb();
372   fade_rgb();
373   LEADER_DICTIONARY() {
374     leading = false;
375     leader_end();
376
377     // begin OS functions
378     SEQ_TWO_KEYS(KC_P, KC_B) {
379       if (current_os == OS_WIN) {
380         long_keystroke(2, (uint16_t[]){KC_LGUI, KC_PAUSE});
381       } else {
382       }
383     }
384     SEQ_TWO_KEYS(KC_LSFT, M_PMOD) {
385       if (current_os == OS_WIN) {
386         long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LSFT, KC_ESC});
387       } else {
388       }
389     }
390     SEQ_TWO_KEYS(KC_S, KC_S) {
391       if (current_os == OS_MAC) {
392         long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_4});
393       } else if (current_os == OS_WIN) {
394         long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_S});
395       } else {
396         return;
397       }
398     }
399     SEQ_THREE_KEYS(KC_C, KC_A, KC_D) {
400       if (current_os == OS_WIN) {
401         long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LALT, KC_DEL});
402       } else {
403       }
404     }
405     SEQ_FOUR_KEYS(KC_C, KC_A, KC_L, KC_C) {
406       if (current_os == OS_WIN) {
407         SEND_STRING(SS_TAP(X_CALCULATOR));
408       } else if (current_os == OS_MAC) {
409         SEND_STRING(SS_DOWN(X_LGUI) SS_TAP(X_SPACE) SS_UP(X_LGUI) "calculator" SS_TAP(X_ENTER));
410       }
411     }
412     // end OS functions
413
414     // begin format functions
415     SEQ_ONE_KEY(KC_B) {
416       surround_type(4, KC_8, true);
417     }
418     SEQ_ONE_KEY(KC_I) {
419       surround_type(2, KC_8, true);
420     }
421     SEQ_ONE_KEY(KC_U) {
422       surround_type(4, KC_MINS, true);
423     }
424     SEQ_ONE_KEY(KC_S) {
425       surround_type(4, KC_GRAVE, true);
426     }
427     SEQ_ONE_KEY(KC_C) {
428       send_unicode_hex_string("00E7");
429     }
430     SEQ_TWO_KEYS(KC_C, KC_C) {
431       surround_type(2, KC_GRAVE, false);
432     }
433     SEQ_THREE_KEYS(KC_C, KC_C, KC_C) {
434       surround_type(6, KC_GRAVE, false);
435     }
436     SEQ_ONE_KEY(KC_E) {
437       send_unicode_hex_string("00E8");
438     }
439     SEQ_TWO_KEYS(KC_E, KC_E) {
440       send_unicode_hex_string("00E9");
441     }
442     // end format functions
443
444     // start fancy functions
445     SEQ_THREE_KEYS(KC_C, KC_C, KC_C) {
446       surround_type(6, KC_GRAVE, false);
447       pri_mod(true);
448       tap_code(KC_V);
449       pri_mod(false);
450       tap_code(KC_RGHT);
451       tap_code(KC_RGHT);
452       tap_code(KC_RGHT);
453       tap_code(KC_ENTER);
454     }
455     // end fancy functions
456
457     // start typing functions
458     SEQ_TWO_KEYS(KC_T, KC_M) {
459       // ™
460       send_unicode_hex_string("2122");
461     }
462     SEQ_TWO_KEYS(KC_D, KC_D) {
463       SEND_STRING(".\\Administrator");
464     }
465     SEQ_THREE_KEYS(KC_L, KC_O, KC_D) {
466       // ಠ__ಠ
467       send_unicode_hex_string("0CA0 005F 005F 0CA0");
468     }
469     SEQ_FOUR_KEYS(KC_R, KC_E, KC_P, KC_O) {
470       SEND_STRING("https://github.com/qmk/qmk_firmware/tree/master/users/arkag");
471     }
472     SEQ_FOUR_KEYS(KC_F, KC_L, KC_I, KC_P) {
473       // (╯‵Д′)╯彡┻━┻
474       send_unicode_hex_string("0028 256F 2035 0414 2032 0029 256F 5F61 253B 2501 253B");
475     }
476     SEQ_FIVE_KEYS(KC_U, KC_F, KC_L, KC_I, KC_P) {
477       // ┬─┬ノ( º _ º ノ)
478       send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 0020 30CE 0029");
479     }
480     SEQ_FIVE_KEYS(KC_L, KC_E, KC_N, KC_N, KC_Y) {
481       // ( ͡° ͜ʖ ͡°)
482       send_unicode_hex_string("0028 0020 0361 00B0 0020 035C 0296 0020 0361 00B0 0029");
483     }
484     SEQ_FIVE_KEYS(KC_S, KC_H, KC_R, KC_U, KC_G) {
485       // ¯\_(ツ)_/¯
486       send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
487     }
488     // end typing functions
489
490   }
491 }
492
493 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
494   if (aesthetic) {
495     switch (keycode) {
496     case KC_A ... KC_0:
497     case KC_SPACE ... KC_SLASH:
498       if (record->event.pressed) {
499         state = active;
500         velocikey_accelerate();
501         tap_code(keycode);
502         tap_code(KC_SPACE);
503       }
504       return false;
505
506     case KC_BSPACE:
507       if (record->event.pressed) {
508         state = active;
509         velocikey_accelerate();
510         tap_code(keycode);
511         tap_code(keycode);
512       }
513       return false;
514     default: // Do nothing
515       break;
516     }
517   }
518
519   if (shifty) {
520     switch (keycode) {
521     case KC_A ... KC_Z:
522       if (record->event.pressed) {
523         int shift = rand() % 2;
524         state = active;
525         velocikey_accelerate();
526         if (shift == 1){
527           register_code(KC_LSFT);
528         }
529         tap_code(keycode);
530         if (shift == 1){
531           unregister_code(KC_LSFT);
532         }
533       }
534       return false;
535     case KC_SPC:
536       if (record->event.pressed) {
537         state = active;
538         velocikey_accelerate();
539         tap_code(keycode);
540       }
541       return false;
542     default: // Do nothing
543       break;
544     }
545   }
546
547   switch (keycode) {
548   case M_PMOD:
549     pri_mod(record->event.pressed);
550     return false;
551
552   case M_SMOD:
553     sec_mod(record->event.pressed);
554     return false;
555
556   case M_OS:
557     if (record->event.pressed){
558       set_os((current_os+1) % _OS_COUNT, true);
559     }
560
561     return false;
562
563   case M_SPC:
564     if(record->event.pressed){
565       if (aesthetic) {
566         aesthetic = false;
567         num_extra_flashes_off = 1;
568       } else {
569         aesthetic = true;
570       }
571       flash_color           = underglow;
572       flash_state           = flash_off;
573       return false;
574     }
575
576
577   case M_SFT:
578     if(record->event.pressed){
579       if (shifty) {
580         shifty = false;
581         num_extra_flashes_off = 1;
582       } else {
583         shifty = true;
584       }
585       flash_color = underglow;
586       flash_state = flash_off;
587       return false;
588     }
589
590
591   default:
592     if (record->event.pressed) {
593       state = active;
594       velocikey_accelerate();
595     }
596     return true;
597   }
598 }
599
600 //Tap Dance Definitions
601 qk_tap_dance_action_t tap_dance_actions[] = {
602   [TD_GRV_3GRV]       = ACTION_TAP_DANCE_FN (dance_grv),
603   [TD_SING_DOUB]      = ACTION_TAP_DANCE_FN (dance_quot),
604   [TD_HYPH_UNDR]      = ACTION_TAP_DANCE_FN (dance_hyph),
605   [TD_BRCK_PARN_O]    = ACTION_TAP_DANCE_FN (dance_obrck),
606   [TD_BRCK_PARN_C]    = ACTION_TAP_DANCE_FN (dance_cbrck),
607   [TD_LALT_RALT]      = ACTION_TAP_DANCE_DOUBLE (KC_LALT, KC_RALT),
608 };