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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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);
  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)
  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. intr_state = SREG;
  30. cli();
  31. UENUM = MOUSE_ENDPOINT;
  32. timeout = UDFNUML + 50;
  33. while (1) {
  34. // are we ready to transmit?
  35. if (UEINTX & (1<<RWAL)) break;
  36. SREG = intr_state;
  37. // has the USB gone offline?
  38. if (!usb_configured()) return -1;
  39. // have we waited too long?
  40. if (UDFNUML == timeout) return -1;
  41. // get ready to try checking again
  42. intr_state = SREG;
  43. cli();
  44. UENUM = MOUSE_ENDPOINT;
  45. }
  46. UEDATX = mouse_buttons;
  47. UEDATX = x;
  48. UEDATX = y;
  49. UEDATX = wheel;
  50. UEINTX = 0x3A;
  51. SREG = intr_state;
  52. return 0;
  53. }