]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/bootmagic.c
Generate API docs from source code comments (#2491)
[qmk_firmware.git] / tmk_core / common / bootmagic.c
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
13 keymap_config_t keymap_config;
14
15 /** \brief Bootmagic
16  *
17  * FIXME: needs doc
18  */
19 void bootmagic(void)
20 {
21     /* check signature */
22     if (!eeconfig_is_enabled()) {
23         eeconfig_init();
24     }
25
26     /* do scans in case of bounce */
27     print("bootmagic scan: ... ");
28     uint8_t scan = 100;
29     while (scan--) { matrix_scan(); wait_ms(10); }
30     print("done.\n");
31
32     /* bootmagic skip */
33     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
34         return;
35     }
36
37     /* eeconfig clear */
38     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
39         eeconfig_init();
40     }
41
42     /* bootloader */
43     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
44         bootloader_jump();
45     }
46
47     /* debug enable */
48     debug_config.raw = eeconfig_read_debug();
49     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
50         if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
51             debug_config.matrix = !debug_config.matrix;
52         } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
53             debug_config.keyboard = !debug_config.keyboard;
54         } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
55             debug_config.mouse = !debug_config.mouse;
56         } else {
57             debug_config.enable = !debug_config.enable;
58         }
59     }
60     eeconfig_update_debug(debug_config.raw);
61
62     /* keymap config */
63     keymap_config.raw = eeconfig_read_keymap();
64     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
65         keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
66     }
67     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
68         keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
69     }
70     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
71         keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
72     }
73     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
74         keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
75     }
76     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
77         keymap_config.no_gui = !keymap_config.no_gui;
78     }
79     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
80         keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
81     }
82     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
83         keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
84     }
85     if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
86         keymap_config.nkro = !keymap_config.nkro;
87     }
88     eeconfig_update_keymap(keymap_config.raw);
89
90     /* default layer */
91     uint8_t default_layer = 0;
92     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
93     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
94     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
95     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
96     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
97     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
98     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
99     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
100     if (default_layer) {
101         eeconfig_update_default_layer(default_layer);
102         default_layer_set((uint32_t)default_layer);
103     } else {
104         default_layer = eeconfig_read_default_layer();
105         default_layer_set((uint32_t)default_layer);
106     }
107 }
108
109 /** \brief Scan Keycode
110  *
111  * FIXME: needs doc
112  */
113 static bool scan_keycode(uint8_t keycode)
114 {
115     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
116         matrix_row_t matrix_row = matrix_get_row(r);
117         for (uint8_t c = 0; c < MATRIX_COLS; c++) {
118             if (matrix_row & ((matrix_row_t)1<<c)) {
119                 if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) {
120                     return true;
121                 }
122             }
123         }
124     }
125     return false;
126 }
127
128 /** \brief Bootmagic Scan Keycode
129  *
130  * FIXME: needs doc
131  */
132 bool bootmagic_scan_keycode(uint8_t keycode)
133 {
134     if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
135
136     return scan_keycode(keycode);
137 }