keymap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // this is the style you want to emulate.
  2. // This is the canonical layout file for the Quantum project. If you want to add another keyboard,
  3. #include QMK_KEYBOARD_H
  4. #ifdef AUDIO_ENABLE
  5. #include "audio.h"
  6. #endif
  7. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  8. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  9. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  10. // entirely and just use numbers.
  11. enum mitosis_layers
  12. {
  13. _QWERTY,
  14. _SHIFTED,
  15. _FUNCTIONPC,
  16. _FUNCTIONMAC,
  17. _FUNCSHIFT,
  18. _ADJUST
  19. };
  20. enum mitosis_keycodes
  21. {
  22. FNKEY = SAFE_RANGE,
  23. SHIFT,
  24. MACSLEEP,
  25. FNMAC,
  26. FNPC,
  27. AUDIOTEST,
  28. DYNAMIC_MACRO_RANGE,
  29. };
  30. // Macro definitions for readability
  31. enum mitosis_macros
  32. {
  33. VOLU,
  34. VOLD,
  35. ESCM
  36. };
  37. #include "dynamic_macro.h"
  38. #define LONGPRESS_DELAY 150
  39. #define LAYER_TOGGLE_DELAY 900
  40. // Fillers to make layering more clear
  41. #define _______ KC_TRNS
  42. #define __MOD__ KC_TRNS
  43. #define XXXXXXX KC_NO
  44. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  45. [_QWERTY] = LAYOUT( /* QWERTY adapted to this crazy thing */
  46. KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P,
  47. KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN,
  48. SFT_T(KC_Z), KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, SFT_T(KC_SLSH),
  49. KC_LCTL, M(ESCM), KC_TAB, KC_QUOT, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT,
  50. KC_LALT, KC_LGUI, KC_SPC, SHIFT, FNKEY, KC_BSPC, KC_ENT, MO(_ADJUST)
  51. ),
  52. [_SHIFTED] = LAYOUT( /* Shifted Layer, layered so that tri_layer can be used, or selectively
  53. able to modify individual key's shifted behaviour */
  54. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  57. __MOD__, KC_DEL, _______, _______, _______, _______, _______, _______,
  58. __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, XXXXXXX
  59. ),
  60. [_FUNCTIONPC] = LAYOUT( /* Function Layer mimicks planck's raise layer somewhat */
  61. KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0,
  62. LCTL(KC_A), LCTL(KC_S), _______, LCTL(KC_F),_______, _______, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC,
  63. LCTL(KC_Z), LCTL(KC_X), LCTL(KC_C), LCTL(KC_V),_______, _______, KC_BSLS, _______, _______, KC_QUOT,
  64. __MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END,
  65. __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, XXXXXXX
  66. ),
  67. [_FUNCTIONMAC] = LAYOUT( /* Function Layer mimicks planck's raise layer somewhat */
  68. KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0,
  69. LGUI(KC_A), LGUI(KC_S), _______, LGUI(KC_F),_______, _______, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC,
  70. LGUI(KC_Z), LGUI(KC_X), LGUI(KC_C), LGUI(KC_V),_______, _______, KC_BSLS, _______, _______, KC_QUOT,
  71. __MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END,
  72. __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, XXXXXXX
  73. ),
  74. [_FUNCSHIFT] = LAYOUT( /* Function Shifted Layer mimicks planck's lower layer somewhat */
  75. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  76. KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, _______, _______, KC_LCBR, KC_RCBR,
  77. KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, _______, _______, _______,
  78. __MOD__, KC_DEL, _______, KC_TILD, _______, _______, _______, _______,
  79. __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, _______
  80. ),
  81. [_ADJUST] = LAYOUT( /* Adjust layer for fancy stuff and macros */
  82. RESET, FNPC, _______, _______, _______, _______, DYN_REC_START1, DYN_REC_START2, _______, _______,
  83. FNMAC, _______, AU_ON, AU_OFF, _______, _______, _______, _______, MACSLEEP, _______,
  84. MUV_DE, MUV_IN, MU_ON, MU_OFF, _______, _______, KC_MUTE, KC_MPRV, KC_MNXT, KC_MPLY,
  85. __MOD__, _______, _______, _______, _______, DYN_MACRO_PLAY1, DYN_MACRO_PLAY2, _______,
  86. __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, __MOD__
  87. )
  88. };
  89. #ifdef AUDIO_ENABLE
  90. float tone_startup[][2] = SONG(STARTUP_SOUND);
  91. float tone_qwerty[][2] = SONG(QWERTY_SOUND);
  92. float tone_dyn_macro_rec[][2] = SONG(DVORAK_SOUND);
  93. float tone_dyn_macro_play[][2] = SONG(COLEMAK_SOUND);
  94. float tone_fnpc[][2] = SONG(PLOVER_SOUND);
  95. float tone_fnmac[][2] = SONG(PLOVER_GOODBYE_SOUND);
  96. float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
  97. float tone_goodbye[][2] = SONG(GOODBYE_SOUND);
  98. #endif
  99. const uint16_t PROGMEM fn_actions[] = {
  100. };
  101. static uint16_t function_layer = _FUNCTIONMAC;
  102. static uint16_t key_timer;
  103. void persistent_function_layer_set(uint16_t new_function_layer) {
  104. // eeconfig_update_function_layer(new_function_layer);
  105. function_layer = new_function_layer;
  106. // should clear layers to avoid getting stuck.
  107. }
  108. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  109. {
  110. // MACRODOWN only works in this function
  111. switch(id) {
  112. //switch multiplexing for media, short tap for volume up, long press for play/pause
  113. case VOLU:
  114. if (record->event.pressed) {
  115. key_timer = timer_read(); // if the key is being pressed, we start the timer.
  116. } else { // this means the key was just released, so we can figure out how long it was pressed for (tap or "held down").
  117. if (timer_elapsed(key_timer) > LONGPRESS_DELAY) { // LONGPRESS_DELAY being 150ms, the threshhold we pick for counting something as a tap.
  118. return MACRO(T(MPLY), END);
  119. } else {
  120. return MACRO(T(VOLU), END);
  121. }
  122. }
  123. break;
  124. //switch multiplexing for media, short tap for volume down, long press for next track
  125. case VOLD:
  126. if (record->event.pressed) {
  127. key_timer = timer_read();
  128. } else {
  129. if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
  130. return MACRO(T(MNXT), END);
  131. } else {
  132. return MACRO(T(VOLD), END);
  133. }
  134. }
  135. break;
  136. //switch multiplexing for escape, short tap for escape, long press for context menu
  137. case ESCM:
  138. if (record->event.pressed) {
  139. key_timer = timer_read();
  140. } else {
  141. if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
  142. return MACRO(T(APP), END);
  143. } else {
  144. return MACRO(T(ESC), END);
  145. }
  146. }
  147. break;
  148. }
  149. return MACRO_NONE;
  150. };
  151. static bool singular_key = false;
  152. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  153. uint16_t macro_kc = (keycode == MO(_ADJUST) ? DYN_REC_STOP : keycode);
  154. if (!process_record_dynamic_macro(macro_kc, record)) {
  155. return false;
  156. }
  157. uint8_t layer;
  158. layer = biton32(layer_state); // get the current layer
  159. //custom layer handling for tri_layer,
  160. switch (keycode) {
  161. case MACSLEEP:
  162. if (record->event.pressed) {
  163. // ACTION_MODS_KEY(MOD_LCTL | MOD_LSFT, KC_POWER);
  164. register_code(KC_RSFT);
  165. register_code(KC_RCTL);
  166. register_code(KC_POWER);
  167. unregister_code(KC_POWER);
  168. unregister_code(KC_RCTL);
  169. unregister_code(KC_RSFT);
  170. }
  171. return false;
  172. break;
  173. case FNKEY:
  174. if (record->event.pressed) {
  175. key_timer = timer_read();
  176. singular_key = true;
  177. // layer_on(_FUNCTION);
  178. layer_on(function_layer);
  179. } else {
  180. if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
  181. layer_off(function_layer);
  182. }
  183. }
  184. update_tri_layer(function_layer, _SHIFTED, _FUNCSHIFT);
  185. return false;
  186. break;
  187. //SHIFT is handled as LSHIFT in the general case
  188. case SHIFT:
  189. if (record->event.pressed) {
  190. key_timer = timer_read();
  191. singular_key = true;
  192. layer_on(_SHIFTED);
  193. register_code(KC_LSFT);
  194. } else {
  195. if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
  196. layer_off(_SHIFTED);
  197. unregister_code(KC_LSFT);
  198. }
  199. }
  200. update_tri_layer(function_layer, _SHIFTED, _FUNCSHIFT);
  201. return false;
  202. break;
  203. case FNPC:
  204. if (record->event.pressed) {
  205. persistent_function_layer_set(_FUNCTIONPC);
  206. #ifdef AUDIO_ENABLE
  207. PLAY_SONG(tone_fnpc);
  208. #endif
  209. }
  210. return false;
  211. break;
  212. case FNMAC:
  213. if (record->event.pressed) {
  214. persistent_function_layer_set(_FUNCTIONMAC);
  215. #ifdef AUDIO_ENABLE
  216. PLAY_SONG(tone_fnmac);
  217. #endif
  218. }
  219. return false;
  220. break;
  221. case AUDIOTEST:
  222. if (record->event.pressed) {
  223. #ifdef AUDIO_ENABLE
  224. PLAY_SONG(music_scale);
  225. register_code(KC_M);
  226. unregister_code(KC_M);
  227. #endif
  228. register_code(KC_A);
  229. } else {
  230. unregister_code(KC_A);
  231. }
  232. return false;
  233. break;
  234. //If any other key was pressed during the layer mod hold period,
  235. //then the layer mod was used momentarily, and should block latching
  236. default:
  237. singular_key = false;
  238. break;
  239. }
  240. //FUNCSHIFT has been shifted by the SHIFT handling, some keys need to be excluded
  241. if (layer == _FUNCSHIFT) {
  242. //F1-F12 should be sent as unshifted keycodes,
  243. //and ] needs to be unshifted or it is sent as }
  244. if ( (keycode >= KC_F1 && keycode <= KC_F12)
  245. || keycode == KC_RBRC ) {
  246. if (record->event.pressed) {
  247. unregister_mods(MOD_LSFT);
  248. } else {
  249. register_mods(MOD_LSFT);
  250. }
  251. }
  252. }
  253. return true;
  254. };
  255. #ifdef AUDIO_ENABLE
  256. void startup_user()
  257. {
  258. _delay_ms(20); // gets rid of tick
  259. PLAY_SONG(tone_startup);
  260. }
  261. void shutdown_user()
  262. {
  263. PLAY_SONG(tone_goodbye);
  264. _delay_ms(150);
  265. stop_all_notes();
  266. }
  267. void music_on_user(void)
  268. {
  269. music_scale_user();
  270. }
  271. void music_scale_user(void)
  272. {
  273. PLAY_SONG(music_scale);
  274. }
  275. #endif
  276. void matrix_scan_user(void) {
  277. uint8_t layer = biton32(layer_state);
  278. switch (layer) {
  279. case _QWERTY:
  280. set_led_off;
  281. break;
  282. case _FUNCTIONMAC:
  283. set_led_blue;
  284. break;
  285. case _FUNCTIONPC:
  286. set_led_cyan;
  287. break;
  288. case _SHIFTED:
  289. set_led_red;
  290. break;
  291. case _FUNCSHIFT:
  292. set_led_green;
  293. break;
  294. case _ADJUST:
  295. set_led_white;
  296. break;
  297. default:
  298. break;
  299. }
  300. };