arkag.c 15 KB

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