arkag.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "arkag.h"
  2. #include "eeprom.h"
  3. /*
  4. Current Layout and Keeb:
  5. https://github.com/arkag/qmk_firmware/blob/master/keyboards/mechmini/v2/keymaps/arkag/keymap.c
  6. */
  7. #include <stdbool.h>
  8. // Start: Written by Chris Lewis
  9. #ifndef MIN
  10. #define MIN(a,b) (((a)<(b))?(a):(b))
  11. #endif
  12. #ifndef MAX
  13. #define MAX(a,b) (((a)>(b))?(a):(b))
  14. #endif
  15. #define TYPING_SPEED_MAX_VALUE 200
  16. uint8_t typing_speed = 0;
  17. void velocikey_accelerate(void) {
  18. if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += (TYPING_SPEED_MAX_VALUE / 50);
  19. }
  20. void velocikey_decelerate(void) {
  21. static uint16_t decay_timer = 0;
  22. if (timer_elapsed(decay_timer) > 500 || decay_timer == 0) {
  23. if (typing_speed > 0) typing_speed -= 1;
  24. //Decay a little faster at half of max speed
  25. if (typing_speed > TYPING_SPEED_MAX_VALUE / 2) typing_speed -= 1;
  26. //Decay even faster at 3/4 of max speed
  27. if (typing_speed > TYPING_SPEED_MAX_VALUE / 4 * 3) typing_speed -= 3;
  28. decay_timer = timer_read();
  29. }
  30. }
  31. uint8_t velocikey_match_speed(uint8_t minValue, uint8_t maxValue) {
  32. return MAX(minValue, maxValue - (maxValue - minValue) * ((float)typing_speed / TYPING_SPEED_MAX_VALUE));
  33. }
  34. // End: Written by Chris Lewis
  35. uint8_t current_os,
  36. mod_primary_mask,
  37. fade_interval,
  38. num_extra_flashes_off = 0;
  39. Color underglow,
  40. flash_color,
  41. saved_color,
  42. hsv_none = {0,0,0};
  43. flashState flash_state = no_flash;
  44. fadeState fade_state = add_fade;
  45. activityState state = boot;
  46. float song_ussr[][2] = SONG(USSR_ANTHEM);
  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 = HUE_MAX - 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 = (HUE_MAX + change_amount) % HUE_MAX;
  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 = HUE_MAX - 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. }
  89. fade_interval = velocikey_match_speed(1, 25);
  90. if (timer_elapsed(active_timer) < INACTIVE_DELAY) {return;}
  91. active_timer = timer_read();
  92. state = inactive;
  93. return;
  94. case inactive:
  95. if (!deactivated) {
  96. deactivated = true;
  97. activated = false;
  98. }
  99. velocikey_decelerate();
  100. fade_interval = velocikey_match_speed(1, 25);
  101. return;
  102. case boot:
  103. return;
  104. }
  105. }
  106. void fade_rgb (void) {
  107. static uint16_t fade_timer;
  108. if (state == boot) {return;}
  109. if (!fade_timer) {fade_timer = timer_read();}
  110. if (timer_elapsed(fade_timer) < fade_interval) {return;}
  111. switch (fade_state) {
  112. case add_fade:
  113. if (underglow.h == HUE_MAX) {
  114. fade_state = sub_fade;
  115. return;
  116. }
  117. underglow.h = underglow.h + 1;
  118. break;
  119. case sub_fade:
  120. if (underglow.h == 0) {
  121. fade_state = add_fade;
  122. return;
  123. }
  124. underglow.h = underglow.h - 1;
  125. break;
  126. }
  127. fade_timer = timer_read();
  128. if (flash_state == no_flash) {
  129. set_color(underglow, false);
  130. }
  131. }
  132. void flash_rgb (void) {
  133. static uint16_t flash_timer;
  134. switch(flash_state) {
  135. case no_flash:
  136. return;
  137. case flash_off:
  138. if (!flash_timer) {flash_timer = timer_read();}
  139. if (timer_elapsed(flash_timer) >= LED_FLASH_DELAY) {
  140. set_color(hsv_none, false);
  141. flash_timer = timer_read();
  142. flash_state = flash_on;
  143. }
  144. return;
  145. case flash_on:
  146. if (timer_elapsed(flash_timer) >= LED_FLASH_DELAY) {
  147. set_color(flash_color, false);
  148. flash_timer = timer_read();
  149. if (num_extra_flashes_off > 0) {
  150. flash_state = flash_off;
  151. num_extra_flashes_off--;
  152. } else {
  153. set_color(underglow, false);
  154. flash_state = no_flash;
  155. }
  156. }
  157. return;
  158. }
  159. }
  160. void set_os (uint8_t os, bool update) {
  161. current_os = os;
  162. if (update) {
  163. eeprom_update_byte(EECONFIG_USERSPACE, current_os);
  164. }
  165. switch (os) {
  166. case OS_MAC:
  167. set_unicode_input_mode(UNICODE_MODE_MACOS);
  168. underglow = (Color){ 213, 255, 255 };
  169. break;
  170. case OS_WIN:
  171. set_unicode_input_mode(UNICODE_MODE_WINCOMPOSE);
  172. underglow = (Color){ 128, 255, 255 };
  173. break;
  174. case OS_NIX:
  175. set_unicode_input_mode(UNICODE_MODE_LINUX);
  176. underglow = (Color){ 43, 255, 255 };
  177. break;
  178. default:
  179. underglow = (Color){ 0, 0, 255 };
  180. }
  181. set_color(underglow, update);
  182. flash_color = underglow;
  183. flash_state = flash_off;
  184. state = boot;
  185. num_extra_flashes_off = 3;
  186. }
  187. // register GUI if Mac or Ctrl if other
  188. void pri_mod(bool press) {
  189. if (press) {
  190. if (current_os == OS_MAC) {
  191. register_code(KC_LGUI);
  192. } else {
  193. register_code(KC_LCTL);
  194. }
  195. } else {
  196. if (current_os == OS_MAC) {
  197. unregister_code(KC_LGUI);
  198. } else {
  199. unregister_code(KC_LCTL);
  200. }
  201. }
  202. }
  203. // register Ctrl if Mac or GUI if other
  204. void sec_mod(bool press) {
  205. if (press) {
  206. if (current_os == OS_MAC) {
  207. register_code(KC_LCTL);
  208. } else {
  209. register_code(KC_LGUI);
  210. }
  211. } else {
  212. if (current_os == OS_MAC) {
  213. unregister_code(KC_LCTL);
  214. } else {
  215. unregister_code(KC_LGUI);
  216. }
  217. }
  218. }
  219. void multi_tap(uint8_t num_of_chars, uint16_t keycode, bool use_shift) {
  220. if (use_shift) {
  221. register_code(KC_LSFT);
  222. }
  223. for (int i = 0; i < num_of_chars; i++) {
  224. tap_code(keycode);
  225. }
  226. if (use_shift) {
  227. unregister_code(KC_LSFT);
  228. }
  229. }
  230. void pair_surround_type(uint8_t num_of_chars, uint16_t keycode, bool use_shift) {
  231. for (int i = 0; i < num_of_chars; i++) {
  232. (use_shift) ? register_mods(MOD_BIT( KC_LSFT)) : NULL;
  233. tap_code(keycode);
  234. tap_code((keycode == KC_LCBR) ? KC_RCBR : (keycode == KC_LBRC) ? KC_RBRC : (keycode == KC_LPRN) ? KC_RPRN : KC_NO);
  235. (use_shift) ? unregister_mods(MOD_BIT( KC_LSFT)) : NULL;
  236. tap_code(KC_LEFT);
  237. }
  238. }
  239. void surround_type(uint8_t num_of_chars, uint16_t keycode, bool use_shift) {
  240. for (int i = 0; i < num_of_chars; i++) {
  241. (use_shift) ? register_mods(MOD_BIT( KC_LSFT)) : NULL;
  242. tap_code(keycode);
  243. (use_shift) ? unregister_mods(MOD_BIT( KC_LSFT)) : NULL;
  244. }
  245. multi_tap(num_of_chars / 2, KC_LEFT, false);
  246. }
  247. void long_keystroke(size_t num_of_keys, uint16_t keys[]) {
  248. for (int i = 0; i < num_of_keys-1; i++) {
  249. register_code(keys[i]);
  250. }
  251. tap_code(keys[num_of_keys-1]);
  252. for (int i = 0; i < num_of_keys-1; i++) {
  253. unregister_code(keys[i]);
  254. }
  255. }
  256. void pri_mod_keystroke(uint16_t key) {
  257. pri_mod(true);
  258. tap_code(key);
  259. pri_mod(false);
  260. }
  261. void leader_end_user(void) {
  262. // begin OS functions
  263. if (leader_sequence_two_keys(KC_P, KC_B)) {
  264. if (current_os == OS_WIN) {
  265. long_keystroke(2, (uint16_t[]){KC_LGUI, KC_PAUSE});
  266. } else {
  267. return;
  268. }
  269. }
  270. if (leader_sequence_two_keys(KC_S, KC_S)) {
  271. if (current_os == OS_MAC) {
  272. long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_4});
  273. } else if (current_os == OS_WIN) {
  274. long_keystroke(3, (uint16_t[]){KC_LGUI, KC_LSFT, KC_S});
  275. } else {
  276. return;
  277. }
  278. }
  279. if (leader_sequence_three_keys(KC_C, KC_A, KC_D)) {
  280. if (current_os == OS_WIN) {
  281. long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LALT, KC_DEL});
  282. } else {
  283. }
  284. }
  285. if (leader_sequence_three_keys(KC_C, KC_A, KC_E)) {
  286. if (current_os == OS_WIN) {
  287. long_keystroke(3, (uint16_t[]){KC_LCTL, KC_LALT, KC_END});
  288. } else {
  289. }
  290. }
  291. // end OS functions
  292. // begin format functions
  293. if (leader_sequence_one_key(KC_B)) {
  294. surround_type(2, KC_8, true);
  295. }
  296. if (leader_sequence_one_key(KC_I)) {
  297. surround_type(2, KC_MINS, true);
  298. }
  299. if (leader_sequence_one_key(KC_U)) {
  300. surround_type(4, KC_MINS, true);
  301. }
  302. if (leader_sequence_one_key(KC_S)) {
  303. surround_type(4, KC_GRAVE, true);
  304. }
  305. if (leader_sequence_one_key(KC_C)) {
  306. register_unicode(0x00E7); // ç
  307. }
  308. if (leader_sequence_two_keys(KC_A, KC_V)) {
  309. surround_type(2, KC_QUOT, true);
  310. pair_surround_type(2, KC_LCBR, true);
  311. surround_type(2, KC_SPC, false);
  312. }
  313. if (leader_sequence_two_keys(KC_M, KC_L)) {
  314. pair_surround_type(1, KC_LBRC, false);
  315. SEND_STRING("LINK_NAME");
  316. tap_code(KC_RGHT);
  317. pair_surround_type(1, KC_LPRN, true);
  318. pri_mod_keystroke(KC_V);
  319. }
  320. if (leader_sequence_two_keys(KC_C, KC_C)) {
  321. surround_type(2, KC_GRAVE, false);
  322. }
  323. if (leader_sequence_three_keys(KC_C, KC_C, KC_C)) {
  324. surround_type(6, KC_GRAVE, false);
  325. }
  326. if (leader_sequence_one_key(KC_E)) {
  327. register_unicode(0x00E8); // è
  328. }
  329. if (leader_sequence_two_keys(KC_E, KC_E)) {
  330. register_unicode(0x00E9); // é
  331. }
  332. // end format functions
  333. // start fancy functions
  334. if (leader_sequence_two_keys(KC_V, KC_P)) {
  335. SEND_STRING("ggvG}x:set paste\ni");
  336. pri_mod_keystroke(KC_V);
  337. }
  338. if (leader_sequence_three_keys(KC_C, KC_C, KC_ENT)) {
  339. surround_type(6, KC_GRAVE, false);
  340. pri_mod_keystroke(KC_V);
  341. multi_tap(3, KC_RGHT, false);
  342. tap_code(KC_ENTER);
  343. }
  344. if (leader_sequence_three_keys(KC_T, KC_C, KC_ENT)) {
  345. multi_tap(3, KC_GRAVE, false);
  346. pri_mod_keystroke(KC_V);
  347. multi_tap(2, KC_ENTER, false);
  348. }
  349. // end fancy functions
  350. // start typing functions
  351. if (leader_sequence_two_keys(KC_T, KC_M)) {
  352. register_unicode(0x2122); // ™
  353. }
  354. if (leader_sequence_two_keys(KC_D, KC_D)) {
  355. SEND_STRING(".\\Administrator");
  356. }
  357. if (leader_sequence_three_keys(KC_D, KC_D, KC_D)) {
  358. SEND_STRING(".\\Administrator");
  359. tap_code(KC_TAB);
  360. pri_mod_keystroke(KC_V);
  361. tap_code(KC_ENTER);
  362. }
  363. if (leader_sequence_three_keys(KC_L, KC_O, KC_D)) {
  364. send_unicode_string("ಠ__ಠ");
  365. }
  366. if (leader_sequence_three_keys(KC_M, KC_A, KC_P)) {
  367. SEND_STRING("https://github.com/qmk/qmk_firmware/tree/master/users/arkag");
  368. }
  369. if (leader_sequence_two_keys(KC_F, KC_F)) {
  370. send_unicode_string("(╯‵Д′)╯彡┻━┻");
  371. }
  372. if (leader_sequence_three_keys(KC_F, KC_F, KC_F)) {
  373. send_unicode_string("┬─┬ノ( º _ º ノ)");
  374. }
  375. if (leader_sequence_three_keys(KC_L, KC_O, KC_L)) {
  376. send_unicode_string("( ͡° ͜ʖ ͡°)");
  377. }
  378. if (leader_sequence_three_keys(KC_S, KC_S, KC_S)) {
  379. send_unicode_string("¯\\_(ツ)_/¯");
  380. }
  381. // end typing functions
  382. }
  383. void matrix_init_user(void) {
  384. current_os = eeprom_read_byte(EECONFIG_USERSPACE);
  385. set_os(current_os, false);
  386. }
  387. void matrix_scan_user(void) {
  388. check_state();
  389. flash_rgb();
  390. fade_rgb();
  391. }
  392. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  393. switch (keycode) {
  394. #ifdef AUDIO_ENABLE
  395. case M_USSR:
  396. PLAY_SONG(song_ussr);
  397. return false;
  398. #endif
  399. case M_OS:
  400. if (record->event.pressed){
  401. set_os((current_os+1) % _OS_COUNT, true);
  402. }
  403. return false;
  404. case M_DASH:
  405. if (record->event.pressed){
  406. register_unicode(0x2014); // —
  407. }
  408. return false;
  409. default:
  410. if (record->event.pressed) {
  411. state = active;
  412. velocikey_accelerate();
  413. }
  414. return true;
  415. }
  416. }