1 /* Copyright (c) 2010-2011 mbed.org, MIT License
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:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
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.
22 bool USBMouse::update(int16_t x
, int16_t y
, uint8_t button
, int8_t z
) {
26 if (!mouseSend(127, 0, button
, z
)) return false;
30 if (!mouseSend(-128, 0, button
, z
)) return false;
34 if (!mouseSend(0, 127, button
, z
)) return false;
38 if (!mouseSend(0, -128, button
, z
)) return false;
41 return mouseSend(x
, y
, button
, z
);
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;
50 report
.data
[5] = button
& 0x07;
60 bool USBMouse::mouseSend(int8_t x
, int8_t y
, uint8_t buttons
, int8_t z
) {
62 report
.data
[0] = buttons
& 0x07;
65 report
.data
[3] = -z
; // >0 to scroll down, <0 to scroll up
72 bool USBMouse::move(int16_t x
, int16_t y
) {
73 return update(x
, y
, button
, 0);
76 bool USBMouse::scroll(int8_t z
) {
77 return update(0, 0, button
, z
);
81 bool USBMouse::doubleClick() {
82 if (!click(MOUSE_LEFT
))
85 return click(MOUSE_LEFT
);
88 bool USBMouse::click(uint8_t button
) {
89 if (!update(0, 0, button
, 0))
92 return update(0, 0, 0, 0);
95 bool USBMouse::press(uint8_t button_
) {
96 button
= button_
& 0x07;
97 return update(0, 0, button
, 0);
100 bool USBMouse::release(uint8_t button_
) {
101 button
= (button
& (~button_
)) & 0x07;
102 return update(0, 0, button
, 0);
106 uint8_t * USBMouse::reportDesc() {
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
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,
124 REPORT_COUNT(1), 0x01,
125 REPORT_SIZE(1), 0x05,
128 REPORT_COUNT(1), 0x03,
129 REPORT_SIZE(1), 0x08,
133 USAGE(1), 0x38, // scroll
134 LOGICAL_MINIMUM(1), 0x81,
135 LOGICAL_MAXIMUM(1), 0x7f,
136 INPUT(1), 0x06, // Relative data
141 reportLength
= sizeof(reportDescriptor
);
142 return reportDescriptor
;
143 } else if (mouse_type
== ABS_MOUSE
) {
144 static uint8_t reportDescriptor
[] = {
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
152 USAGE_PAGE(1), 0x01, // Generic Desktop
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
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
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
184 reportLength
= sizeof(reportDescriptor
);
185 return reportDescriptor
;
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))
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
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)
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)
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)
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)
244 return configurationDescriptor
;