aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/common/bootmagic.h
blob: c64dc17858448163b9d9b6291e95b791534d838c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef BOOTMAGIC_H
#define BOOTMAGIC_H

/* FIXME: Add special doxygen comments for defines here. */

/* bootmagic salt key */
#ifndef BOOTMAGIC_KEY_SALT
#    define BOOTMAGIC_KEY_SALT KC_SPACE
#endif

/* skip bootmagic and eeconfig */
#ifndef BOOTMAGIC_KEY_SKIP
#    define BOOTMAGIC_KEY_SKIP KC_ESC
#endif

/* eeprom clear */
#ifndef BOOTMAGIC_KEY_EEPROM_CLEAR
#    define BOOTMAGIC_KEY_EEPROM_CLEAR KC_BSPACE
#endif

/* kick up bootloader */
#ifndef BOOTMAGIC_KEY_BOOTLOADER
#    define BOOTMAGIC_KEY_BOOTLOADER KC_B
#endif

/* debug enable */
#ifndef BOOTMAGIC_KEY_DEBUG_ENABLE
#    define BOOTMAGIC_KEY_DEBUG_ENABLE KC_D
#endif
#ifndef BOOTMAGIC_KEY_DEBUG_MATRIX
#    define BOOTMAGIC_KEY_DEBUG_MATRIX KC_X
#endif
#ifndef BOOTMAGIC_KEY_DEBUG_KEYBOARD
#    define BOOTMAGIC_KEY_DEBUG_KEYBOARD KC_K
#endif
#ifndef BOOTMAGIC_KEY_DEBUG_MOUSE
#    define BOOTMAGIC_KEY_DEBUG_MOUSE KC_M
#endif
#ifndef BOOTMAGIC_KEY_EE_HANDS_LEFT
#    define BOOTMAGIC_KEY_EE_HANDS_LEFT KC_L
#endif
#ifndef BOOTMAGIC_KEY_EE_HANDS_RIGHT
#    define BOOTMAGIC_KEY_EE_HANDS_RIGHT KC_R
#endif

/*
 * keymap config
 */
#ifndef BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK
#    define BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK KC_LCTRL
#endif
#ifndef BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL
#    define BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL KC_CAPSLOCK
#endif
#ifndef BOOTMAGIC_KEY_SWAP_LALT_LGUI
#    define BOOTMAGIC_KEY_SWAP_LALT_LGUI KC_LALT
#endif
#ifndef BOOTMAGIC_KEY_SWAP_RALT_RGUI
#    define BOOTMAGIC_KEY_SWAP_RALT_RGUI KC_RALT
#endif
#ifndef BOOTMAGIC_KEY_NO_GUI
#    define BOOTMAGIC_KEY_NO_GUI KC_LGUI
#endif
#ifndef BOOTMAGIC_KEY_SWAP_GRAVE_ESC
#    define BOOTMAGIC_KEY_SWAP_GRAVE_ESC KC_GRAVE
#endif
#ifndef BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE
#    define BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE KC_BSLASH
#endif
#ifndef BOOTMAGIC_HOST_NKRO
#    define BOOTMAGIC_HOST_NKRO KC_N
#endif

/*
 * change default layer
 */
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_0
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_0 KC_0
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_1
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_1 KC_1
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_2
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_2 KC_2
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_3
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_3 KC_3
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_4
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_4 KC_4
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_5
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_5 KC_5
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_6
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_6 KC_6
#endif
#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_7
#    define BOOTMAGIC_KEY_DEFAULT_LAYER_7 KC_7
#endif

void bootmagic(void);
bool bootmagic_scan_keycode(uint8_t keycode);

#endif
void); /* * Mouse keys acceleration algorithm * http://en.wikipedia.org/wiki/Mouse_keys * * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000) */ /* milliseconds between the initial key press and first repeated motion event (0-2550) */ uint8_t mk_delay = MOUSEKEY_DELAY/10; /* milliseconds between repeated motion events (0-255) */ uint8_t mk_interval = MOUSEKEY_INTERVAL; /* steady speed (in action_delta units) applied each event (0-255) */ uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED; /* number of events (count) accelerating to steady speed (0-255) */ uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX; /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */ //int8_t mk_curve = 0; /* wheel params */ uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; static uint16_t last_timer = 0; static uint8_t move_unit(void) { uint16_t unit; if (mousekey_accel & (1<<0)) { unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4; } else if (mousekey_accel & (1<<1)) { unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2; } else if (mousekey_accel & (1<<2)) { unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed); } else if (mousekey_repeat == 0) { unit = MOUSEKEY_MOVE_DELTA; } else if (mousekey_repeat >= mk_time_to_max) { unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; } else { unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; } return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); } static uint8_t wheel_unit(void) { uint16_t unit; if (mousekey_accel & (1<<0)) { unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4; } else if (mousekey_accel & (1<<1)) { unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2; } else if (mousekey_accel & (1<<2)) { unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed); } else if (mousekey_repeat == 0) { unit = MOUSEKEY_WHEEL_DELTA; } else if (mousekey_repeat >= mk_wheel_time_to_max) { unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; } else { unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; } return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); } void mousekey_task(void) { if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10)) return; if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) return; if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; if (mouse_report.x > 0) mouse_report.x = move_unit(); if (mouse_report.x < 0) mouse_report.x = move_unit() * -1; if (mouse_report.y > 0) mouse_report.y = move_unit(); if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; /* diagonal move [1/sqrt(2) = 0.7] */ if (mouse_report.x && mouse_report.y) { mouse_report.x *= 0.7; mouse_report.y *= 0.7; } if (mouse_report.v > 0) mouse_report.v = wheel_unit(); if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1; if (mouse_report.h > 0) mouse_report.h = wheel_unit(); if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1; mousekey_send(); } void mousekey_on(uint8_t code) { if (code == KC_MS_UP) mouse_report.y = move_unit() * -1; else if (code == KC_MS_DOWN) mouse_report.y = move_unit(); else if (code == KC_MS_LEFT) mouse_report.x = move_unit() * -1; else if (code == KC_MS_RIGHT) mouse_report.x = move_unit(); else if (code == KC_MS_WH_UP) mouse_report.v = wheel_unit(); else if (code == KC_MS_WH_DOWN) mouse_report.v = wheel_unit() * -1; else if (code == KC_MS_WH_LEFT) mouse_report.h = wheel_unit() * -1; else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit(); else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; else if (code == KC_MS_ACCEL0) mousekey_accel |= (1<<0); else if (code == KC_MS_ACCEL1) mousekey_accel |= (1<<1); else if (code == KC_MS_ACCEL2) mousekey_accel |= (1<<2); } void mousekey_off(uint8_t code) { if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0); else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1); else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2); if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) mousekey_repeat = 0; } void mousekey_send(void) { mousekey_debug(); host_mouse_send(&mouse_report); last_timer = timer_read(); } void mousekey_clear(void) { mouse_report = (report_mouse_t){}; mousekey_repeat = 0; mousekey_accel = 0; } static void mousekey_debug(void) { if (!debug_mouse) return; print("mousekey [btn|x y v h](rep/acl): ["); phex(mouse_report.buttons); print("|"); print_decs(mouse_report.x); print(" "); print_decs(mouse_report.y); print(" "); print_decs(mouse_report.v); print(" "); print_decs(mouse_report.h); print("]("); print_dec(mousekey_repeat); print("/"); print_dec(mousekey_accel); print(")\n"); }