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

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