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
;