]> git.gir.st - tmk_keyboard.git/blob - protocol/pjrc/usb_mouse.c
Squashed 'tmk_core/' content from commit 05caacc
[tmk_keyboard.git] / protocol / pjrc / usb_mouse.c
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
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "usb_mouse.h"
27 #include "print.h"
28 #include "debug.h"
29
30
31 uint8_t usb_mouse_protocol=1;
32
33
34 int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons)
35 {
36 uint8_t intr_state, timeout;
37
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;
43 intr_state = SREG;
44 cli();
45 UENUM = MOUSE_ENDPOINT;
46 timeout = UDFNUML + 50;
47 while (1) {
48 // are we ready to transmit?
49 if (UEINTX & (1<<RWAL)) break;
50 SREG = intr_state;
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
56 intr_state = SREG;
57 cli();
58 UENUM = MOUSE_ENDPOINT;
59 }
60 UEDATX = buttons;
61 UEDATX = x;
62 UEDATX = y;
63 if (usb_mouse_protocol) {
64 UEDATX = wheel_v;
65 UEDATX = wheel_h;
66 }
67
68 UEINTX = 0x3A;
69 SREG = intr_state;
70 return 0;
71 }
72
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("|");
77 phex(x); print(" ");
78 phex(y); print(" ");
79 phex(wheel_v); print(" ");
80 phex(wheel_h); print("\n");
81 }
Imprint / Impressum