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.

304 lines
8.3 KiB

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