keymap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. 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] = { /* 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. {XXXXXXX, KC_LCTL, M(ESCM), KC_TAB, KC_QUOT, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT, XXXXXXX },
  50. {XXXXXXX, KC_LALT, KC_LGUI, KC_SPC, SHIFT, FNKEY, KC_BSPC, KC_ENT, MO(_ADJUST), XXXXXXX }
  51. },
  52. [_SHIFTED] = { /* 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. {XXXXXXX, __MOD__, KC_DEL, _______, _______, _______, _______, _______, _______, XXXXXXX },
  58. {XXXXXXX, __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, KC_NO, XXXXXXX }
  59. },
  60. [_FUNCTIONPC] = { /* 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. {XXXXXXX, __MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END, XXXXXXX },
  65. {XXXXXXX, __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, KC_NO, XXXXXXX }
  66. },
  67. [_FUNCTIONMAC] = { /* 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. {XXXXXXX, __MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END, XXXXXXX },
  72. {XXXXXXX, __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, KC_NO, XXXXXXX }
  73. },
  74. [_FUNCSHIFT] = { /* 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. {XXXXXXX, __MOD__, KC_DEL, _______, KC_TILD, _______, _______, _______, _______, XXXXXXX },
  79. {XXXXXXX, __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, _______, XXXXXXX }
  80. },
  81. [_ADJUST] = { /* 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. {XXXXXXX, __MOD__, _______, _______, _______, _______, DYN_MACRO_PLAY1, DYN_MACRO_PLAY2, _______, XXXXXXX },
  86. {XXXXXXX, __MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, __MOD__, XXXXXXX }
  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_NOTE_ARRAY(tone_fnpc, false, 0);
  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_NOTE_ARRAY(tone_fnmac, false, 0);
  217. #endif
  218. }
  219. return false;
  220. break;
  221. //If any other key was pressed during the layer mod hold period,
  222. //then the layer mod was used momentarily, and should block latching
  223. default:
  224. singular_key = false;
  225. break;
  226. }
  227. //FUNCSHIFT has been shifted by the SHIFT handling, some keys need to be excluded
  228. if (layer == _FUNCSHIFT) {
  229. //F1-F12 should be sent as unshifted keycodes,
  230. //and ] needs to be unshifted or it is sent as }
  231. if ( (keycode >= KC_F1 && keycode <= KC_F12)
  232. || keycode == KC_RBRC ) {
  233. if (record->event.pressed) {
  234. unregister_mods(MOD_LSFT);
  235. } else {
  236. register_mods(MOD_LSFT);
  237. }
  238. }
  239. }
  240. return true;
  241. };
  242. #ifdef AUDIO_ENABLE
  243. void startup_user()
  244. {
  245. _delay_ms(20); // gets rid of tick
  246. PLAY_NOTE_ARRAY(tone_startup, false, 0);
  247. }
  248. void shutdown_user()
  249. {
  250. PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
  251. _delay_ms(150);
  252. stop_all_notes();
  253. }
  254. void music_on_user(void)
  255. {
  256. music_scale_user();
  257. }
  258. void music_scale_user(void)
  259. {
  260. PLAY_NOTE_ARRAY(music_scale, false, 0);
  261. }
  262. #endif
  263. void matrix_scan_user(void) {
  264. uint8_t layer = biton32(layer_state);
  265. switch (layer) {
  266. case _QWERTY:
  267. set_led_off;
  268. break;
  269. case _FUNCTIONMAC:
  270. set_led_blue;
  271. break;
  272. case _FUNCTIONPC:
  273. set_led_cyan;
  274. break;
  275. case _SHIFTED:
  276. set_led_red;
  277. break;
  278. case _FUNCSHIFT:
  279. set_led_green;
  280. break;
  281. case _ADJUST:
  282. set_led_white;
  283. break;
  284. default:
  285. break;
  286. }
  287. };