wpm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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) {
  52. current_wpm = new_wpm;
  53. }
  54. uint8_t get_current_wpm(void) {
  55. return current_wpm;
  56. }
  57. bool wpm_keycode(uint16_t keycode) {
  58. return wpm_keycode_kb(keycode);
  59. }
  60. __attribute__((weak)) bool wpm_keycode_kb(uint16_t keycode) {
  61. return wpm_keycode_user(keycode);
  62. }
  63. __attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) {
  64. 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)) {
  65. keycode = keycode & 0xFF;
  66. } else if (keycode > 0xFF) {
  67. keycode = 0;
  68. }
  69. if ((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. #if defined(WPM_ALLOW_COUNT_REGRESSION)
  75. __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) {
  76. bool weak_modded = (keycode >= QK_LCTL && keycode < QK_LSFT) || (keycode >= QK_RCTL && keycode < QK_RSFT);
  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_DELETE || keycode == KC_BACKSPACE) {
  83. if (((get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) || weak_modded) {
  84. return WPM_ESTIMATED_WORD_SIZE;
  85. } else {
  86. return 1;
  87. }
  88. } else {
  89. return 0;
  90. }
  91. }
  92. #endif
  93. // Outside 'raw' mode we smooth results over time.
  94. void update_wpm(uint16_t keycode) {
  95. if (wpm_keycode(keycode) && period_presses[current_period] < INT16_MAX) {
  96. period_presses[current_period]++;
  97. }
  98. #if defined(WPM_ALLOW_COUNT_REGRESSION)
  99. uint8_t regress = wpm_regress_count(keycode);
  100. if (regress && period_presses[current_period] > INT16_MIN) {
  101. period_presses[current_period]--;
  102. }
  103. #endif
  104. }
  105. void decay_wpm(void) {
  106. int32_t presses = period_presses[0];
  107. for (int i = 1; i <= periods; i++) {
  108. presses += period_presses[i];
  109. }
  110. if (presses < 0) {
  111. presses = 0;
  112. }
  113. int32_t elapsed = timer_elapsed32(wpm_timer);
  114. uint32_t duration = (((periods)*PERIOD_DURATION) + elapsed);
  115. int32_t wpm_now = (60000 * presses) / (duration * WPM_ESTIMATED_WORD_SIZE);
  116. if (wpm_now < 0) // set some reasonable WPM measurement limits
  117. wpm_now = 0;
  118. if (wpm_now > 240) wpm_now = 240;
  119. if (elapsed > PERIOD_DURATION) {
  120. current_period = (current_period + 1) % MAX_PERIODS;
  121. period_presses[current_period] = 0;
  122. periods = (periods < MAX_PERIODS - 1) ? periods + 1 : MAX_PERIODS - 1;
  123. elapsed = 0;
  124. wpm_timer = timer_read32();
  125. }
  126. if (presses < 2) // don't guess high WPM based on a single keypress.
  127. wpm_now = 0;
  128. #if defined(WPM_LAUNCH_CONTROL)
  129. /*
  130. * If the `WPM_LAUNCH_CONTROL` option is enabled, then whenever our WPM
  131. * drops to absolute zero due to no typing occurring within our sample
  132. * ring buffer, we reset and start measuring fresh, which lets our WPM
  133. * immediately reach the correct value even before a full sampling buffer
  134. * has been filled.
  135. */
  136. if (presses == 0) {
  137. current_period = 0;
  138. periods = 0;
  139. wpm_now = 0;
  140. period_presses[0] = 0;
  141. }
  142. #endif // WPM_LAUNCH_CONTROL
  143. #if defined(WPM_UNFILTERED)
  144. current_wpm = wpm_now;
  145. #else
  146. int32_t latency = timer_elapsed32(smoothing_timer);
  147. if (latency > LATENCY) {
  148. smoothing_timer = timer_read32();
  149. prev_wpm = current_wpm;
  150. next_wpm = wpm_now;
  151. }
  152. current_wpm = prev_wpm + (latency * ((int)next_wpm - (int)prev_wpm) / LATENCY);
  153. #endif
  154. }