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