Skip to content

Commit

Permalink
Refactor kinetic mouse key feature (qmk#21164)
Browse files Browse the repository at this point in the history
  • Loading branch information
filterpaper authored and csolje committed Oct 21, 2023
1 parent c3823bd commit 2a348ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/feature_mouse_keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ This is an extension of the accelerated mode. The kinetic mode uses a quadratic
Tips:

* The smoothness of the cursor movement depends on the `MOUSEKEY_INTERVAL` setting. The shorter the interval is set the smoother the movement will be. Setting the value too low makes the cursor unresponsive. Lower settings are possible if the micro processor is fast enough. For example: At an interval of `8` milliseconds, `125` movements per second will be initiated. With a base speed of `1000` each movement will move the cursor by `8` pixels.
* Mouse wheel movements are implemented differently from cursor movements. While it's okay for the cursor to move multiple pixels at once for the mouse wheel this would lead to jerky movements. Instead, the mouse wheel operates at step size `2`. Setting mouse wheel speed is done by adjusting the number of wheel movements per second.
* Mouse wheel movements are implemented differently from cursor movements. While it's okay for the cursor to move multiple pixels at once for the mouse wheel this would lead to jerky movements. Instead, the mouse wheel operates at step size `1`. Setting mouse wheel speed is done by adjusting the number of wheel movements per second.

### Constant mode

Expand Down
39 changes: 24 additions & 15 deletions quantum/mousekey.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static uint8_t wheel_unit(void) {
/*
* Kinetic movement acceleration algorithm
*
* current speed = I + A * T/50 + A * 0.5 * T^2 | maximum B
* current speed = I + A * T/50 + A * (T/50)^2 * 1/2 | maximum B
*
* T: time since the mouse movement started
* E: mouse events per second (set through MOUSEKEY_INTERVAL, UHK sends 250, the
Expand All @@ -192,37 +192,46 @@ const uint16_t mk_initial_speed = MOUSEKEY_INITIAL_SPEED;
static uint8_t move_unit(void) {
uint16_t speed = mk_initial_speed;

if (mousekey_accel & ((1 << 0) | (1 << 2))) {
speed = mousekey_accel & (1 << 2) ? mk_accelerated_speed : mk_decelerated_speed;
if (mousekey_accel & (1 << 0)) {
speed = mk_decelerated_speed;
} else if (mousekey_accel & (1 << 2)) {
speed = mk_accelerated_speed;
} else if (mousekey_repeat && mouse_timer) {
const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
speed = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + MOUSEKEY_MOVE_DELTA * 0.5 * time_elapsed * time_elapsed;

speed = speed > mk_base_speed ? mk_base_speed : speed;
speed = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + (MOUSEKEY_MOVE_DELTA * time_elapsed * time_elapsed) / 2;
if (speed > mk_base_speed) {
speed = mk_base_speed;
}
}

/* convert speed to USB mouse speed 1 to 127 */
speed = (uint8_t)(speed / (1000U / mk_interval));
speed = speed < 1 ? 1 : speed;

return speed > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : speed;
if (speed > MOUSEKEY_MOVE_MAX) {
speed = MOUSEKEY_MOVE_MAX;
} else if (speed < 1) {
speed = 1;
}
return speed;
}

static uint8_t wheel_unit(void) {
uint16_t speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;

if (mousekey_accel & ((1 << 0) | (1 << 2))) {
speed = mousekey_accel & (1 << 2) ? MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS : MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS;
if (mousekey_accel & (1 << 0)) {
speed = MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS;
} else if (mousekey_accel & (1 << 2)) {
speed = MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS;
} else if (mousekey_wheel_repeat && mouse_timer) {
if (mk_wheel_interval != MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + 1 * 0.5 * time_elapsed * time_elapsed;
speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + (1 * time_elapsed * time_elapsed) / 2;
}
if (speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
speed = MOUSEKEY_WHEEL_BASE_MOVEMENTS;
}
speed = speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS ? MOUSEKEY_WHEEL_BASE_MOVEMENTS : speed;
}
mk_wheel_interval = 1000U / speed;

return (uint8_t)speed > MOUSEKEY_WHEEL_INITIAL_MOVEMENTS ? 2 : 1;
return 1;
}

# endif /* #ifndef MK_KINETIC_SPEED */
Expand Down

0 comments on commit 2a348ee

Please sign in to comment.