Add USB HID Gamepad hosting support to mbed board (depends on USBHost library)
Dependents: mbed_gamepad_example
USBHostGamepad.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 "USBHostGamepad.h" 00018 00019 #if USBHOST_GAMEPAD 00020 00021 USBHostGamepad::USBHostGamepad() 00022 { 00023 host = USBHost::getHostInst(); 00024 init(); 00025 } 00026 00027 void USBHostGamepad::init() 00028 { 00029 dev = NULL; 00030 int_in = NULL; 00031 onUpdate = NULL; 00032 report_id = 0; 00033 dev_connected = false; 00034 gamepad_device_found = false; 00035 gamepad_intf = -1; 00036 } 00037 00038 bool USBHostGamepad::connected() { 00039 return dev_connected; 00040 } 00041 00042 bool USBHostGamepad::connect() 00043 { 00044 int len_listen; 00045 00046 if (dev_connected) { 00047 return true; 00048 } 00049 00050 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00051 00052 if (dev = host->getDevice(i)) { 00053 if(host->enumerate(dev, this)) { 00054 break; 00055 } 00056 if (gamepad_device_found) { 00057 { 00058 /* As this is done in a specific thread 00059 * this lock is taken to avoid to process the device 00060 * disconnect in usb process during the device registering */ 00061 USBHost::Lock Lock(host); 00062 00063 int_in = dev->getEndpoint(gamepad_intf, INTERRUPT_ENDPOINT, IN); 00064 if (!int_in) { 00065 break; 00066 } 00067 00068 USB_INFO("New Gamepad device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, gamepad_intf); 00069 dev->setName("Gamepad", gamepad_intf); 00070 host->registerDriver(dev, gamepad_intf, this, &USBHostGamepad::init); 00071 00072 int_in->attach(this, &USBHostGamepad::rxHandler); 00073 len_listen = int_in->getSize(); 00074 if (len_listen > sizeof(report)) { 00075 len_listen = sizeof(report); 00076 } 00077 } 00078 int ret=host->interruptRead(dev, int_in, report, len_listen, false); 00079 MBED_ASSERT((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING) || (ret == USB_TYPE_FREE)); 00080 if ((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING)) { 00081 dev_connected = true; 00082 } 00083 if (ret == USB_TYPE_FREE) { 00084 dev_connected = false; 00085 } 00086 return true; 00087 } 00088 } 00089 } 00090 init(); 00091 return false; 00092 } 00093 00094 void USBHostGamepad::rxHandler() 00095 { 00096 int len_listen = int_in->getLengthTransferred(); 00097 00098 if (len_listen !=0) { 00099 uint16_t buttons = 0x0; 00100 if (onUpdate) { 00101 buttons |= ((report[5] & 0xF0) >> 4) | (report[6] << 4); 00102 (*onUpdate)(report[0], report[1], report[3], report[4], buttons); 00103 } 00104 } 00105 00106 /* set again the maximum value */ 00107 len_listen = int_in->getSize(); 00108 00109 if (len_listen > sizeof(report)) { 00110 len_listen = sizeof(report); 00111 } 00112 00113 if (dev) { 00114 host->interruptRead(dev, int_in, report, len_listen, false); 00115 } 00116 } 00117 00118 /*virtual*/ void USBHostGamepad::setVidPid(uint16_t vid, uint16_t pid) 00119 { 00120 // we don't check VID/PID for Gamepad driver 00121 USB_INFO("VID: %X, PID: %X\n\r", vid, pid); 00122 } 00123 00124 /*virtual*/ bool USBHostGamepad::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 00125 { 00126 if ((gamepad_intf == -1) && 00127 (intf_class == HID_CLASS) && 00128 (intf_subclass == 0x00) && 00129 (intf_protocol == 0x00)) { 00130 gamepad_intf = intf_nb; 00131 return true; 00132 } 00133 return false; 00134 } 00135 00136 /*virtual*/ bool USBHostGamepad::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00137 { 00138 if (intf_nb == gamepad_intf) { 00139 if (type == INTERRUPT_ENDPOINT && dir == IN) { 00140 gamepad_device_found = true; 00141 return true; 00142 } 00143 } 00144 return false; 00145 } 00146 00147 #endif
Generated on Tue Jul 12 2022 20:44:17 by 1.7.2