summaryrefslogtreecommitdiffstats
path: root/quantum/mousekey.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/mousekey.c')
-rw-r--r--quantum/mousekey.c68
1 files changed, 41 insertions, 27 deletions
diff --git a/quantum/mousekey.c b/quantum/mousekey.c
index df8aa613be..c982a2f40b 100644
--- a/quantum/mousekey.c
+++ b/quantum/mousekey.c
@@ -25,11 +25,16 @@
#include "mousekey.h"
static inline int8_t times_inv_sqrt2(int8_t x) {
- // 181/256 is pretty close to 1/sqrt(2)
- // 0.70703125 0.707106781
- // 1 too small for x=99 and x=198
- // This ends up being a mult and discard lower 8 bits
- return (x * 181) >> 8;
+ // 181/256 (0.70703125) is used as an approximation for 1/sqrt(2)
+ // because it is close to the exact value which is 0.707106781
+ const int16_t n = x * 181;
+ const uint16_t d = 256;
+
+ // To ensure that the integer result is rounded accurately after
+ // division, check the sign of the numerator:
+ // If negative, subtract half of the denominator before dividing
+ // Otherwise, add half of the denominator before dividing
+ return n < 0 ? (n - d / 2) / d : (n + d / 2) / d;
}
static report_mouse_t mouse_report = {0};
@@ -74,7 +79,7 @@ uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;
/* milliseconds between repeated motion events (0-255) */
# ifdef MK_KINETIC_SPEED
-float mk_wheel_interval = 1000.0f / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
+uint16_t mk_wheel_interval = 1000U / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
# else
uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL;
# endif
@@ -175,7 +180,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
@@ -190,39 +195,48 @@ const uint16_t mk_decelerated_speed = MOUSEKEY_DECELERATED_SPEED;
const uint16_t mk_initial_speed = MOUSEKEY_INITIAL_SPEED;
static uint8_t move_unit(void) {
- float speed = mk_initial_speed;
+ 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 float 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;
+ const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
+ 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 / (1000.0f / mk_interval));
- speed = speed < 1 ? 1 : speed;
+ speed = (uint8_t)(speed / (1000U / mk_interval));
- 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) {
- float speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
+ 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 float time_elapsed = timer_elapsed(mouse_timer) / 50;
- speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + 1 * 0.5 * time_elapsed * time_elapsed;
+ const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
+ 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 = 1000.0f / speed;
-
- return (uint8_t)speed > MOUSEKEY_WHEEL_INITIAL_MOVEMENTS ? 2 : 1;
+ mk_wheel_interval = 1000U / speed;
+ return 1;
}
# endif /* #ifndef MK_KINETIC_SPEED */