usb_mouse.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. uint8_t intr_state, timeout;
  31. if (!usb_configured()) return -1;
  32. if (x == -128) x = -127;
  33. if (y == -128) y = -127;
  34. if (wheel_v == -128) wheel_v = -127;
  35. if (wheel_h == -128) wheel_h = -127;
  36. intr_state = SREG;
  37. cli();
  38. UENUM = MOUSE_ENDPOINT;
  39. timeout = UDFNUML + 50;
  40. while (1) {
  41. // are we ready to transmit?
  42. if (UEINTX & (1 << RWAL)) break;
  43. SREG = intr_state;
  44. // has the USB gone offline?
  45. if (!usb_configured()) return -1;
  46. // have we waited too long?
  47. if (UDFNUML == timeout) return -1;
  48. // get ready to try checking again
  49. intr_state = SREG;
  50. cli();
  51. UENUM = MOUSE_ENDPOINT;
  52. }
  53. UEDATX = buttons;
  54. UEDATX = x;
  55. UEDATX = y;
  56. if (usb_mouse_protocol) {
  57. UEDATX = wheel_v;
  58. UEDATX = wheel_h;
  59. }
  60. UEINTX = 0x3A;
  61. SREG = intr_state;
  62. return 0;
  63. }
  64. void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) {
  65. if (!debug_mouse) return;
  66. print("usb_mouse[btn|x y v h]: ");
  67. phex(buttons);
  68. print("|");
  69. phex(x);
  70. print(" ");
  71. phex(y);
  72. print(" ");
  73. phex(wheel_v);
  74. print(" ");
  75. phex(wheel_h);
  76. print("\n");
  77. }