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

13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if (mouse_protocol) {
  54. UEDATX = wheel;
  55. UEDATX = hwheel;
  56. }
  57. UEINTX = 0x3A;
  58. SREG = intr_state;
  59. is_sent = true;
  60. return 0;
  61. }
  62. void usb_mouse_clear(void) {
  63. is_sent = false;
  64. }
  65. bool usb_mouse_is_sent(void) {
  66. return is_sent;
  67. }
  68. void usb_mouse_print(int8_t mouse_x, int8_t mouse_y, int8_t wheel_v, int8_t wheel_h) {
  69. if (!debug_mouse) return;
  70. print("mouse btn|x y v h: ");
  71. phex(mouse_buttons); print("|");
  72. phex(mouse_x); print(" ");
  73. phex(mouse_y); print(" ");
  74. phex(wheel_v); print(" ");
  75. phex(wheel_h); print("\n");
  76. }