]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBDevice/USBHID/USBMouse.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBDevice / USBHID / USBMouse.cpp
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #include "stdint.h"
20 #include "USBMouse.h"
21
22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
23     switch (mouse_type) {
24         case REL_MOUSE:
25             while (x > 127) {
26                 if (!mouseSend(127, 0, button, z)) return false;
27                 x = x - 127;
28             }
29             while (x < -128) {
30                 if (!mouseSend(-128, 0, button, z)) return false;
31                 x = x + 128;
32             }
33             while (y > 127) {
34                 if (!mouseSend(0, 127, button, z)) return false;
35                 y = y - 127;
36             }
37             while (y < -128) {
38                 if (!mouseSend(0, -128, button, z)) return false;
39                 y = y + 128;
40             }
41             return mouseSend(x, y, button, z);
42         case ABS_MOUSE:
43             HID_REPORT report;
44
45             report.data[0] = x & 0xff;
46             report.data[1] = (x >> 8) & 0xff;
47             report.data[2] = y & 0xff;
48             report.data[3] = (y >> 8) & 0xff;
49             report.data[4] = -z;
50             report.data[5] = button & 0x07;
51
52             report.length = 6;
53
54             return send(&report);
55         default:
56             return false;
57     }
58 }
59
60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
61     HID_REPORT report;
62     report.data[0] = buttons & 0x07;
63     report.data[1] = x;
64     report.data[2] = y;
65     report.data[3] = -z; // >0 to scroll down, <0 to scroll up
66
67     report.length = 4;
68
69     return send(&report);
70 }
71
72 bool USBMouse::move(int16_t x, int16_t y) {
73     return update(x, y, button, 0);
74 }
75
76 bool USBMouse::scroll(int8_t z) {
77     return update(0, 0, button, z);
78 }
79
80
81 bool USBMouse::doubleClick() {
82     if (!click(MOUSE_LEFT))
83         return false;
84     wait(0.1);
85     return click(MOUSE_LEFT);
86 }
87
88 bool USBMouse::click(uint8_t button) {
89     if (!update(0, 0, button, 0))
90         return false;
91     wait(0.01);
92     return update(0, 0, 0, 0);
93 }
94
95 bool USBMouse::press(uint8_t button_) {
96     button = button_ & 0x07;
97     return update(0, 0, button, 0);
98 }
99
100 bool USBMouse::release(uint8_t button_) {
101     button = (button & (~button_)) & 0x07;
102     return update(0, 0, button, 0);
103 }
104
105
106 uint8_t * USBMouse::reportDesc() {
107
108     if (mouse_type == REL_MOUSE) {
109         static uint8_t reportDescriptor[] = {
110             USAGE_PAGE(1),      0x01,       // Genric Desktop
111             USAGE(1),           0x02,       // Mouse
112             COLLECTION(1),      0x01,       // Application
113             USAGE(1),           0x01,       // Pointer
114             COLLECTION(1),      0x00,       // Physical
115
116             REPORT_COUNT(1),    0x03,
117             REPORT_SIZE(1),     0x01,
118             USAGE_PAGE(1),      0x09,       // Buttons
119             USAGE_MINIMUM(1),       0x1,
120             USAGE_MAXIMUM(1),       0x3,
121             LOGICAL_MINIMUM(1),     0x00,
122             LOGICAL_MAXIMUM(1),     0x01,
123             INPUT(1),           0x02,
124             REPORT_COUNT(1),    0x01,
125             REPORT_SIZE(1),     0x05,
126             INPUT(1),           0x01,
127
128             REPORT_COUNT(1),    0x03,
129             REPORT_SIZE(1),     0x08,
130             USAGE_PAGE(1),      0x01,
131             USAGE(1),           0x30,       // X
132             USAGE(1),           0x31,       // Y
133             USAGE(1),           0x38,       // scroll
134             LOGICAL_MINIMUM(1),     0x81,
135             LOGICAL_MAXIMUM(1),     0x7f,
136             INPUT(1),           0x06,       // Relative data
137
138             END_COLLECTION(0),
139             END_COLLECTION(0),
140         };
141         reportLength = sizeof(reportDescriptor);
142         return reportDescriptor;
143     } else if (mouse_type == ABS_MOUSE) {
144         static uint8_t reportDescriptor[] = {
145
146             USAGE_PAGE(1), 0x01,           // Generic Desktop
147             USAGE(1), 0x02,                // Mouse
148             COLLECTION(1), 0x01,           // Application
149             USAGE(1), 0x01,                // Pointer
150             COLLECTION(1), 0x00,           // Physical
151
152             USAGE_PAGE(1), 0x01,            // Generic Desktop
153             USAGE(1), 0x30,                 // X
154             USAGE(1), 0x31,                 // Y
155             LOGICAL_MINIMUM(1), 0x00,       // 0
156             LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
157             REPORT_SIZE(1), 0x10,
158             REPORT_COUNT(1), 0x02,
159             INPUT(1), 0x02,                 // Data, Variable, Absolute
160
161             USAGE_PAGE(1), 0x01,            // Generic Desktop
162             USAGE(1), 0x38,                 // scroll
163             LOGICAL_MINIMUM(1), 0x81,       // -127
164             LOGICAL_MAXIMUM(1), 0x7f,       // 127
165             REPORT_SIZE(1), 0x08,
166             REPORT_COUNT(1), 0x01,
167             INPUT(1), 0x06,                 // Data, Variable, Relative
168
169             USAGE_PAGE(1), 0x09,            // Buttons
170             USAGE_MINIMUM(1), 0x01,
171             USAGE_MAXIMUM(1), 0x03,
172             LOGICAL_MINIMUM(1), 0x00,       // 0
173             LOGICAL_MAXIMUM(1), 0x01,       // 1
174             REPORT_COUNT(1), 0x03,
175             REPORT_SIZE(1), 0x01,
176             INPUT(1), 0x02,                 // Data, Variable, Absolute
177             REPORT_COUNT(1), 0x01,
178             REPORT_SIZE(1), 0x05,
179             INPUT(1), 0x01,                 // Constant
180
181             END_COLLECTION(0),
182             END_COLLECTION(0)
183         };
184         reportLength = sizeof(reportDescriptor);
185         return reportDescriptor;
186     }
187     return NULL;
188 }
189
190 #define DEFAULT_CONFIGURATION (1)
191 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
192                                + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
193                                + (1 * HID_DESCRIPTOR_LENGTH) \
194                                + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
195
196 uint8_t * USBMouse::configurationDesc() {
197     static uint8_t configurationDescriptor[] = {
198         CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
199         CONFIGURATION_DESCRIPTOR,       // bDescriptorType
200         LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
201         MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
202         0x01,                           // bNumInterfaces
203         DEFAULT_CONFIGURATION,          // bConfigurationValue
204         0x00,                           // iConfiguration
205         C_RESERVED | C_SELF_POWERED,    // bmAttributes
206         C_POWER(0),                     // bMaxPowerHello World from Mbed
207
208         INTERFACE_DESCRIPTOR_LENGTH,    // bLength
209         INTERFACE_DESCRIPTOR,           // bDescriptorType
210         0x00,                           // bInterfaceNumber
211         0x00,                           // bAlternateSetting
212         0x02,                           // bNumEndpoints
213         HID_CLASS,                      // bInterfaceClass
214         1,                              // bInterfaceSubClass
215         2,                              // bInterfaceProtocol (mouse)
216         0x00,                           // iInterface
217
218         HID_DESCRIPTOR_LENGTH,          // bLength
219         HID_DESCRIPTOR,                 // bDescriptorType
220         LSB(HID_VERSION_1_11),          // bcdHID (LSB)
221         MSB(HID_VERSION_1_11),          // bcdHID (MSB)
222         0x00,                           // bCountryCode
223         0x01,                           // bNumDescriptors
224         REPORT_DESCRIPTOR,              // bDescriptorType
225         (uint8_t)(LSB(reportDescLength())),        // wDescriptorLength (LSB)
226         (uint8_t)(MSB(reportDescLength())),        // wDescriptorLength (MSB)
227
228         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
229         ENDPOINT_DESCRIPTOR,            // bDescriptorType
230         PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
231         E_INTERRUPT,                    // bmAttributes
232         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
233         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
234         1,                              // bInterval (milliseconds)
235
236         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
237         ENDPOINT_DESCRIPTOR,            // bDescriptorType
238         PHY_TO_DESC(EPINT_OUT),         // bEndpointAddress
239         E_INTERRUPT,                    // bmAttributes
240         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
241         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
242         1,                              // bInterval (milliseconds)
243     };
244     return configurationDescriptor;
245 }