wpm.c
6226 bytes
1/*
2 * Copyright 2020 Richard Sutherland (rich@brickbots.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "wpm.h"
19#include "timer.h"
20#include "keycode.h"
21#include "quantum_keycodes.h"
22#include "action_util.h"
23#include <math.h>
24
25// WPM Stuff
26static uint8_t current_wpm = 0;
27static uint32_t wpm_timer = 0;
28
29/* The WPM calculation works by specifying a certain number of 'periods' inside
30 * a ring buffer, and we count the number of keypresses which occur in each of
31 * those periods. Then to calculate WPM, we add up all of the keypresses in
32 * the whole ring buffer, divide by the number of keypresses in a 'word', and
33 * then adjust for how much time is captured by our ring buffer. The size
34 * of the ring buffer can be configured using the keymap configuration
35 * value `WPM_SAMPLE_PERIODS`.
36 *
37 */
38#define MAX_PERIODS (WPM_SAMPLE_PERIODS)
39#define PERIOD_DURATION (1000 * WPM_SAMPLE_SECONDS / MAX_PERIODS)
40
41static int16_t period_presses[MAX_PERIODS] = {0};
42static uint8_t current_period = 0;
43static uint8_t periods = 1;
44
45#if !defined(WPM_UNFILTERED)
46/* LATENCY is used as part of filtering, and controls how quickly the reported
47 * WPM trails behind our actual instantaneous measured WPM value, and is
48 * defined in milliseconds. So for LATENCY == 100, the displayed WPM is
49 * smoothed out over periods of 0.1 seconds. This results in a nice,
50 * smoothly-moving reported WPM value which nevertheless is never more than
51 * 0.1 seconds behind the typist's actual current WPM.
52 *
53 * LATENCY is not used if WPM_UNFILTERED is defined.
54 */
55# define LATENCY (100)
56static uint32_t smoothing_timer = 0;
57static uint8_t prev_wpm = 0;
58static uint8_t next_wpm = 0;
59#endif
60
61void set_current_wpm(uint8_t new_wpm) {
62 current_wpm = new_wpm;
63}
64uint8_t get_current_wpm(void) {
65 return current_wpm;
66}
67
68bool wpm_keycode(uint16_t keycode) {
69 return wpm_keycode_kb(keycode);
70}
71
72__attribute__((weak)) bool wpm_keycode_kb(uint16_t keycode) {
73 return wpm_keycode_user(keycode);
74}
75
76__attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) {
77 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)) {
78 keycode = keycode & 0xFF;
79 } else if (keycode > 0xFF) {
80 keycode = 0;
81 }
82 if ((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) {
83 return true;
84 }
85
86 return false;
87}
88
89#if defined(WPM_ALLOW_COUNT_REGRESSION)
90__attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) {
91 bool weak_modded = (keycode >= QK_LCTL && keycode < QK_LSFT) || (keycode >= QK_RCTL && keycode < QK_RSFT);
92
93 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)) {
94 keycode = keycode & 0xFF;
95 } else if (keycode > 0xFF) {
96 keycode = 0;
97 }
98 if (keycode == KC_DELETE || keycode == KC_BACKSPACE) {
99 if (((get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) || weak_modded) {
100 return WPM_ESTIMATED_WORD_SIZE;
101 } else {
102 return 1;
103 }
104 } else {
105 return 0;
106 }
107}
108#endif
109
110// Outside 'raw' mode we smooth results over time.
111
112void update_wpm(uint16_t keycode) {
113 if (wpm_keycode(keycode) && period_presses[current_period] < INT16_MAX) {
114 period_presses[current_period]++;
115 }
116#if defined(WPM_ALLOW_COUNT_REGRESSION)
117 uint8_t regress = wpm_regress_count(keycode);
118 if (regress && period_presses[current_period] > INT16_MIN) {
119 period_presses[current_period]--;
120 }
121#endif
122}
123
124void decay_wpm(void) {
125 int32_t presses = period_presses[0];
126 for (int i = 1; i <= periods; i++) {
127 presses += period_presses[i];
128 }
129 if (presses < 0) {
130 presses = 0;
131 }
132 int32_t elapsed = timer_elapsed32(wpm_timer);
133 uint32_t duration = (((periods)*PERIOD_DURATION) + elapsed);
134 int32_t wpm_now = (60000 * presses) / (duration * WPM_ESTIMATED_WORD_SIZE);
135
136 if (wpm_now < 0) // set some reasonable WPM measurement limits
137 wpm_now = 0;
138 if (wpm_now > 240) wpm_now = 240;
139
140 if (elapsed > PERIOD_DURATION) {
141 current_period = (current_period + 1) % MAX_PERIODS;
142 period_presses[current_period] = 0;
143 periods = (periods < MAX_PERIODS - 1) ? periods + 1 : MAX_PERIODS - 1;
144 elapsed = 0;
145 wpm_timer = timer_read32();
146 }
147 if (presses < 2) // don't guess high WPM based on a single keypress.
148 wpm_now = 0;
149
150#if defined(WPM_LAUNCH_CONTROL)
151 /*
152 * If the `WPM_LAUNCH_CONTROL` option is enabled, then whenever our WPM
153 * drops to absolute zero due to no typing occurring within our sample
154 * ring buffer, we reset and start measuring fresh, which lets our WPM
155 * immediately reach the correct value even before a full sampling buffer
156 * has been filled.
157 */
158 if (presses == 0) {
159 current_period = 0;
160 periods = 0;
161 wpm_now = 0;
162 period_presses[0] = 0;
163 }
164#endif // WPM_LAUNCH_CONTROL
165
166#if defined(WPM_UNFILTERED)
167 current_wpm = wpm_now;
168#else
169 int32_t latency = timer_elapsed32(smoothing_timer);
170 if (latency > LATENCY) {
171 smoothing_timer = timer_read32();
172 prev_wpm = current_wpm;
173 next_wpm = wpm_now;
174 }
175
176 current_wpm = prev_wpm + (latency * ((int)next_wpm - (int)prev_wpm) / LATENCY);
177#endif
178}