diff --git a/common/mousekey.c b/common/mousekey.c index 6fe8e2d2..4b1fe174 100644 --- a/common/mousekey.c +++ b/common/mousekey.c @@ -32,41 +32,87 @@ static uint8_t mousekey_repeat = 0; static void mousekey_debug(void); -/* - * TODO: fix acceleration algorithm - * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys - */ -#ifndef MOUSEKEY_DELAY_TIME -# define MOUSEKEY_DELAY_TIME 100 +/* max value on report descriptor */ +#define MOUSEKEY_MOVE_MAX 127 +#define MOUSEKEY_WHEEL_MAX 15 + +#ifndef MOUSEKEY_MOVE_DELTA +#define MOUSEKEY_MOVE_DELTA 5 +#endif +#ifndef MOUSEKEY_WHEEL_DELTA +#define MOUSEKEY_WHEEL_DELTA 1 +#endif +#ifndef MOUSEKEY_DELAY +#define MOUSEKEY_DELAY 300 +#endif +#ifndef MOUSEKEY_INTERVAL +#define MOUSEKEY_INTERVAL 50 +#endif +#ifndef MOUSEKEY_MAX_SPEED +#define MOUSEKEY_MAX_SPEED 10 +#endif +#ifndef MOUSEKEY_TIME_TO_MAX +#define MOUSEKEY_TIME_TO_MAX 20 +#endif +#ifndef MOUSEKEY_WHEEL_MAX_SPEED +#define MOUSEKEY_WHEEL_MAX_SPEED 8 +#endif +#ifndef MOUSEKEY_WHEEL_TIME_TO_MAX +#define MOUSEKEY_WHEEL_TIME_TO_MAX 40 #endif -#define MOUSEKEY_MOVE_INIT 5 -#define MOUSEKEY_WHEEL_INIT 1 -#define MOUSEKEY_MOVE_ACCEL 5 -#define MOUSEKEY_WHEEL_ACCEL 1 + +/* + * 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) */ +static uint8_t mk_delay = MOUSEKEY_DELAY/10; +/* milliseconds between repeated motion events (0-255) */ +static uint8_t mk_interval = MOUSEKEY_INTERVAL; +/* steady speed (in action_delta units) applied each event (0-255) */ +static uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED; +/* number of events (count) accelerating to steady speed (0-255) */ +static uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX; +/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */ +//static int8_t mk_curve = 0; + +static uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; +static uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; + static uint16_t last_timer = 0; -// acceleration parameters -//uint8_t mousekey_move_unit = 2; -//uint8_t mousekey_resolution = 5; - -static inline uint8_t move_unit(void) +static uint8_t move_unit(void) { - uint16_t unit = MOUSEKEY_MOVE_INIT + MOUSEKEY_MOVE_ACCEL * mousekey_repeat; - return (unit > 63 ? 63 : unit); + uint16_t unit; + 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; + } + if (unit == 0) return 1; + return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : unit); } -static inline uint8_t wheel_unit(void) +static uint8_t wheel_unit(void) { - uint16_t unit = MOUSEKEY_WHEEL_INIT + MOUSEKEY_WHEEL_ACCEL * mousekey_repeat; - return (unit > 15 ? 15 : unit); + uint16_t unit; + if (mousekey_repeat > mk_time_to_max) { + unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; + } else { + unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_time_to_max; + } + if (unit == 0) return 1; + return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : unit); } void mousekey_task(void) { - if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME) + if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10)) return; if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) @@ -96,14 +142,14 @@ void mousekey_task(void) void mousekey_on(uint8_t code) { - if (code == KC_MS_UP) report.y = MOUSEKEY_MOVE_INIT * -1; - else if (code == KC_MS_DOWN) report.y = MOUSEKEY_MOVE_INIT; - else if (code == KC_MS_LEFT) report.x = MOUSEKEY_MOVE_INIT * -1; - else if (code == KC_MS_RIGHT) report.x = MOUSEKEY_MOVE_INIT; - else if (code == KC_MS_WH_UP) report.v = MOUSEKEY_WHEEL_INIT; - else if (code == KC_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_INIT * -1; - else if (code == KC_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_INIT * -1; - else if (code == KC_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_INIT; + if (code == KC_MS_UP) report.y = MOUSEKEY_MOVE_DELTA * -1; + else if (code == KC_MS_DOWN) report.y = MOUSEKEY_MOVE_DELTA; + else if (code == KC_MS_LEFT) report.x = MOUSEKEY_MOVE_DELTA * -1; + else if (code == KC_MS_RIGHT) report.x = MOUSEKEY_MOVE_DELTA; + else if (code == KC_MS_WH_UP) report.v = MOUSEKEY_WHEEL_DELTA; + else if (code == KC_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_DELTA * -1; + else if (code == KC_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_DELTA * -1; + else if (code == KC_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_DELTA; else if (code == KC_MS_BTN1) report.buttons |= MOUSE_BTN1; else if (code == KC_MS_BTN2) report.buttons |= MOUSE_BTN2; else if (code == KC_MS_BTN3) report.buttons |= MOUSE_BTN3;