Keyboard firmwares for Atmel AVR and Cortex-M
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.

bootmagic.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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("bootmagic scan: ... ");
  21. uint8_t scan = 100;
  22. while (scan--) { matrix_scan(); wait_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_init();
  31. }
  32. /* bootloader */
  33. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
  34. bootloader_jump();
  35. }
  36. /* debug enable */
  37. debug_config.raw = eeconfig_read_debug();
  38. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
  39. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
  40. debug_config.matrix = !debug_config.matrix;
  41. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
  42. debug_config.keyboard = !debug_config.keyboard;
  43. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
  44. debug_config.mouse = !debug_config.mouse;
  45. } else {
  46. debug_config.enable = !debug_config.enable;
  47. }
  48. }
  49. eeconfig_write_debug(debug_config.raw);
  50. /* keymap config */
  51. keymap_config.raw = eeconfig_read_keymap();
  52. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
  53. keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
  54. }
  55. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
  56. keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
  57. }
  58. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
  59. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  60. }
  61. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
  62. keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
  63. }
  64. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
  65. keymap_config.no_gui = !keymap_config.no_gui;
  66. }
  67. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
  68. keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
  69. }
  70. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
  71. keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
  72. }
  73. if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
  74. keymap_config.nkro = !keymap_config.nkro;
  75. }
  76. eeconfig_write_keymap(keymap_config.raw);
  77. #ifdef NKRO_ENABLE
  78. keyboard_nkro = keymap_config.nkro;
  79. #endif
  80. /* default layer */
  81. uint8_t default_layer = 0;
  82. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
  83. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
  84. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
  85. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
  86. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
  87. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
  88. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
  89. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
  90. if (default_layer) {
  91. eeconfig_write_default_layer(default_layer);
  92. default_layer_set((uint32_t)default_layer);
  93. } else {
  94. default_layer = eeconfig_read_default_layer();
  95. default_layer_set((uint32_t)default_layer);
  96. }
  97. }
  98. static bool scan_keycode(uint8_t keycode)
  99. {
  100. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  101. matrix_row_t matrix_row = matrix_get_row(r);
  102. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  103. if (matrix_row & ((matrix_row_t)1<<c)) {
  104. if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) {
  105. return true;
  106. }
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. bool bootmagic_scan_keycode(uint8_t keycode)
  113. {
  114. if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
  115. return scan_keycode(keycode);
  116. }