Skip to content

Commit

Permalink
Use division by integer for kinetic mouse key
Browse files Browse the repository at this point in the history
Replace multiplication of fractional decimal numbers with
division by integer to avoid floating point operations.
This change will result with smaller compiled firmware size.
  • Loading branch information
filterpaper committed Jun 9, 2023
1 parent 5427494 commit 8db030d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 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 @@ -196,7 +196,7 @@ static uint8_t move_unit(void) {
speed = mousekey_accel & (1 << 2) ? mk_accelerated_speed : mk_decelerated_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 = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + (MOUSEKEY_MOVE_DELTA * time_elapsed * time_elapsed) / 2;

speed = speed > mk_base_speed ? mk_base_speed : speed;
}
Expand All @@ -216,7 +216,7 @@ static uint8_t wheel_unit(void) {
} 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;
}
speed = speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS ? MOUSEKEY_WHEEL_BASE_MOVEMENTS : speed;
}
Expand Down

0 comments on commit 8db030d

Please sign in to comment.