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.
Dependencies: ChaNFSSD mbed ChaNFS
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 HID_REPORT report; 00044 00045 report.data[0] = x & 0xff; 00046 report.data[1] = (x >> 8) & 0xff; 00047 report.data[2] = y & 0xff; 00048 report.data[3] = (y >> 8) & 0xff; 00049 report.data[4] = -z; 00050 report.data[5] = button & 0x07; 00051 00052 report.length = 6; 00053 00054 return send(&report); 00055 default: 00056 return false; 00057 } 00058 } 00059 00060 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { 00061 HID_REPORT report; 00062 report.data[0] = buttons & 0x07; 00063 report.data[1] = x; 00064 report.data[2] = y; 00065 report.data[3] = -z; // >0 to scroll down, <0 to scroll up 00066 00067 report.length = 4; 00068 00069 return send(&report); 00070 } 00071 00072 bool USBMouse::move(int16_t x, int16_t y) { 00073 return update(x, y, button, 0); 00074 } 00075 00076 bool USBMouse::scroll(int8_t z) { 00077 return update(0, 0, button, z); 00078 } 00079 00080 00081 bool USBMouse::doubleClick() { 00082 if (!click(MOUSE_LEFT)) 00083 return false; 00084 wait(0.1); 00085 return click(MOUSE_LEFT); 00086 } 00087 00088 bool USBMouse::click(uint8_t button) { 00089 if (!update(0, 0, button, 0)) 00090 return false; 00091 wait(0.01); 00092 return update(0, 0, 0, 0); 00093 } 00094 00095 bool USBMouse::press(uint8_t button_) { 00096 button = button_ & 0x07; 00097 return update(0, 0, button, 0); 00098 } 00099 00100 bool USBMouse::release(uint8_t button_) { 00101 button = (button & (~button_)) & 0x07; 00102 return update(0, 0, button, 0); 00103 } 00104 00105 00106 uint8_t * USBMouse::reportDesc() { 00107 00108 if (mouse_type == REL_MOUSE) { 00109 static uint8_t reportDescriptor[] = { 00110 USAGE_PAGE(1), 0x01, // Genric Desktop 00111 USAGE(1), 0x02, // Mouse 00112 COLLECTION(1), 0x01, // Application 00113 USAGE(1), 0x01, // Pointer 00114 COLLECTION(1), 0x00, // Physical 00115 00116 REPORT_COUNT(1), 0x03, 00117 REPORT_SIZE(1), 0x01, 00118 USAGE_PAGE(1), 0x09, // Buttons 00119 USAGE_MINIMUM(1), 0x1, 00120 USAGE_MAXIMUM(1), 0x3, 00121 LOGICAL_MINIMUM(1), 0x00, 00122 LOGICAL_MAXIMUM(1), 0x01, 00123 INPUT(1), 0x02, 00124 REPORT_COUNT(1), 0x01, 00125 REPORT_SIZE(1), 0x05, 00126 INPUT(1), 0x01, 00127 00128 REPORT_COUNT(1), 0x03, 00129 REPORT_SIZE(1), 0x08, 00130 USAGE_PAGE(1), 0x01, 00131 USAGE(1), 0x30, // X 00132 USAGE(1), 0x31, // Y 00133 USAGE(1), 0x38, // scroll 00134 LOGICAL_MINIMUM(1), 0x81, 00135 LOGICAL_MAXIMUM(1), 0x7f, 00136 INPUT(1), 0x06, // Relative data 00137 00138 END_COLLECTION(0), 00139 END_COLLECTION(0), 00140 }; 00141 reportLength = sizeof(reportDescriptor); 00142 return reportDescriptor; 00143 } else if (mouse_type == ABS_MOUSE) { 00144 static uint8_t reportDescriptor[] = { 00145 00146 USAGE_PAGE(1), 0x01, // Generic Desktop 00147 USAGE(1), 0x02, // Mouse 00148 COLLECTION(1), 0x01, // Application 00149 USAGE(1), 0x01, // Pointer 00150 COLLECTION(1), 0x00, // Physical 00151 00152 USAGE_PAGE(1), 0x01, // Generic Desktop 00153 USAGE(1), 0x30, // X 00154 USAGE(1), 0x31, // Y 00155 LOGICAL_MINIMUM(1), 0x00, // 0 00156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 00157 REPORT_SIZE(1), 0x10, 00158 REPORT_COUNT(1), 0x02, 00159 INPUT(1), 0x02, // Data, Variable, Absolute 00160 00161 USAGE_PAGE(1), 0x01, // Generic Desktop 00162 USAGE(1), 0x38, // scroll 00163 LOGICAL_MINIMUM(1), 0x81, // -127 00164 LOGICAL_MAXIMUM(1), 0x7f, // 127 00165 REPORT_SIZE(1), 0x08, 00166 REPORT_COUNT(1), 0x01, 00167 INPUT(1), 0x06, // Data, Variable, Relative 00168 00169 USAGE_PAGE(1), 0x09, // Buttons 00170 USAGE_MINIMUM(1), 0x01, 00171 USAGE_MAXIMUM(1), 0x03, 00172 LOGICAL_MINIMUM(1), 0x00, // 0 00173 LOGICAL_MAXIMUM(1), 0x01, // 1 00174 REPORT_COUNT(1), 0x03, 00175 REPORT_SIZE(1), 0x01, 00176 INPUT(1), 0x02, // Data, Variable, Absolute 00177 REPORT_COUNT(1), 0x01, 00178 REPORT_SIZE(1), 0x05, 00179 INPUT(1), 0x01, // Constant 00180 00181 END_COLLECTION(0), 00182 END_COLLECTION(0) 00183 }; 00184 reportLength = sizeof(reportDescriptor); 00185 return reportDescriptor; 00186 } 00187 return NULL; 00188 } 00189 00190
Generated on Wed Jul 13 2022 11:42:57 by
1.7.2