logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

voyager.c (9133B)


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