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

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