arkag.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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(2);
  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 matrix_init_user(void) {
  330. current_os = eeprom_read_byte(EECONFIG_USERSPACE);
  331. set_os(current_os, false);
  332. }
  333. LEADER_EXTERNS();
  334. void matrix_scan_user(void) {
  335. check_state();
  336. flash_rgb();
  337. fade_rgb();
  338. LEADER_DICTIONARY() {
  339. leading = false;
  340. leader_end();
  341. // begin OS functions
  342. SEQ_TWO_KEYS(KC_P, KC_B) {
  343. if (current_os == OS_WIN) {
  344. long_keystroke(2, (uint16_t[]){KC_LGUI, KC_PAUSE});
  345. } else {
  346. }
  347. }
  348. SEQ_TWO_KEYS(KC_LSFT, M_PMOD) {
  349. if (current_os == OS_WIN) {
  350. long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LSFT, KC_ESC});
  351. } else {
  352. }
  353. }
  354. SEQ_TWO_KEYS(KC_S, KC_S) {
  355. if (current_os == OS_MAC) {
  356. long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_4});
  357. } else if (current_os == OS_WIN) {
  358. long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_S});
  359. } else {
  360. return;
  361. }
  362. }
  363. SEQ_THREE_KEYS(KC_C, KC_A, KC_D) {
  364. if (current_os == OS_WIN) {
  365. long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LALT, KC_DEL});
  366. } else {
  367. }
  368. }
  369. SEQ_FOUR_KEYS(KC_C, KC_A, KC_L, KC_C) {
  370. if (current_os == OS_WIN) {
  371. SEND_STRING(SS_TAP(X_CALCULATOR));
  372. } else if (current_os == OS_MAC) {
  373. SEND_STRING(SS_DOWN(X_LGUI) SS_TAP(X_SPACE) SS_UP(X_LGUI) "calculator" SS_TAP(X_ENTER));
  374. }
  375. }
  376. // end OS functions
  377. // begin format functions
  378. SEQ_ONE_KEY(KC_B) {
  379. surround_type(4, KC_8, true);
  380. }
  381. SEQ_ONE_KEY(KC_I) {
  382. surround_type(2, KC_8, true);
  383. }
  384. SEQ_ONE_KEY(KC_U) {
  385. surround_type(4, KC_MINS, true);
  386. }
  387. SEQ_ONE_KEY(KC_S) {
  388. surround_type(4, KC_GRAVE, true);
  389. }
  390. SEQ_ONE_KEY(KC_C) {
  391. send_unicode_hex_string("00E7");
  392. }
  393. SEQ_TWO_KEYS(KC_C, KC_C) {
  394. surround_type(2, KC_GRAVE, false);
  395. }
  396. SEQ_THREE_KEYS(KC_C, KC_C, KC_C) {
  397. surround_type(6, KC_GRAVE, false);
  398. }
  399. SEQ_ONE_KEY(KC_E) {
  400. send_unicode_hex_string("00E8");
  401. }
  402. SEQ_TWO_KEYS(KC_E, KC_E) {
  403. send_unicode_hex_string("00E9");
  404. }
  405. // end format functions
  406. // start fancy functions
  407. SEQ_THREE_KEYS(KC_C, KC_C, KC_C) {
  408. surround_type(6, KC_GRAVE, false);
  409. pri_mod(true);
  410. tap_code(KC_V);
  411. pri_mod(false);
  412. tap_code(KC_RGHT);
  413. tap_code(KC_RGHT);
  414. tap_code(KC_RGHT);
  415. tap_code(KC_ENTER);
  416. }
  417. // end fancy functions
  418. // start typing functions
  419. SEQ_TWO_KEYS(KC_T, KC_M) {
  420. // ™
  421. send_unicode_hex_string("2122");
  422. }
  423. SEQ_TWO_KEYS(KC_D, KC_D) {
  424. SEND_STRING(".\\Administrator");
  425. }
  426. SEQ_THREE_KEYS(KC_L, KC_O, KC_D) {
  427. // ಠ__ಠ
  428. send_unicode_hex_string("0CA0 005F 005F 0CA0");
  429. }
  430. SEQ_FOUR_KEYS(KC_R, KC_E, KC_P, KC_O) {
  431. SEND_STRING("https://github.com/qmk/qmk_firmware/tree/master/users/arkag");
  432. }
  433. SEQ_FOUR_KEYS(KC_F, KC_L, KC_I, KC_P) {
  434. // (╯‵Д′)╯彡┻━┻
  435. send_unicode_hex_string("0028 256F 2035 0414 2032 0029 256F 5F61 253B 2501 253B");
  436. }
  437. SEQ_FIVE_KEYS(KC_U, KC_F, KC_L, KC_I, KC_P) {
  438. // ┬─┬ノ( º _ º ノ)
  439. send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 0020 30CE 0029");
  440. }
  441. SEQ_FIVE_KEYS(KC_L, KC_E, KC_N, KC_N, KC_Y) {
  442. // ( ͡° ͜ʖ ͡°)
  443. send_unicode_hex_string("0028 0020 0361 00B0 0020 035C 0296 0020 0361 00B0 0029");
  444. }
  445. SEQ_FIVE_KEYS(KC_S, KC_H, KC_R, KC_U, KC_G) {
  446. // ¯\_(ツ)_/¯
  447. send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
  448. }
  449. // end typing functions
  450. }
  451. }
  452. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  453. if (aesthetic) {
  454. switch (keycode) {
  455. case KC_A ... KC_0:
  456. case KC_SPACE ... KC_SLASH:
  457. if (record->event.pressed) {
  458. state = active;
  459. velocikey_accelerate();
  460. tap_code(keycode);
  461. tap_code(KC_SPACE);
  462. }
  463. return false;
  464. case KC_BSPACE:
  465. if (record->event.pressed) {
  466. state = active;
  467. velocikey_accelerate();
  468. tap_code(keycode);
  469. tap_code(keycode);
  470. }
  471. return false;
  472. default: // Do nothing
  473. break;
  474. }
  475. }
  476. if (shifty) {
  477. switch (keycode) {
  478. case KC_A ... KC_Z:
  479. if (record->event.pressed) {
  480. int shift = rand() % 2;
  481. state = active;
  482. velocikey_accelerate();
  483. if (shift == 1){
  484. register_code(KC_LSFT);
  485. }
  486. tap_code(keycode);
  487. if (shift == 1){
  488. unregister_code(KC_LSFT);
  489. }
  490. }
  491. return false;
  492. case KC_SPC:
  493. if (record->event.pressed) {
  494. state = active;
  495. velocikey_accelerate();
  496. tap_code(keycode);
  497. }
  498. return false;
  499. default: // Do nothing
  500. break;
  501. }
  502. }
  503. switch (keycode) {
  504. case M_PMOD:
  505. pri_mod(record->event.pressed);
  506. return false;
  507. case M_SMOD:
  508. sec_mod(record->event.pressed);
  509. return false;
  510. case M_OS:
  511. if (record->event.pressed){
  512. set_os((current_os+1) % _OS_COUNT, true);
  513. }
  514. return false;
  515. case M_SPC:
  516. if(record->event.pressed){
  517. if (aesthetic) {
  518. aesthetic = false;
  519. num_extra_flashes_off = 1;
  520. } else {
  521. aesthetic = true;
  522. }
  523. flash_color = underglow;
  524. flash_state = flash_off;
  525. return false;
  526. }
  527. case M_SFT:
  528. if(record->event.pressed){
  529. if (shifty) {
  530. shifty = false;
  531. num_extra_flashes_off = 1;
  532. } else {
  533. shifty = true;
  534. }
  535. flash_color = underglow;
  536. flash_state = flash_off;
  537. return false;
  538. }
  539. default:
  540. if (record->event.pressed) {
  541. state = active;
  542. velocikey_accelerate();
  543. }
  544. return true;
  545. }
  546. }
  547. //Tap Dance Definitions
  548. qk_tap_dance_action_t tap_dance_actions[] = {
  549. [TD_GRV_3GRV] = ACTION_TAP_DANCE_FN (dance_grv),
  550. [TD_SING_DOUB] = ACTION_TAP_DANCE_FN (dance_quot),
  551. [TD_HYPH_UNDR] = ACTION_TAP_DANCE_FN (dance_hyph),
  552. [TD_BRCK_PARN_O] = ACTION_TAP_DANCE_FN (dance_obrck),
  553. [TD_BRCK_PARN_C] = ACTION_TAP_DANCE_FN (dance_cbrck),
  554. [TD_LALT_RALT] = ACTION_TAP_DANCE_DOUBLE (KC_LALT, KC_RALT),
  555. };