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

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