1
0
This repo is archived. You can view files and clone it, but cannot push or open issues or pull requests.
tmk_keyboard_custom/tmk_core/common/bootmagic.c

152 lines
5.0 KiB
C
Raw Normal View History

#include <stdint.h>
#include <stdbool.h>
#include "wait.h"
#include "matrix.h"
#include "bootloader.h"
#include "debug.h"
#include "keymap.h"
2016-03-03 07:18:19 +00:00
#include "actionmap.h"
#include "host.h"
#include "action_layer.h"
#include "eeconfig.h"
#include "keymap_in_eeprom.h"
2014-07-24 06:03:58 +00:00
#include "ledmap_in_eeprom.h"
#include "bootmagic.h"
Squashed 'tmk_core/' changes from ee8c5ba..d5c5ac6 d5c5ac6 Merge branch 'develop' 5957682 Merge branch 'hotfix-mediakey' a478c62 Merge branch 'hotfix-vusb' cccebfe Merge branch 'njbair-docfix' 0aaab57 Clean up wording in keymap example dc8bbc3 Clarify layer precedence 9e0b4c1 clarify layer documentation 915eb48 core: Fix media/consumer keys 88f90f3 Fix for VUSB configuration 3e290cd Fix including board.mk in chibios.mk 32c69e0 Merge branch 'newapi' into develop c9a56f9 Merge remote-tracking branch 'flabbergast/chibios' into develop 01e33ea Fix chibios and mbed common.mk for hook.c bea79d9 hook: Change func name of usb events 3e97536 hook: Change file and func names(*_hook -> hook_*) c286d8c Merge pull request #10 from fredizzimo/chibios-contrib2 062d74e Update ChibiOS instructions d47150f Add support for new version of ChibiOS and Contrib 62b5401 Chibios: disable LTO (link-time optimisation). c64e9aa hooks: Fix for LUFA 54e68b0 hooks: Remove led_restore_hook 325c09d Chibios: make the default bootloader_jump redefinable (weak). 078c722 Chibios: fix STM32_BOOTLOADER_ADDRESS name. e73cfe5 hooks: Fix for keyboard LED update e6120c5 Implement basic hooks. 7c370e9 Chibios: Update the main chibios README. 7f0198d Chibios: implement sleep LED for STM32. afef9b4 Fix hard-coded path of CHIBIOS 95c5b19 Merge pull request #7 from fredizzimo/sysvsize 27128a8 Sysv format for ChibiOS arm-none-eabi-size d4b8e68 core: Fix chibios user compile options b85d462 Merge branch 'chibios' of https://github.com/flabbergast/tmk_keyboard into flabbergast_chibios de41aa1 core: Fix ps2_mouse.c debug print d79d925 Removed duplicate debug message code and surrounded it with IFDEF as needed 8f28589 Chibios: Revert common.mk change (fix AVR linking problem). ec9eff2 Chibios: cleanup usb_main code. 28c4665 Chibios: Fix a HardFault bug (wait after start). git-subtree-dir: tmk_core git-subtree-split: d5c5ac63e60dfc6da6661a21bd968b4d577a27d5
2016-04-21 05:35:48 +00:00
#include "hook.h"
keymap_config_t keymap_config;
void bootmagic(void)
{
/* check signature */
if (!eeconfig_is_enabled()) {
eeconfig_init();
}
/* do scans in case of bounce */
2015-09-07 20:33:06 +00:00
print("bootmagic scan: ... ");
uint8_t scan = 100;
while (scan--) { matrix_scan(); wait_ms(10); }
2013-09-21 12:21:34 +00:00
print("done.\n");
/* bootmagic skip */
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_SKIP)) {
return;
}
/* eeconfig clear */
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
eeconfig_init();
#ifdef KEYMAP_IN_EEPROM_ENABLE
write_keymap_to_eeprom();
2014-07-24 06:03:58 +00:00
#endif
#ifdef LEDMAP_IN_EEPROM_ENABLE
write_ledmap_to_eeprom();
#endif
}
/* bootloader */
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_BOOTLOADER)) {
bootloader_jump();
}
Squashed 'tmk_core/' changes from ee8c5ba..d5c5ac6 d5c5ac6 Merge branch 'develop' 5957682 Merge branch 'hotfix-mediakey' a478c62 Merge branch 'hotfix-vusb' cccebfe Merge branch 'njbair-docfix' 0aaab57 Clean up wording in keymap example dc8bbc3 Clarify layer precedence 9e0b4c1 clarify layer documentation 915eb48 core: Fix media/consumer keys 88f90f3 Fix for VUSB configuration 3e290cd Fix including board.mk in chibios.mk 32c69e0 Merge branch 'newapi' into develop c9a56f9 Merge remote-tracking branch 'flabbergast/chibios' into develop 01e33ea Fix chibios and mbed common.mk for hook.c bea79d9 hook: Change func name of usb events 3e97536 hook: Change file and func names(*_hook -> hook_*) c286d8c Merge pull request #10 from fredizzimo/chibios-contrib2 062d74e Update ChibiOS instructions d47150f Add support for new version of ChibiOS and Contrib 62b5401 Chibios: disable LTO (link-time optimisation). c64e9aa hooks: Fix for LUFA 54e68b0 hooks: Remove led_restore_hook 325c09d Chibios: make the default bootloader_jump redefinable (weak). 078c722 Chibios: fix STM32_BOOTLOADER_ADDRESS name. e73cfe5 hooks: Fix for keyboard LED update e6120c5 Implement basic hooks. 7c370e9 Chibios: Update the main chibios README. 7f0198d Chibios: implement sleep LED for STM32. afef9b4 Fix hard-coded path of CHIBIOS 95c5b19 Merge pull request #7 from fredizzimo/sysvsize 27128a8 Sysv format for ChibiOS arm-none-eabi-size d4b8e68 core: Fix chibios user compile options b85d462 Merge branch 'chibios' of https://github.com/flabbergast/tmk_keyboard into flabbergast_chibios de41aa1 core: Fix ps2_mouse.c debug print d79d925 Removed duplicate debug message code and surrounded it with IFDEF as needed 8f28589 Chibios: Revert common.mk change (fix AVR linking problem). ec9eff2 Chibios: cleanup usb_main code. 28c4665 Chibios: Fix a HardFault bug (wait after start). git-subtree-dir: tmk_core git-subtree-split: d5c5ac63e60dfc6da6661a21bd968b4d577a27d5
2016-04-21 05:35:48 +00:00
/* user-defined checks */
hook_bootmagic();
/* debug enable */
debug_config.raw = eeconfig_read_debug();
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
debug_config.matrix = !debug_config.matrix;
2016-03-03 07:18:19 +00:00
} else if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
debug_config.keyboard = !debug_config.keyboard;
2016-03-03 07:18:19 +00:00
} else if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
debug_config.mouse = !debug_config.mouse;
} else {
debug_config.enable = !debug_config.enable;
}
}
eeconfig_write_debug(debug_config.raw);
2013-03-10 10:22:54 +00:00
/* keymap config */
keymap_config.raw = eeconfig_read_keymap();
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_NO_GUI)) {
keymap_config.no_gui = !keymap_config.no_gui;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
2013-03-10 10:22:54 +00:00
}
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_HOST_NKRO)) {
keymap_config.nkro = !keymap_config.nkro;
}
eeconfig_write_keymap(keymap_config.raw);
2013-06-22 07:14:56 +00:00
#ifdef NKRO_ENABLE
keyboard_nkro = keymap_config.nkro;
#endif
2013-06-22 07:14:56 +00:00
/* default layer */
uint8_t default_layer = 0;
2016-03-03 07:18:19 +00:00
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
2013-06-22 07:14:56 +00:00
if (default_layer) {
eeconfig_write_default_layer(default_layer);
default_layer_set((uint32_t)default_layer);
} else {
default_layer = eeconfig_read_default_layer();
default_layer_set((uint32_t)default_layer);
}
}
2016-03-03 07:18:19 +00:00
static bool scan_key(uint16_t code)
{
for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
matrix_row_t matrix_row = matrix_get_row(r);
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
if (matrix_row & ((matrix_row_t)1<<c)) {
2016-03-03 07:18:19 +00:00
// read key from Layer 0
action_t action = action_for_key(0, (keypos_t){ .row = r, .col = c });
if (action.code == code ||
((action.kind.id == ACT_LMODS ||
action.kind.id == ACT_RMODS ||
action.kind.id == ACT_LMODS_TAP ||
action.kind.id == ACT_RMODS_TAP ||
action.kind.id == ACT_LAYER_TAP ||
action.kind.id == ACT_LAYER_TAP_EXT) &&
action.key.code == code)) {
return true;
}
}
}
}
return false;
}
2016-03-03 07:18:19 +00:00
bool bootmagic_scan_key(uint16_t code)
{
2016-03-03 07:18:19 +00:00
if (!scan_key(BOOTMAGIC_KEY_SALT)) return false;
2016-03-03 07:18:19 +00:00
return scan_key(code);
}