etchamouse.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright 2020 Stephen J. Bush
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include QMK_KEYBOARD_H
  17. #include "etchamouse.h"
  18. #include "pointing_device.h"
  19. #if defined(POINTING_DEVICE_ENABLE) && defined(ENCODER_ENABLE)
  20. /** Track movement separately in both directions. This will allow us to
  21. * smooth out the movement along diagonals
  22. */
  23. typedef struct {
  24. bool clockwise : 1;
  25. uint8_t count : 7;
  26. uint16_t timer : 16;
  27. uint16_t elapsed : 16;
  28. } key_tracker_t;
  29. static key_tracker_t tracker_x = {false, 0, 0, 0};
  30. static key_tracker_t tracker_y = {false, 0, 0, 0};
  31. /**
  32. * @brief Calculate the mouse move units for the given tracker.
  33. *
  34. * By using a key tracker rederence, we can minimize the amount of space
  35. * required on the stack. As we will have the tracker object, we will also
  36. * take the clockwise direction into account, which completely internalizes
  37. * the movement unit logic within this single function.
  38. *
  39. * @param tracker: Pointer to a key tracker object.
  40. * @return A integer from -127 to 127
  41. */
  42. static int8_t move_unit(key_tracker_t *tracker) {
  43. if (0 == tracker->count) return 0;
  44. const uint16_t modifier = TAPPING_TERM_MOUSE_ENCODER < tracker->elapsed ? 1 : (TAPPING_TERM_MOUSE_ENCODER - tracker->elapsed) >> 1;
  45. uint16_t speed = MOUSEKEY_INITIAL_SPEED + MOUSEKEY_MOVE_DELTA * modifier * (tracker->count >> 1);
  46. /* convert speed to USB mouse speed 1 to 127 */
  47. speed = (uint8_t)(speed / (1000.0f / MOUSEKEY_INTERVAL));
  48. speed = speed < 1 ? 1 : speed;
  49. return (tracker->clockwise ? 1 : -1) * (speed > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : speed);
  50. }
  51. /**
  52. * @brief Update key press tracker
  53. *
  54. * Update the time elapsed since the last keypress.
  55. * If the key has not been pressed since the tapping term, then reset the count to zero.
  56. * If the key was pressed, update the timer and increment the count.
  57. * Number of keypresses will degrade based on tapping term and zero out based
  58. * on the persistenc term.
  59. *
  60. * @param tracker: The object to update
  61. * @param pressed: A boolean indicating whether or not the key was pressed
  62. * @return None.
  63. */
  64. static void update_tracker(key_tracker_t *tracker, bool pressed, bool clockwise) {
  65. tracker->elapsed = timer_elapsed(tracker->timer);
  66. if (pressed) {
  67. tracker->timer = timer_read();
  68. tracker->count += 1;
  69. tracker->clockwise = clockwise;
  70. } else if (TAPPING_TERM_PERSISTENCE < tracker->elapsed) {
  71. tracker->count = 0;
  72. } else if (TAPPING_TERM_MOUSE_ENCODER < tracker->elapsed) {
  73. tracker->count >>= 1;
  74. }
  75. }
  76. bool encoder_update_mouse(uint8_t index, bool clockwise) {
  77. report_mouse_t curr_report = pointing_device_get_report();
  78. update_tracker(&tracker_x, 0 == index, clockwise);
  79. update_tracker(&tracker_y, 1 == index, clockwise);
  80. curr_report.x += move_unit(&tracker_x);
  81. curr_report.y += move_unit(&tracker_y);
  82. pointing_device_set_report(curr_report);
  83. pointing_device_send();
  84. return true;
  85. }
  86. #endif