mirror of
https://github.com/Keychron/qmk_firmware.git
synced 2024-11-25 18:06:34 +06:00
Fix overflows in WPM calculations (#13128)
* 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. * Fix WPM being limited to 235 due to float/int logic.
This commit is contained in:
parent
f2fc23d1b1
commit
2b097d670a
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
#include "wpm.h"
|
#include "wpm.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
// WPM Stuff
|
// WPM Stuff
|
||||||
static uint8_t current_wpm = 0;
|
static uint8_t current_wpm = 0;
|
||||||
static uint16_t wpm_timer = 0;
|
static uint16_t wpm_timer = 0;
|
||||||
|
@ -69,14 +71,22 @@ __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) {
|
||||||
void update_wpm(uint16_t keycode) {
|
void update_wpm(uint16_t keycode) {
|
||||||
if (wpm_keycode(keycode)) {
|
if (wpm_keycode(keycode)) {
|
||||||
if (wpm_timer > 0) {
|
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 += ceilf((latest_wpm - current_wpm) * wpm_smoothing);
|
||||||
}
|
}
|
||||||
wpm_timer = timer_read();
|
wpm_timer = timer_read();
|
||||||
}
|
}
|
||||||
#ifdef WPM_ALLOW_COUNT_REGRESSION
|
#ifdef WPM_ALLOW_COUNT_REGRESSION
|
||||||
uint8_t regress = wpm_regress_count(keycode);
|
uint8_t regress = wpm_regress_count(keycode);
|
||||||
if (regress) {
|
if (regress) {
|
||||||
current_wpm -= regress;
|
if (current_wpm < regress) {
|
||||||
|
current_wpm = 0;
|
||||||
|
} else {
|
||||||
|
current_wpm -= regress;
|
||||||
|
}
|
||||||
wpm_timer = timer_read();
|
wpm_timer = timer_read();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user