keymap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include QMK_KEYBOARD_H
  2. #include "taphold.h"
  3. #include "seq.h"
  4. #include "ds1307.h"
  5. #include "lightmode.h"
  6. #include <stdio.h>
  7. /* Note: don't forget there's some more code in qmk_firmware/users/anderson dir */
  8. #define _MAIN 0
  9. #define _ALPHA 1
  10. #define _BETA 2
  11. enum custom_keycodes {
  12. KC_MAIN = SAFE_RANGE,
  13. KC_ALPHA,
  14. KC_BETA,
  15. #ifdef LIGHTMODE_ENABLE
  16. KC_LIGHT_MODE,
  17. #endif
  18. KC_SEQ,
  19. KC_SET_TIME,
  20. };
  21. #ifdef LIGHTMODE_ENABLE
  22. #endif
  23. /* TapHold is my own implementation of the `LT` macro. It's processed in `process_record_user()`. */
  24. #define TAPHOLD_CONFIG_SIZE 3
  25. taphold_t taphold_config[TAPHOLD_CONFIG_SIZE] = {
  26. {.key=KC_ALPHA, .mode=TAPHOLD_LAYER, .shortAction=KC_ESC, .longAction=_ALPHA},
  27. {.key=KC_BETA, .mode=TAPHOLD_LAYER, .shortAction=KC_EQL, .longAction=_BETA},
  28. {.key=KC_RCTRL, .mode=TAPHOLD_MOD, .shortAction=KC_MINS, .longAction=KC_LCTRL},
  29. };
  30. uint16_t taphold_config_size = TAPHOLD_CONFIG_SIZE;
  31. uint32_t taphold_timeout = 90;
  32. /* Seq is implementation of unicode macros similar to UCIS, but with unicode strings. */
  33. #define SEQ_CONFIG_SIZE 3
  34. seq_t seq_config[SEQ_CONFIG_SIZE] = {
  35. {.sequence="temp", .result="42°C"},
  36. {.sequence="table", .result="┳━━┳"},
  37. {.sequence="shrug", .result="¯\\_(ツ)_/¯"}
  38. };
  39. uint16_t seq_config_size = SEQ_CONFIG_SIZE;
  40. /* Colors */
  41. uint32_t layer_colors[3] = {
  42. [_MAIN] = 0xFF0010,
  43. [_ALPHA] = 0x4020FF,
  44. [_BETA] = 0x20FF00,
  45. };
  46. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  47. /* Main layer
  48. │MUTE │ │L_MOD│
  49. ┏━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┓
  50. ┃ TAB ┃ Q │ W │ E │ R │ T ┃ Y │ U │ I │ O │ P ┃ BSP ┃
  51. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  52. ┃𝛼/ESC┃ A │ S │ D │ F │ G ┃ H │ J │ K │ L │ ; ┃ RET ┃
  53. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  54. ┃SHIFT┃ Z │ X │ C │ V │ B ┃ N │ M │ , │ . │ / ┃CTL/-┃
  55. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  56. ┃LCTRL┃ │ │ ALT │ GUI │SPACE┃SPACE│ 𝛽/= │ ' │ │ ┃ \ ┃
  57. ┗━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┛
  58. */
  59. [_MAIN] = LAYOUT( \
  60. KC_MUTE, LCTL(KC_D),
  61. KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC, \
  62. KC_ALPHA,KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_ENT, \
  63. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RCTRL,\
  64. KC_LCTRL,_______, _______, KC_LALT, KC_LGUI, KC_SPC, KC_SPC, KC_BETA, KC_QUOT, _______, _______, KC_BSLS \
  65. ),
  66. /* Alpha layer (𝛼)
  67. │ │ │ │
  68. ┏━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┓
  69. ┃ ┃PREV │PLAY │NEXT │ │NUMLK┃ - │ ^^^ │ ^ │ vvv │ ~ ┃ DEL ┃
  70. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  71. ┃ ┃ │VOL -│VOL +│ │CPSLK┃HOME │ <-- │ v │ --> │ ` ┃ \ ┃
  72. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  73. ┃ ┃ │ │ │ │SCRLK┃ END │ = │ [ │ ] │ ( ┃ ) ┃
  74. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  75. ┃ ┃ │ │ │ │ ┃ │ │ │ │ ┃ ┃
  76. ┗━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┛
  77. */
  78. [_ALPHA] = LAYOUT( \
  79. _______, _______, \
  80. _______, KC_MPRV, KC_MPLY, KC_MNXT, _______, KC_NLCK, KC_MINS, KC_PGUP, KC_UP, KC_PGDN, KC_TILD, KC_DEL, \
  81. _______, _______, KC_VOLD, KC_VOLU, _______, KC_CAPS, KC_HOME, KC_LEFT, KC_DOWN, KC_RIGHT,KC_GRV, KC_BSLS, \
  82. _______, _______, _______, _______, _______, KC_SLCK, KC_END, KC_EQL, KC_LBRC, KC_RBRC, KC_LPRN ,KC_RPRN, \
  83. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
  84. ),
  85. /* Beta layer (𝛽)
  86. │ │ │ │
  87. ┏━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┓
  88. ┃ RGB ┃ 1 │ 2 │ 3 │ 4 │ 5 ┃ 6 │ 7 │ 8 │ 9 │ 0 ┃ F12 ┃
  89. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  90. ┃L_MOD┃ F1 │ F2 │ F3 │ F4 │ F5 ┃ F6 │ F7 │ F8 │ F9 │ F10 ┃ F11 ┃
  91. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  92. ┃ ┃RESET│DEBUG│ │ │TIME ┃SLEEP│ SEQ │ { │ } │PTSCR┃ ┃
  93. ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
  94. ┃ ┃ │ │ │ │ ┃ │ │ │ │ ┃ ┃
  95. ┗━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┛
  96. */
  97. [_BETA] = LAYOUT( \
  98. _______, _______, \
  99. RGB_TOG, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_F12,
  100. #ifdef LIGHTMODE_ENABLE
  101. KC_LIGHT_MODE,
  102. #else
  103. _______,
  104. #endif
  105. KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, \
  106. _______, RESET, DEBUG, _______, _______, KC_SET_TIME,KC_SLEP,KC_SEQ,KC_LCBR, KC_RCBR, KC_PSCR, _______, \
  107. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
  108. )
  109. };
  110. static bool alpha_pressed = false;
  111. static bool beta_pressed = false;
  112. static bool ctrl_pressed = false;
  113. static bool alt_pressed = false;
  114. static bool shift_pressed = false;
  115. static bool gui_pressed = false;
  116. static bool is_in_seq = false;
  117. void keyboard_post_init_user(void) {
  118. /* debug_enable = true; */
  119. /* debug_matrix = true; */
  120. }
  121. void eeconfig_init_user(void) {
  122. set_unicode_input_mode(UC_LNX);
  123. }
  124. void matrix_init_user(void) {
  125. #ifdef LIGHTMODE_ENABLE
  126. set_light_mode(SMOOTHLED, layer_colors[_MAIN]);
  127. #endif
  128. }
  129. static uint32_t last_update = 0;
  130. static uint8_t hours, minutes, seconds;
  131. void matrix_scan_user(void) {
  132. uint32_t now = timer_read32();
  133. if (now - last_update > 500) {
  134. ds1307_get_time(&hours, &minutes, &seconds);
  135. last_update = now;
  136. }
  137. }
  138. static bool is_in_set_time = false;
  139. static char new_time[6];
  140. static uint8_t new_time_index = 0;
  141. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  142. if (keycode == KC_SEQ && record->event.pressed) {
  143. seq_start();
  144. layer_off(_BETA);
  145. is_in_seq = true;
  146. return false;
  147. } else if (is_in_seq) {
  148. if (record->event.pressed) {
  149. if (!seq_feed(keycode)) {
  150. is_in_seq = false;
  151. }
  152. }
  153. return false;
  154. }
  155. if (keycode == KC_SET_TIME && record->event.pressed) {
  156. is_in_set_time = true;
  157. new_time_index = 0;
  158. } else if (is_in_set_time) {
  159. if (!record->event.pressed && keycode >= KC_1 && keycode <= KC_0) {
  160. new_time[new_time_index++] = (keycode == KC_0) ? 0 : keycode - KC_1 + 1;
  161. if (new_time_index == 6) {
  162. is_in_set_time = false;
  163. ds1307_set_time(
  164. (new_time[0]) * 10 + (new_time[1]),
  165. (new_time[2]) * 10 + (new_time[3]),
  166. (new_time[4]) * 10 + (new_time[5])
  167. );
  168. for (int i = 0; i < 6; i++) {
  169. tap_code(KC_BSPACE);
  170. }
  171. }
  172. }
  173. }
  174. if (keycode == KC_LCTRL || keycode == KC_RCTRL) {
  175. ctrl_pressed = record->event.pressed;
  176. } else if (keycode == KC_LALT) {
  177. alt_pressed = record->event.pressed;
  178. } else if (keycode == KC_LSFT) {
  179. shift_pressed = record->event.pressed;
  180. } else if (keycode == KC_LGUI) {
  181. gui_pressed = record->event.pressed;
  182. } else if (keycode == KC_ALPHA) {
  183. alpha_pressed = record->event.pressed;
  184. } else if (keycode == KC_BETA) {
  185. beta_pressed = record->event.pressed;
  186. }
  187. if (keycode == RESET) {
  188. rgblight_setrgb(255, 255, 0);
  189. }
  190. #ifdef LIGHTMODE_ENABLE
  191. if (record->event.pressed && keycode == KC_LIGHT_MODE) {
  192. next_light_mode(layer_colors[_MAIN]);
  193. }
  194. #endif
  195. if (keycode == KC_LCTRL) {
  196. /* Some Overlay1_Enable fuckery! */
  197. (record->event.pressed ? register_code : unregister_code)(KC_LCTRL);
  198. return false;
  199. }
  200. return taphold_process(keycode, record);
  201. }
  202. layer_state_t layer_state_set_user(layer_state_t state) {
  203. #ifdef LIGHTMODE_ENABLE
  204. uint8_t layer = get_highest_layer(state);
  205. update_light_mode(layer_colors[layer]);
  206. #endif
  207. return state;
  208. }
  209. bool encoder_update_user(uint8_t index, bool clockwise) {
  210. if (index == 0) {
  211. if (!alpha_pressed) {
  212. tap_code(clockwise ? KC_VOLD : KC_VOLU);
  213. } else {
  214. tap_code(clockwise ? KC_MPRV : KC_MNXT);
  215. }
  216. } else if (index == 1) {
  217. if (!alpha_pressed) {
  218. tap_code(clockwise ? KC_UP : KC_DOWN);
  219. } else {
  220. tap_code(clockwise ? KC_PGUP : KC_PGDN);
  221. }
  222. }
  223. return true;
  224. }
  225. #ifdef OLED_DRIVER_ENABLE
  226. oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  227. return OLED_ROTATION_0;
  228. }
  229. void oled_task_user(void) {
  230. /* Host Keyboard Layer Status */
  231. uint8_t current_layer = get_highest_layer(layer_state);
  232. /* Layer */
  233. static const char PROGMEM icons[4][3][6] = {
  234. {
  235. { 0x80, 0x81, 0x82, 0x83, 0x84, 0 },
  236. { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0 },
  237. { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0 }
  238. },
  239. {
  240. { 0x85, 0x86, 0x87, 0x88, 0x89, 0 },
  241. { 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0 },
  242. { 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0 }
  243. },
  244. {
  245. { 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0 },
  246. { 0xaa, 0xab, 0xac, 0xad, 0xae, 0 },
  247. { 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0 }
  248. },
  249. {
  250. { 0x8f, 0x90, 0x91, 0x92, 0x93, 0 },
  251. { 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0 },
  252. { 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0 }
  253. }
  254. };
  255. uint8_t icon_index = current_layer == _MAIN ? 3 : current_layer == _ALPHA ? 1 : 2;
  256. for (int i = 0; i < 3; i++) {
  257. oled_set_cursor(0, i + 1);
  258. oled_write_P(icons[icon_index][i], false);
  259. }
  260. /* Time */
  261. oled_set_cursor(6, 0);
  262. // oled_write_P(PSTR("-D48 Custom-\n"), false);
  263. char buf[16];
  264. sprintf(
  265. buf,
  266. "%02d:%02d:%02d", hours, minutes, seconds
  267. );
  268. oled_write(buf, false);
  269. /* Modifiers */
  270. static const char PROGMEM mods[][2] = {
  271. {0x94, 0x95}, // CTL
  272. {0x96, 0x97}, // ALT
  273. {0x98, 0x99}, // GUI
  274. {0x9a, 0x9b}, // SFT
  275. /* {0x9c, 0x9d}, // EMPTY */
  276. };
  277. char mod_data[13] = "\x9c\x9d\x9c\x9d\x9c\x9d\x9c\x9d \x07\x07\x07\0";
  278. if (ctrl_pressed) strncpy(mod_data, mods[0], 2);
  279. if (alt_pressed) strncpy(mod_data + 2, mods[1], 2);
  280. if (gui_pressed) strncpy(mod_data + 4, mods[2], 2);
  281. if (shift_pressed) strncpy(mod_data + 6, mods[3], 2);
  282. led_t led_usb_state = host_keyboard_led_state();
  283. if (led_usb_state.num_lock) mod_data[9] = 'N';
  284. if (led_usb_state.caps_lock) mod_data[10] = 'C';
  285. if (led_usb_state.scroll_lock) mod_data[11] = 'S';
  286. oled_set_cursor(6, 1);
  287. oled_write(mod_data, false);
  288. /* Matrix */
  289. static const char PROGMEM matrix_chars[] = {
  290. 0xb4, // None
  291. 0xb5, // Upper
  292. 0xb6, // Lower
  293. 0xb7 // Both
  294. };
  295. for (uint8_t row = 1; row < MATRIX_ROWS; row += 2) {
  296. // Skip first row because it's used by the encoders.
  297. uint16_t bits1 = matrix_get_row(row);
  298. uint16_t bits2 = matrix_get_row(row + 1);
  299. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  300. uint8_t matrix_char = matrix_chars[((bits1 & (1 << col)) ? 1 : 0) | ((bits2 & (1 << col)) ? 2 : 0)];
  301. oled_set_cursor(6 + col, 2 + (row - 1) / 2);
  302. oled_write_char(matrix_char, false);
  303. }
  304. }
  305. }
  306. #endif