Keyboard firmwares for Atmel AVR and Cortex-M
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <util/delay.h>
  16. #include "usb_keycodes.h"
  17. #include "host.h"
  18. #include "timer.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "mousekey.h"
  22. static report_mouse_t report;
  23. static uint8_t mousekey_repeat = 0;
  24. static void mousekey_debug(void);
  25. /*
  26. * TODO: fix acceleration algorithm
  27. * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
  28. */
  29. #ifndef MOUSEKEY_DELAY_TIME
  30. # define MOUSEKEY_DELAY_TIME 20
  31. #endif
  32. #define MOUSEKEY_MOVE_INIT 5
  33. #define MOUSEKEY_WHEEL_INIT 1
  34. #define MOUSEKEY_MOVE_ACCEL 5
  35. #define MOUSEKEY_WHEEL_ACCEL 1
  36. static uint16_t last_timer = 0;
  37. // acceleration parameters
  38. //uint8_t mousekey_move_unit = 2;
  39. //uint8_t mousekey_resolution = 5;
  40. static inline uint8_t move_unit(void)
  41. {
  42. uint16_t unit = 5 + mousekey_repeat*4;
  43. return (unit > 63 ? 63 : unit);
  44. }
  45. void mousekey_task(void)
  46. {
  47. if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME)
  48. return;
  49. if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
  50. return;
  51. if (mousekey_repeat != UINT8_MAX)
  52. mousekey_repeat++;
  53. if (report.x > 0) report.x = move_unit();
  54. if (report.x < 0) report.x = move_unit() * -1;
  55. if (report.y > 0) report.y = move_unit();
  56. if (report.y < 0) report.y = move_unit() * -1;
  57. if (report.x && report.y) {
  58. report.x *= 0.7;
  59. report.y *= 0.7;
  60. }
  61. if (report.v > 0) report.v = move_unit();
  62. if (report.v < 0) report.v = move_unit() * -1;
  63. if (report.h > 0) report.h = move_unit();
  64. if (report.h < 0) report.h = move_unit() * -1;
  65. mousekey_send();
  66. }
  67. void mousekey_on(uint8_t code)
  68. {
  69. if (code == KB_MS_UP) report.y = MOUSEKEY_MOVE_INIT * -1;
  70. else if (code == KB_MS_DOWN) report.y = MOUSEKEY_MOVE_INIT;
  71. else if (code == KB_MS_LEFT) report.x = MOUSEKEY_MOVE_INIT * -1;
  72. else if (code == KB_MS_RIGHT) report.x = MOUSEKEY_MOVE_INIT;
  73. else if (code == KB_MS_WH_UP) report.v = MOUSEKEY_WHEEL_INIT;
  74. else if (code == KB_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_INIT * -1;
  75. else if (code == KB_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_INIT * -1;
  76. else if (code == KB_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_INIT;
  77. else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
  78. else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
  79. else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
  80. else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
  81. else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
  82. }
  83. void mousekey_off(uint8_t code)
  84. {
  85. if (code == KB_MS_UP && report.y < 0) report.y = 0;
  86. else if (code == KB_MS_DOWN && report.y > 0) report.y = 0;
  87. else if (code == KB_MS_LEFT && report.x < 0) report.x = 0;
  88. else if (code == KB_MS_RIGHT && report.x > 0) report.x = 0;
  89. else if (code == KB_MS_WH_UP && report.v > 0) report.v = 0;
  90. else if (code == KB_MS_WH_DOWN && report.v < 0) report.v = 0;
  91. else if (code == KB_MS_WH_LEFT && report.h < 0) report.h = 0;
  92. else if (code == KB_MS_WH_RIGHT && report.h > 0) report.h = 0;
  93. else if (code == KB_MS_BTN1) report.buttons &= ~MOUSE_BTN1;
  94. else if (code == KB_MS_BTN2) report.buttons &= ~MOUSE_BTN2;
  95. else if (code == KB_MS_BTN3) report.buttons &= ~MOUSE_BTN3;
  96. else if (code == KB_MS_BTN4) report.buttons &= ~MOUSE_BTN4;
  97. else if (code == KB_MS_BTN5) report.buttons &= ~MOUSE_BTN5;
  98. if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
  99. mousekey_repeat = 0;
  100. }
  101. void mousekey_send(void)
  102. {
  103. mousekey_debug();
  104. host_mouse_send(&report);
  105. last_timer = timer_read();
  106. }
  107. void mousekey_clear(void)
  108. {
  109. report = (report_mouse_t){};
  110. /*
  111. report.buttons = 0;
  112. report.x = 0;
  113. report.y = 0;
  114. report.v = 0;
  115. report.h = 0;
  116. */
  117. }
  118. static void mousekey_debug(void)
  119. {
  120. if (!debug_mouse) return;
  121. print("mousekey [btn|x y v h]rep: [");
  122. phex(report.buttons); print("|");
  123. phex(report.x); print(" ");
  124. phex(report.y); print(" ");
  125. phex(report.v); print(" ");
  126. phex(report.h); print("]");
  127. phex(mousekey_repeat);
  128. print("\n");
  129. }