2
0

keymap.c 11 KB

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