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 2.7KB

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