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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* USB Mouse Plus Debug Channel Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_mouse.html
  3. * Copyright (c) 2009 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/interrupt.h>
  24. #include <util/delay.h>
  25. #include "usb_mouse.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. uint8_t usb_mouse_protocol=1;
  29. int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons)
  30. {
  31. uint8_t intr_state, timeout;
  32. if (!usb_configured()) return -1;
  33. if (x == -128) x = -127;
  34. if (y == -128) y = -127;
  35. if (wheel_v == -128) wheel_v = -127;
  36. if (wheel_h == -128) wheel_h = -127;
  37. intr_state = SREG;
  38. cli();
  39. UENUM = MOUSE_ENDPOINT;
  40. timeout = UDFNUML + 50;
  41. while (1) {
  42. // are we ready to transmit?
  43. if (UEINTX & (1<<RWAL)) break;
  44. SREG = intr_state;
  45. // has the USB gone offline?
  46. if (!usb_configured()) return -1;
  47. // have we waited too long?
  48. if (UDFNUML == timeout) return -1;
  49. // get ready to try checking again
  50. intr_state = SREG;
  51. cli();
  52. UENUM = MOUSE_ENDPOINT;
  53. }
  54. UEDATX = buttons;
  55. UEDATX = x;
  56. UEDATX = y;
  57. if (usb_mouse_protocol) {
  58. UEDATX = wheel_v;
  59. UEDATX = wheel_h;
  60. }
  61. UEINTX = 0x3A;
  62. SREG = intr_state;
  63. return 0;
  64. }
  65. void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) {
  66. if (!debug_mouse) return;
  67. print("usb_mouse[btn|x y v h]: ");
  68. phex(buttons); print("|");
  69. phex(x); print(" ");
  70. phex(y); print(" ");
  71. phex(wheel_v); print(" ");
  72. phex(wheel_h); print("\n");
  73. }