keymap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright 2021 Kyle McCreery
  2. * Copyright 2021 Jonavin Eng
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include QMK_KEYBOARD_H
  18. #include "jonavin.h"
  19. #include "layout_landscape.h"
  20. #define LANDSCAPE_MODE
  21. // Defines names for use in layer keycodes and the keymap
  22. enum layer_names {
  23. _FN2 = 2,
  24. _FN3,
  25. _FN4,
  26. _RGB
  27. };
  28. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  29. [_BASE] = LAYOUT_landscape(
  30. TT(_FN1), TT(_FN2), KC_MUTE,
  31. KC_NLCK, KC_PSLS, KC_PAST, KC_PMNS, KC_PPLS, KC_BSPC,
  32. KC_TAB, KC_P7, KC_P8, KC_P9, KC_PDOT, KC_RGUI,
  33. KC_RSFT, KC_P4, KC_P5, KC_P6, KC_COMMA, KC_RCTL,
  34. KC_P0, KC_P1, KC_P2, KC_P3, KC_PENT, KC_RALT,
  35. TT(_FN3), TT(_FN4), TT(_RGB)
  36. ),
  37. [_FN1] = LAYOUT_landscape(
  38. _______, _______, ENCFUNC,
  39. _______, KC_F10, KC_F11, KC_F12, KC_PSCR, _______,
  40. _______, KC_F7, KC_F8, KC_F9, KC_SLCK, _______,
  41. _______, KC_F4, KC_F5, KC_F6, KC_PAUS, _______,
  42. _______, KC_F1, KC_F2, KC_F3, _______, _______,
  43. _______, _______, _______
  44. ),
  45. [_FN2] = LAYOUT_landscape(
  46. _______, _______, RESET,
  47. _______, _______, _______, _______, _______, _______,
  48. _______, _______, _______, _______, _______, _______,
  49. _______, _______, _______, _______, _______, _______,
  50. _______, _______, _______, _______, _______, _______,
  51. _______, _______, _______
  52. ),
  53. [_FN3] = LAYOUT_landscape(
  54. _______, _______, _______,
  55. _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, _______, _______,
  57. _______, _______, _______, _______, _______, _______,
  58. _______, _______, _______, _______, _______, _______,
  59. _______, _______, _______
  60. ),
  61. [_FN4] = LAYOUT_landscape(
  62. _______, _______, _______,
  63. _______, _______, _______, _______, _______, _______,
  64. _______, _______, _______, _______, _______, _______,
  65. _______, _______, _______, _______, _______, _______,
  66. _______, _______, _______, _______, _______, _______,
  67. _______, _______, _______
  68. ),
  69. [_RGB] = LAYOUT_landscape(
  70. _______, _______, _______,
  71. _______, _______, _______, _______, _______, _______,
  72. _______, RGB_HUD, RGB_SPI, RGB_HUI, RGB_SAI, _______,
  73. _______, RGB_RMOD, RGB_TOG, RGB_MOD, RGB_SAD, _______,
  74. _______, RGB_VAD, RGB_SPD, RGB_VAI, KC_NO, _______,
  75. _______, _______, _______
  76. ),
  77. };
  78. typedef struct {
  79. char keydesc[6]; // this will be displayed on OLED
  80. uint16_t keycode; // this is the keycode that will be sent when activted
  81. } keycodedescType;
  82. static const keycodedescType PROGMEM keyselection[] = {
  83. // list of key codes that will be scrolled through by encoder and description
  84. {"TASK", KC_TASK},
  85. {"INS", KC_INS},
  86. {"DEL", KC_DEL},
  87. {"PrtSc", KC_PSCR},
  88. {"ScrLk", KC_SCLN},
  89. {"Break", KC_PAUS},
  90. {"C-A-D", KC_CAD}, // Ctrl-Alt-Del
  91. {"AltF4", KC_AF4},
  92. {"PLAY", KC_MEDIA_PLAY_PAUSE},
  93. {"RESET", RESET}, // firmware flash mode
  94. };
  95. #define MAX_KEYSELECTION sizeof(keyselection)/sizeof(keyselection[0])
  96. static uint8_t selectedkey_idx = 0;
  97. static keycodedescType selectedkey_rec;
  98. static void set_selectedkey(uint8_t idx) {
  99. // make a copy from PROGMEM
  100. memcpy_P (&selectedkey_rec, &keyselection[idx], sizeof selectedkey_rec);
  101. //selectedkey_rec = keyselection[idx];
  102. }
  103. void keyboard_post_init_keymap(void) {
  104. // Call the keyboard post init code.
  105. set_selectedkey(selectedkey_idx);
  106. }
  107. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  108. switch (keycode) {
  109. case ENCFUNC:
  110. if (record->event.pressed) {
  111. selectedkey_rec.keycode == RESET ? reset_keyboard() : tap_code16(selectedkey_rec.keycode); // handle RESET code
  112. } else {
  113. // when keycode is released
  114. }
  115. break;
  116. }
  117. return true;
  118. };
  119. #ifdef ENCODER_ENABLE // Encoder Functionality
  120. uint8_t selected_layer = 0;
  121. bool encoder_update_user(uint8_t index, bool clockwise) {
  122. #ifdef OLED_ENABLE
  123. oled_clear();
  124. oled_render();
  125. #endif
  126. switch (index) {
  127. case 0: // This is the only encoder right now, keeping for consistency
  128. switch(get_highest_layer(layer_state)){ // special handling per layer
  129. case _FN1: // on Fn layer select what the encoder does when pressed
  130. if (!keyboard_report->mods) {
  131. if ( clockwise ) {
  132. if ( selectedkey_idx < MAX_KEYSELECTION-1) {
  133. selectedkey_idx ++;
  134. } else {
  135. // do nothing
  136. }
  137. } else if ( !clockwise ) {
  138. if ( selectedkey_idx > 0){
  139. selectedkey_idx --;
  140. } else {
  141. // do nothing
  142. }
  143. }
  144. set_selectedkey(selectedkey_idx);
  145. break;
  146. } else {
  147. // continue to default
  148. }
  149. default: // all other layers
  150. if ( clockwise ) {
  151. if (keyboard_report->mods & MOD_BIT(KC_RSFT) ) { // If you are holding Right Shift, encoder changes layers
  152. if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT-1)) {
  153. selected_layer ++;
  154. layer_move(selected_layer);
  155. }
  156. } else if (keyboard_report->mods & MOD_BIT(KC_RCTL)) { // if holding Right Ctrl, navigate next word
  157. tap_code16(LCTL(KC_RGHT));
  158. } else if (keyboard_report->mods & MOD_BIT(KC_RALT)) { // if holding Right Alt, change media next track
  159. tap_code(KC_MEDIA_NEXT_TRACK);
  160. } else {
  161. tap_code(KC_VOLU); // Otherwise it just changes volume
  162. }
  163. } else if ( !clockwise ) {
  164. if (keyboard_report->mods & MOD_BIT(KC_RSFT) ) {
  165. if (selected_layer > 0) {
  166. selected_layer --;
  167. layer_move(selected_layer);
  168. }
  169. } else if (keyboard_report->mods & MOD_BIT(KC_RCTL)) { // if holding Right Ctrl, navigate previous word
  170. tap_code16(LCTL(KC_LEFT));
  171. } else if (keyboard_report->mods & MOD_BIT(KC_RALT)) { // if holding Right Alt, change media previous track
  172. tap_code(KC_MEDIA_PREV_TRACK);
  173. } else {
  174. tap_code(KC_VOLD);
  175. }
  176. }
  177. break;
  178. }
  179. break;
  180. }
  181. return true;
  182. }
  183. #endif
  184. #ifdef OLED_ENABLE
  185. static void render_logo(void) { // Render MechWild "MW" Logo
  186. static const char PROGMEM logo_1[] = {0x8A, 0x8B, 0x8C, 0x8D, 0x00};
  187. static const char PROGMEM logo_2[] = {0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0x00};
  188. static const char PROGMEM logo_3[] = {0xCA, 0xCB, 0xCC, 0xCD, 0x00};
  189. static const char PROGMEM logo_4[] = {0x20, 0x8E, 0x8F, 0x90, 0x00};
  190. oled_set_cursor(0,0);
  191. oled_write_P(logo_1, false);
  192. oled_set_cursor(0,1);
  193. oled_write_P(logo_2, false);
  194. oled_set_cursor(0,2);
  195. oled_write_P(logo_3, false);
  196. oled_set_cursor(0,3);
  197. oled_write_P(logo_4, false);
  198. }
  199. #ifdef LANDSCAPE_MODE
  200. void oled_task_user(void) {
  201. render_logo();
  202. oled_set_cursor(8,2);
  203. switch(selected_layer){
  204. case _BASE:
  205. oled_write_P(PSTR("BASE"), false);
  206. break;
  207. case _FN1:
  208. oled_write_P(PSTR("FN "), false);
  209. oled_write(selectedkey_rec.keydesc, false);
  210. break;
  211. case _FN2:
  212. oled_write_P(PSTR("FN2 "), false);
  213. break;
  214. case _FN3:
  215. oled_write_P(PSTR("FN3 "), false);
  216. break;
  217. case _FN4:
  218. oled_write_P(PSTR("FN4 "), false);
  219. break;
  220. case _RGB:
  221. oled_write_P(PSTR("RGB "), false);
  222. break;
  223. default:
  224. oled_write_P(PSTR(" ?? "), false); // Should never display, here as a catchall
  225. }
  226. oled_write_P(keymap_config.no_gui ? PSTR(" WL") : PSTR(" "), false);
  227. oled_set_cursor(8,3);
  228. if (get_highest_layer(layer_state) == selected_layer) {
  229. oled_write_P(PSTR(" "), false);
  230. } else {
  231. switch (get_highest_layer(layer_state)) {
  232. case _BASE:
  233. oled_write_P(PSTR("Temp BASE"), false);
  234. break;
  235. case _FN1:
  236. oled_write_P(PSTR("Temp FN "), false);
  237. oled_write(selectedkey_rec.keydesc, false);
  238. break;
  239. case _FN2:
  240. oled_write_P(PSTR("Temp FN2 "), false);
  241. break;
  242. case _FN3:
  243. oled_write_P(PSTR("Temp FN3 "), false);
  244. break;
  245. case _FN4:
  246. oled_write_P(PSTR("Temp FN4 "), false);
  247. break;
  248. case _RGB:
  249. oled_write_P(PSTR("Temp RGB "), false);
  250. break;
  251. default:
  252. oled_write_P(PSTR("Temp ????"), false); // Should never display, here as a catchall
  253. }
  254. }
  255. led_t led_state = host_keyboard_led_state();
  256. oled_set_cursor(8,0);
  257. oled_write_P(PSTR(" JONAVIN "), false);
  258. oled_set_cursor(8,1);
  259. oled_write_P(led_state.num_lock ? PSTR("NUM ") : PSTR(" "), false);
  260. oled_write_P(led_state.caps_lock ? PSTR("CAPS ") : PSTR(" "), false);
  261. oled_write_P(led_state.scroll_lock ? PSTR("SCR") : PSTR(" "), false);
  262. }
  263. #endif // LANDSCAPE_MODE
  264. // regular mode
  265. #ifndef LANDSCAPE_MODE
  266. oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  267. return OLED_ROTATION_270; // flips the display 270 degrees
  268. }
  269. void oled_task_user(void) {
  270. render_logo();
  271. oled_set_cursor(0,6);
  272. oled_write_ln_P(PSTR("Layer"), false);
  273. switch (get_highest_layer(layer_state)) {
  274. case _BASE:
  275. oled_write_ln_P(PSTR("Base"), false);
  276. break;
  277. case _FN1:
  278. oled_write_ln_P(PSTR("FN 1"), false);
  279. break;
  280. case _FN2:
  281. oled_write_ln_P(PSTR("FN 2"), false);
  282. break;
  283. case _FN3:
  284. oled_write_ln_P(PSTR("FN 3"), false);
  285. break;
  286. case _FN4:
  287. oled_write_ln_P(PSTR("FN 4"), false);
  288. break;
  289. case _RGB:
  290. oled_write_ln_P(PSTR("RGB "), false);
  291. break;
  292. default:
  293. oled_write_ln_P(PSTR("Undef"), false);
  294. }
  295. oled_write_ln_P(PSTR(""), false);
  296. // Host Keyboard LED Status
  297. led_t led_state = host_keyboard_led_state();
  298. oled_write_ln_P(led_state.num_lock ? PSTR("NUM ") : PSTR(" "), false);
  299. oled_write_ln_P(led_state.caps_lock ? PSTR("CAP ") : PSTR(" "), false);
  300. oled_write_ln_P(led_state.scroll_lock ? PSTR("SCR ") : PSTR(" "), false);
  301. }
  302. #endif // !LANDSCAPE_MODE
  303. void suspend_power_down_user(void) { // shutdown oled when powered down to prevent OLED from showing Mercutio all the time
  304. oled_off();
  305. }
  306. #endif