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 #include "mbed.h" 00022 00023 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) { 00024 switch (mouse_type) { 00025 case REL_MOUSE: 00026 while (x > 127) { 00027 if (!mouseSend(127, 0, button, z)) return false; 00028 x = x - 127; 00029 } 00030 while (x < -128) { 00031 if (!mouseSend(-128, 0, button, z)) return false; 00032 x = x + 128; 00033 } 00034 while (y > 127) { 00035 if (!mouseSend(0, 127, button, z)) return false; 00036 y = y - 127; 00037 } 00038 while (y < -128) { 00039 if (!mouseSend(0, -128, button, z)) return false; 00040 y = y + 128; 00041 } 00042 return mouseSend(x, y, button, z); 00043 case ABS_MOUSE: 00044 HID_REPORT report; 00045 00046 report.data[0] = x & 0xff; 00047 report.data[1] = (x >> 8) & 0xff; 00048 report.data[2] = y & 0xff; 00049 report.data[3] = (y >> 8) & 0xff; 00050 report.data[4] = -z; 00051 report.data[5] = button & 0x07; 00052 00053 report.length = 6; 00054 00055 return send(&report); 00056 default: 00057 return false; 00058 } 00059 } 00060 00061 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { 00062 HID_REPORT report; 00063 report.data[0] = buttons & 0x07; 00064 report.data[1] = x; 00065 report.data[2] = y; 00066 report.data[3] = -z; // >0 to scroll down, <0 to scroll up 00067 00068 report.length = 4; 00069 00070 return send(&report); 00071 } 00072 00073 bool USBMouse::move(int16_t x, int16_t y) { 00074 return update(x, y, button, 0); 00075 } 00076 00077 bool USBMouse::scroll(int8_t z) { 00078 return update(0, 0, button, z); 00079 } 00080 00081 00082 bool USBMouse::doubleClick() { 00083 if (!click(MOUSE_LEFT)) 00084 return false; 00085 wait(0.1); 00086 return click(MOUSE_LEFT); 00087 } 00088 00089 bool USBMouse::click(uint8_t button) { 00090 if (!update(0, 0, button, 0)) 00091 return false; 00092 wait(0.01); 00093 return update(0, 0, 0, 0); 00094 } 00095 00096 bool USBMouse::press(uint8_t button_) { 00097 button = button_ & 0x07; 00098 return update(0, 0, button, 0); 00099 } 00100 00101 bool USBMouse::release(uint8_t button_) { 00102 button = (button & (~button_)) & 0x07; 00103 return update(0, 0, button, 0); 00104 } 00105 00106 00107 uint8_t * USBMouse::reportDesc() { 00108 00109 if (mouse_type == REL_MOUSE) { 00110 static uint8_t reportDescriptor[] = { 00111 USAGE_PAGE(1), 0x01, // Genric Desktop 00112 USAGE(1), 0x02, // Mouse 00113 COLLECTION(1), 0x01, // Application 00114 USAGE(1), 0x01, // Pointer 00115 COLLECTION(1), 0x00, // Physical 00116 00117 REPORT_COUNT(1), 0x03, 00118 REPORT_SIZE(1), 0x01, 00119 USAGE_PAGE(1), 0x09, // Buttons 00120 USAGE_MINIMUM(1), 0x1, 00121 USAGE_MAXIMUM(1), 0x3, 00122 LOGICAL_MINIMUM(1), 0x00, 00123 LOGICAL_MAXIMUM(1), 0x01, 00124 INPUT(1), 0x02, 00125 REPORT_COUNT(1), 0x01, 00126 REPORT_SIZE(1), 0x05, 00127 INPUT(1), 0x01, 00128 00129 REPORT_COUNT(1), 0x03, 00130 REPORT_SIZE(1), 0x08, 00131 USAGE_PAGE(1), 0x01, 00132 USAGE(1), 0x30, // X 00133 USAGE(1), 0x31, // Y 00134 USAGE(1), 0x38, // scroll 00135 LOGICAL_MINIMUM(1), 0x81, 00136 LOGICAL_MAXIMUM(1), 0x7f, 00137 INPUT(1), 0x06, // Relative data 00138 00139 END_COLLECTION(0), 00140 END_COLLECTION(0), 00141 }; 00142 reportLength = sizeof(reportDescriptor); 00143 return reportDescriptor; 00144 } else if (mouse_type == ABS_MOUSE) { 00145 static uint8_t reportDescriptor[] = { 00146 00147 USAGE_PAGE(1), 0x01, // Generic Desktop 00148 USAGE(1), 0x02, // Mouse 00149 COLLECTION(1), 0x01, // Application 00150 USAGE(1), 0x01, // Pointer 00151 COLLECTION(1), 0x00, // Physical 00152 00153 USAGE_PAGE(1), 0x01, // Generic Desktop 00154 USAGE(1), 0x30, // X 00155 USAGE(1), 0x31, // Y 00156 LOGICAL_MINIMUM(1), 0x00, // 0 00157 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 00158 REPORT_SIZE(1), 0x10, 00159 REPORT_COUNT(1), 0x02, 00160 INPUT(1), 0x02, // Data, Variable, Absolute 00161 00162 USAGE_PAGE(1), 0x01, // Generic Desktop 00163 USAGE(1), 0x38, // scroll 00164 LOGICAL_MINIMUM(1), 0x81, // -127 00165 LOGICAL_MAXIMUM(1), 0x7f, // 127 00166 REPORT_SIZE(1), 0x08, 00167 REPORT_COUNT(1), 0x01, 00168 INPUT(1), 0x06, // Data, Variable, Relative 00169 00170 USAGE_PAGE(1), 0x09, // Buttons 00171 USAGE_MINIMUM(1), 0x01, 00172 USAGE_MAXIMUM(1), 0x03, 00173 LOGICAL_MINIMUM(1), 0x00, // 0 00174 LOGICAL_MAXIMUM(1), 0x01, // 1 00175 REPORT_COUNT(1), 0x03, 00176 REPORT_SIZE(1), 0x01, 00177 INPUT(1), 0x02, // Data, Variable, Absolute 00178 REPORT_COUNT(1), 0x01, 00179 REPORT_SIZE(1), 0x05, 00180 INPUT(1), 0x01, // Constant 00181 00182 END_COLLECTION(0), 00183 END_COLLECTION(0) 00184 }; 00185 reportLength = sizeof(reportDescriptor); 00186 return reportDescriptor; 00187 } 00188 return NULL; 00189 } 00190 00191 #define DEFAULT_CONFIGURATION (1) 00192 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ 00193 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ 00194 + (1 * HID_DESCRIPTOR_LENGTH) \ 00195 + (2 * ENDPOINT_DESCRIPTOR_LENGTH)) 00196 00197 uint8_t * USBMouse::configurationDesc() { 00198 static uint8_t configurationDescriptor[] = { 00199 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength 00200 CONFIGURATION_DESCRIPTOR, // bDescriptorType 00201 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) 00202 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) 00203 0x01, // bNumInterfaces 00204 DEFAULT_CONFIGURATION, // bConfigurationValue 00205 0x00, // iConfiguration 00206 C_RESERVED | C_SELF_POWERED, // bmAttributes 00207 C_POWER(0), // bMaxPowerHello World from Mbed 00208 00209 INTERFACE_DESCRIPTOR_LENGTH, // bLength 00210 INTERFACE_DESCRIPTOR, // bDescriptorType 00211 0x00, // bInterfaceNumber 00212 0x00, // bAlternateSetting 00213 0x02, // bNumEndpoints 00214 HID_CLASS, // bInterfaceClass 00215 1, // bInterfaceSubClass 00216 2, // bInterfaceProtocol (mouse) 00217 0x00, // iInterface 00218 00219 HID_DESCRIPTOR_LENGTH, // bLength 00220 HID_DESCRIPTOR, // bDescriptorType 00221 LSB(HID_VERSION_1_11), // bcdHID (LSB) 00222 MSB(HID_VERSION_1_11), // bcdHID (MSB) 00223 0x00, // bCountryCode 00224 0x01, // bNumDescriptors 00225 REPORT_DESCRIPTOR, // bDescriptorType 00226 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB) 00227 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB) 00228 00229 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00230 ENDPOINT_DESCRIPTOR, // bDescriptorType 00231 PHY_TO_DESC(EPINT_IN), // bEndpointAddress 00232 E_INTERRUPT, // bmAttributes 00233 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00234 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00235 1, // bInterval (milliseconds) 00236 00237 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00238 ENDPOINT_DESCRIPTOR, // bDescriptorType 00239 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress 00240 E_INTERRUPT, // bmAttributes 00241 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00242 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00243 1, // bInterval (milliseconds) 00244 }; 00245 return configurationDescriptor; 00246 }
Generated on Fri Jul 15 2022 03:02:00 by
1.7.2
