From b5f01e38f4fb477dbeea81b5328ef91e9fb3821d Mon Sep 17 00:00:00 2001 From: Joakim Tufvegren Date: Mon, 7 Jun 2021 10:50:56 +0200 Subject: [PATCH 1/2] Fix overflow in WPM calculations. First, the "fresh" WPM calculation could end up being up to 12000 (with default `WPM_ESTIMATED_WORD_SIZE`) if keys were pressed more or less simultaneously. This value has now been clamped down to 255, in effect clamping WPM to its max value of 255. Second, with `WPM_ALLOW_COUNT_REGRESSION` enabled, it was possible to regress the WPM below 0 (i.e. to 255) by just repeatedly pressing backspace. --- quantum/wpm.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/quantum/wpm.c b/quantum/wpm.c index bec419a48e57..e6e2d09bd260 100644 --- a/quantum/wpm.c +++ b/quantum/wpm.c @@ -69,14 +69,22 @@ __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) { void update_wpm(uint16_t keycode) { if (wpm_keycode(keycode)) { if (wpm_timer > 0) { - current_wpm += ((60000 / timer_elapsed(wpm_timer) / WPM_ESTIMATED_WORD_SIZE) - current_wpm) * wpm_smoothing; + uint16_t latest_wpm = 60000 / timer_elapsed(wpm_timer) / WPM_ESTIMATED_WORD_SIZE; + if (latest_wpm > UINT8_MAX) { + latest_wpm = UINT8_MAX; + } + current_wpm += (latest_wpm - current_wpm) * wpm_smoothing; } wpm_timer = timer_read(); } #ifdef WPM_ALLOW_COUNT_REGRESSION uint8_t regress = wpm_regress_count(keycode); if (regress) { - current_wpm -= regress; + if (current_wpm < regress) { + current_wpm = 0; + } else { + current_wpm -= regress; + } wpm_timer = timer_read(); } #endif From 26f51e6554df5f10678f77f599b39490feeb7726 Mon Sep 17 00:00:00 2001 From: Joakim Tufvegren Date: Sun, 20 Jun 2021 10:55:39 +0200 Subject: [PATCH 2/2] Fix WPM being limited to 235 due to float/int logic. --- quantum/wpm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/quantum/wpm.c b/quantum/wpm.c index e6e2d09bd260..e711e9fe73e0 100644 --- a/quantum/wpm.c +++ b/quantum/wpm.c @@ -17,6 +17,8 @@ #include "wpm.h" +#include + // WPM Stuff static uint8_t current_wpm = 0; static uint16_t wpm_timer = 0; @@ -73,7 +75,7 @@ void update_wpm(uint16_t keycode) { if (latest_wpm > UINT8_MAX) { latest_wpm = UINT8_MAX; } - current_wpm += (latest_wpm - current_wpm) * wpm_smoothing; + current_wpm += ceilf((latest_wpm - current_wpm) * wpm_smoothing); } wpm_timer = timer_read(); }