diff options
| author | Drashna Jaelre <drashna@live.com> | 2021-04-18 23:26:37 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-18 23:26:37 -0700 |
| commit | 180a32ec59339cf07323412457ec771fba69ea61 (patch) | |
| tree | a3f59e56d06926608499ccba11ea318d53add529 /quantum | |
| parent | f65a5d2fb5f0e48d0b0cf7a5198da32129fbb52e (diff) | |
| download | qmk_firmware-180a32ec59339cf07323412457ec771fba69ea61.tar.gz qmk_firmware-180a32ec59339cf07323412457ec771fba69ea61.zip | |
Enhancement of WPM feature (#11727)
Diffstat (limited to 'quantum')
| -rw-r--r-- | quantum/wpm.c | 36 | ||||
| -rw-r--r-- | quantum/wpm.h | 12 |
2 files changed, 43 insertions, 5 deletions
diff --git a/quantum/wpm.c b/quantum/wpm.c index da30bd252..c44b1172b 100644 --- a/quantum/wpm.c +++ b/quantum/wpm.c | |||
| @@ -19,11 +19,10 @@ | |||
| 19 | 19 | ||
| 20 | // WPM Stuff | 20 | // WPM Stuff |
| 21 | static uint8_t current_wpm = 0; | 21 | static uint8_t current_wpm = 0; |
| 22 | static uint8_t latest_wpm = 0; | ||
| 23 | static uint16_t wpm_timer = 0; | 22 | static uint16_t wpm_timer = 0; |
| 24 | 23 | ||
| 25 | // This smoothing is 40 keystrokes | 24 | // This smoothing is 40 keystrokes |
| 26 | static const float wpm_smoothing = 0.0487; | 25 | static const float wpm_smoothing = WPM_SMOOTHING; |
| 27 | 26 | ||
| 28 | void set_current_wpm(uint8_t new_wpm) { current_wpm = new_wpm; } | 27 | void set_current_wpm(uint8_t new_wpm) { current_wpm = new_wpm; } |
| 29 | 28 | ||
| @@ -46,19 +45,46 @@ __attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) { | |||
| 46 | return false; | 45 | return false; |
| 47 | } | 46 | } |
| 48 | 47 | ||
| 48 | #ifdef WPM_ALLOW_COUNT_REGRESSION | ||
| 49 | __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) { | ||
| 50 | bool weak_modded = (keycode >= QK_LCTL && keycode < QK_LSFT) || (keycode >= QK_RCTL && keycode < QK_RSFT); | ||
| 51 | |||
| 52 | if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX) || (keycode >= QK_MODS && keycode <= QK_MODS_MAX)) { | ||
| 53 | keycode = keycode & 0xFF; | ||
| 54 | } else if (keycode > 0xFF) { | ||
| 55 | keycode = 0; | ||
| 56 | } | ||
| 57 | if (keycode == KC_DEL || keycode == KC_BSPC) { | ||
| 58 | if (((get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) || weak_modded) { | ||
| 59 | return WPM_ESTIMATED_WORD_SIZE; | ||
| 60 | } else { | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | } else { | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | #endif | ||
| 68 | |||
| 49 | void update_wpm(uint16_t keycode) { | 69 | void update_wpm(uint16_t keycode) { |
| 50 | if (wpm_keycode(keycode)) { | 70 | if (wpm_keycode(keycode)) { |
| 51 | if (wpm_timer > 0) { | 71 | if (wpm_timer > 0) { |
| 52 | latest_wpm = 60000 / timer_elapsed(wpm_timer) / 5; | 72 | current_wpm += ((60000 / timer_elapsed(wpm_timer) / WPM_ESTIMATED_WORD_SIZE) - current_wpm) * wpm_smoothing; |
| 53 | current_wpm = (latest_wpm - current_wpm) * wpm_smoothing + current_wpm; | ||
| 54 | } | 73 | } |
| 55 | wpm_timer = timer_read(); | 74 | wpm_timer = timer_read(); |
| 56 | } | 75 | } |
| 76 | #ifdef WPM_ALLOW_COUNT_REGRESSION | ||
| 77 | uint8_t regress = wpm_regress_count(keycode); | ||
| 78 | if (regress) { | ||
| 79 | current_wpm -= regress; | ||
| 80 | wpm_timer = timer_read(); | ||
| 81 | } | ||
| 82 | #endif | ||
| 57 | } | 83 | } |
| 58 | 84 | ||
| 59 | void decay_wpm(void) { | 85 | void decay_wpm(void) { |
| 60 | if (timer_elapsed(wpm_timer) > 1000) { | 86 | if (timer_elapsed(wpm_timer) > 1000) { |
| 61 | current_wpm = (0 - current_wpm) * wpm_smoothing + current_wpm; | 87 | current_wpm += (-current_wpm) * wpm_smoothing; |
| 62 | wpm_timer = timer_read(); | 88 | wpm_timer = timer_read(); |
| 63 | } | 89 | } |
| 64 | } | 90 | } |
diff --git a/quantum/wpm.h b/quantum/wpm.h index 15ab4ffcd..079401eb4 100644 --- a/quantum/wpm.h +++ b/quantum/wpm.h | |||
| @@ -19,10 +19,22 @@ | |||
| 19 | 19 | ||
| 20 | #include "quantum.h" | 20 | #include "quantum.h" |
| 21 | 21 | ||
| 22 | |||
| 23 | #ifndef WPM_ESTIMATED_WORD_SIZE | ||
| 24 | # define WPM_ESTIMATED_WORD_SIZE 5 | ||
| 25 | #endif | ||
| 26 | #ifndef WPM_SMOOTHING | ||
| 27 | # define WPM_SMOOTHING 0.0487 | ||
| 28 | #endif | ||
| 29 | |||
| 22 | bool wpm_keycode(uint16_t keycode); | 30 | bool wpm_keycode(uint16_t keycode); |
| 23 | bool wpm_keycode_kb(uint16_t keycode); | 31 | bool wpm_keycode_kb(uint16_t keycode); |
| 24 | bool wpm_keycode_user(uint16_t keycode); | 32 | bool wpm_keycode_user(uint16_t keycode); |
| 25 | 33 | ||
| 34 | #ifdef WPM_ALLOW_COUNT_REGRESSION | ||
| 35 | uint8_t wpm_regress_count(uint16_t keycode); | ||
| 36 | #endif | ||
| 37 | |||
| 26 | void set_current_wpm(uint8_t); | 38 | void set_current_wpm(uint8_t); |
| 27 | uint8_t get_current_wpm(void); | 39 | uint8_t get_current_wpm(void); |
| 28 | void update_wpm(uint16_t); | 40 | void update_wpm(uint16_t); |
