wpm.c 6.0 KB

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