2018.07.26
Dependencies: FATFileSystem3 mbed-rtos
Fork of USBHost by
USBHostHID/USBHostGamepad.h@32:e6717a485577, 2015-06-02 (annotated)
- Committer:
- sayzyas
- Date:
- Tue Jun 02 05:57:44 2015 +0000
- Revision:
- 32:e6717a485577
- Child:
- 33:86c22c0c8aae
20150602
Who changed what in which revision?
User | Revision | Line number | New 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 | 32:e6717a485577 | 70 | inline void attachEvent(void (*ptr)(uint8_t btnX, uint8_t btnY, uint8_t btnABCD, uint8_t btnSpecial)) { |
sayzyas | 32:e6717a485577 | 71 | if (ptr != NULL) { |
sayzyas | 32:e6717a485577 | 72 | onUpdate = ptr; |
sayzyas | 32:e6717a485577 | 73 | } |
sayzyas | 32:e6717a485577 | 74 | } |
sayzyas | 32:e6717a485577 | 75 | |
sayzyas | 32:e6717a485577 | 76 | /** |
sayzyas | 32:e6717a485577 | 77 | * Request the HID report descriptor |
sayzyas | 32:e6717a485577 | 78 | * |
sayzyas | 32:e6717a485577 | 79 | * @param dev request the device descriptor on this device |
sayzyas | 32:e6717a485577 | 80 | * @param buf buffer to store the device descriptor |
sayzyas | 32:e6717a485577 | 81 | * @param max_len_buf maximum size of buf |
sayzyas | 32:e6717a485577 | 82 | * @param len_dev_descr pointer to store the length of the packet transferred |
sayzyas | 32:e6717a485577 | 83 | */ |
sayzyas | 32:e6717a485577 | 84 | USB_TYPE getReportDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_rep_descr = NULL); |
sayzyas | 32:e6717a485577 | 85 | |
sayzyas | 32:e6717a485577 | 86 | protected: |
sayzyas | 32:e6717a485577 | 87 | //From IUSBEnumerator |
sayzyas | 32:e6717a485577 | 88 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
sayzyas | 32:e6717a485577 | 89 | 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 | 90 | 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 | 91 | |
sayzyas | 32:e6717a485577 | 92 | private: |
sayzyas | 32:e6717a485577 | 93 | USBHost * host; |
sayzyas | 32:e6717a485577 | 94 | USBDeviceConnected * dev; |
sayzyas | 32:e6717a485577 | 95 | USBEndpoint * int_in; |
sayzyas | 32:e6717a485577 | 96 | uint8_t report[8]; |
sayzyas | 32:e6717a485577 | 97 | |
sayzyas | 32:e6717a485577 | 98 | bool dev_connected; |
sayzyas | 32:e6717a485577 | 99 | bool gamepad_device_found; |
sayzyas | 32:e6717a485577 | 100 | int gamepad_intf; |
sayzyas | 32:e6717a485577 | 101 | |
sayzyas | 32:e6717a485577 | 102 | uint8_t btnX; |
sayzyas | 32:e6717a485577 | 103 | uint8_t btnY; |
sayzyas | 32:e6717a485577 | 104 | uint8_t btnABCD; |
sayzyas | 32:e6717a485577 | 105 | uint8_t btnSpecial; |
sayzyas | 32:e6717a485577 | 106 | |
sayzyas | 32:e6717a485577 | 107 | void rxHandler(); |
sayzyas | 32:e6717a485577 | 108 | void (*onUpdate)(uint8_t btnX, uint8_t btnY, uint8_t btnABCD, uint8_t btnSpecial); |
sayzyas | 32:e6717a485577 | 109 | int report_id; |
sayzyas | 32:e6717a485577 | 110 | void init(); |
sayzyas | 32:e6717a485577 | 111 | bool parseHidDescr(); |
sayzyas | 32:e6717a485577 | 112 | }; |
sayzyas | 32:e6717a485577 | 113 | |
sayzyas | 32:e6717a485577 | 114 | //#endif |
sayzyas | 32:e6717a485577 | 115 | |
sayzyas | 32:e6717a485577 | 116 | #endif |