]> git.gir.st - tmk_keyboard.git/blob - common/bootmagic.c
036d49044004156b5f4f313fbdaea81923bef31f
[tmk_keyboard.git] / common / bootmagic.c
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
12
13 void bootmagic(void)
14 {
15 /* check signature */
16 if (!eeconfig_is_enabled()) {
17 eeconfig_init();
18 }
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
26 /* bootmagic skip */
27 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
28 return;
29 }
30
31 /* eeconfig clear */
32 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
33 eeconfig_init();
34 }
35
36 /* bootloader */
37 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
38 bootloader_jump();
39 }
40
41 /* debug enable */
42 debug_config.raw = eeconfig_read_debug();
43 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
44 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
45 debug_config.matrix = !debug_config.matrix;
46 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
47 debug_config.keyboard = !debug_config.keyboard;
48 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
49 debug_config.mouse = !debug_config.mouse;
50 } else {
51 debug_config.enable = !debug_config.enable;
52 }
53 }
54 eeconfig_write_debug(debug_config.raw);
55
56 /* keymap config */
57 keymap_config.raw = eeconfig_read_keymap();
58 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
59 keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
60 }
61 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
62 keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
63 }
64 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
65 keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
66 }
67 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
68 keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
69 }
70 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
71 keymap_config.no_gui = !keymap_config.no_gui;
72 }
73 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
74 keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
75 }
76 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
77 keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
78 }
79 eeconfig_write_keymap(keymap_config.raw);
80
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
100 static bool scan_keycode(uint8_t keycode)
101 {
102 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
103 matrix_row_t matrix_row = matrix_get_row(r);
104 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
105 if (matrix_row & ((matrix_row_t)1<<c)) {
106 if (keycode == keymap_key_to_keycode(0, (key_t){ .row = r, .col = c })) {
107 return true;
108 }
109 }
110 }
111 }
112 return false;
113 }
114
115 bool bootmagic_scan_keycode(uint8_t keycode)
116 {
117 if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
118
119 return scan_keycode(keycode);
120 }
Imprint / Impressum