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 USBHost by
USBHostMouse.cpp
00001 /* mbed USBHost Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "USBHostMouse.h" 00018 00019 #if USBHOST_MOUSE 00020 00021 USBHostMouse::USBHostMouse() { 00022 host = USBHost::getHostInst(); 00023 init(); 00024 } 00025 00026 void USBHostMouse::init() { 00027 dev = NULL; 00028 int_in = NULL; 00029 onUpdate = NULL; 00030 onButtonUpdate = NULL; 00031 onXUpdate = NULL; 00032 onYUpdate = NULL; 00033 onZUpdate = NULL; 00034 report_id = 0; 00035 dev_connected = false; 00036 mouse_device_found = false; 00037 mouse_intf = -1; 00038 00039 buttons = 0; 00040 x = 0; 00041 y = 0; 00042 z = 0; 00043 } 00044 00045 bool USBHostMouse::connected() { 00046 return dev_connected; 00047 } 00048 00049 bool USBHostMouse::connect() { 00050 int len_listen; 00051 00052 if (dev_connected) { 00053 return true; 00054 } 00055 00056 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00057 if ((dev = host->getDevice(i)) != NULL) { 00058 00059 if(host->enumerate(dev, this)) 00060 break; 00061 00062 if (mouse_device_found) { 00063 00064 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN); 00065 if (!int_in) 00066 break; 00067 00068 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf); 00069 dev->setName("Mouse", mouse_intf); 00070 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init); 00071 00072 int_in->attach(this, &USBHostMouse::rxHandler); 00073 len_listen = int_in->getSize(); 00074 if (len_listen > sizeof(report)) { 00075 len_listen = sizeof(report); 00076 } 00077 host->interruptRead(dev, int_in, report, len_listen, false); 00078 00079 dev_connected = true; 00080 return true; 00081 } 00082 } 00083 } 00084 init(); 00085 return false; 00086 } 00087 00088 void USBHostMouse::rxHandler() { 00089 int len_listen = int_in->getSize(); 00090 00091 if (onUpdate) { 00092 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]); 00093 } 00094 00095 if (onButtonUpdate && (buttons != (report[0] & 0x07))) { 00096 (*onButtonUpdate)(report[0] & 0x07); 00097 } 00098 00099 if (onXUpdate && (x != report[1])) { 00100 (*onXUpdate)(report[1]); 00101 } 00102 00103 if (onYUpdate && (y != report[2])) { 00104 (*onYUpdate)(report[2]); 00105 } 00106 00107 if (onZUpdate && (z != report[3])) { 00108 (*onZUpdate)(report[3]); 00109 } 00110 00111 // update mouse state 00112 buttons = report[0] & 0x07; 00113 x = report[1]; 00114 y = report[2]; 00115 z = report[3]; 00116 00117 if (len_listen > sizeof(report)) { 00118 len_listen = sizeof(report); 00119 } 00120 00121 if (dev) 00122 host->interruptRead(dev, int_in, report, len_listen, false); 00123 } 00124 00125 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid) 00126 { 00127 // we don't check VID/PID for mouse driver 00128 } 00129 00130 /*virtual*/ bool USBHostMouse::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed 00131 { 00132 if ((mouse_intf == -1) && 00133 (intf_class == HID_CLASS) && 00134 (intf_subclass == 0x01) && 00135 (intf_protocol == 0x02)) { 00136 mouse_intf = intf_nb; 00137 return true; 00138 } 00139 return false; 00140 } 00141 00142 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00143 { 00144 if (intf_nb == mouse_intf) { 00145 if (type == INTERRUPT_ENDPOINT && dir == IN) { 00146 mouse_device_found = true; 00147 return true; 00148 } 00149 } 00150 return false; 00151 } 00152 00153 #endif
Generated on Fri Jul 15 2022 11:42:47 by
1.7.2
