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