wpm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "wpm.h"
  18. #include "timer.h"
  19. #include "keycode.h"
  20. #include "quantum_keycodes.h"
  21. #include "action_util.h"
  22. #include <math.h>
  23. // WPM Stuff
  24. static uint8_t current_wpm = 0;
  25. static uint32_t wpm_timer = 0;
  26. /* The WPM calculation works by specifying a certain number of 'periods' inside
  27. * a ring buffer, and we count the number of keypresses which occur in each of
  28. * those periods. Then to calculate WPM, we add up all of the keypresses in
  29. * the whole ring buffer, divide by the number of keypresses in a 'word', and
  30. * then adjust for how much time is captured by our ring buffer. The size
  31. * of the ring buffer can be configured using the keymap configuration
  32. * value `WPM_SAMPLE_PERIODS`.
  33. *
  34. */
  35. #define MAX_PERIODS (WPM_SAMPLE_PERIODS)
  36. #define PERIOD_DURATION (1000 * WPM_SAMPLE_SECONDS / MAX_PERIODS)
  37. static int16_t period_presses[MAX_PERIODS] = {0};
  38. static uint8_t current_period = 0;
  39. static uint8_t periods = 1;
  40. #if !defined(WPM_UNFILTERED)
  41. /* LATENCY is used as part of filtering, and controls how quickly the reported
  42. * WPM trails behind our actual instantaneous measured WPM value, and is
  43. * defined in milliseconds. So for LATENCY == 100, the displayed WPM is
  44. * smoothed out over periods of 0.1 seconds. This results in a nice,
  45. * smoothly-moving reported WPM value which nevertheless is never more than
  46. * 0.1 seconds behind the typist's actual current WPM.
  47. *
  48. * LATENCY is not used if WPM_UNFILTERED is defined.
  49. */
  50. # define LATENCY (100)
  51. static uint32_t smoothing_timer = 0;
  52. static uint8_t prev_wpm = 0;
  53. static uint8_t next_wpm = 0;
  54. #endif
  55. void set_current_wpm(uint8_t new_wpm) {
  56. current_wpm = new_wpm;
  57. }
  58. uint8_t get_current_wpm(void) {
  59. return current_wpm;
  60. }
  61. bool wpm_keycode(uint16_t keycode) {
  62. return wpm_keycode_kb(keycode);
  63. }
  64. __attribute__((weak)) bool wpm_keycode_kb(uint16_t keycode) {
  65. return wpm_keycode_user(keycode);
  66. }
  67. __attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) {
  68. 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)) {
  69. keycode = keycode & 0xFF;
  70. } else if (keycode > 0xFF) {
  71. keycode = 0;
  72. }
  73. if ((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. #if defined(WPM_ALLOW_COUNT_REGRESSION)
  79. __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) {
  80. bool weak_modded = (keycode >= QK_LCTL && keycode < QK_LSFT) || (keycode >= QK_RCTL && keycode < QK_RSFT);
  81. 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)) {
  82. keycode = keycode & 0xFF;
  83. } else if (keycode > 0xFF) {
  84. keycode = 0;
  85. }
  86. if (keycode == KC_DELETE || keycode == KC_BACKSPACE) {
  87. if (((get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) || weak_modded) {
  88. return WPM_ESTIMATED_WORD_SIZE;
  89. } else {
  90. return 1;
  91. }
  92. } else {
  93. return 0;
  94. }
  95. }
  96. #endif
  97. // Outside 'raw' mode we smooth results over time.
  98. void update_wpm(uint16_t keycode) {
  99. if (wpm_keycode(keycode) && period_presses[current_period] < INT16_MAX) {
  100. period_presses[current_period]++;
  101. }
  102. #if defined(WPM_ALLOW_COUNT_REGRESSION)
  103. uint8_t regress = wpm_regress_count(keycode);
  104. if (regress && period_presses[current_period] > INT16_MIN) {
  105. period_presses[current_period]--;
  106. }
  107. #endif
  108. }
  109. void decay_wpm(void) {
  110. int32_t presses = period_presses[0];
  111. for (int i = 1; i <= periods; i++) {
  112. presses += period_presses[i];
  113. }
  114. if (presses < 0) {
  115. presses = 0;
  116. }
  117. int32_t elapsed = timer_elapsed32(wpm_timer);
  118. uint32_t duration = (((periods)*PERIOD_DURATION) + elapsed);
  119. int32_t wpm_now = (60000 * presses) / (duration * WPM_ESTIMATED_WORD_SIZE);
  120. if (wpm_now < 0) // set some reasonable WPM measurement limits
  121. wpm_now = 0;
  122. if (wpm_now > 240) wpm_now = 240;
  123. if (elapsed > PERIOD_DURATION) {
  124. current_period = (current_period + 1) % MAX_PERIODS;
  125. period_presses[current_period] = 0;
  126. periods = (periods < MAX_PERIODS - 1) ? periods + 1 : MAX_PERIODS - 1;
  127. elapsed = 0;
  128. wpm_timer = timer_read32();
  129. }
  130. if (presses < 2) // don't guess high WPM based on a single keypress.
  131. wpm_now = 0;
  132. #if defined(WPM_LAUNCH_CONTROL)
  133. /*
  134. * If the `WPM_LAUNCH_CONTROL` option is enabled, then whenever our WPM
  135. * drops to absolute zero due to no typing occurring within our sample
  136. * ring buffer, we reset and start measuring fresh, which lets our WPM
  137. * immediately reach the correct value even before a full sampling buffer
  138. * has been filled.
  139. */
  140. if (presses == 0) {
  141. current_period = 0;
  142. periods = 0;
  143. wpm_now = 0;
  144. period_presses[0] = 0;
  145. }
  146. #endif // WPM_LAUNCH_CONTROL
  147. #if defined(WPM_UNFILTERED)
  148. current_wpm = wpm_now;
  149. #else
  150. int32_t latency = timer_elapsed32(smoothing_timer);
  151. if (latency > LATENCY) {
  152. smoothing_timer = timer_read32();
  153. prev_wpm = current_wpm;
  154. next_wpm = wpm_now;
  155. }
  156. current_wpm = prev_wpm + (latency * ((int)next_wpm - (int)prev_wpm) / LATENCY);
  157. #endif
  158. }