diff options
Diffstat (limited to 'quantum/wpm.c')
| -rw-r--r-- | quantum/wpm.c | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/quantum/wpm.c b/quantum/wpm.c index da30bd252..bec419a48 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 | } |
