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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "action_layer.h"
  9. #include "eeconfig.h"
  10. #include "keymap_in_eeprom.h"
  11. #include "ledmap_in_eeprom.h"
  12. #include "bootmagic.h"
  13. void bootmagic(void)
  14. {
  15. /* check signature */
  16. if (!eeconfig_is_enabled()) {
  17. eeconfig_init();
  18. }
  19. /* do scans in case of bounce */
  20. print("boogmagic scan: ... ");
  21. uint8_t scan = 100;
  22. while (scan--) { matrix_scan(); _delay_ms(10); }
  23. print("done.\n");
  24. /* bootmagic skip */
  25. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
  26. return;
  27. }
  28. /* eeconfig clear */
  29. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
  30. eeconfig_disable();
  31. eeconfig_init();
  32. #ifdef KEYMAP_IN_EEPROM_ENABLE
  33. write_keymap_to_eeprom();
  34. #endif
  35. #ifdef LEDMAP_IN_EEPROM_ENABLE
  36. write_ledmap_to_eeprom();
  37. #endif
  38. }
  39. /* bootloader */
  40. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
  41. bootloader_jump();
  42. }
  43. /* debug enable */
  44. debug_config.raw = eeconfig_read_debug();
  45. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
  46. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
  47. debug_config.matrix = !debug_config.matrix;
  48. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
  49. debug_config.keyboard = !debug_config.keyboard;
  50. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
  51. debug_config.mouse = !debug_config.mouse;
  52. } else {
  53. debug_config.enable = !debug_config.enable;
  54. }
  55. }
  56. eeconfig_write_debug(debug_config.raw);
  57. /* keymap config */
  58. keymap_config.raw = eeconfig_read_keymap();
  59. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
  60. keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
  61. }
  62. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
  63. keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
  64. }
  65. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
  66. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  67. }
  68. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
  69. keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
  70. }
  71. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
  72. keymap_config.no_gui = !keymap_config.no_gui;
  73. }
  74. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
  75. keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
  76. }
  77. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
  78. keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
  79. }
  80. eeconfig_write_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_write_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. static bool scan_keycode(uint8_t keycode)
  100. {
  101. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  102. matrix_row_t matrix_row = matrix_get_row(r);
  103. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  104. if (matrix_row & ((matrix_row_t)1<<c)) {
  105. if (keycode == keymap_key_to_keycode(0, (key_t){ .row = r, .col = c })) {
  106. return true;
  107. }
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. bool bootmagic_scan_keycode(uint8_t keycode)
  114. {
  115. if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
  116. return scan_keycode(keycode);
  117. }