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.0KB

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