z ysaito / USBHost2

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostGamepad.h Source File

USBHostGamepad.h

00001 /* mbed USBHost Gamepad driver sample
00002  * Copyright (c) 2014 Yuuichi Akagawa
00003  *
00004  * modified from mbed USBHostMouse
00005  *
00006  * Copyright (c) 2014 mbed.org, MIT License
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00009  * and associated documentation files (the "Software"), to deal in the Software without
00010  * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in all copies or
00015  * substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00018  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00020  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  */
00023 
00024 #ifndef USBHOSTGAMEPAD_H
00025 #define USBHOSTGAMEPAD_H
00026 
00027 #include "USBHostConf.h"
00028 
00029 //#if USBHOST_GAMEPAD
00030 
00031 #include "USBHost.h"
00032 //HID Class Request
00033 #define HID_GET_REPORT                                 0x01
00034 #define HID_GET_IDLE                                   0x02
00035 #define HID_GET_PROTOCOL                               0x03
00036 #define HID_GET_DESCRIPTOR                             0x06
00037 #define HID_SET_REPORT                                 0x09
00038 #define HID_SET_IDLE                                   0x0a
00039 #define HID_SET_PROTOCOL                               0x0b
00040 
00041 /** 
00042  * A class to communicate a USB MIDI device
00043  */
00044 class USBHostGamepad : public IUSBEnumerator {
00045 public:
00046     /**
00047      * Constructor
00048      */
00049     USBHostGamepad();
00050 
00051     /**
00052      * Try to connect a gamepad device
00053      *
00054      * @return true if connection was successful
00055      */
00056     bool connect();
00057 
00058     /**
00059     * Check if a gamepad is connected
00060     *
00061     * @returns true if a gamepad is connected
00062     */
00063     bool connected();
00064 
00065     /**
00066      * Attach a callback called when a gamepad event is received
00067      *
00068      * @param ptr function pointer
00069      */
00070 //    inline void attachEvent(void (*ptr)(uint8_t btnX, uint8_t btnY, uint8_t btnABCD, uint8_t btnSpecial)) {
00071     inline void attachEvent(void (*ptr)(uint8_t btn00, uint8_t btn01, uint8_t btn02, uint8_t btn03, uint8_t btn04, uint8_t btn05, uint8_t btn06, uint8_t btn07, uint8_t btn08, uint8_t btn09, uint8_t btn10, uint8_t btn11, uint8_t btn12, uint8_t btn13, uint8_t btn14, uint8_t btn15, uint16_t gamePadVID, uint16_t gamePadPID)) {
00072         if (ptr != NULL) {
00073             onUpdate = ptr;
00074         }
00075     }
00076 
00077     /**
00078     * Request the HID report descriptor
00079     *
00080     * @param dev request the device descriptor on this device
00081     * @param buf buffer to store the device descriptor
00082     * @param max_len_buf maximum size of buf
00083     * @param len_dev_descr pointer to store the length of the packet transferred
00084     */
00085     USB_TYPE getReportDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_rep_descr = NULL);
00086     
00087 protected:
00088     //From IUSBEnumerator
00089     virtual void setVidPid(uint16_t vid, uint16_t pid);
00090     virtual bool 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
00091     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00092 
00093 private:
00094     USBHost * host;
00095     USBDeviceConnected * dev;
00096     USBEndpoint * int_in;
00097 //    uint8_t report[8];
00098     uint8_t report[16];
00099     
00100     uint16_t    gamePad_VID;
00101     uint16_t    gamePad_PID;
00102 
00103     bool dev_connected;
00104     bool gamepad_device_found;
00105     int gamepad_intf;
00106 
00107     uint8_t btnX;
00108     uint8_t btnY;
00109     uint8_t btnABCD;
00110     uint8_t btnSpecial;
00111 
00112     uint8_t btn00;
00113     uint8_t btn01;
00114     uint8_t btn02;
00115     uint8_t btn03;
00116     uint8_t btn04;
00117     uint8_t btn05;
00118     uint8_t btn06;
00119     uint8_t btn07;
00120     uint8_t btn08;
00121     uint8_t btn09;
00122     uint8_t btn10;
00123     uint8_t btn11;
00124     uint8_t btn12;
00125     uint8_t btn13;
00126     uint8_t btn14;
00127     uint8_t btn15;
00128         
00129 
00130     void rxHandler();
00131 //    void (*onUpdate)(uint8_t btnX, uint8_t btnY, uint8_t btnABCD, uint8_t btnSpecial);
00132     void (*onUpdate)(uint8_t btn00, uint8_t btn01, uint8_t btn02, uint8_t btn03, uint8_t btn04, uint8_t btn05, uint8_t btn06, uint8_t btn07, uint8_t btn08, uint8_t btn09, uint8_t btn10, uint8_t btn11, uint8_t btn12, uint8_t btn13,uint8_t btn14,uint8_t btn15, uint16_t gamePadVID, uint16_t gamePadPID );
00133     
00134     int report_id;
00135     void init();
00136     bool parseHidDescr();
00137 };
00138 
00139 //#endif
00140 
00141 #endif