re-enabling serial
[clinton/Smoothieware.git] / gcc4mbed / samples / MSTest / USBDevice / USBHID / USBMouse.cpp
CommitLineData
cd011f58
AW
1/* Copyright (c) 2010-2011 mbed.org, MIT License\r
2*\r
3* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
4* and associated documentation files (the "Software"), to deal in the Software without\r
5* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
6* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
7* Software is furnished to do so, subject to the following conditions:\r
8*\r
9* The above copyright notice and this permission notice shall be included in all copies or\r
10* substantial portions of the Software.\r
11*\r
12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
13* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
14* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
15* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
16* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
17*/\r
18\r
19#include "stdint.h"\r
20#include "USBMouse.h"\r
21\r
22bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {\r
23 switch (mouse_type) {\r
24 case REL_MOUSE:\r
25 while (x > 127) {\r
26 if (!mouseSend(127, 0, button, z)) return false;\r
27 x = x - 127;\r
28 }\r
29 while (x < -128) {\r
30 if (!mouseSend(-128, 0, button, z)) return false;\r
31 x = x + 128;\r
32 }\r
33 while (y > 127) {\r
34 if (!mouseSend(0, 127, button, z)) return false;\r
35 y = y - 127;\r
36 }\r
37 while (y < -128) {\r
38 if (!mouseSend(0, -128, button, z)) return false;\r
39 y = y + 128;\r
40 }\r
41 return mouseSend(x, y, button, z);\r
42 case ABS_MOUSE:\r
43 HID_REPORT report;\r
44\r
45 report.data[0] = x & 0xff;\r
46 report.data[1] = (x >> 8) & 0xff;\r
47 report.data[2] = y & 0xff;\r
48 report.data[3] = (y >> 8) & 0xff;\r
49 report.data[4] = -z;\r
50 report.data[5] = button & 0x07;\r
51\r
52 report.length = 6;\r
53\r
54 return send(&report);\r
55 default:\r
56 return false;\r
57 }\r
58}\r
59\r
60bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {\r
61 HID_REPORT report;\r
62 report.data[0] = buttons & 0x07;\r
63 report.data[1] = x;\r
64 report.data[2] = y;\r
65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up\r
66\r
67 report.length = 4;\r
68\r
69 return send(&report);\r
70}\r
71\r
72bool USBMouse::move(int16_t x, int16_t y) {\r
73 return update(x, y, button, 0);\r
74}\r
75\r
76bool USBMouse::scroll(int8_t z) {\r
77 return update(0, 0, button, z);\r
78}\r
79\r
80\r
81bool USBMouse::doubleClick() {\r
82 if (!click(MOUSE_LEFT))\r
83 return false;\r
84 wait(0.1);\r
85 return click(MOUSE_LEFT);\r
86}\r
87\r
88bool USBMouse::click(uint8_t button) {\r
89 if (!update(0, 0, button, 0))\r
90 return false;\r
91 wait(0.01);\r
92 return update(0, 0, 0, 0);\r
93}\r
94\r
95bool USBMouse::press(uint8_t button_) {\r
96 button = button_ & 0x07;\r
97 return update(0, 0, button, 0);\r
98}\r
99\r
100bool USBMouse::release(uint8_t button_) {\r
101 button = (button & (~button_)) & 0x07;\r
102 return update(0, 0, button, 0);\r
103}\r
104\r
105\r
106uint8_t * USBMouse::reportDesc() {\r
107\r
108 if (mouse_type == REL_MOUSE) {\r
109 static uint8_t reportDescriptor[] = {\r
110 USAGE_PAGE(1), 0x01, // Genric Desktop\r
111 USAGE(1), 0x02, // Mouse\r
112 COLLECTION(1), 0x01, // Application\r
113 USAGE(1), 0x01, // Pointer\r
114 COLLECTION(1), 0x00, // Physical\r
115\r
116 REPORT_COUNT(1), 0x03,\r
117 REPORT_SIZE(1), 0x01,\r
118 USAGE_PAGE(1), 0x09, // Buttons\r
119 USAGE_MINIMUM(1), 0x1,\r
120 USAGE_MAXIMUM(1), 0x3,\r
121 LOGICAL_MINIMUM(1), 0x00,\r
122 LOGICAL_MAXIMUM(1), 0x01,\r
123 INPUT(1), 0x02,\r
124 REPORT_COUNT(1), 0x01,\r
125 REPORT_SIZE(1), 0x05,\r
126 INPUT(1), 0x01,\r
127\r
128 REPORT_COUNT(1), 0x03,\r
129 REPORT_SIZE(1), 0x08,\r
130 USAGE_PAGE(1), 0x01,\r
131 USAGE(1), 0x30, // X\r
132 USAGE(1), 0x31, // Y\r
133 USAGE(1), 0x38, // scroll\r
134 LOGICAL_MINIMUM(1), 0x81,\r
135 LOGICAL_MAXIMUM(1), 0x7f,\r
136 INPUT(1), 0x06, // Relative data\r
137\r
138 END_COLLECTION(0),\r
139 END_COLLECTION(0),\r
140 };\r
141 reportLength = sizeof(reportDescriptor);\r
142 return reportDescriptor;\r
143 } else if (mouse_type == ABS_MOUSE) {\r
144 static uint8_t reportDescriptor[] = {\r
145\r
146 USAGE_PAGE(1), 0x01, // Generic Desktop\r
147 USAGE(1), 0x02, // Mouse\r
148 COLLECTION(1), 0x01, // Application\r
149 USAGE(1), 0x01, // Pointer\r
150 COLLECTION(1), 0x00, // Physical\r
151\r
152 USAGE_PAGE(1), 0x01, // Generic Desktop\r
153 USAGE(1), 0x30, // X\r
154 USAGE(1), 0x31, // Y\r
155 LOGICAL_MINIMUM(1), 0x00, // 0\r
156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767\r
157 REPORT_SIZE(1), 0x10,\r
158 REPORT_COUNT(1), 0x02,\r
159 INPUT(1), 0x02, // Data, Variable, Absolute\r
160\r
161 USAGE_PAGE(1), 0x01, // Generic Desktop\r
162 USAGE(1), 0x38, // scroll\r
163 LOGICAL_MINIMUM(1), 0x81, // -127\r
164 LOGICAL_MAXIMUM(1), 0x7f, // 127\r
165 REPORT_SIZE(1), 0x08,\r
166 REPORT_COUNT(1), 0x01,\r
167 INPUT(1), 0x06, // Data, Variable, Relative\r
168\r
169 USAGE_PAGE(1), 0x09, // Buttons\r
170 USAGE_MINIMUM(1), 0x01,\r
171 USAGE_MAXIMUM(1), 0x03,\r
172 LOGICAL_MINIMUM(1), 0x00, // 0\r
173 LOGICAL_MAXIMUM(1), 0x01, // 1\r
174 REPORT_COUNT(1), 0x03,\r
175 REPORT_SIZE(1), 0x01,\r
176 INPUT(1), 0x02, // Data, Variable, Absolute\r
177 REPORT_COUNT(1), 0x01,\r
178 REPORT_SIZE(1), 0x05,\r
179 INPUT(1), 0x01, // Constant\r
180\r
181 END_COLLECTION(0),\r
182 END_COLLECTION(0)\r
183 };\r
184 reportLength = sizeof(reportDescriptor);\r
185 return reportDescriptor;\r
186 }\r
187 return NULL;\r
188}\r
189\r
190\r