]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBHost/USBHostHID/USBHostMouse.cpp
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBHost / USBHostHID / USBHostMouse.cpp
1 /* mbed USBHost Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "USBHostMouse.h"
18
19 #if USBHOST_MOUSE
20
21 USBHostMouse::USBHostMouse() {
22 host = USBHost::getHostInst();
23 init();
24 }
25
26 void USBHostMouse::init() {
27 dev = NULL;
28 int_in = NULL;
29 onUpdate = NULL;
30 onButtonUpdate = NULL;
31 onXUpdate = NULL;
32 onYUpdate = NULL;
33 onZUpdate = NULL;
34 report_id = 0;
35 dev_connected = false;
36 mouse_device_found = false;
37 mouse_intf = -1;
38
39 buttons = 0;
40 x = 0;
41 y = 0;
42 z = 0;
43 }
44
45 bool USBHostMouse::connected() {
46 return dev_connected;
47 }
48
49 bool USBHostMouse::connect() {
50 int len_listen;
51
52 if (dev_connected) {
53 return true;
54 }
55
56 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
57 if ((dev = host->getDevice(i)) != NULL) {
58
59 if(host->enumerate(dev, this))
60 break;
61
62 if (mouse_device_found) {
63
64 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
65 if (!int_in)
66 break;
67
68 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
69 dev->setName("Mouse", mouse_intf);
70 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
71
72 int_in->attach(this, &USBHostMouse::rxHandler);
73 len_listen = int_in->getSize();
74 if (len_listen > sizeof(report)) {
75 len_listen = sizeof(report);
76 }
77 host->interruptRead(dev, int_in, report, len_listen, false);
78
79 dev_connected = true;
80 return true;
81 }
82 }
83 }
84 init();
85 return false;
86 }
87
88 void USBHostMouse::rxHandler() {
89 int len_listen = int_in->getSize();
90
91 if (onUpdate) {
92 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
93 }
94
95 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
96 (*onButtonUpdate)(report[0] & 0x07);
97 }
98
99 if (onXUpdate && (x != report[1])) {
100 (*onXUpdate)(report[1]);
101 }
102
103 if (onYUpdate && (y != report[2])) {
104 (*onYUpdate)(report[2]);
105 }
106
107 if (onZUpdate && (z != report[3])) {
108 (*onZUpdate)(report[3]);
109 }
110
111 // update mouse state
112 buttons = report[0] & 0x07;
113 x = report[1];
114 y = report[2];
115 z = report[3];
116
117 if (len_listen > sizeof(report)) {
118 len_listen = sizeof(report);
119 }
120
121 if (dev)
122 host->interruptRead(dev, int_in, report, len_listen, false);
123 }
124
125 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
126 {
127 // we don't check VID/PID for mouse driver
128 }
129
130 /*virtual*/ bool USBHostMouse::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
131 {
132 if ((mouse_intf == -1) &&
133 (intf_class == HID_CLASS) &&
134 (intf_subclass == 0x01) &&
135 (intf_protocol == 0x02)) {
136 mouse_intf = intf_nb;
137 return true;
138 }
139 return false;
140 }
141
142 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
143 {
144 if (intf_nb == mouse_intf) {
145 if (type == INTERRUPT_ENDPOINT && dir == IN) {
146 mouse_device_found = true;
147 return true;
148 }
149 }
150 return false;
151 }
152
153 #endif
Imprint / Impressum