zach_common_functions.c 14 KB

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