wpm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef WPM_UNFILTERED
  23. static uint32_t smoothing_timer = 0;
  24. #endif
  25. /* The WPM calculation works by specifying a certain number of 'periods' inside
  26. * a ring buffer, and we count the number of keypresses which occur in each of
  27. * those periods. Then to calculate WPM, we add up all of the keypresses in
  28. * the whole ring buffer, divide by the number of keypresses in a 'word', and
  29. * then adjust for how much time is captured by our ring buffer. Right now
  30. * the ring buffer is hardcoded below to be six half-second periods, accounting
  31. * for a total WPM sampling period of up to three seconds of typing.
  32. *
  33. * Whenever our WPM drops to absolute zero due to no typing occurring within
  34. * any contiguous three seconds, we reset and start measuring fresh,
  35. * which lets our WPM immediately reach the correct value even before a full
  36. * three second sampling buffer has been filled.
  37. */
  38. #define MAX_PERIODS (WPM_SAMPLE_PERIODS)
  39. #define PERIOD_DURATION (1000 * WPM_SAMPLE_SECONDS / MAX_PERIODS)
  40. #define LATENCY (100)
  41. static int8_t period_presses[MAX_PERIODS] = {0};
  42. static uint8_t current_period = 0;
  43. static uint8_t periods = 1;
  44. #if !defined(WPM_UNFILTERED)
  45. static uint8_t prev_wpm = 0;
  46. static uint8_t next_wpm = 0;
  47. #endif
  48. void set_current_wpm(uint8_t new_wpm) { current_wpm = new_wpm; }
  49. uint8_t get_current_wpm(void) { return current_wpm; }
  50. bool wpm_keycode(uint16_t keycode) { return wpm_keycode_kb(keycode); }
  51. __attribute__((weak)) bool wpm_keycode_kb(uint16_t keycode) { return wpm_keycode_user(keycode); }
  52. __attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) {
  53. 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)) {
  54. keycode = keycode & 0xFF;
  55. } else if (keycode > 0xFF) {
  56. keycode = 0;
  57. }
  58. if ((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. #ifdef WPM_ALLOW_COUNT_REGRESSION
  64. __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) {
  65. bool weak_modded = (keycode >= QK_LCTL && keycode < QK_LSFT) || (keycode >= QK_RCTL && keycode < QK_RSFT);
  66. 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)) {
  67. keycode = keycode & 0xFF;
  68. } else if (keycode > 0xFF) {
  69. keycode = 0;
  70. }
  71. if (keycode == KC_DELETE || keycode == KC_BACKSPACE) {
  72. if (((get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) || weak_modded) {
  73. return WPM_ESTIMATED_WORD_SIZE;
  74. } else {
  75. return 1;
  76. }
  77. } else {
  78. return 0;
  79. }
  80. }
  81. #endif
  82. // Outside 'raw' mode we smooth results over time.
  83. void update_wpm(uint16_t keycode) {
  84. if (wpm_keycode(keycode)) {
  85. period_presses[current_period]++;
  86. }
  87. #ifdef WPM_ALLOW_COUNT_REGRESSION
  88. uint8_t regress = wpm_regress_count(keycode);
  89. if (regress) {
  90. period_presses[current_period]--;
  91. }
  92. #endif
  93. }
  94. void decay_wpm(void) {
  95. int32_t presses = period_presses[0];
  96. for (int i = 1; i <= periods; i++) {
  97. presses += period_presses[i];
  98. }
  99. if (presses < 0) {
  100. presses = 0;
  101. }
  102. int32_t elapsed = timer_elapsed32(wpm_timer);
  103. uint32_t duration = (((periods)*PERIOD_DURATION) + elapsed);
  104. uint32_t wpm_now = (60000 * presses) / (duration * WPM_ESTIMATED_WORD_SIZE);
  105. wpm_now = (wpm_now > 240) ? 240 : wpm_now;
  106. if (elapsed > PERIOD_DURATION) {
  107. current_period = (current_period + 1) % MAX_PERIODS;
  108. period_presses[current_period] = 0;
  109. periods = (periods < MAX_PERIODS - 1) ? periods + 1 : MAX_PERIODS - 1;
  110. elapsed = 0;
  111. /* if (wpm_timer == 0) { */
  112. wpm_timer = timer_read32();
  113. /* } else { */
  114. /* wpm_timer += PERIOD_DURATION; */
  115. /* } */
  116. }
  117. if (presses < 2) // don't guess high WPM based on a single keypress.
  118. wpm_now = 0;
  119. #if defined WPM_LAUNCH_CONTROL
  120. if (presses == 0) {
  121. current_period = 0;
  122. periods = 0;
  123. wpm_now = 0;
  124. }
  125. #endif // WPM_LAUNCH_CONTROL
  126. #ifndef WPM_UNFILTERED
  127. int32_t latency = timer_elapsed32(smoothing_timer);
  128. if (latency > LATENCY) {
  129. smoothing_timer = timer_read32();
  130. prev_wpm = current_wpm;
  131. next_wpm = wpm_now;
  132. }
  133. current_wpm = prev_wpm + (latency * ((int)next_wpm - (int)prev_wpm) / LATENCY);
  134. #else
  135. current_wpm = wpm_now;
  136. #endif
  137. }