You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
8.7 KiB

8 months ago
  1. // Copyright 2023 ZSA Technology Labs, Inc <@zsa>
  2. // Copyright 2023 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include "voyager.h"
  5. keyboard_config_t keyboard_config;
  6. bool mcp23018_leds[2] = {0, 0};
  7. bool is_launching = false;
  8. #if defined(DEFERRED_EXEC_ENABLE)
  9. # if defined(DYNAMIC_MACRO_ENABLE)
  10. deferred_token dynamic_macro_token = INVALID_DEFERRED_TOKEN;
  11. static uint32_t dynamic_macro_led(uint32_t trigger_time, void *cb_arg) {
  12. static bool led_state = true;
  13. if (!is_launching) {
  14. led_state = !led_state;
  15. STATUS_LED_3(led_state);
  16. }
  17. return 100;
  18. }
  19. void dynamic_macro_record_start_user(void) {
  20. if (my_token == INVALID_DEFERRED_TOKEN) {
  21. STATUS_LED_3(true);
  22. dynamic_macro_token = defer_exec(100, dynamic_macro_led, NULL);
  23. }
  24. }
  25. void dynamic_macro_record_end_user(int8_t direction) {
  26. if (cancel_deferred_exec(dynamic_macro_token)) {
  27. dynamic_macro_token = INVALID_DEFERRED_TOKEN;
  28. STATUS_LED_3(false);
  29. }
  30. }
  31. # endif
  32. static uint32_t startup_exec(uint32_t trigger_time, void *cb_arg) {
  33. static uint8_t startup_loop = 0;
  34. switch (startup_loop++) {
  35. case 0:
  36. STATUS_LED_1(true);
  37. STATUS_LED_2(false);
  38. STATUS_LED_3(false);
  39. STATUS_LED_4(false);
  40. break;
  41. case 1:
  42. STATUS_LED_2(true);
  43. break;
  44. case 2:
  45. STATUS_LED_3(true);
  46. break;
  47. case 3:
  48. STATUS_LED_4(true);
  49. break;
  50. case 4:
  51. STATUS_LED_1(false);
  52. break;
  53. case 5:
  54. STATUS_LED_2(false);
  55. break;
  56. case 6:
  57. STATUS_LED_3(false);
  58. break;
  59. case 7:
  60. STATUS_LED_4(false);
  61. break;
  62. case 8:
  63. is_launching = false;
  64. layer_state_set_kb(layer_state);
  65. return 0;
  66. }
  67. return 250;
  68. }
  69. #endif
  70. void keyboard_pre_init_kb(void) {
  71. // Initialize Reset pins
  72. gpio_set_pin_input(A8);
  73. gpio_set_pin_output(A9);
  74. gpio_write_pin_low(A9);
  75. gpio_set_pin_output(B5);
  76. gpio_set_pin_output(B4);
  77. gpio_set_pin_output(B3);
  78. gpio_write_pin_low(B5);
  79. gpio_write_pin_low(B4);
  80. gpio_write_pin_low(B3);
  81. keyboard_pre_init_user();
  82. }
  83. #if !defined(VOYAGER_USER_LEDS)
  84. layer_state_t layer_state_set_kb(layer_state_t state) {
  85. state = layer_state_set_user(state);
  86. if (is_launching || !keyboard_config.led_level) return state;
  87. uint8_t layer = get_highest_layer(state);
  88. STATUS_LED_1(layer & (1 << 0));
  89. STATUS_LED_2(layer & (1 << 1));
  90. STATUS_LED_3(layer & (1 << 2));
  91. # if !defined(CAPS_LOCK_STATUS)
  92. STATUS_LED_4(layer & (1 << 3));
  93. # endif
  94. return state;
  95. }
  96. #endif
  97. #ifdef RGB_MATRIX_ENABLE
  98. // clang-format off
  99. const is31_led PROGMEM g_is31_leds[RGB_MATRIX_LED_COUNT] = {
  100. /* Refer to IS31 manual for these locations
  101. * driver
  102. * | R location
  103. * | | G location
  104. * | | | B location
  105. * | | | | */
  106. {0, C2_2, C1_2, C4_3},
  107. {0, C2_3, C1_3, C3_3},
  108. {0, C2_4, C1_4, C3_4},
  109. {0, C2_5, C1_5, C3_5},
  110. {0, C2_6, C1_6, C3_6},
  111. {0, C2_7, C1_7, C3_7},
  112. {0, C2_8, C1_8, C3_8},
  113. {0, C8_1, C7_1, C9_1},
  114. {0, C8_2, C7_2, C9_2},
  115. {0, C8_3, C7_3, C9_3},
  116. {0, C8_4, C7_4, C9_4},
  117. {0, C8_5, C7_5, C9_5},
  118. {0, C8_6, C7_6, C9_6},
  119. {0, C2_10, C1_10, C4_11},
  120. {0, C2_11, C1_11, C3_11},
  121. {0, C2_12, C1_12, C3_12},
  122. {0, C2_13, C1_13, C3_13},
  123. {0, C2_14, C1_14, C3_14},
  124. {0, C2_15, C1_15, C3_15},
  125. {0, C2_16, C1_16, C3_16},
  126. {0, C8_9, C7_9, C9_9},
  127. {0, C8_10, C7_10, C9_10},
  128. {0, C8_11, C7_11, C9_11},
  129. {0, C8_12, C7_12, C9_12},
  130. {0, C8_13, C7_13, C9_13},
  131. {0, C8_14, C7_14, C9_14},
  132. {1, C2_7, C1_7, C3_7},
  133. {1, C2_6, C1_6, C3_6},
  134. {1, C2_5, C1_5, C3_5},
  135. {1, C2_4, C1_4, C3_4},
  136. {1, C2_3, C1_3, C3_3},
  137. {1, C2_2, C1_2, C4_3},
  138. {1, C8_5, C7_5, C9_5},
  139. {1, C8_4, C7_4, C9_4},
  140. {1, C8_3, C7_3, C9_3},
  141. {1, C8_2, C7_2, C9_2},
  142. {1, C8_1, C7_1, C9_1},
  143. {1, C2_8, C1_8, C3_8},
  144. {1, C2_14, C1_14, C3_14},
  145. {1, C2_13, C1_13, C3_13},
  146. {1, C2_12, C1_12, C3_12},
  147. {1, C2_11, C1_11, C3_11},
  148. {1, C2_10, C1_10, C4_11},
  149. {1, C8_6, C7_6, C9_6},
  150. {1, C8_12, C7_12, C9_12},
  151. {1, C8_11, C7_11, C9_11},
  152. {1, C8_10, C7_10, C9_10},
  153. {1, C8_9, C7_9, C9_9},
  154. {1, C2_16, C1_16, C3_16},
  155. {1, C2_15, C1_15, C3_15},
  156. {1, C8_14, C7_14, C9_14},
  157. {1, C8_13, C7_13, C9_13},
  158. };
  159. // clang-format on
  160. #endif
  161. #ifdef SWAP_HANDS_ENABLE
  162. // swap-hands action needs a matrix to define the swap
  163. // clang-format off
  164. const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  165. /* Left hand, matrix positions */
  166. {{6,6}, {5,6}, {4,6}, {3,6}, {2,6}, {1,6},{0,6}},
  167. {{6,7}, {5,7}, {4,7}, {3,7}, {2,7}, {1,7},{0,7}},
  168. {{6,8}, {5,8}, {4,8}, {3,8}, {2,8}, {1,8},{0,8}},
  169. {{6,9}, {5,9}, {4,9}, {3,9}, {2,9}, {1,9},{0,9}},
  170. {{6,10},{5,10},{4,10},{3,10},{2,10},{1,10},{0,10}},
  171. {{6,11},{5,11},{4,11},{3,11},{2,11},{1,11},{0,11}},
  172. /* Right hand, matrix positions */
  173. {{6,0}, {5,0}, {4,0}, {3,0}, {2,0}, {1,0},{0,0}},
  174. {{6,1}, {5,1}, {4,1}, {3,1}, {2,1}, {1,1},{0,1}},
  175. {{6,2}, {5,2}, {4,2}, {3,2}, {2,2}, {1,2},{0,2}},
  176. {{6,3}, {5,3}, {4,3}, {3,3}, {2,3}, {1,3},{0,3}},
  177. {{6,4}, {5,4}, {4,4}, {3,4}, {2,4}, {1,4},{0,4}},
  178. {{6,5}, {5,5}, {4,5}, {3,5}, {2,5}, {1,5},{0,5}},
  179. };
  180. // clang-format on
  181. #endif
  182. #ifdef CAPS_LOCK_STATUS
  183. bool led_update_kb(led_t led_state) {
  184. bool res = led_update_user(led_state);
  185. if (res) {
  186. STATUS_LED_4(led_state.caps_lock);
  187. }
  188. return res;
  189. }
  190. #endif
  191. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  192. if (!process_record_user(keycode, record)) {
  193. return false;
  194. }
  195. switch (keycode) {
  196. #if !defined(VOYAGER_USER_LEDS)
  197. case LED_LEVEL:
  198. if (record->event.pressed) {
  199. keyboard_config.led_level ^= 1;
  200. eeconfig_update_kb(keyboard_config.raw);
  201. if (keyboard_config.led_level) {
  202. layer_state_set_kb(layer_state);
  203. } else {
  204. STATUS_LED_1(false);
  205. STATUS_LED_2(false);
  206. STATUS_LED_3(false);
  207. STATUS_LED_4(false);
  208. }
  209. }
  210. break;
  211. #endif
  212. #ifdef RGB_MATRIX_ENABLE
  213. case TOGGLE_LAYER_COLOR:
  214. if (record->event.pressed) {
  215. keyboard_config.disable_layer_led ^= 1;
  216. if (keyboard_config.disable_layer_led) rgb_matrix_set_color_all(0, 0, 0);
  217. }
  218. break;
  219. case RGB_TOG:
  220. if (record->event.pressed) {
  221. switch (rgb_matrix_get_flags()) {
  222. case LED_FLAG_ALL: {
  223. rgb_matrix_set_flags(LED_FLAG_NONE);
  224. rgb_matrix_set_color_all(0, 0, 0);
  225. } break;
  226. default: {
  227. rgb_matrix_set_flags(LED_FLAG_ALL);
  228. } break;
  229. }
  230. }
  231. return false;
  232. #endif
  233. }
  234. return true;
  235. }
  236. void keyboard_post_init_kb(void) {
  237. #ifdef RGB_MATRIX_ENABLE
  238. rgb_matrix_enable_noeeprom();
  239. #endif
  240. keyboard_config.raw = eeconfig_read_kb();
  241. if (!keyboard_config.led_level && !keyboard_config.led_level_res) {
  242. keyboard_config.led_level = true;
  243. keyboard_config.led_level_res = 0b11;
  244. eeconfig_update_kb(keyboard_config.raw);
  245. }
  246. #if defined(DEFERRED_EXEC_ENABLE)
  247. is_launching = true;
  248. defer_exec(500, startup_exec, NULL);
  249. #endif
  250. keyboard_post_init_user();
  251. }
  252. void eeconfig_init_kb(void) { // EEPROM is getting reset!
  253. keyboard_config.raw = 0;
  254. keyboard_config.led_level = true;
  255. keyboard_config.led_level_res = 0b11;
  256. eeconfig_update_kb(keyboard_config.raw);
  257. eeconfig_init_user();
  258. }
  259. __attribute__((weak)) void bootloader_jump(void) {
  260. // The ignition bootloader is checking for a high signal on A8 for 100ms when powering on the board.
  261. // Setting both A8 and A9 high will charge the capacitor quickly.
  262. // Setting A9 low before reset will cause the capacitor to discharge
  263. // thus making the bootloder unlikely to trigger twice between power cycles.
  264. gpio_set_pin_output_push_pull(A9);
  265. gpio_set_pin_output_push_pull(A8);
  266. gpio_write_pin_high(A9);
  267. gpio_write_pin_high(A8);
  268. wait_ms(500);
  269. gpio_write_pin_low(A9);
  270. NVIC_SystemReset();
  271. }
  272. __attribute__((weak)) void mcu_reset(void) {
  273. gpio_set_pin_output_push_pull(A9);
  274. gpio_set_pin_output_push_pull(A8);
  275. gpio_write_pin_low(A8);
  276. gpio_write_pin_low(A9);
  277. NVIC_SystemReset();
  278. }