wpm.c 3.0 KB

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