diff options
Diffstat (limited to 'quantum/wpm.c')
-rw-r--r-- | quantum/wpm.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/quantum/wpm.c b/quantum/wpm.c new file mode 100644 index 000000000..d4c971f31 --- /dev/null +++ b/quantum/wpm.c | |||
@@ -0,0 +1,67 @@ | |||
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 | |||
20 | //WPM Stuff | ||
21 | static uint8_t current_wpm = 0; | ||
22 | static uint8_t latest_wpm = 0; | ||
23 | static uint16_t wpm_timer = 0; | ||
24 | |||
25 | //This smoothing is 40 keystrokes | ||
26 | static const float wpm_smoothing = 0.0487; | ||
27 | |||
28 | void set_current_wpm(uint8_t new_wpm) { current_wpm = new_wpm; } | ||
29 | |||
30 | uint8_t get_current_wpm(void) { return current_wpm; } | ||
31 | |||
32 | bool wpm_keycode(uint16_t keycode) { return wpm_keycode_kb(keycode); } | ||
33 | |||
34 | __attribute__((weak)) bool wpm_keycode_kb(uint16_t keycode) { return wpm_keycode_user(keycode); } | ||
35 | |||
36 | __attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) { | ||
37 | |||
38 | 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)) { | ||
39 | keycode = keycode & 0xFF; | ||
40 | } else if (keycode > 0xFF) { | ||
41 | keycode = 0; | ||
42 | } | ||
43 | if((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) { | ||
44 | return true; | ||
45 | } | ||
46 | |||
47 | return false; | ||
48 | } | ||
49 | |||
50 | |||
51 | void update_wpm(uint16_t keycode) { | ||
52 | if(wpm_keycode(keycode)) { | ||
53 | if(wpm_timer > 0) { | ||
54 | latest_wpm = 60000 / timer_elapsed(wpm_timer) / 5; | ||
55 | current_wpm = (latest_wpm - current_wpm) * wpm_smoothing + current_wpm; | ||
56 | } | ||
57 | wpm_timer = timer_read(); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | void decay_wpm(void) { | ||
62 | if (timer_elapsed(wpm_timer) > 1000) { | ||
63 | current_wpm = (0 - current_wpm) * wpm_smoothing + | ||
64 | current_wpm; | ||
65 | wpm_timer = timer_read(); | ||
66 | } | ||
67 | } | ||