zach_common_functions.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #ifndef ZACH_COMMON_FUNCTIONS
  2. #define ZACH_COMMON_FUNCTIONS
  3. #include "eeconfig.h"
  4. #include "action_layer.h"
  5. #include "keymap_colemak.h"
  6. #include "extra_functions.c"
  7. extern keymap_config_t keymap_config;
  8. // Fillers to make layering more clear
  9. #define _______ KC_TRNS
  10. #define XXXXXXX KC_NO
  11. #define C(n) RCTL(n)
  12. #define CADKEY RCTL(RALT(KC_DEL))
  13. void tap(uint16_t keycode){
  14. register_code(keycode);
  15. unregister_code(keycode);
  16. };
  17. void persistant_default_layer_set(uint16_t default_layer){
  18. eeconfig_update_default_layer(default_layer);
  19. default_layer_set(default_layer);
  20. };
  21. // Automatic number generation of important keywords
  22. enum my_keycodes{
  23. // Layer numbers
  24. _COLEMAK = 0,
  25. _SWCOLE,
  26. _RAISE,
  27. _LOWER,
  28. _ADJUST,
  29. _UNICODES,
  30. // These use process_record_user()
  31. COLEMAK = SAFE_RANGE,
  32. SWCOLE,
  33. LOWER,
  34. RAISE,
  35. SHFT_CAP,
  36. CTRLB,
  37. CPYPST,
  38. FACE,
  39. UNIWIN,
  40. UNILIN,
  41. DISFACE,
  42. TFLIP,
  43. TPUT,
  44. SHRUG,
  45. PENIS,
  46. BOOBS,
  47. Sil_Pas,
  48. Sil_Usr,
  49. UltiU,
  50. UltiP,
  51. TappyR,
  52. TappyL,
  53. RANDIG,
  54. FINGER,
  55. // Tap_Dance nums
  56. RAI = 0,
  57. LOW,
  58. SUP
  59. };
  60. #ifdef AUDIO_ENABLE
  61. #include "audio.h"
  62. float tone_startup[][2] = SONG(STARTUP_SOUND);
  63. float tone_goodbye[][2] = SONG(GOODBYE_SOUND);
  64. float tone_colemak[][2] = SONG(COLEMAK_SOUND);
  65. float tone_swcole[][2] = SONG(QWERTY_SOUND);
  66. float tone_capslock_on[][2] = SONG(CAPS_LOCK_ON_SOUND);
  67. float tone_capslock_off[][2] = SONG(CAPS_LOCK_OFF_SOUND);
  68. float tone_ctrl_mod[][2] = SONG(COIN_SOUND);
  69. float tone_copy[][2] = SONG(SCROLL_LOCK_ON_SOUND);
  70. float tone_paste[][2] = SONG(SCROLL_LOCK_OFF_SOUND);
  71. float uniwin[][2] = SONG(UNICODE_WINDOWS);
  72. float unilin[][2] = SONG(UNICODE_LINUX);
  73. #endif
  74. #ifdef TAP_DANCE_ENABLE
  75. #define TAPPING_TERM 200
  76. uint8_t Lstate = 0, Rstate = 0;
  77. uint32_t Ltimer = 0, Rtimer = 0;
  78. uint32_t Ltimes[3], Rtimes[4]; // Ratio of tap times should be about 1.335 (L/R)
  79. void rhythm_parse(void){
  80. int L = Ltimes[0] + Ltimes[1] + Ltimes[2]; // Start to end time
  81. int R = Rtimes[0] + Rtimes[1] + Rtimes[2] + Rtimes[3];
  82. if(abs(R-L) > 10){
  83. tap(KC_N); tap(KC_O);
  84. return;
  85. } else {
  86. L = (L / 3)*100; // Average time per tap * 100
  87. R = (R / 4);
  88. if(abs(abs(L/R)-133) > 1){
  89. tap(KC_N); tap(KC_O);
  90. tap(KC_P); tap(KC_E);
  91. return;
  92. } else {
  93. tap(KC_O); tap(KC_K);
  94. return;
  95. }
  96. }
  97. };
  98. void dance_raise_press(qk_tap_dance_state_t *state, void *user_data){// Called on each tap
  99. switch(state->count){ // Only turn the layer on once
  100. case 1:
  101. layer_off(_UNICODES);
  102. layer_on(_RAISE);
  103. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  104. break;
  105. }
  106. };
  107. void dance_raise_lift(qk_tap_dance_state_t *state, void *user_data){ // Called on release
  108. switch(state->count){
  109. case 1: // Normal action. Turn off layers
  110. layer_off(_RAISE);
  111. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  112. layer_off(_UNICODES);
  113. break;
  114. }
  115. };
  116. /////////////////////////////////////////////////////////////////////
  117. void dance_lower_press(qk_tap_dance_state_t *state, void *user_data){// Called on tap
  118. switch(state->count){
  119. case 1: // Turn on lower
  120. layer_off(_UNICODES);
  121. layer_on(_LOWER);
  122. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  123. break;
  124. }
  125. };
  126. void dance_lower_lift(qk_tap_dance_state_t *state, void *user_data){ // Called on release
  127. switch(state->count){
  128. case 1: // Normal action. Turn off layers
  129. layer_off(_LOWER);
  130. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  131. layer_off(_UNICODES);
  132. break;
  133. case 2: // Turn on _UNICODES layer
  134. layer_off(_LOWER);
  135. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  136. layer_on(_UNICODES);
  137. #ifdef AUDIO_ENABLE
  138. PLAY_NOTE_ARRAY(tone_ctrl_mod, false, 0);
  139. #endif
  140. break;
  141. }
  142. };
  143. /////////////////////////////////////////////////////////////////////
  144. void dance_super_press(qk_tap_dance_state_t *state, void *user_data){ // Called on down
  145. if(state->count == 1){
  146. register_code(KC_LGUI);
  147. }
  148. }
  149. void dance_super_done(qk_tap_dance_state_t *state, void *user_data){ // Called on timeout
  150. switch(state->count){
  151. case 2:
  152. register_code(KC_LGUI);
  153. tap(KC_L);
  154. unregister_code(KC_LGUI);
  155. break;
  156. }
  157. }
  158. void dance_super_lift(qk_tap_dance_state_t *state, void *user_data){ // Called on up
  159. unregister_code(KC_LGUI);
  160. }
  161. qk_tap_dance_action_t tap_dance_actions[] = {
  162. [RAI] = ACTION_TAP_DANCE_FN_ADVANCED(dance_raise_press, NULL, dance_raise_lift),
  163. [LOW] = ACTION_TAP_DANCE_FN_ADVANCED(dance_lower_press, NULL, dance_lower_lift),
  164. [SUP] = ACTION_TAP_DANCE_FN_ADVANCED(dance_super_press, dance_super_done, dance_super_lift)
  165. };
  166. #endif
  167. #ifdef UNICODE_ENABLE
  168. // Unicode shortcuts
  169. #define IBANG UC(0x203D)
  170. #define RAROW UC(0x2192)
  171. #define LAROW UC(0x2190)
  172. #define DEGREE UC(0x00B0)
  173. #define OMEGA UC(0x03A9)
  174. #define WOMEGA UC(0x03C9)
  175. #define MICRO UC(0x00B5)
  176. #define PLUMIN UC(0x00B1)
  177. #define SUPA2 UC(0x00B2)
  178. #define ROMAN1 UC(0x2160)
  179. #define ROMAN2 UC(0x2161)
  180. #define ROMAN3 UC(0x2162)
  181. #define ROMAN4 UC(0x2163)
  182. #define ROMAN5 UC(0x2164)
  183. #define ROMAN6 UC(0x2165)
  184. #define ROMAN7 UC(0x2166)
  185. #define roman1 UC(0x2170)
  186. #define roman2 UC(0x2171)
  187. #define roman3 UC(0x2172)
  188. #define roman4 UC(0x2173)
  189. #define roman5 UC(0x2174)
  190. #define roman6 UC(0x2175)
  191. #define roman7 UC(0x2176)
  192. #ifdef UNICODEMAP_ENABLE // For Unicode characters larger than 0x8000. Send with X(<unicode>)
  193. enum Ext_Unicode{
  194. PENGUIN = 0,
  195. BOAR,
  196. MONKEY,
  197. DRAGON,
  198. CHICK,
  199. TUMBLER
  200. };
  201. const uint32_t PROGMEM unicode_map[] = {
  202. [PENGUIN] = 0x1F427,
  203. [BOAR] = 0x1F417,
  204. [MONKEY] = 0x1F412,
  205. [DRAGON] = 0x1F409,
  206. [CHICK] = 0x1F425,
  207. [TUMBLER] = 0x1F943
  208. };
  209. #define PENGY X(PENGUIN)
  210. #define BOARY X(BOAR)
  211. #define MNKY X(MONKEY)
  212. #define DRGN X(DRAGON)
  213. #define DUCK X(CHICK)
  214. #define TMBL X(TUMBLER)
  215. #endif
  216. #endif
  217. static uint16_t key_timer;
  218. static uint8_t caps_status = 0;
  219. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  220. switch (keycode) {
  221. case COLEMAK:
  222. if(record->event.pressed){
  223. persistant_default_layer_set(1UL<<_COLEMAK);
  224. #ifdef AUDIO_ENABLE
  225. PLAY_NOTE_ARRAY(tone_colemak, false, 0);
  226. #endif
  227. }
  228. return false;
  229. break;
  230. case SWCOLE:
  231. if(record->event.pressed){
  232. persistant_default_layer_set(1UL<<_SWCOLE);
  233. #ifdef AUDIO_ENABLE
  234. PLAY_NOTE_ARRAY(tone_swcole, false, 0);
  235. #endif
  236. }
  237. return false;
  238. break;
  239. #ifndef TAP_DANCE_ENABLE
  240. case RAISE:
  241. if(record->event.pressed){
  242. layer_on(_RAISE);
  243. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  244. } else {
  245. layer_off(_RAISE);
  246. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  247. }
  248. return false;
  249. break;
  250. case LOWER:
  251. if(record->event.pressed){
  252. layer_on(_LOWER);
  253. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  254. } else {
  255. layer_off(_LOWER);
  256. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  257. }
  258. return false;
  259. break;
  260. #endif
  261. case SHFT_CAP:
  262. if(record->event.pressed){
  263. key_timer = timer_read(); // if the key is being pressed, we start the timer.
  264. register_code(KC_LSHIFT);
  265. } else { // this means the key was just released (tap or "held down")
  266. if(timer_elapsed(key_timer) < 152){ // Time in ms, the threshold we pick for counting something as a tap.
  267. tap(KC_CAPS);
  268. if(caps_status == 0){
  269. caps_status = 1;
  270. #ifdef AUDIO_ENABLE
  271. PLAY_NOTE_ARRAY(tone_capslock_on, false, 0);
  272. #endif
  273. } else {
  274. caps_status = 0;
  275. #ifdef AUDIO_ENABLE
  276. PLAY_NOTE_ARRAY(tone_capslock_off, false, 0);
  277. #endif
  278. }
  279. }
  280. unregister_code(KC_LSHIFT);
  281. }
  282. return false;
  283. break;
  284. case CTRLB: // Control-B on tap (bold)
  285. if(record->event.pressed){
  286. key_timer = timer_read(); // if the key is being pressed, we start the timer.
  287. register_code(KC_LCTL);
  288. } else { // this means the key was just released (tap or "held down")
  289. if (timer_elapsed(key_timer) < 152) { // Time in ms, the threshold we pick for counting something as a tap.
  290. tap(KC_B);
  291. #ifdef AUDIO_ENABLE
  292. PLAY_NOTE_ARRAY(tone_ctrl_mod, false, 0);
  293. #endif
  294. #ifdef BACKLIGHT_BREATHING
  295. breathing_speed_set(2);
  296. breathing_pulse();
  297. #endif
  298. }
  299. unregister_code(KC_LCTL);
  300. }
  301. return false;
  302. break;
  303. case CPYPST: // One key copy/paste
  304. if(record->event.pressed){
  305. key_timer = timer_read();
  306. } else {
  307. if (timer_elapsed(key_timer) > 152) { // Hold, copy
  308. register_code(KC_LCTL);
  309. tap(KC_C);
  310. unregister_code(KC_LCTL);
  311. #ifdef AUDIO_ENABLE
  312. PLAY_NOTE_ARRAY(tone_copy, false, 0);
  313. #endif
  314. } else { // Tap, paste
  315. register_code(KC_LCTL);
  316. tap(KC_V);
  317. unregister_code(KC_LCTL);
  318. #ifdef AUDIO_ENABLE
  319. PLAY_NOTE_ARRAY(tone_paste, false, 0);
  320. #endif
  321. }
  322. }
  323. return false;
  324. break;
  325. #ifdef UNICODE_ENABLE
  326. case UNIWIN:
  327. if(record->event.pressed){
  328. set_unicode_input_mode(UC_WIN);
  329. #ifdef AUDIO_ENABLE
  330. PLAY_NOTE_ARRAY(uniwin, false, 0);
  331. #endif
  332. }
  333. return false;
  334. break;
  335. case UNILIN:
  336. if(record->event.pressed){
  337. set_unicode_input_mode(UC_LNX);
  338. #ifdef AUDIO_ENABLE
  339. PLAY_NOTE_ARRAY(unilin, false, 0);
  340. #endif
  341. }
  342. return false;
  343. break;
  344. case DISFACE: // ಠ_ಠ
  345. if(record->event.pressed){
  346. process_unicode((0x0CA0|QK_UNICODE), record); // Eye
  347. register_code(KC_RSFT);
  348. tap(KC_MINS);
  349. unregister_code(KC_RSFT);
  350. process_unicode((0x0CA0|QK_UNICODE), record); // Eye
  351. }
  352. return false;
  353. break;
  354. case TFLIP: // (╯°□°)╯ ︵ ┻━┻
  355. if(record->event.pressed){
  356. register_code(KC_RSFT);
  357. tap(KC_9);
  358. unregister_code(KC_RSFT);
  359. process_unicode((0x256F|QK_UNICODE), record); // Arm
  360. process_unicode((0x00B0|QK_UNICODE), record); // Eye
  361. process_unicode((0x25A1|QK_UNICODE), record); // Mouth
  362. process_unicode((0x00B0|QK_UNICODE), record); // Eye
  363. register_code(KC_RSFT);
  364. tap(KC_0);
  365. unregister_code(KC_RSFT);
  366. process_unicode((0x256F|QK_UNICODE), record); // Arm
  367. tap(KC_SPC);
  368. process_unicode((0x0361|QK_UNICODE), record); // Flippy
  369. tap(KC_SPC);
  370. process_unicode((0x253B|QK_UNICODE), record); // Table
  371. process_unicode((0x2501|QK_UNICODE), record); // Table
  372. process_unicode((0x253B|QK_UNICODE), record); // Table
  373. }
  374. return false;
  375. break;
  376. case TPUT: // ┬──┬ ノ( ゜-゜ノ)
  377. if(record->event.pressed){
  378. process_unicode((0x252C|QK_UNICODE), record); // Table
  379. process_unicode((0x2500|QK_UNICODE), record); // Table
  380. process_unicode((0x2500|QK_UNICODE), record); // Table
  381. process_unicode((0x252C|QK_UNICODE), record); // Table
  382. tap(KC_SPC);
  383. process_unicode((0x30CE|QK_UNICODE), record); // Arm
  384. register_code(KC_RSFT);
  385. tap(KC_9);
  386. unregister_code(KC_RSFT);
  387. tap(KC_SPC);
  388. process_unicode((0x309C|QK_UNICODE), record); // Eye
  389. tap(KC_MINS);
  390. process_unicode((0x309C|QK_UNICODE), record); // Eye
  391. process_unicode((0x30CE|QK_UNICODE), record); // Arm
  392. register_code(KC_RSFT);
  393. tap(KC_0);
  394. unregister_code(KC_RSFT);
  395. }
  396. return false;
  397. break;
  398. case SHRUG: // ¯\_(ツ)_/¯
  399. if(record->event.pressed){
  400. process_unicode((0x00AF|QK_UNICODE), record); // Hand
  401. tap(KC_BSLS); // Arm
  402. register_code(KC_RSFT);
  403. tap(KC_UNDS); // Arm
  404. tap(KC_LPRN); // Head
  405. unregister_code(KC_RSFT);
  406. process_unicode((0x30C4|QK_UNICODE), record); // Face
  407. register_code(KC_RSFT);
  408. tap(KC_RPRN); // Head
  409. tap(KC_UNDS); // Arm
  410. unregister_code(KC_RSFT);
  411. tap(KC_SLSH); // Arm
  412. process_unicode((0x00AF|QK_UNICODE), record); // Hand
  413. }
  414. return false;
  415. break;
  416. #endif
  417. case FACE: // (o_O)
  418. if(record->event.pressed){
  419. register_code(KC_RSFT);
  420. tap(KC_LPRN);
  421. unregister_code(KC_RSFT);
  422. tap(KC_O);
  423. register_code(KC_RSFT);
  424. tap(KC_UNDS);
  425. tap(KC_O);
  426. tap(KC_RPRN);
  427. unregister_code(KC_RSFT);
  428. }
  429. return false;
  430. break;
  431. #ifdef TAP_DANCE_ENABLE
  432. case TappyR:
  433. if(record->event.pressed){
  434. if(timer_elapsed32(Rtimer) > 1052){
  435. Rstate = 0;
  436. }
  437. switch(Rstate){
  438. case 0:
  439. Rtimer = timer_read32();
  440. Rstate++;
  441. break;
  442. case 1:
  443. Rtimes[0] = timer_elapsed32(Rtimer);
  444. Rtimer = timer_read32();
  445. Rstate++;
  446. break;
  447. case 2:
  448. Rtimes[1] = timer_elapsed32(Rtimer);
  449. Rtimer = timer_read32();
  450. Rstate++;
  451. break;
  452. case 3:
  453. Rtimes[2] = timer_elapsed32(Rtimer);
  454. Rstate = 0;
  455. break;
  456. }
  457. if(Rstate == 0 && Lstate == 0) rhythm_parse();
  458. }
  459. return false;
  460. break;
  461. case TappyL:
  462. if(record->event.pressed){
  463. if(timer_elapsed32(Ltimer) > 1052){
  464. Lstate = 0;
  465. }
  466. switch(Lstate){
  467. case 0:
  468. Ltimer = timer_read32();
  469. Lstate++;
  470. break;
  471. case 1:
  472. Ltimes[0] = timer_elapsed32(Ltimer);
  473. Ltimer = timer_read32();
  474. Lstate++;
  475. break;
  476. case 2:
  477. Ltimes[1] = timer_elapsed32(Ltimer);
  478. Lstate = 0;
  479. break;
  480. }
  481. if(Rstate == 0 && Lstate == 0) rhythm_parse();
  482. }
  483. return false;
  484. break;
  485. #endif
  486. #endif
  487. case RANDIG:
  488. if (record->event.pressed) {
  489. tap_random_base64();
  490. }
  491. return false;
  492. break;
  493. }
  494. return true;
  495. };
  496. #ifdef AUDIO_ENABLE
  497. void matrix_init_user(void){ // Run once at startup
  498. #ifdef AUDIO_ENABLE
  499. _delay_ms(50); // gets rid of tick
  500. PLAY_NOTE_ARRAY(tone_startup, false, 0);
  501. #endif
  502. }
  503. void play_goodbye_tone(void){
  504. PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
  505. _delay_ms(150);
  506. }
  507. void shutdown_user(){
  508. PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
  509. _delay_ms(150);
  510. stop_all_notes();
  511. }
  512. void music_on_user(void){ // Run when the music layer is turned on
  513. PLAY_NOTE_ARRAY(tone_startup, false, 0);
  514. }
  515. void music_off_user(void){ // Run when music is turned off
  516. PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
  517. }
  518. #endif
  519. #endif