keymap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "drashna.h"
  17. #ifdef BACKLIGHT_ENABLE
  18. enum planck_keycodes {
  19. BACKLIT = NEW_SAFE_RANGE,
  20. TH_LVL,
  21. };
  22. #else
  23. # define BACKLIT OSM(MOD_LSFT)
  24. enum planck_keycodes {
  25. TH_LVL = NEW_SAFE_RANGE,
  26. };
  27. #endif
  28. #ifdef KEYBOARD_planck_ez
  29. # define PLNK_1 BK_LWER
  30. # define PLNK_2 SP_LWER
  31. # define PLNK_3 KC_NO
  32. # define PLNK_4 ET_RAIS
  33. #else
  34. # define PLNK_1 SP_LWER
  35. # define PLNK_2 BK_LWER
  36. # define PLNK_3 DL_RAIS
  37. # define PLNK_4 ET_RAIS
  38. #endif
  39. /*
  40. * The `LAYOUT_ortho_4x12_base` macro is a template to allow the use of identical
  41. * modifiers for the default layouts (eg QWERTY, Colemak, Dvorak, etc), so
  42. * that there is no need to set them up for each layout, and modify all of
  43. * them if I want to change them. This helps to keep consistency and ease
  44. * of use. K## is a placeholder to pass through the individual keycodes
  45. */
  46. // clang-format off
  47. #define LAYOUT_ortho_4x12_wrapper(...) LAYOUT_ortho_4x12(__VA_ARGS__)
  48. #define LAYOUT_ortho_4x12_base( \
  49. K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, \
  50. K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, \
  51. K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A \
  52. ) \
  53. LAYOUT_ortho_4x12_wrapper( \
  54. KC_ESC, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, KC_DEL, \
  55. LALT_T(KC_TAB), K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, RALT_T(K1B), \
  56. KC_MLSF, CTL_T(K21), K22, K23, K24, K25, K26, K27, K28, K29, RCTL_T(K2A), KC_ENT, \
  57. BACKLIT, OS_LCTL, OS_LALT, OS_LGUI, PLNK_1, PLNK_2, PLNK_3, PLNK_4, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT \
  58. )
  59. #define LAYOUT_base_wrapper(...) LAYOUT_ortho_4x12_base(__VA_ARGS__)
  60. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  61. [_DEFAULT_LAYER_1] = LAYOUT_base_wrapper(
  62. _________________QWERTY_L1_________________, _________________QWERTY_R1_________________,
  63. _________________QWERTY_L2_________________, _________________QWERTY_R2_________________,
  64. _________________QWERTY_L3_________________, _________________QWERTY_R3_________________
  65. ),
  66. [_DEFAULT_LAYER_2] = LAYOUT_base_wrapper(
  67. ______________COLEMAK_MOD_DH_L1____________, ______________COLEMAK_MOD_DH_R1____________,
  68. ______________COLEMAK_MOD_DH_L2____________, ______________COLEMAK_MOD_DH_R2____________,
  69. ______________COLEMAK_MOD_DH_L3____________, ______________COLEMAK_MOD_DH_R3____________
  70. ),
  71. [_DEFAULT_LAYER_3] = LAYOUT_base_wrapper(
  72. _________________COLEMAK_L1________________, _________________COLEMAK_R1________________,
  73. _________________COLEMAK_L2________________, _________________COLEMAK_R2________________,
  74. _________________COLEMAK_L3________________, _________________COLEMAK_R3________________
  75. ),
  76. [_DEFAULT_LAYER_4] = LAYOUT_base_wrapper(
  77. _________________DVORAK_L1_________________, _________________DVORAK_R1_________________,
  78. _________________DVORAK_L2_________________, _________________DVORAK_R2_________________,
  79. _________________DVORAK_L3_________________, _________________DVORAK_R3_________________
  80. ),
  81. [_LOWER] = LAYOUT_ortho_4x12_wrapper(
  82. KC_TILD, _________________LOWER_L1__________________, _________________LOWER_R1__________________, KC_BSPC,
  83. KC_DEL, _________________LOWER_L2__________________, _________________LOWER_R2__________________, KC_PIPE,
  84. _______, _________________LOWER_L3__________________, _________________LOWER_R3__________________, _______,
  85. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  86. ),
  87. [_RAISE] = LAYOUT_ortho_4x12_wrapper(
  88. KC_GRV, _________________RAISE_L1__________________, _________________RAISE_R1__________________, KC_BSPC,
  89. KC_DEL, _________________RAISE_L2__________________, _________________RAISE_R2__________________, KC_BSLS,
  90. _______, _________________RAISE_L3__________________, _________________RAISE_R3__________________, _______,
  91. _______, _______, _______, _______, _______, _______, _______, _________________RAISE_R3__________________
  92. ),
  93. [_ADJUST] = LAYOUT_ortho_4x12_wrapper(
  94. QK_MAKE, _________________ADJUST_L1_________________, _________________ADJUST_R1_________________, QK_BOOT,
  95. VRSN, _________________ADJUST_L2_________________, _________________ADJUST_R2_________________, EE_CLR,
  96. TH_LVL, _________________ADJUST_L3_________________, _________________ADJUST_R3_________________, RGB_IDL,
  97. KEYLOCK, _______, _______, QK_RBT, _______, KC_NUKE, _______, _______, AUTO_CTN,_______, _______, TG_MODS
  98. )
  99. };
  100. #ifdef ENCODER_MAP_ENABLE
  101. const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
  102. [_DEFAULT_LAYER_1] = { { KC_DOWN, KC_UP } },
  103. [_DEFAULT_LAYER_2] = { { _______, _______ } },
  104. [_DEFAULT_LAYER_3] = { { _______, _______ } },
  105. [_DEFAULT_LAYER_4] = { { _______, _______ } },
  106. [_GAMEPAD] = { { _______, _______ } },
  107. [_DIABLO] = { { _______, _______ } },
  108. [_MOUSE] = { { KC_WH_D, KC_WH_U } },
  109. [_MEDIA] = { { _______, _______ } },
  110. [_RAISE] = { { KC_VOLD, KC_VOLU } },
  111. [_LOWER] = { { RGB_MOD, RGB_RMOD} },
  112. [_ADJUST] = { { CK_DOWN, CK_UP } },
  113. };
  114. #endif
  115. // clang-format on
  116. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  117. switch (keycode) {
  118. #ifdef BACKLIGHT_ENABLE
  119. case BACKLIT:
  120. if (record->event.pressed) {
  121. register_code(KC_RSFT);
  122. # ifdef BACKLIGHT_ENABLE
  123. backlight_step();
  124. # endif
  125. } else {
  126. unregister_code(KC_RSFT);
  127. }
  128. break;
  129. #endif
  130. #ifdef KEYBOARD_planck_ez
  131. case TH_LVL:
  132. if (record->event.pressed) {
  133. keyboard_config.led_level++;
  134. if (keyboard_config.led_level > 4) {
  135. keyboard_config.led_level = 0;
  136. }
  137. planck_ez_right_led_level((uint8_t)keyboard_config.led_level * 255 / 4);
  138. planck_ez_left_led_level((uint8_t)keyboard_config.led_level * 255 / 4);
  139. eeconfig_update_kb(keyboard_config.raw);
  140. layer_state_set_kb(layer_state);
  141. }
  142. break;
  143. #endif
  144. }
  145. return true;
  146. }
  147. bool music_mask_user(uint16_t keycode) {
  148. switch (keycode) {
  149. case RAISE:
  150. case LOWER:
  151. case BK_LWER:
  152. case SP_LWER:
  153. case DL_RAIS:
  154. case ET_RAIS:
  155. return false;
  156. default:
  157. return true;
  158. }
  159. }
  160. #ifdef RGB_MATRIX_ENABLE
  161. # ifdef KEYBOARD_planck_rev6
  162. // clang-format off
  163. led_config_t g_led_config = {
  164. {
  165. // Key Matrix to LED Index
  166. { NO_LED, 6, NO_LED, NO_LED, 5, NO_LED },
  167. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  168. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, 0 },
  169. { NO_LED, 7, NO_LED, NO_LED, 2, NO_LED },
  170. { NO_LED, 4, NO_LED, NO_LED, 3, NO_LED },
  171. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  172. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  173. { NO_LED, 1, NO_LED, NO_LED, 8, NO_LED }
  174. }, {
  175. // LED Index to Physical Position
  176. {112, 39}, {148, 60}, {206, 53}, {206, 3}, {150, 3}, {74, 3}, {18, 3}, {18, 54}, {77, 60}
  177. }, {
  178. // LED Index to Flag
  179. LED_FLAG_ALL, LED_FLAG_ALL, LED_FLAG_ALL, LED_FLAG_ALL, LED_FLAG_ALL,
  180. LED_FLAG_ALL, LED_FLAG_ALL, LED_FLAG_ALL, LED_FLAG_ALL
  181. }
  182. };
  183. // clange-format on
  184. # endif
  185. bool rgb_matrix_indicators_advanced_keymap(uint8_t led_min, uint8_t led_max) {
  186. uint8_t this_mod = get_mods();
  187. uint8_t this_led = host_keyboard_leds();
  188. uint8_t this_osm = get_oneshot_mods();
  189. # ifdef KEYBOARD_planck_ez
  190. # define THUMB_LED 41
  191. # else
  192. # define THUMB_LED 42
  193. # endif
  194. # define RGB_MATRIX_INDICATOR_SET_COLOR_wrapper(...) RGB_MATRIX_INDICATOR_SET_COLOR(__VA_ARGS__)
  195. switch (get_highest_layer(default_layer_state)) {
  196. case _DEFAULT_LAYER_1:
  197. RGB_MATRIX_INDICATOR_SET_COLOR_wrapper(THUMB_LED, DEFAULT_LAYER_1_RGB);
  198. break;
  199. case _DEFAULT_LAYER_2:
  200. RGB_MATRIX_INDICATOR_SET_COLOR_wrapper(THUMB_LED, DEFAULT_LAYER_2_RGB);
  201. break;
  202. case _DEFAULT_LAYER_3:
  203. RGB_MATRIX_INDICATOR_SET_COLOR_wrapper(THUMB_LED, DEFAULT_LAYER_3_RGB);
  204. break;
  205. case _DEFAULT_LAYER_4:
  206. RGB_MATRIX_INDICATOR_SET_COLOR_wrapper(THUMB_LED, DEFAULT_LAYER_4_RGB);
  207. break;
  208. }
  209. if ((this_mod | this_osm) & MOD_MASK_SHIFT || this_led & (1 << USB_LED_CAPS_LOCK)) {
  210. if (!layer_state_cmp(layer_state, _ADJUST)) {
  211. RGB_MATRIX_INDICATOR_SET_COLOR(24, 0x00, 0xFF, 0x00);
  212. }
  213. RGB_MATRIX_INDICATOR_SET_COLOR(36, 0x00, 0xFF, 0x00);
  214. }
  215. if ((this_mod | this_osm) & MOD_MASK_CTRL) {
  216. RGB_MATRIX_INDICATOR_SET_COLOR(25, 0xFF, 0x00, 0x00);
  217. RGB_MATRIX_INDICATOR_SET_COLOR(34, 0xFF, 0x00, 0x00);
  218. RGB_MATRIX_INDICATOR_SET_COLOR(37, 0xFF, 0x00, 0x00);
  219. }
  220. if ((this_mod | this_osm) & MOD_MASK_GUI) {
  221. RGB_MATRIX_INDICATOR_SET_COLOR(39, 0xFF, 0xD9, 0x00);
  222. }
  223. if ((this_mod | this_osm) & MOD_MASK_ALT) {
  224. RGB_MATRIX_INDICATOR_SET_COLOR(38, 0x00, 0x00, 0xFF);
  225. }
  226. return true;
  227. }
  228. void matrix_init_keymap(void) {
  229. # ifdef KEYBOARD_planck_light
  230. writePinLow(D6);
  231. # endif
  232. // rgblight_mode(RGB_MATRIX_MULTISPLASH);
  233. }
  234. #else // RGB_MATRIX_INIT
  235. void matrix_init_keymap(void) {
  236. # if !defined(CONVERT_TO_PROTON_C) && !defined(KEYBOARD_planck)
  237. setPinOutput(D5);
  238. writePinHigh(D5);
  239. setPinOutput(B0);
  240. writePinHigh(B0);
  241. # endif
  242. }
  243. #endif // RGB_MATRIX_INIT
  244. #ifdef ENCODER_ENABLE
  245. bool encoder_update_user(uint8_t index, bool clockwise) {
  246. switch (get_highest_layer(layer_state)) {
  247. case _RAISE:
  248. clockwise ? tap_code(KC_VOLD) : tap_code(KC_VOLU);
  249. break;
  250. case _LOWER:
  251. # ifdef RGB_MATRIX_ENABLE
  252. clockwise ? rgb_matrix_step() : rgb_matrix_step_reverse();
  253. # else
  254. clockwise ? tap_code(KC_PGDN) : tap_code(KC_PGUP);
  255. # endif
  256. break;
  257. case _ADJUST:
  258. # ifdef AUDIO_CLICKY
  259. clockwise ? clicky_freq_up() : clicky_freq_down();
  260. # endif
  261. break;
  262. default:
  263. clockwise ? tap_code(KC_DOWN) : tap_code(KC_UP);
  264. }
  265. # ifdef AUDIO_CLICKY
  266. clicky_play();
  267. # endif
  268. return true;
  269. }
  270. #endif // ENCODER_ENABLE
  271. #ifdef KEYBOARD_planck_rev6
  272. bool dip_switch_update_user(uint8_t index, bool active) {
  273. switch (index) {
  274. case 0:
  275. if (active) {
  276. audio_on();
  277. } else {
  278. audio_off();
  279. }
  280. break;
  281. case 1:
  282. if (active) {
  283. clicky_on();
  284. } else {
  285. clicky_off();
  286. }
  287. break;
  288. case 2:
  289. keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = active;
  290. break;
  291. case 3:
  292. userspace_config.nuke_switch = active;
  293. break;
  294. }
  295. return true;
  296. }
  297. #endif // KEYBOARD_planck_rev6
  298. #ifdef KEYBOARD_planck_ez
  299. layer_state_t layer_state_set_keymap(layer_state_t state) {
  300. planck_ez_left_led_off();
  301. planck_ez_right_led_off();
  302. switch (get_highest_layer(state)) {
  303. case _LOWER:
  304. planck_ez_left_led_on();
  305. break;
  306. case _RAISE:
  307. planck_ez_right_led_on();
  308. break;
  309. case _ADJUST:
  310. planck_ez_right_led_on();
  311. planck_ez_left_led_on();
  312. break;
  313. default:
  314. break;
  315. }
  316. return state;
  317. }
  318. #endif