keymap.c 10 KB

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