I have ported my old project “pNesX” game console emulator to the nucleo.

Dependencies:   SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostGamepad.cpp Source File

USBHostGamepad.cpp

00001 /*
00002  * mbed USBHost Gamepad driver sample
00003  * Copyright (c) 2014 Yuuichi Akagawa
00004  *
00005  * modified from mbed USBHostMouse
00006  *
00007  * mbed USBHost Library
00008  * Copyright (c) 2006-2013 ARM Limited
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022  
00023 #include "USBHostGamepad.h"
00024 //#if USBHOST_GAMEPAD
00025 
00026 USBHostGamepad::USBHostGamepad() {
00027     host = USBHost::getHostInst();
00028     init();
00029 }
00030 
00031 void USBHostGamepad::init() {
00032     dev = NULL;
00033     int_in = NULL;
00034     dev_connected = false;
00035     gamepad_device_found = false;
00036     gamepad_intf = -1;
00037 }
00038 
00039 bool USBHostGamepad::connected() {
00040     return dev_connected;
00041 }
00042 
00043 bool USBHostGamepad::connect() {
00044     if (dev_connected) {
00045         return true;
00046     }
00047 
00048     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00049         if ((dev = host->getDevice(i)) != NULL) {
00050 
00051             if(host->enumerate(dev, this))
00052                 break;
00053 
00054             if (gamepad_device_found) {
00055 
00056                 int_in = dev->getEndpoint(gamepad_intf, INTERRUPT_ENDPOINT, IN);
00057                 if (!int_in)
00058                     break;
00059 
00060                 USB_INFO("New Gamepad/Joystick device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, gamepad_intf);
00061 #if DEBUG > 3
00062                 //Parse HID Report Descriptor
00063                 parseHidDescr();
00064 #endif
00065                 dev->setName("Gamepad", gamepad_intf);
00066                 host->registerDriver(dev, gamepad_intf, this, &USBHostGamepad::init);
00067 
00068                 int_in->attach(this, &USBHostGamepad::rxHandler);
00069                 rxSize = int_in->getSize();
00070                 if (rxSize > sizeof(report))
00071                     rxSize = sizeof(report);
00072                 host->interruptRead(dev, int_in, report, rxSize, false);
00073 
00074                 dev_connected = true;
00075                 return true;
00076             }
00077         }
00078     }
00079     init();
00080     return false;
00081 }
00082 
00083 void USBHostGamepad::rxHandler() {
00084     if (dev)
00085         host->interruptRead(dev, int_in, report, rxSize, false);
00086         
00087     /* modified 2016 Racoon */
00088         
00089 }
00090 
00091 /*virtual*/ void USBHostGamepad::setVidPid(uint16_t vid, uint16_t pid)
00092 {
00093     // we don't check VID/PID for gamepad driver
00094 }
00095 
00096 /*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
00097 {
00098     if ((gamepad_intf == -1) &&
00099         (intf_class == HID_CLASS) &&
00100         (intf_subclass == 0x00) &&
00101         (intf_protocol == 0x00)) {
00102         gamepad_intf = intf_nb;
00103         return true;
00104     }
00105     return false;
00106 }
00107 
00108 /*virtual*/ bool USBHostGamepad::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00109 {
00110     if (intf_nb == gamepad_intf) {
00111         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00112             gamepad_device_found = true;
00113             return true;
00114         }
00115     }
00116     return false;
00117 }
00118 
00119