qmk

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

arkag.c (15239B)


      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(5);
    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 dance_game (qk_tap_dance_state_t *state, void *user_data) {
    363   if (state->count == 1) {
    364 
    365   } else if (state->count == 2) {
    366 
    367   } else if (state->count == 3) {
    368     uint8_t layer = biton32(layer_state);
    369     if (layer == _QWERTY) {
    370         layer_off(_QWERTY);
    371         layer_on(_GAMING);
    372         // swirling rgb
    373         rgblight_mode_noeeprom(12);
    374     } else {
    375         layer_off(_GAMING);
    376         layer_on(_QWERTY);
    377         rgblight_mode_noeeprom(1);
    378     }
    379   }
    380 }
    381 
    382 void matrix_init_user(void) {
    383   current_os = eeprom_read_byte(EECONFIG_USERSPACE);
    384   set_os(current_os, false);
    385 }
    386 
    387 LEADER_EXTERNS();
    388 
    389 void matrix_scan_user(void) {
    390   check_state();
    391   flash_rgb();
    392   fade_rgb();
    393   LEADER_DICTIONARY() {
    394     leading = false;
    395     leader_end();
    396 
    397     // begin OS functions
    398     SEQ_TWO_KEYS(KC_P, KC_B) {
    399       if (current_os == OS_WIN) {
    400         long_keystroke(2, (uint16_t[]){KC_LGUI, KC_PAUSE});
    401       } else {
    402         return;
    403       }
    404     }
    405     SEQ_TWO_KEYS(KC_LSFT, M_PMOD) {
    406       if (current_os == OS_WIN) {
    407         long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LSFT, KC_ESC});
    408       } else {
    409       }
    410     }
    411     SEQ_TWO_KEYS(KC_S, KC_S) {
    412       if (current_os == OS_MAC) {
    413         long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_4});
    414       } else if (current_os == OS_WIN) {
    415         long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_S});
    416       } else {
    417         return;
    418       }
    419     }
    420     SEQ_THREE_KEYS(KC_C, KC_A, KC_D) {
    421       if (current_os == OS_WIN) {
    422         long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LALT, KC_DEL});
    423       } else {
    424       }
    425     }
    426     SEQ_FOUR_KEYS(KC_C, KC_A, KC_L, KC_C) {
    427       if (current_os == OS_WIN) {
    428         SEND_STRING(SS_TAP(X_CALCULATOR));
    429       } else if (current_os == OS_MAC) {
    430         SEND_STRING(SS_DOWN(X_LGUI) SS_TAP(X_SPACE) SS_UP(X_LGUI) "calculator" SS_TAP(X_ENTER));
    431       }
    432     }
    433     // end OS functions
    434 
    435     // begin format functions
    436     SEQ_ONE_KEY(KC_B) {
    437       surround_type(4, KC_8, true);
    438     }
    439     SEQ_ONE_KEY(KC_I) {
    440       surround_type(2, KC_8, true);
    441     }
    442     SEQ_ONE_KEY(KC_U) {
    443       surround_type(4, KC_MINS, true);
    444     }
    445     SEQ_ONE_KEY(KC_S) {
    446       surround_type(4, KC_GRAVE, true);
    447     }
    448     SEQ_ONE_KEY(KC_C) {
    449       send_unicode_hex_string("00E7");
    450     }
    451     SEQ_TWO_KEYS(KC_C, KC_C) {
    452       surround_type(2, KC_GRAVE, false);
    453     }
    454     SEQ_THREE_KEYS(KC_C, KC_C, KC_C) {
    455       surround_type(6, KC_GRAVE, false);
    456     }
    457     SEQ_ONE_KEY(KC_E) {
    458       send_unicode_hex_string("00E8");
    459     }
    460     SEQ_TWO_KEYS(KC_E, KC_E) {
    461       send_unicode_hex_string("00E9");
    462     }
    463     // end format functions
    464 
    465     // start fancy functions
    466     SEQ_THREE_KEYS(KC_C, KC_C, KC_ENT) {
    467       surround_type(6, KC_GRAVE, false);
    468       pri_mod(true);
    469       tap_code(KC_V);
    470       pri_mod(false);
    471       tap_code(KC_RGHT);
    472       tap_code(KC_RGHT);
    473       tap_code(KC_RGHT);
    474       tap_code(KC_ENTER);
    475     }
    476     // end fancy functions
    477 
    478     // start typing functions
    479     SEQ_TWO_KEYS(KC_T, KC_M) {
    480       // ™
    481       send_unicode_hex_string("2122");
    482     }
    483     SEQ_TWO_KEYS(KC_D, KC_D) {
    484       SEND_STRING(".\\Administrator");
    485     }
    486     SEQ_THREE_KEYS(KC_L, KC_O, KC_D) {
    487       // ಠ__ಠ
    488       send_unicode_hex_string("0CA0 005F 005F 0CA0");
    489     }
    490     SEQ_FOUR_KEYS(KC_R, KC_E, KC_P, KC_O) {
    491       SEND_STRING("https://github.com/qmk/qmk_firmware/tree/master/users/arkag");
    492     }
    493     SEQ_FOUR_KEYS(KC_F, KC_L, KC_I, KC_P) {
    494       // (╯‵Д′)╯彡┻━┻
    495       send_unicode_hex_string("0028 256F 2035 0414 2032 0029 256F 5F61 253B 2501 253B");
    496     }
    497     SEQ_FIVE_KEYS(KC_U, KC_F, KC_L, KC_I, KC_P) {
    498       // ┬─┬ノ( º _ º ノ)
    499       send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 0020 30CE 0029");
    500     }
    501     SEQ_FIVE_KEYS(KC_L, KC_E, KC_N, KC_N, KC_Y) {
    502       // ( ͡° ͜ʖ ͡°)
    503       send_unicode_hex_string("0028 0020 0361 00B0 0020 035C 0296 0020 0361 00B0 0029");
    504     }
    505     SEQ_FIVE_KEYS(KC_S, KC_H, KC_R, KC_U, KC_G) {
    506       // ¯\_(ツ)_/¯
    507       send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
    508     }
    509     // end typing functions
    510 
    511   }
    512 }
    513 
    514 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    515   if (aesthetic) {
    516     switch (keycode) {
    517     case KC_A ... KC_0:
    518     case KC_SPACE ... KC_SLASH:
    519       if (record->event.pressed) {
    520         state = active;
    521         velocikey_accelerate();
    522         tap_code(keycode);
    523         tap_code(KC_SPACE);
    524       }
    525       return false;
    526 
    527     case KC_BSPACE:
    528       if (record->event.pressed) {
    529         state = active;
    530         velocikey_accelerate();
    531         tap_code(keycode);
    532         tap_code(keycode);
    533       }
    534       return false;
    535     default: // Do nothing
    536       break;
    537     }
    538   }
    539 
    540   if (shifty) {
    541     switch (keycode) {
    542     case KC_A ... KC_Z:
    543       if (record->event.pressed) {
    544         int shift = rand() % 2;
    545         state = active;
    546         velocikey_accelerate();
    547         if (shift == 1){
    548           register_code(KC_LSFT);
    549         }
    550         tap_code(keycode);
    551         if (shift == 1){
    552           unregister_code(KC_LSFT);
    553         }
    554       }
    555       return false;
    556     case KC_SPC:
    557       if (record->event.pressed) {
    558         state = active;
    559         velocikey_accelerate();
    560         tap_code(keycode);
    561       }
    562       return false;
    563     default: // Do nothing
    564       break;
    565     }
    566   }
    567 
    568   switch (keycode) {
    569   case M_PMOD:
    570     pri_mod(record->event.pressed);
    571     return false;
    572 
    573   case M_SMOD:
    574     sec_mod(record->event.pressed);
    575     return false;
    576 
    577   case M_OS:
    578     if (record->event.pressed){
    579       set_os((current_os+1) % _OS_COUNT, true);
    580     }
    581 
    582     return false;
    583 
    584   case M_SPC:
    585     if(record->event.pressed){
    586       if (aesthetic) {
    587         aesthetic = false;
    588         rgblight_mode_noeeprom(1);
    589       } else {
    590         aesthetic = true;
    591         shifty = false;
    592         // snake mode
    593         rgblight_mode_noeeprom(20);
    594       }
    595       return false;
    596     }
    597 
    598   case M_SFT:
    599     if(record->event.pressed){
    600       if (shifty) {
    601         shifty = false;
    602         rgblight_mode_noeeprom(1);
    603       } else {
    604         shifty = true;
    605         aesthetic = false;
    606         // knight mode
    607         rgblight_mode_noeeprom(23);
    608       }
    609       return false;
    610     }
    611 
    612   default:
    613     if (record->event.pressed) {
    614       state = active;
    615       velocikey_accelerate();
    616     }
    617     return true;
    618   }
    619 }
    620 
    621 //Tap Dance Definitions
    622 qk_tap_dance_action_t tap_dance_actions[] = {
    623   [TD_GRV_3GRV]       = ACTION_TAP_DANCE_FN (dance_grv),
    624   [TD_SING_DOUB]      = ACTION_TAP_DANCE_FN (dance_quot),
    625   [TD_HYPH_UNDR]      = ACTION_TAP_DANCE_FN (dance_hyph),
    626   [TD_BRCK_PARN_O]    = ACTION_TAP_DANCE_FN (dance_obrck),
    627   [TD_BRCK_PARN_C]    = ACTION_TAP_DANCE_FN (dance_cbrck),
    628   [TD_LALT_RALT]      = ACTION_TAP_DANCE_DOUBLE (KC_LALT, KC_RALT),
    629   [TD_GAME]           = ACTION_TAP_DANCE_FN (dance_game),
    630 };