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