335 final proj

Dependencies:   mbed USBDevice

Committer:
jresnik
Date:
Mon Nov 23 19:44:11 2020 +0000
Revision:
0:aee369fac3bd
Initial commit

Who changed what in which revision?

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