wpm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 uint16_t wpm_timer = 0;
  22. // This smoothing is 40 keystrokes
  23. static const float wpm_smoothing = WPM_SMOOTHING;
  24. void set_current_wpm(uint8_t new_wpm) { current_wpm = new_wpm; }
  25. uint8_t get_current_wpm(void) { return current_wpm; }
  26. bool wpm_keycode(uint16_t keycode) { return wpm_keycode_kb(keycode); }
  27. __attribute__((weak)) bool wpm_keycode_kb(uint16_t keycode) { return wpm_keycode_user(keycode); }
  28. __attribute__((weak)) bool wpm_keycode_user(uint16_t keycode) {
  29. 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)) {
  30. keycode = keycode & 0xFF;
  31. } else if (keycode > 0xFF) {
  32. keycode = 0;
  33. }
  34. if ((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. #ifdef WPM_ALLOW_COUNT_REGRESSION
  40. __attribute__((weak)) uint8_t wpm_regress_count(uint16_t keycode) {
  41. bool weak_modded = (keycode >= QK_LCTL && keycode < QK_LSFT) || (keycode >= QK_RCTL && keycode < QK_RSFT);
  42. 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)) {
  43. keycode = keycode & 0xFF;
  44. } else if (keycode > 0xFF) {
  45. keycode = 0;
  46. }
  47. if (keycode == KC_DEL || keycode == KC_BSPC) {
  48. if (((get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) || weak_modded) {
  49. return WPM_ESTIMATED_WORD_SIZE;
  50. } else {
  51. return 1;
  52. }
  53. } else {
  54. return 0;
  55. }
  56. }
  57. #endif
  58. void update_wpm(uint16_t keycode) {
  59. if (wpm_keycode(keycode)) {
  60. if (wpm_timer > 0) {
  61. uint16_t latest_wpm = 60000 / timer_elapsed(wpm_timer) / WPM_ESTIMATED_WORD_SIZE;
  62. if (latest_wpm > UINT8_MAX) {
  63. latest_wpm = UINT8_MAX;
  64. }
  65. current_wpm += ceilf((latest_wpm - current_wpm) * wpm_smoothing);
  66. }
  67. wpm_timer = timer_read();
  68. }
  69. #ifdef WPM_ALLOW_COUNT_REGRESSION
  70. uint8_t regress = wpm_regress_count(keycode);
  71. if (regress) {
  72. if (current_wpm < regress) {
  73. current_wpm = 0;
  74. } else {
  75. current_wpm -= regress;
  76. }
  77. wpm_timer = timer_read();
  78. }
  79. #endif
  80. }
  81. void decay_wpm(void) {
  82. if (timer_elapsed(wpm_timer) > 1000) {
  83. current_wpm += (-current_wpm) * wpm_smoothing;
  84. wpm_timer = timer_read();
  85. }
  86. }