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