USBMOUSE
Dependents: ESD_Project_USBMouse
USBMouse.cpp
00001 00002 00003 #include "stdint.h" 00004 #include "USBMouse.h" 00005 00006 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) { 00007 switch (mouse_type) { 00008 case REL_MOUSE: 00009 while (x > 127) { 00010 if (!mouseSend(127, 0, button, z)) return false; 00011 x = x - 127; 00012 } 00013 while (x < -128) { 00014 if (!mouseSend(-128, 0, button, z)) return false; 00015 x = x + 128; 00016 } 00017 while (y > 127) { 00018 if (!mouseSend(0, 127, button, z)) return false; 00019 y = y - 127; 00020 } 00021 while (y < -128) { 00022 if (!mouseSend(0, -128, button, z)) return false; 00023 y = y + 128; 00024 } 00025 return mouseSend(x, y, button, z); 00026 00027 default: 00028 return false; 00029 } 00030 } 00031 00032 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { 00033 HID_REPORT report; 00034 report.data[0] = buttons & 0x07; 00035 report.data[1] = x; 00036 report.data[2] = y; 00037 report.data[3] = -z; // >0 to scroll down, <0 to scroll up 00038 00039 report.length = 4; 00040 00041 return send(&report); 00042 } 00043 00044 bool USBMouse::move(int16_t x, int16_t y) { 00045 return update(x, y, button, 0); 00046 } 00047 00048 bool USBMouse::scroll(int8_t z) { 00049 return update(0, 0, button, z); 00050 } 00051 00052 00053 bool USBMouse::doubleClick() { 00054 if (!click(MOUSE_LEFT)) 00055 return false; 00056 wait(0.1); 00057 return click(MOUSE_LEFT); 00058 } 00059 00060 bool USBMouse::click(uint8_t button) { 00061 if (!update(0, 0, button, 0)) 00062 return false; 00063 wait(0.01); 00064 return update(0, 0, 0, 0); 00065 } 00066 00067 bool USBMouse::press(uint8_t button_) { 00068 button = button_ & 0x07; 00069 return update(0, 0, button, 0); 00070 } 00071 00072 bool USBMouse::release(uint8_t button_) { 00073 button = (button & (~button_)) & 0x07; 00074 return update(0, 0, button, 0); 00075 } 00076 00077 00078 uint8_t * USBMouse::reportDesc() { 00079 00080 if (mouse_type == REL_MOUSE) { 00081 static uint8_t reportDescriptor[] = { 00082 USAGE_PAGE(1), 0x01, // Genric Desktop 00083 USAGE(1), 0x02, // Mouse 00084 COLLECTION(1), 0x01, // Application 00085 USAGE(1), 0x01, // Pointer 00086 COLLECTION(1), 0x00, // Physical 00087 00088 REPORT_COUNT(1), 0x03, 00089 REPORT_SIZE(1), 0x01, 00090 USAGE_PAGE(1), 0x09, // Buttons 00091 USAGE_MINIMUM(1), 0x1, 00092 USAGE_MAXIMUM(1), 0x3, 00093 LOGICAL_MINIMUM(1), 0x00, 00094 LOGICAL_MAXIMUM(1), 0x01, 00095 INPUT(1), 0x02, 00096 REPORT_COUNT(1), 0x01, 00097 REPORT_SIZE(1), 0x05, 00098 INPUT(1), 0x01, 00099 00100 REPORT_COUNT(1), 0x03, 00101 REPORT_SIZE(1), 0x08, 00102 USAGE_PAGE(1), 0x01, 00103 USAGE(1), 0x30, // X 00104 USAGE(1), 0x31, // Y 00105 USAGE(1), 0x38, // scroll 00106 LOGICAL_MINIMUM(1), 0x81, 00107 LOGICAL_MAXIMUM(1), 0x7f, 00108 INPUT(1), 0x06, // Relative data 00109 00110 END_COLLECTION(0), 00111 END_COLLECTION(0), 00112 }; 00113 reportLength = sizeof(reportDescriptor); 00114 return reportDescriptor; 00115 } 00116 // reportLength = sizeof(reportDescriptor); 00117 //return reportDescriptor; 00118 00119 return NULL; 00120 } 00121 00122 #define DEFAULT_CONFIGURATION (1) 00123 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ 00124 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ 00125 + (1 * HID_DESCRIPTOR_LENGTH) \ 00126 + (2 * ENDPOINT_DESCRIPTOR_LENGTH)) 00127 00128 uint8_t * USBMouse::configurationDesc() { 00129 static uint8_t configurationDescriptor[] = { 00130 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength 00131 CONFIGURATION_DESCRIPTOR, // bDescriptorType 00132 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) 00133 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) 00134 0x01, // bNumInterfaces 00135 DEFAULT_CONFIGURATION, // bConfigurationValue 00136 0x00, // iConfiguration 00137 C_RESERVED | C_SELF_POWERED, // bmAttributes 00138 C_POWER(0), // bMaxPowerHello World from Mbed 00139 00140 INTERFACE_DESCRIPTOR_LENGTH, // bLength 00141 INTERFACE_DESCRIPTOR, // bDescriptorType 00142 0x00, // bInterfaceNumber 00143 0x00, // bAlternateSetting 00144 0x02, // bNumEndpoints 00145 HID_CLASS, // bInterfaceClass 00146 1, // bInterfaceSubClass 00147 2, // bInterfaceProtocol (mouse) 00148 0x00, // iInterface 00149 00150 HID_DESCRIPTOR_LENGTH, // bLength 00151 HID_DESCRIPTOR, // bDescriptorType 00152 LSB(HID_VERSION_1_11), // bcdHID (LSB) 00153 MSB(HID_VERSION_1_11), // bcdHID (MSB) 00154 0x00, // bCountryCode 00155 0x01, // bNumDescriptors 00156 REPORT_DESCRIPTOR, // bDescriptorType 00157 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB) 00158 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB) 00159 00160 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00161 ENDPOINT_DESCRIPTOR, // bDescriptorType 00162 PHY_TO_DESC(EPINT_IN), // bEndpointAddress 00163 E_INTERRUPT, // bmAttributes 00164 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00165 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00166 1, // bInterval (milliseconds) 00167 00168 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00169 ENDPOINT_DESCRIPTOR, // bDescriptorType 00170 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress 00171 E_INTERRUPT, // bmAttributes 00172 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00173 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00174 1, // bInterval (milliseconds) 00175 }; 00176 return configurationDescriptor; 00177 }
Generated on Sat Jul 16 2022 01:37:14 by
1.7.2