keymap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright 2022 Era James(@Era1112)
  2. // SPDX - License - Identifier: GPL - 2.0 - or -later
  3. #include QMK_KEYBOARD_H // Default statement
  4. #define HSV_RETRO_CONSOLE 36, 150, 255 //HSV_YELLOW = 43, 255, 255
  5. //----------- RGB Default Settings -----------//
  6. //--------------------------------------------//
  7. #ifdef RGBLIGHT_ENABLE
  8. void keyboard_post_init_user(void) {
  9. rgblight_enable_noeeprom(); // Enables RGB, without saving settings
  10. rgblight_sethsv_noeeprom(HSV_RETRO_CONSOLE);
  11. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  12. }
  13. #endif // RGBLIGHT_ENABLE
  14. //----------- Layer names -----------//
  15. //-----------------------------------//
  16. enum preonic_layers {
  17. _QWERTY,
  18. _LOWER,
  19. _RAISE,
  20. _ADJUST,
  21. };
  22. //----------- Sounds -----------//
  23. //------------------------------//
  24. #ifdef AUDIO_ENABLE
  25. float capslockToggleSound[][2] = SONG(STARTUP_SOUND);
  26. float dynamicBufferRecordSound[][2] = SONG(STARTUP_SOUND);
  27. float dynamicBufferFullSound[][2] = SONG(GOODBYE_SOUND);
  28. #endif // AUDIO_ENABLE
  29. //----------- Called when dynamic buffer full -----------//
  30. //-------------------------------------------------------//
  31. void backlight_toggle(void) {
  32. #ifdef AUDIO_ENABLE
  33. PLAY_SONG(dynamicBufferFullSound);
  34. #endif // AUDIO_ENABLE
  35. }
  36. //----------- Tapdance prelims -----------//
  37. //----------------------------------------//
  38. typedef enum {
  39. TD_NONE,
  40. TD_UNKNOWN,
  41. TD_1_TAP,
  42. TD_1_HOLD,
  43. TD_2_TAP,
  44. TD_2_HOLD,
  45. } td_state_t;
  46. typedef struct {
  47. bool is_press_action;
  48. td_state_t state;
  49. } td_tap_t;
  50. td_state_t cur_dance(qk_tap_dance_state_t* state);
  51. /* Return an integer that corresponds to what kind of tap dance should be executed.
  52. *
  53. * How to figure out tap dance state: interrupted and pressed.
  54. *
  55. * Interrupted: If the state of a dance dance is "interrupted", that means that another key has been hit
  56. * under the tapping term. This is typically indicitive that you are trying to "tap" the key.
  57. *
  58. * Pressed: Whether or not the key is still being pressed. If this value is true, that means the tapping term
  59. * has ended, but the key is still being pressed down. This generally means the key is being "held".
  60. *
  61. * One thing that is currenlty not possible with qmk software in regards to tap dance is to mimic the "permissive hold"
  62. * feature. In general, advanced tap dances do not work well if they are used with commonly typed letters.
  63. * For example "A". Tap dances are best used on non-letter keys that are not hit while typing letters.
  64. *
  65. * Good places to put an advanced tap dance:
  66. * z,q,x,j,k,v,b, any function key, home/end, comma, semi-colon
  67. *
  68. * Criteria for "good placement" of a tap dance key:
  69. * Not a key that is hit frequently in a sentence
  70. * Not a key that is used frequently to double tap, for example 'tab' is often double tapped in a terminal, or
  71. * in a web form. So 'tab' would be a poor choice for a tap dance.
  72. * Letters used in common words as a double. For example 'p' in 'pepper'. If a tap dance function existed on the
  73. * letter 'p', the word 'pepper' would be quite frustating to type.
  74. *
  75. * For the third point, there does exist the 'TD_DOUBLE_SINGLE_TAP', however this is not fully tested
  76. *
  77. */
  78. td_state_t cur_dance(qk_tap_dance_state_t* state) {
  79. if (state->count == 1) {
  80. if (state->interrupted || !state->pressed) {
  81. return TD_1_TAP;
  82. // Key has not been interrupted, but the key is still held. Means you want to send a 'HOLD'.
  83. } else {
  84. return TD_1_HOLD;
  85. }
  86. } else if (state->count == 2) {
  87. // TD_DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap
  88. // action when hitting 'pp'. Suggested use case for this return value is when you want to send two
  89. // keystrokes of the key, and not the 'double tap' action/macro.
  90. if (state->pressed) return TD_2_HOLD;
  91. else return TD_2_TAP;
  92. } else {
  93. return TD_UNKNOWN;
  94. }
  95. }
  96. //----------- 2tap capslock --------------//
  97. //----------------------------------------//
  98. void twoCapsLock_finished(qk_tap_dance_state_t* state, void* user_data);
  99. void twoCapsLock_reset(qk_tap_dance_state_t* state, void* user_data);
  100. static td_tap_t twoCapsLock_tap_state = {
  101. .is_press_action = true,
  102. .state = TD_NONE
  103. };
  104. void twoCapsLock_finished(qk_tap_dance_state_t* state, void* user_data) {
  105. twoCapsLock_tap_state.state = cur_dance(state);
  106. switch (twoCapsLock_tap_state.state) {
  107. case TD_NONE: register_code(KC_LSFT); break;
  108. case TD_UNKNOWN: register_code(KC_LSFT); break;
  109. case TD_1_TAP: register_code(KC_LSFT); break;
  110. case TD_1_HOLD: register_code(KC_LSFT); break;
  111. case TD_2_TAP:
  112. register_code(KC_CAPS);
  113. #ifdef AUDIO_ENABLE
  114. PLAY_SONG(capslockToggleSound);
  115. #endif // AUDIO_ENABLE
  116. break;
  117. case TD_2_HOLD: register_code(KC_LSFT); break;
  118. }
  119. }
  120. void twoCapsLock_reset(qk_tap_dance_state_t* state, void* user_data) {
  121. switch (twoCapsLock_tap_state.state) {
  122. case TD_UNKNOWN: unregister_code(KC_LSFT); break;
  123. case TD_NONE: unregister_code(KC_LSFT); break;
  124. case TD_1_TAP: unregister_code(KC_LSFT); break;
  125. case TD_1_HOLD: unregister_code(KC_LSFT); break;
  126. case TD_2_TAP:
  127. unregister_code(KC_CAPS);
  128. #ifdef AUDIO_ENABLE
  129. PLAY_SONG(capslockToggleSound);
  130. #endif // AUDIO_ENABLE
  131. break;
  132. case TD_2_HOLD: unregister_code(KC_LSFT); break;
  133. }
  134. twoCapsLock_tap_state.state = TD_NONE;
  135. }
  136. //----------- Rotary Encoder --------------//
  137. //----------------------------------------//
  138. bool encoder_update_user(uint8_t index, bool clockwise) {
  139. if (layer_state_is(_QWERTY)) {
  140. if (clockwise) {
  141. tap_code(KC_WH_U);
  142. } else {
  143. tap_code(KC_WH_D);
  144. }
  145. }
  146. else if (layer_state_is(_LOWER)) {
  147. if (clockwise) {
  148. tap_code16(S(KC_F3));
  149. } else {
  150. tap_code(KC_F3);
  151. }
  152. } else if (layer_state_is(_RAISE)) {
  153. if (clockwise) {
  154. tap_code16(C(KC_Z));
  155. } else {
  156. tap_code16(C(KC_Y));
  157. }
  158. }
  159. return false;
  160. }
  161. //----------- Custom keycodes ------------//
  162. //----------------------------------------//
  163. enum {
  164. TD_2_CAPSLOCK
  165. };
  166. enum custom_keycodes {
  167. CU_BLNKON = SAFE_RANGE,
  168. CU_BLNKOFF,
  169. CU_RGBON,
  170. CU_RGBOFF,
  171. ENC_MODE
  172. };
  173. static bool blinky = false; // Track blinky behavior on/off for keycode
  174. qk_tap_dance_action_t tap_dance_actions[] = {
  175. [TD_2_CAPSLOCK] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, twoCapsLock_finished, twoCapsLock_reset)
  176. };
  177. //----------- Intercepts and overrides ------------//
  178. //-------------------------------------=-----------//
  179. bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  180. switch (keycode) {
  181. // Turn RGB LEDs off
  182. case CU_RGBOFF:
  183. // If pressed
  184. if (record->event.pressed) {
  185. rgblight_sethsv_noeeprom(HSV_OFF);
  186. return true;
  187. // If released
  188. } else {
  189. return true;
  190. }
  191. // Turn RGB LEDs on
  192. case CU_RGBON:
  193. // If pressed
  194. if (record->event.pressed) {
  195. rgblight_sethsv_noeeprom(HSV_RETRO_CONSOLE);
  196. return true;
  197. // If released
  198. } else {
  199. return true;
  200. }
  201. // Turn blinky LEDs off
  202. case CU_BLNKOFF:
  203. // If pressed
  204. if (record->event.pressed) {
  205. blinky = false;
  206. return true;
  207. // If released
  208. } else {
  209. return true;
  210. }
  211. // Turn blinky LEDs on
  212. case CU_BLNKON:
  213. // If pressed
  214. if (record->event.pressed) {
  215. blinky = true;
  216. return true;
  217. // If released
  218. } else {
  219. return true;
  220. }
  221. // Sound when Dynamic recording started
  222. case QK_DYNAMIC_MACRO_RECORD_START_1:
  223. // If pressed
  224. if (record->event.pressed) {
  225. #ifdef AUDIO_ENABLE
  226. PLAY_SONG(dynamicBufferRecordSound);
  227. #endif // AUDIO_ENABLE
  228. return true; // Let QMK send the press/release events
  229. // If released
  230. } else {
  231. return true; // Let QMK send the press/release events
  232. }
  233. // Sound when Dynamic recording stopped
  234. case QK_DYNAMIC_MACRO_RECORD_STOP:
  235. // If pressed
  236. if (record->event.pressed) {
  237. #ifdef AUDIO_ENABLE
  238. PLAY_SONG(dynamicBufferFullSound);
  239. #endif // AUDIO_ENABLE
  240. return true; // Let QMK send the enter press/release events
  241. // If released
  242. } else {
  243. return true; // Let QMK send the press/release events
  244. }
  245. // Encoder Click
  246. case ENC_MODE:
  247. if (record->event.pressed) {
  248. if (layer_state_is(_QWERTY)) {
  249. tap_code(KC_BTN1);
  250. return false;
  251. } else if (layer_state_is(_LOWER)) {
  252. return false;
  253. } else if (layer_state_is(_RAISE)) {
  254. return false;
  255. }
  256. }
  257. // Adds blinks if blinky is on
  258. default:
  259. if (blinky == true) {
  260. rgblight_toggle();
  261. }
  262. return true; // Process all other keycodes normally
  263. }
  264. }
  265. //----------- Keymap ------------//
  266. //-------------------------------//
  267. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  268. // main layer
  269. [_QWERTY] = LAYOUT_ortho_5x12 (
  270. // (Non-disabled top row), uncomment and replace if you want preonic-style instead of planck-style
  271. // KC_MINS, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_EQL,
  272. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  273. KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC,
  274. KC_ESC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_ENT,
  275. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, TD(TD_2_CAPSLOCK),
  276. ENC_MODE, KC_LCTL, KC_LGUI, KC_LALT, MO(_LOWER), KC_SPC, KC_SPC, MO(_RAISE), KC_LEFT, KC_DOWN, KC_UP, KC_RGHT
  277. ),
  278. // lower key
  279. [_LOWER] = LAYOUT_ortho_5x12 (
  280. DM_PLY1, DM_REC1, DM_RSTP, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  281. KC_TRNS, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_DEL,
  282. KC_BSPC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_QUOT, KC_GRV, KC_LCBR, KC_RCBR, KC_TRNS,
  283. KC_TRNS, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_MINS, KC_EQL, KC_TRNS, KC_BSLS, KC_TRNS,
  284. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_ADJUST), KC_HOME, KC_PGDN, KC_PGUP, KC_END
  285. ),
  286. // raise key
  287. [_RAISE] = LAYOUT_ortho_5x12 (
  288. DM_PLY1, DM_REC1, DM_RSTP, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  289. KC_TRNS, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_DEL,
  290. KC_DEL, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_DQUO, KC_TILD, KC_LBRC, KC_RBRC, KC_TRNS,
  291. KC_TRNS, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_UNDS, KC_PLUS , KC_TRNS, KC_PIPE, KC_TRNS,
  292. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_ADJUST), KC_TRNS, KC_TRNS, KC_TRNS, KC_MUTE, KC_VOLD, KC_VOLU, KC_F24
  293. ),
  294. // hardware adjust layer, raise+lower
  295. [_ADJUST] = LAYOUT_ortho_5x12 (
  296. AU_ON, AU_OFF, CK_ON, CK_OFF, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  297. CU_RGBON, CU_RGBOFF, CU_BLNKON, CU_BLNKOFF, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  298. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  299. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  300. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO
  301. )
  302. };