]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/common/bootmagic.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[max/tmk_keyboard.git] / tmk_core / 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 "host.h"
9 #include "action_layer.h"
10 #include "eeconfig.h"
11 #include "bootmagic.h"
12
13
14 void bootmagic(void)
15 {
16     /* check signature */
17     if (!eeconfig_is_enabled()) {
18         eeconfig_init();
19     }
20
21     /* do scans in case of bounce */
22     print("boogmagic scan: ... ");
23     uint8_t scan = 100;
24     while (scan--) { matrix_scan(); _delay_ms(10); }
25     print("done.\n");
26
27     /* bootmagic skip */
28     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
29         return;
30     }
31
32     /* eeconfig clear */
33     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
34         eeconfig_init();
35     }
36
37     /* bootloader */
38     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
39         bootloader_jump();
40     }
41
42     /* debug enable */
43     debug_config.raw = eeconfig_read_debug();
44     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
45         if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
46             debug_config.matrix = !debug_config.matrix;
47         } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
48             debug_config.keyboard = !debug_config.keyboard;
49         } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
50             debug_config.mouse = !debug_config.mouse;
51         } else {
52             debug_config.enable = !debug_config.enable;
53         }
54     }
55     eeconfig_write_debug(debug_config.raw);
56
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     if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
81         keymap_config.nkro = !keymap_config.nkro;
82     }
83     eeconfig_write_keymap(keymap_config.raw);
84
85 #ifdef NKRO_ENABLE
86     keyboard_nkro = keymap_config.nkro;
87 #endif
88
89     /* default layer */
90     uint8_t default_layer = 0;
91     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
92     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
93     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
94     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
95     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
96     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
97     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
98     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
99     if (default_layer) {
100         eeconfig_write_default_layer(default_layer);
101         default_layer_set((uint32_t)default_layer);
102     } else {
103         default_layer = eeconfig_read_default_layer();
104         default_layer_set((uint32_t)default_layer);
105     }
106 }
107
108 static bool scan_keycode(uint8_t keycode)
109 {
110     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
111         matrix_row_t matrix_row = matrix_get_row(r);
112         for (uint8_t c = 0; c < MATRIX_COLS; c++) {
113             if (matrix_row & ((matrix_row_t)1<<c)) {
114                 if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) {
115                     return true;
116                 }
117             }
118         }
119     }
120     return false;
121 }
122
123 bool bootmagic_scan_keycode(uint8_t keycode)
124 {
125     if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
126
127     return scan_keycode(keycode);
128 }