Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBDevice by
USBMouse.cpp
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "stdint.h" 00020 #include "USBMouse.h" 00021 00022 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) { 00023 switch (mouse_type) { 00024 case REL_MOUSE: 00025 while (x > 127) { 00026 if (!mouseSend(127, 0, button, z)) return false; 00027 x = x - 127; 00028 } 00029 while (x < -128) { 00030 if (!mouseSend(-128, 0, button, z)) return false; 00031 x = x + 128; 00032 } 00033 while (y > 127) { 00034 if (!mouseSend(0, 127, button, z)) return false; 00035 y = y - 127; 00036 } 00037 while (y < -128) { 00038 if (!mouseSend(0, -128, button, z)) return false; 00039 y = y + 128; 00040 } 00041 return mouseSend(x, y, button, z); 00042 case ABS_MOUSE: 00043 report.data[0] = x & 0xff; 00044 report.data[1] = (x >> 8) & 0xff; 00045 report.data[2] = y & 0xff; 00046 report.data[3] = (y >> 8) & 0xff; 00047 report.data[4] = -z; 00048 report.data[5] = button & 0x07; 00049 00050 report.length = 6; 00051 00052 return send(&report); 00053 default: 00054 return false; 00055 } 00056 } 00057 00058 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { 00059 report.data[0] = buttons & 0x07; 00060 report.data[1] = x; 00061 report.data[2] = y; 00062 report.data[3] = -z; // >0 to scroll down, <0 to scroll up 00063 00064 report.length = 4; 00065 00066 return send(&report); 00067 } 00068 00069 bool USBMouse::move(int16_t x, int16_t y) { 00070 return update(x, y, button, 0); 00071 } 00072 00073 bool USBMouse::scroll(int8_t z) { 00074 return update(0, 0, button, z); 00075 } 00076 00077 00078 bool USBMouse::doubleClick() { 00079 if (!click(MOUSE_LEFT)) 00080 return false; 00081 wait(0.1); 00082 return click(MOUSE_LEFT); 00083 } 00084 00085 bool USBMouse::click(uint8_t button) { 00086 if (!update(0, 0, button, 0)) 00087 return false; 00088 wait(0.01); 00089 return update(0, 0, 0, 0); 00090 } 00091 00092 bool USBMouse::press(uint8_t button_) { 00093 button = button_ & 0x07; 00094 return update(0, 0, button, 0); 00095 } 00096 00097 bool USBMouse::release(uint8_t button_) { 00098 button = (button & (~button_)) & 0x07; 00099 return update(0, 0, button, 0); 00100 } 00101 00102 00103 uint8_t * USBMouse::reportDesc() { 00104 00105 if (mouse_type == REL_MOUSE) { 00106 static uint8_t reportDescriptor[] = { 00107 USAGE_PAGE(1), 0x01, // Genric Desktop 00108 USAGE(1), 0x02, // Mouse 00109 COLLECTION(1), 0x01, // Application 00110 USAGE(1), 0x01, // Pointer 00111 COLLECTION(1), 0x00, // Physical 00112 00113 REPORT_COUNT(1), 0x03, 00114 REPORT_SIZE(1), 0x01, 00115 USAGE_PAGE(1), 0x09, // Buttons 00116 USAGE_MINIMUM(1), 0x1, 00117 USAGE_MAXIMUM(1), 0x3, 00118 LOGICAL_MINIMUM(1), 0x00, 00119 LOGICAL_MAXIMUM(1), 0x01, 00120 INPUT(1), 0x02, 00121 REPORT_COUNT(1), 0x01, 00122 REPORT_SIZE(1), 0x05, 00123 INPUT(1), 0x01, 00124 00125 REPORT_COUNT(1), 0x03, 00126 REPORT_SIZE(1), 0x08, 00127 USAGE_PAGE(1), 0x01, 00128 USAGE(1), 0x30, // X 00129 USAGE(1), 0x31, // Y 00130 USAGE(1), 0x38, // scroll 00131 LOGICAL_MINIMUM(1), 0x81, 00132 LOGICAL_MAXIMUM(1), 0x7f, 00133 INPUT(1), 0x06, // Relative data 00134 00135 END_COLLECTION(0), 00136 END_COLLECTION(0), 00137 }; 00138 reportLength = sizeof(reportDescriptor); 00139 return reportDescriptor; 00140 } else if (mouse_type == ABS_MOUSE) { 00141 static uint8_t reportDescriptor[] = { 00142 00143 USAGE_PAGE(1), 0x01, // Generic Desktop 00144 USAGE(1), 0x02, // Mouse 00145 COLLECTION(1), 0x01, // Application 00146 USAGE(1), 0x01, // Pointer 00147 COLLECTION(1), 0x00, // Physical 00148 00149 USAGE_PAGE(1), 0x01, // Generic Desktop 00150 USAGE(1), 0x30, // X 00151 USAGE(1), 0x31, // Y 00152 LOGICAL_MINIMUM(1), 0x00, // 0 00153 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 00154 REPORT_SIZE(1), 0x10, 00155 REPORT_COUNT(1), 0x02, 00156 INPUT(1), 0x02, // Data, Variable, Absolute 00157 00158 USAGE_PAGE(1), 0x01, // Generic Desktop 00159 USAGE(1), 0x38, // scroll 00160 LOGICAL_MINIMUM(1), 0x81, // -127 00161 LOGICAL_MAXIMUM(1), 0x7f, // 127 00162 REPORT_SIZE(1), 0x08, 00163 REPORT_COUNT(1), 0x01, 00164 INPUT(1), 0x06, // Data, Variable, Relative 00165 00166 USAGE_PAGE(1), 0x09, // Buttons 00167 USAGE_MINIMUM(1), 0x01, 00168 USAGE_MAXIMUM(1), 0x03, 00169 LOGICAL_MINIMUM(1), 0x00, // 0 00170 LOGICAL_MAXIMUM(1), 0x01, // 1 00171 REPORT_COUNT(1), 0x03, 00172 REPORT_SIZE(1), 0x01, 00173 INPUT(1), 0x02, // Data, Variable, Absolute 00174 REPORT_COUNT(1), 0x01, 00175 REPORT_SIZE(1), 0x05, 00176 INPUT(1), 0x01, // Constant 00177 00178 END_COLLECTION(0), 00179 END_COLLECTION(0) 00180 }; 00181 reportLength = sizeof(reportDescriptor); 00182 return reportDescriptor; 00183 } 00184 return NULL; 00185 } 00186 00187 #define DEFAULT_CONFIGURATION (1) 00188 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ 00189 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ 00190 + (1 * HID_DESCRIPTOR_LENGTH) \ 00191 + (2 * ENDPOINT_DESCRIPTOR_LENGTH)) 00192 00193 uint8_t * USBMouse::configurationDesc() { 00194 static uint8_t configurationDescriptor[] = { 00195 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength 00196 CONFIGURATION_DESCRIPTOR, // bDescriptorType 00197 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) 00198 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) 00199 0x01, // bNumInterfaces 00200 DEFAULT_CONFIGURATION, // bConfigurationValue 00201 0x00, // iConfiguration 00202 C_RESERVED | C_SELF_POWERED, // bmAttributes 00203 C_POWER(0), // bMaxPowerHello World from Mbed 00204 00205 INTERFACE_DESCRIPTOR_LENGTH, // bLength 00206 INTERFACE_DESCRIPTOR, // bDescriptorType 00207 0x00, // bInterfaceNumber 00208 0x00, // bAlternateSetting 00209 0x02, // bNumEndpoints 00210 HID_CLASS, // bInterfaceClass 00211 1, // bInterfaceSubClass 00212 2, // bInterfaceProtocol (mouse) 00213 0x00, // iInterface 00214 00215 HID_DESCRIPTOR_LENGTH, // bLength 00216 HID_DESCRIPTOR, // bDescriptorType 00217 LSB(HID_VERSION_1_11), // bcdHID (LSB) 00218 MSB(HID_VERSION_1_11), // bcdHID (MSB) 00219 0x00, // bCountryCode 00220 0x01, // bNumDescriptors 00221 REPORT_DESCRIPTOR, // bDescriptorType 00222 LSB(reportDescLength()), // wDescriptorLength (LSB) 00223 MSB(reportDescLength()), // wDescriptorLength (MSB) 00224 00225 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00226 ENDPOINT_DESCRIPTOR, // bDescriptorType 00227 PHY_TO_DESC(EPINT_IN), // bEndpointAddress 00228 E_INTERRUPT, // bmAttributes 00229 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00230 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00231 1, // bInterval (milliseconds) 00232 00233 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00234 ENDPOINT_DESCRIPTOR, // bDescriptorType 00235 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress 00236 E_INTERRUPT, // bmAttributes 00237 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00238 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00239 1, // bInterval (milliseconds) 00240 }; 00241 return configurationDescriptor; 00242 }
Generated on Tue Jul 12 2022 20:53:38 by
1.7.2
