USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

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