Zoltan Hudak / USBHost-STM32F4

Dependents:   STM32F407VET6_USBHostMSD STM32F407VET6_USBHostMouse STM32F407VET6_USBHostKeyboard STM32F407VET6_USBHostMSD_1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostGamepad.cpp Source File

USBHostGamepad.cpp

00001 /* mbed USBHost Gamepad driver sample
00002  * Copyright (c) 2014 Yuuichi Akagawa
00003  *
00004  * modified from mbed USBHostMouse
00005  *
00006  * mbed USBHost Library
00007  * Copyright (c) 2006-2013 ARM Limited
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 #include "USBHostGamepad.h"
00022 
00023 //#if USBHOST_GAMEPAD
00024 USBHostGamepad::USBHostGamepad()
00025 {
00026     host = USBHost::getHostInst();
00027     init();
00028 }
00029 
00030 /**
00031  * @brief
00032  * @note
00033  * @param
00034  * @retval
00035  */
00036 void USBHostGamepad::init()
00037 {
00038     dev = NULL;
00039     int_in = NULL;
00040     onUpdate = NULL;
00041     report_id = 0;
00042     dev_connected = false;
00043     gamepad_device_found = false;
00044     gamepad_intf = -1;
00045 
00046     btnX = 0;
00047     btnY = 0;
00048     btnABCD = 0;
00049     btnSpecial = 0;
00050 }
00051 
00052 /**
00053  * @brief
00054  * @note
00055  * @param
00056  * @retval
00057  */
00058 bool USBHostGamepad::connected()
00059 {
00060     return dev_connected;
00061 }
00062 
00063 /**
00064  * @brief
00065  * @note
00066  * @param
00067  * @retval
00068  */
00069 bool USBHostGamepad::connect()
00070 {
00071     int len_listen;
00072 
00073     if (dev_connected) {
00074         return true;
00075     }
00076 
00077     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00078         if ((dev = host->getDevice(i)) != NULL) {
00079             if (host->enumerate(dev, this))
00080                 break;
00081 
00082             if (gamepad_device_found) {
00083                 int_in = dev->getEndpoint(gamepad_intf, INTERRUPT_ENDPOINT, IN);
00084                 if (!int_in)
00085                     break;
00086 
00087                 USB_INFO
00088                 (
00089                     "New Gamepad/Joystick device: VID:%04x PID:%04x [dev: %p - intf: %d]",
00090                     dev->getVid(),
00091                     dev->getPid(),
00092                     dev,
00093                     gamepad_intf
00094                 );
00095 #if DEBUG > 3
00096                 //Parse HID Report Descriptor
00097 
00098                 parseHidDescr();
00099 #endif
00100                 dev->setName("Gamepad", gamepad_intf);
00101                 host->registerDriver(dev, gamepad_intf, this, &USBHostGamepad::init);
00102 
00103                 int_in->attach(this, &USBHostGamepad::rxHandler);
00104                 len_listen = int_in->getSize();
00105                 if (len_listen > sizeof(report)) {
00106                     len_listen = sizeof(report);
00107                 }
00108 
00109                 host->interruptRead(dev, int_in, report, len_listen, false);
00110 
00111                 dev_connected = true;
00112                 return true;
00113             }
00114         }
00115     }
00116 
00117     init();
00118     return false;
00119 }
00120 
00121 /**
00122  * @brief
00123  * @note
00124  * @param
00125  * @retval
00126  */
00127 void USBHostGamepad::rxHandler()
00128 {
00129     int len_listen = int_in->getSize();
00130 #if DEBUG > 3
00131     USB_DBG("USBHostGamepad::rxHandler() len_listen=%d\r\n", len_listen);
00132     for (int i = 0; i < len_listen; i++)
00133         printf("%02X ", report[i]);
00134     printf("\r\n\r\n");
00135 #endif
00136     if (onUpdate) {
00137         (*onUpdate) (report[0], report[1], report[5], report[6]);
00138     }
00139 
00140     // update gamepad state
00141     btnX = report[0];
00142     btnY = report[1];
00143     btnABCD = report[5];
00144     btnSpecial = report[6];
00145 
00146     if (len_listen > sizeof(report)) {
00147         len_listen = sizeof(report);
00148     }
00149 
00150     if (dev)
00151         host->interruptRead(dev, int_in, report, len_listen, false);
00152 }
00153 
00154 /*virtual*/
00155 void USBHostGamepad::setVidPid(uint16_t vid, uint16_t pid)
00156 {
00157     // we don't check VID/PID for gamepad driver
00158 }
00159 
00160 /*virtual*/
00161 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
00162 {
00163     if ((gamepad_intf == -1) && (intf_class == HID_CLASS) && (intf_subclass == 0x00) && (intf_protocol == 0x00)) {
00164         gamepad_intf = intf_nb;
00165         return true;
00166     }
00167 
00168     return false;
00169 }
00170 
00171 //Must return true if the endpoint will be used
00172 
00173 /*virtual*/
00174 bool USBHostGamepad::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir)
00175 {
00176     if (intf_nb == gamepad_intf) {
00177         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00178             gamepad_device_found = true;
00179             return true;
00180         }
00181     }
00182 
00183     return false;
00184 }
00185 
00186 /**
00187  * @brief
00188  * @note
00189  * @param
00190  * @retval
00191  */
00192 USB_TYPE USBHostGamepad::getReportDescriptor
00193 (
00194     USBDeviceConnected*     dev,
00195     uint8_t*                buf,
00196     uint16_t                max_len_buf,
00197     uint16_t*               len_rep_descr
00198 )
00199 {
00200     USB_TYPE    t = host->controlRead
00201         (
00202             dev,
00203             USB_DEVICE_TO_HOST | USB_RECIPIENT_INTERFACE | USB_REQUEST_TYPE_STANDARD,
00204             HID_GET_DESCRIPTOR,
00205             0x2200,
00206             0,
00207             buf,
00208             max_len_buf
00209         );
00210     if (len_rep_descr)
00211         *len_rep_descr = max_len_buf;
00212 
00213     return t;
00214 }
00215 
00216 /*
00217  * HID Report Descriptor parser
00218  */
00219 //bool USBHostGamepad::parseHidDescr()
00220 //{
00221 //    bool        ret = true;
00222 //    uint8_t*    buf;
00223 //    uint16_t    desclen = host->getLengthReportDescr();
00224 //    //allocate report descriptor's buffer
00225 //    buf = (uint8_t*)malloc(desclen);
00226 //    if (buf == NULL) {
00227 //        return false;
00228 //    }
00229 //    getReportDescriptor(dev, buf, desclen);
00230 //#if (DEBUG > 3)
00231 //    USB_DBG("HID REPORT DESCRIPTOR:\r\n");
00232 //    for (int i = 0; i < desclen; i++)
00233 //        printf("%02X ", buf[i]);
00234 //    printf("\r\n\r\n");
00235 //#endif
00236 //    if (buf[0] == 0x05 //Usage page
00237 //    && buf[1] == 0x01 //Generic Desktop
00238 //    && buf[2] == 0x09 //Usage
00239 //    ) {
00240 //        if (buf[3] == 0x04) {
00241 //            USB_DBG("GAMEPAD");
00242 //        }
00243 //        else
00244 //        if (buf[3] == 0x05) {
00245 //            USB_DBG("JOYSTICK");
00246 //        }
00247 //        else {
00248 //            ret = false;
00249 //        }
00250 //    }
00251 //    free(buf);
00252 //    return ret;
00253 //}
00254 //#endif