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.

mousekey.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <stdint.h>
  2. #include <util/delay.h>
  3. #include "usb_keycodes.h"
  4. #include "host.h"
  5. #include "timer.h"
  6. #include "print.h"
  7. #include "debug.h"
  8. #include "mousekey.h"
  9. static report_mouse_t report;
  10. static report_mouse_t report_prev;
  11. static uint8_t mousekey_repeat = 0;
  12. static void mousekey_debug(void);
  13. /*
  14. * TODO: fix acceleration algorithm
  15. * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
  16. */
  17. #ifndef MOUSEKEY_DELAY_TIME
  18. # define MOUSEKEY_DELAY_TIME 255
  19. #endif
  20. static inline uint8_t move_unit(void)
  21. {
  22. uint16_t unit = 10 + (mousekey_repeat);
  23. return (unit > 127 ? 127 : unit);
  24. }
  25. void mousekey_decode(uint8_t code)
  26. {
  27. if (code == KB_MS_UP) report.y = -move_unit();
  28. else if (code == KB_MS_DOWN) report.y = move_unit();
  29. else if (code == KB_MS_LEFT) report.x = -move_unit();
  30. else if (code == KB_MS_RIGHT) report.x = move_unit();
  31. else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
  32. else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
  33. else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
  34. /*
  35. else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
  36. else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
  37. else if (code == KB_MS_WH_UP) report.v += 1;
  38. else if (code == KB_MS_WH_DOWN) report.v -= 1;
  39. else if (code == KB_MS_WH_LEFT) report.h -= 1;
  40. else if (code == KB_MS_WH_RIGHT)report.h += 1;
  41. */
  42. }
  43. bool mousekey_changed(void)
  44. {
  45. return (report.buttons != report_prev.buttons ||
  46. report.x != report_prev.x ||
  47. report.y != report_prev.y ||
  48. report.x || report.y);
  49. //return (report.buttons != report_prev.buttons || report.x || report.y);
  50. }
  51. void mousekey_send(void)
  52. {
  53. static uint16_t last_timer = 0;
  54. if (!mousekey_changed()) {
  55. mousekey_repeat = 0;
  56. return;
  57. }
  58. // send immediately when buttun state is changed
  59. if (report.buttons == report_prev.buttons) {
  60. // TODO: delay parameter setting
  61. if ((timer_elapsed(last_timer) < (mousekey_repeat == 1 ? 20 : 5))) {
  62. return;
  63. }
  64. }
  65. if (report.x && report.y) {
  66. report.x *= 0.7;
  67. report.y *= 0.7;
  68. }
  69. /*
  70. print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
  71. print("timer: "); phex16(timer_read()); print("\n");
  72. print("last_timer: "); phex16(last_timer); print("\n");
  73. print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
  74. */
  75. mousekey_debug();
  76. host_mouse_send(&report);
  77. report_prev.buttons = report.buttons;
  78. report_prev.x = report.x;
  79. report_prev.y = report.y;
  80. if (mousekey_repeat != 0xFF) mousekey_repeat++;
  81. last_timer = timer_read();
  82. mousekey_clear_report();
  83. }
  84. void mousekey_clear_report(void)
  85. {
  86. report.buttons = 0;
  87. report.x = 0;
  88. report.y = 0;
  89. }
  90. static void mousekey_debug(void)
  91. {
  92. if (!debug_mouse) return;
  93. print("mousekey[btn|x y v h]: ");
  94. phex(report.buttons); print("|");
  95. phex(report.x); print(" ");
  96. phex(report.y); print(" ");
  97. /*
  98. phex(report.v); print(" ");
  99. phex(report.h);
  100. */
  101. print("\n");
  102. }