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.

usb_mouse.c 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <avr/interrupt.h>
  2. #include <util/delay.h>
  3. #include "usb_mouse.h"
  4. #include "print.h"
  5. #include "debug.h"
  6. static bool is_sent = false;
  7. // which buttons are currently pressed
  8. uint8_t mouse_buttons=0;
  9. // protocol setting from the host. We use exactly the same report
  10. // either way, so this variable only stores the setting since we
  11. // are required to be able to report which setting is in use.
  12. uint8_t mouse_protocol=1;
  13. // Set the mouse buttons. To create a "click", 2 calls are needed,
  14. // one to push the button down and the second to release it
  15. int8_t usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right)
  16. {
  17. uint8_t mask=0;
  18. if (left) mask |= 1;
  19. if (middle) mask |= 4;
  20. if (right) mask |= 2;
  21. mouse_buttons = mask;
  22. return usb_mouse_move(0, 0, 0, 0);
  23. }
  24. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
  25. int8_t usb_mouse_move(int8_t x, int8_t y, int8_t wheel, int8_t hwheel)
  26. {
  27. uint8_t intr_state, timeout;
  28. if (!usb_configured()) return -1;
  29. if (x == -128) x = -127;
  30. if (y == -128) y = -127;
  31. if (wheel == -128) wheel = -127;
  32. if (hwheel == -128) hwheel = -127;
  33. intr_state = SREG;
  34. cli();
  35. UENUM = MOUSE_ENDPOINT;
  36. timeout = UDFNUML + 50;
  37. while (1) {
  38. // are we ready to transmit?
  39. if (UEINTX & (1<<RWAL)) break;
  40. SREG = intr_state;
  41. // has the USB gone offline?
  42. if (!usb_configured()) return -1;
  43. // have we waited too long?
  44. if (UDFNUML == timeout) return -1;
  45. // get ready to try checking again
  46. intr_state = SREG;
  47. cli();
  48. UENUM = MOUSE_ENDPOINT;
  49. }
  50. UEDATX = mouse_buttons;
  51. UEDATX = x;
  52. UEDATX = y;
  53. UEDATX = wheel;
  54. UEDATX = hwheel;
  55. UEINTX = 0x3A;
  56. SREG = intr_state;
  57. is_sent = true;
  58. return 0;
  59. }
  60. void usb_mouse_clear(void) {
  61. is_sent = false;
  62. }
  63. bool usb_mouse_is_sent(void) {
  64. return is_sent;
  65. }
  66. void usb_mouse_print(int8_t mouse_x, int8_t mouse_y, int8_t wheel_v, int8_t wheel_h) {
  67. if (!debug_mouse) return;
  68. print("mouse btn|x y v h: ");
  69. phex(mouse_buttons); print("|");
  70. phex(mouse_x); print(" ");
  71. phex(mouse_y); print(" ");
  72. phex(wheel_v); print(" ");
  73. phex(wheel_h); print("\n");
  74. }