2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jun 18 08:05:23 2015 +0000
Revision:
33:86c22c0c8aae
Parent:
32:e6717a485577
Child:
34:cac1e8336448
2015.06.18; ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sayzyas 32:e6717a485577 1 /* mbed USBHost Gamepad driver sample
sayzyas 32:e6717a485577 2 * Copyright (c) 2014 Yuuichi Akagawa
sayzyas 32:e6717a485577 3 *
sayzyas 32:e6717a485577 4 * modified from mbed USBHostMouse
sayzyas 32:e6717a485577 5 *
sayzyas 32:e6717a485577 6 * Copyright (c) 2014 mbed.org, MIT License
sayzyas 32:e6717a485577 7 *
sayzyas 32:e6717a485577 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
sayzyas 32:e6717a485577 9 * and associated documentation files (the "Software"), to deal in the Software without
sayzyas 32:e6717a485577 10 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
sayzyas 32:e6717a485577 11 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
sayzyas 32:e6717a485577 12 * Software is furnished to do so, subject to the following conditions:
sayzyas 32:e6717a485577 13 *
sayzyas 32:e6717a485577 14 * The above copyright notice and this permission notice shall be included in all copies or
sayzyas 32:e6717a485577 15 * substantial portions of the Software.
sayzyas 32:e6717a485577 16 *
sayzyas 32:e6717a485577 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
sayzyas 32:e6717a485577 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
sayzyas 32:e6717a485577 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
sayzyas 32:e6717a485577 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sayzyas 32:e6717a485577 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sayzyas 32:e6717a485577 22 */
sayzyas 32:e6717a485577 23
sayzyas 32:e6717a485577 24 #ifndef USBHOSTGAMEPAD_H
sayzyas 32:e6717a485577 25 #define USBHOSTGAMEPAD_H
sayzyas 32:e6717a485577 26
sayzyas 32:e6717a485577 27 #include "USBHostConf.h"
sayzyas 32:e6717a485577 28
sayzyas 32:e6717a485577 29 //#if USBHOST_GAMEPAD
sayzyas 32:e6717a485577 30
sayzyas 32:e6717a485577 31 #include "USBHost.h"
sayzyas 32:e6717a485577 32 //HID Class Request
sayzyas 32:e6717a485577 33 #define HID_GET_REPORT 0x01
sayzyas 32:e6717a485577 34 #define HID_GET_IDLE 0x02
sayzyas 32:e6717a485577 35 #define HID_GET_PROTOCOL 0x03
sayzyas 32:e6717a485577 36 #define HID_GET_DESCRIPTOR 0x06
sayzyas 32:e6717a485577 37 #define HID_SET_REPORT 0x09
sayzyas 32:e6717a485577 38 #define HID_SET_IDLE 0x0a
sayzyas 32:e6717a485577 39 #define HID_SET_PROTOCOL 0x0b
sayzyas 32:e6717a485577 40
sayzyas 32:e6717a485577 41 /**
sayzyas 32:e6717a485577 42 * A class to communicate a USB MIDI device
sayzyas 32:e6717a485577 43 */
sayzyas 32:e6717a485577 44 class USBHostGamepad : public IUSBEnumerator {
sayzyas 32:e6717a485577 45 public:
sayzyas 32:e6717a485577 46 /**
sayzyas 32:e6717a485577 47 * Constructor
sayzyas 32:e6717a485577 48 */
sayzyas 32:e6717a485577 49 USBHostGamepad();
sayzyas 32:e6717a485577 50
sayzyas 32:e6717a485577 51 /**
sayzyas 32:e6717a485577 52 * Try to connect a gamepad device
sayzyas 32:e6717a485577 53 *
sayzyas 32:e6717a485577 54 * @return true if connection was successful
sayzyas 32:e6717a485577 55 */
sayzyas 32:e6717a485577 56 bool connect();
sayzyas 32:e6717a485577 57
sayzyas 32:e6717a485577 58 /**
sayzyas 32:e6717a485577 59 * Check if a gamepad is connected
sayzyas 32:e6717a485577 60 *
sayzyas 32:e6717a485577 61 * @returns true if a gamepad is connected
sayzyas 32:e6717a485577 62 */
sayzyas 32:e6717a485577 63 bool connected();
sayzyas 32:e6717a485577 64
sayzyas 32:e6717a485577 65 /**
sayzyas 32:e6717a485577 66 * Attach a callback called when a gamepad event is received
sayzyas 32:e6717a485577 67 *
sayzyas 32:e6717a485577 68 * @param ptr function pointer
sayzyas 32:e6717a485577 69 */
sayzyas 33:86c22c0c8aae 70 // inline void attachEvent(void (*ptr)(uint8_t btnX, uint8_t btnY, uint8_t btnABCD, uint8_t btnSpecial)) {
sayzyas 33:86c22c0c8aae 71 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)) {
sayzyas 32:e6717a485577 72 if (ptr != NULL) {
sayzyas 32:e6717a485577 73 onUpdate = ptr;
sayzyas 32:e6717a485577 74 }
sayzyas 32:e6717a485577 75 }
sayzyas 32:e6717a485577 76
sayzyas 32:e6717a485577 77 /**
sayzyas 32:e6717a485577 78 * Request the HID report descriptor
sayzyas 32:e6717a485577 79 *
sayzyas 32:e6717a485577 80 * @param dev request the device descriptor on this device
sayzyas 32:e6717a485577 81 * @param buf buffer to store the device descriptor
sayzyas 32:e6717a485577 82 * @param max_len_buf maximum size of buf
sayzyas 32:e6717a485577 83 * @param len_dev_descr pointer to store the length of the packet transferred
sayzyas 32:e6717a485577 84 */
sayzyas 32:e6717a485577 85 USB_TYPE getReportDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_rep_descr = NULL);
sayzyas 32:e6717a485577 86
sayzyas 32:e6717a485577 87 protected:
sayzyas 32:e6717a485577 88 //From IUSBEnumerator
sayzyas 32:e6717a485577 89 virtual void setVidPid(uint16_t vid, uint16_t pid);
sayzyas 32:e6717a485577 90 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
sayzyas 32:e6717a485577 91 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
sayzyas 32:e6717a485577 92
sayzyas 32:e6717a485577 93 private:
sayzyas 32:e6717a485577 94 USBHost * host;
sayzyas 32:e6717a485577 95 USBDeviceConnected * dev;
sayzyas 32:e6717a485577 96 USBEndpoint * int_in;
sayzyas 33:86c22c0c8aae 97 // uint8_t report[8];
sayzyas 33:86c22c0c8aae 98 uint8_t report[16];
sayzyas 32:e6717a485577 99
sayzyas 32:e6717a485577 100 bool dev_connected;
sayzyas 32:e6717a485577 101 bool gamepad_device_found;
sayzyas 32:e6717a485577 102 int gamepad_intf;
sayzyas 32:e6717a485577 103
sayzyas 32:e6717a485577 104 uint8_t btnX;
sayzyas 32:e6717a485577 105 uint8_t btnY;
sayzyas 32:e6717a485577 106 uint8_t btnABCD;
sayzyas 32:e6717a485577 107 uint8_t btnSpecial;
sayzyas 32:e6717a485577 108
sayzyas 33:86c22c0c8aae 109 uint8_t btn00;
sayzyas 33:86c22c0c8aae 110 uint8_t btn01;
sayzyas 33:86c22c0c8aae 111 uint8_t btn02;
sayzyas 33:86c22c0c8aae 112 uint8_t btn03;
sayzyas 33:86c22c0c8aae 113 uint8_t btn04;
sayzyas 33:86c22c0c8aae 114 uint8_t btn05;
sayzyas 33:86c22c0c8aae 115 uint8_t btn06;
sayzyas 33:86c22c0c8aae 116 uint8_t btn07;
sayzyas 33:86c22c0c8aae 117 uint8_t btn08;
sayzyas 33:86c22c0c8aae 118 uint8_t btn09;
sayzyas 33:86c22c0c8aae 119 uint8_t btn10;
sayzyas 33:86c22c0c8aae 120 uint8_t btn11;
sayzyas 33:86c22c0c8aae 121 uint8_t btn12;
sayzyas 33:86c22c0c8aae 122 uint8_t btn13;
sayzyas 33:86c22c0c8aae 123 uint8_t btn14;
sayzyas 33:86c22c0c8aae 124 uint8_t btn15;
sayzyas 33:86c22c0c8aae 125
sayzyas 33:86c22c0c8aae 126
sayzyas 32:e6717a485577 127 void rxHandler();
sayzyas 33:86c22c0c8aae 128 // void (*onUpdate)(uint8_t btnX, uint8_t btnY, uint8_t btnABCD, uint8_t btnSpecial);
sayzyas 33:86c22c0c8aae 129 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 );
sayzyas 33:86c22c0c8aae 130
sayzyas 32:e6717a485577 131 int report_id;
sayzyas 32:e6717a485577 132 void init();
sayzyas 32:e6717a485577 133 bool parseHidDescr();
sayzyas 32:e6717a485577 134 };
sayzyas 32:e6717a485577 135
sayzyas 32:e6717a485577 136 //#endif
sayzyas 32:e6717a485577 137
sayzyas 32:e6717a485577 138 #endif