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