bootmagic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "wait.h"
  4. #include "matrix.h"
  5. #include "bootloader.h"
  6. #include "debug.h"
  7. #include "keymap.h"
  8. #include "host.h"
  9. #include "action_layer.h"
  10. #include "eeconfig.h"
  11. #include "bootmagic.h"
  12. keymap_config_t keymap_config;
  13. /** \brief Bootmagic
  14. *
  15. * FIXME: needs doc
  16. */
  17. void bootmagic(void)
  18. {
  19. /* check signature */
  20. if (!eeconfig_is_enabled()) {
  21. eeconfig_init();
  22. }
  23. /* do scans in case of bounce */
  24. print("bootmagic scan: ... ");
  25. uint8_t scan = 100;
  26. while (scan--) { matrix_scan(); wait_ms(10); }
  27. print("done.\n");
  28. /* bootmagic skip */
  29. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
  30. return;
  31. }
  32. /* eeconfig clear */
  33. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
  34. eeconfig_init();
  35. }
  36. /* bootloader */
  37. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
  38. bootloader_jump();
  39. }
  40. /* debug enable */
  41. debug_config.raw = eeconfig_read_debug();
  42. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
  43. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
  44. debug_config.matrix = !debug_config.matrix;
  45. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
  46. debug_config.keyboard = !debug_config.keyboard;
  47. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
  48. debug_config.mouse = !debug_config.mouse;
  49. } else {
  50. debug_config.enable = !debug_config.enable;
  51. }
  52. }
  53. eeconfig_update_debug(debug_config.raw);
  54. /* keymap config */
  55. keymap_config.raw = eeconfig_read_keymap();
  56. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
  57. keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
  58. }
  59. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
  60. keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
  61. }
  62. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
  63. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  64. }
  65. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
  66. keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
  67. }
  68. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
  69. keymap_config.no_gui = !keymap_config.no_gui;
  70. }
  71. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
  72. keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
  73. }
  74. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
  75. keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
  76. }
  77. if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
  78. keymap_config.nkro = !keymap_config.nkro;
  79. }
  80. eeconfig_update_keymap(keymap_config.raw);
  81. /* default layer */
  82. uint8_t default_layer = 0;
  83. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
  84. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
  85. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
  86. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
  87. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
  88. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
  89. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
  90. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
  91. if (default_layer) {
  92. eeconfig_update_default_layer(default_layer);
  93. default_layer_set((uint32_t)default_layer);
  94. } else {
  95. default_layer = eeconfig_read_default_layer();
  96. default_layer_set((uint32_t)default_layer);
  97. }
  98. }
  99. /** \brief Scan Keycode
  100. *
  101. * FIXME: needs doc
  102. */
  103. static bool scan_keycode(uint8_t keycode)
  104. {
  105. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  106. matrix_row_t matrix_row = matrix_get_row(r);
  107. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  108. if (matrix_row & ((matrix_row_t)1<<c)) {
  109. if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) {
  110. return true;
  111. }
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. /** \brief Bootmagic Scan Keycode
  118. *
  119. * FIXME: needs doc
  120. */
  121. bool bootmagic_scan_keycode(uint8_t keycode)
  122. {
  123. if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
  124. return scan_keycode(keycode);
  125. }