You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB

  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 uint8_t latest_wpm = 0;
  21. static uint16_t wpm_timer = 0;
  22. // This smoothing is 40 keystrokes
  23. static const float wpm_smoothing = 0.0487;
  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. void update_wpm(uint16_t keycode) {
  40. if (wpm_keycode(keycode)) {
  41. if (wpm_timer > 0) {
  42. latest_wpm = 60000 / timer_elapsed(wpm_timer) / 5;
  43. current_wpm = (latest_wpm - current_wpm) * wpm_smoothing + current_wpm;
  44. }
  45. wpm_timer = timer_read();
  46. }
  47. }
  48. void decay_wpm(void) {
  49. if (timer_elapsed(wpm_timer) > 1000) {
  50. current_wpm = (0 - current_wpm) * wpm_smoothing + current_wpm;
  51. wpm_timer = timer_read();
  52. }
  53. }