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
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "usb_mouse.h"
31 uint8_t usb_mouse_protocol=1;
34 int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons)
36 uint8_t intr_state, timeout;
38 if (!usb_configured()) return -1;
39 if (x == -128) x = -127;
40 if (y == -128) y = -127;
41 if (wheel_v == -128) wheel_v = -127;
42 if (wheel_h == -128) wheel_h = -127;
45 UENUM = MOUSE_ENDPOINT;
46 timeout = UDFNUML + 50;
48 // are we ready to transmit?
49 if (UEINTX & (1<<RWAL)) break;
51 // has the USB gone offline?
52 if (!usb_configured()) return -1;
53 // have we waited too long?
54 if (UDFNUML == timeout) return -1;
55 // get ready to try checking again
58 UENUM = MOUSE_ENDPOINT;
63 if (usb_mouse_protocol) {
73 void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) {
74 if (!debug_mouse) return;
75 print("usb_mouse[btn|x y v h]: ");
76 phex(buttons); print("|");
79 phex(wheel_v); print(" ");
80 phex(wheel_h); print("\n");