Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

13 роки тому
13 роки тому
13 роки тому
13 роки тому
11 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "keycode.h"
  17. #include "host.h"
  18. #include "timer.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "mousekey.h"
  22. static uint8_t mousekey_repeat = 0;
  23. static void mousekey_debug(void);
  24. /* max value on report descriptor */
  25. #define MOUSEKEY_MOVE_MAX 127
  26. #define MOUSEKEY_WHEEL_MAX 15
  27. #ifndef MOUSEKEY_MOVE_DELTA
  28. #define MOUSEKEY_MOVE_DELTA 5
  29. #endif
  30. #ifndef MOUSEKEY_WHEEL_DELTA
  31. #define MOUSEKEY_WHEEL_DELTA 1
  32. #endif
  33. #ifndef MOUSEKEY_DELAY
  34. #define MOUSEKEY_DELAY 300
  35. #endif
  36. #ifndef MOUSEKEY_INTERVAL
  37. #define MOUSEKEY_INTERVAL 50
  38. #endif
  39. #ifndef MOUSEKEY_MAX_SPEED
  40. #define MOUSEKEY_MAX_SPEED 10
  41. #endif
  42. #ifndef MOUSEKEY_TIME_TO_MAX
  43. #define MOUSEKEY_TIME_TO_MAX 20
  44. #endif
  45. #ifndef MOUSEKEY_WHEEL_MAX_SPEED
  46. #define MOUSEKEY_WHEEL_MAX_SPEED 8
  47. #endif
  48. #ifndef MOUSEKEY_WHEEL_TIME_TO_MAX
  49. #define MOUSEKEY_WHEEL_TIME_TO_MAX 40
  50. #endif
  51. /*
  52. * Mouse keys acceleration algorithm
  53. * http://en.wikipedia.org/wiki/Mouse_keys
  54. *
  55. * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
  56. */
  57. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  58. static uint8_t mk_delay = MOUSEKEY_DELAY/10;
  59. /* milliseconds between repeated motion events (0-255) */
  60. static uint8_t mk_interval = MOUSEKEY_INTERVAL;
  61. /* steady speed (in action_delta units) applied each event (0-255) */
  62. static uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
  63. /* number of events (count) accelerating to steady speed (0-255) */
  64. static uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  65. /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
  66. //static int8_t mk_curve = 0;
  67. static uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  68. static uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  69. static uint16_t last_timer = 0;
  70. static uint8_t move_unit(void)
  71. {
  72. uint16_t unit;
  73. if (mousekey_repeat > mk_time_to_max) {
  74. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  75. } else {
  76. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  77. }
  78. if (unit == 0) return 1;
  79. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : unit);
  80. }
  81. static uint8_t wheel_unit(void)
  82. {
  83. uint16_t unit;
  84. if (mousekey_repeat > mk_time_to_max) {
  85. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  86. } else {
  87. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
  88. }
  89. if (unit == 0) return 1;
  90. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : unit);
  91. }
  92. void mousekey_task(void)
  93. {
  94. if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10))
  95. return;
  96. if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
  97. return;
  98. if (mousekey_repeat != UINT8_MAX)
  99. mousekey_repeat++;
  100. if (mouse_report.x > 0) mouse_report.x = move_unit();
  101. if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
  102. if (mouse_report.y > 0) mouse_report.y = move_unit();
  103. if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
  104. if (mouse_report.x && mouse_report.y) {
  105. mouse_report.x *= 0.7;
  106. mouse_report.y *= 0.7;
  107. }
  108. if (mouse_report.v > 0) mouse_report.v = wheel_unit();
  109. if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
  110. if (mouse_report.h > 0) mouse_report.h = wheel_unit();
  111. if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;
  112. mousekey_send();
  113. }
  114. void mousekey_on(uint8_t code)
  115. {
  116. if (code == KC_MS_UP) mouse_report.y = MOUSEKEY_MOVE_DELTA * -1;
  117. else if (code == KC_MS_DOWN) mouse_report.y = MOUSEKEY_MOVE_DELTA;
  118. else if (code == KC_MS_LEFT) mouse_report.x = MOUSEKEY_MOVE_DELTA * -1;
  119. else if (code == KC_MS_RIGHT) mouse_report.x = MOUSEKEY_MOVE_DELTA;
  120. else if (code == KC_MS_WH_UP) mouse_report.v = MOUSEKEY_WHEEL_DELTA;
  121. else if (code == KC_MS_WH_DOWN) mouse_report.v = MOUSEKEY_WHEEL_DELTA * -1;
  122. else if (code == KC_MS_WH_LEFT) mouse_report.h = MOUSEKEY_WHEEL_DELTA * -1;
  123. else if (code == KC_MS_WH_RIGHT) mouse_report.h = MOUSEKEY_WHEEL_DELTA;
  124. else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1;
  125. else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2;
  126. else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3;
  127. else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4;
  128. else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5;
  129. }
  130. void mousekey_off(uint8_t code)
  131. {
  132. if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0;
  133. else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0;
  134. else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0;
  135. else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0;
  136. else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0;
  137. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0;
  138. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0;
  139. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0;
  140. else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1;
  141. else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2;
  142. else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3;
  143. else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4;
  144. else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5;
  145. if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
  146. mousekey_repeat = 0;
  147. }
  148. void mousekey_send(void)
  149. {
  150. mousekey_debug();
  151. host_mouse_send(&mouse_report);
  152. last_timer = timer_read();
  153. }
  154. void mousekey_clear(void)
  155. {
  156. mouse_report = (report_mouse_t){};
  157. }
  158. static void mousekey_debug(void)
  159. {
  160. if (!debug_mouse) return;
  161. print("mousekey [btn|x y v h]rep: [");
  162. phex(mouse_report.buttons); print("|");
  163. phex(mouse_report.x); print(" ");
  164. phex(mouse_report.y); print(" ");
  165. phex(mouse_report.v); print(" ");
  166. phex(mouse_report.h); print("]");
  167. phex(mousekey_repeat);
  168. print("\n");
  169. }