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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

bootmagic.c 4.6KB

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