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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <avr/interrupt.h>
  2. #include <util/delay.h>
  3. #include "usb_mouse.h"
  4. #include "print.h"
  5. #include "debug.h"
  6. uint8_t usb_mouse_protocol=1;
  7. int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons)
  8. {
  9. uint8_t intr_state, timeout;
  10. if (!usb_configured()) return -1;
  11. if (x == -128) x = -127;
  12. if (y == -128) y = -127;
  13. if (wheel_v == -128) wheel_v = -127;
  14. if (wheel_h == -128) wheel_h = -127;
  15. intr_state = SREG;
  16. cli();
  17. UENUM = MOUSE_ENDPOINT;
  18. timeout = UDFNUML + 50;
  19. while (1) {
  20. // are we ready to transmit?
  21. if (UEINTX & (1<<RWAL)) break;
  22. SREG = intr_state;
  23. // has the USB gone offline?
  24. if (!usb_configured()) return -1;
  25. // have we waited too long?
  26. if (UDFNUML == timeout) return -1;
  27. // get ready to try checking again
  28. intr_state = SREG;
  29. cli();
  30. UENUM = MOUSE_ENDPOINT;
  31. }
  32. UEDATX = buttons;
  33. UEDATX = x;
  34. UEDATX = y;
  35. if (usb_mouse_protocol) {
  36. UEDATX = wheel_v;
  37. UEDATX = wheel_h;
  38. }
  39. UEINTX = 0x3A;
  40. SREG = intr_state;
  41. return 0;
  42. }
  43. void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) {
  44. if (!debug_mouse) return;
  45. print("usb_mouse[btn|x y v h]: ");
  46. phex(buttons); print("|");
  47. phex(x); print(" ");
  48. phex(y); print(" ");
  49. phex(wheel_v); print(" ");
  50. phex(wheel_h); print("\n");
  51. }