Add USB HID Gamepad hosting support to mbed board (depends on USBHost library)
Dependents: mbed_gamepad_example
USBHostGamepad.h@0:5311825d0590, 2017-12-20 (annotated)
- Committer:
- abougouffa
- Date:
- Wed Dec 20 11:14:43 2017 +0000
- Revision:
- 0:5311825d0590
USBHostGamepad Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abougouffa | 0:5311825d0590 | 1 | /* mbed USBHost Library |
abougouffa | 0:5311825d0590 | 2 | * Copyright (c) 2006-2013 ARM Limited |
abougouffa | 0:5311825d0590 | 3 | * |
abougouffa | 0:5311825d0590 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
abougouffa | 0:5311825d0590 | 5 | * you may not use this file except in compliance with the License. |
abougouffa | 0:5311825d0590 | 6 | * You may obtain a copy of the License at |
abougouffa | 0:5311825d0590 | 7 | * |
abougouffa | 0:5311825d0590 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
abougouffa | 0:5311825d0590 | 9 | * |
abougouffa | 0:5311825d0590 | 10 | * Unless required by applicable law or agreed to in writing, software |
abougouffa | 0:5311825d0590 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
abougouffa | 0:5311825d0590 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
abougouffa | 0:5311825d0590 | 13 | * See the License for the specific language governing permissions and |
abougouffa | 0:5311825d0590 | 14 | * limitations under the License. |
abougouffa | 0:5311825d0590 | 15 | */ |
abougouffa | 0:5311825d0590 | 16 | |
abougouffa | 0:5311825d0590 | 17 | #ifndef USBHOSTGAMEPAD_H |
abougouffa | 0:5311825d0590 | 18 | #define USBHOSTGAMEPAD_H |
abougouffa | 0:5311825d0590 | 19 | |
abougouffa | 0:5311825d0590 | 20 | #include "USBHostConf.h" |
abougouffa | 0:5311825d0590 | 21 | |
abougouffa | 0:5311825d0590 | 22 | #define USBHOST_GAMEPAD 1 |
abougouffa | 0:5311825d0590 | 23 | |
abougouffa | 0:5311825d0590 | 24 | #if USBHOST_GAMEPAD |
abougouffa | 0:5311825d0590 | 25 | |
abougouffa | 0:5311825d0590 | 26 | #include "USBHost.h" |
abougouffa | 0:5311825d0590 | 27 | |
abougouffa | 0:5311825d0590 | 28 | /** |
abougouffa | 0:5311825d0590 | 29 | * A class to communicate a USB Gamepad |
abougouffa | 0:5311825d0590 | 30 | */ |
abougouffa | 0:5311825d0590 | 31 | class USBHostGamepad : public IUSBEnumerator |
abougouffa | 0:5311825d0590 | 32 | { |
abougouffa | 0:5311825d0590 | 33 | public: |
abougouffa | 0:5311825d0590 | 34 | |
abougouffa | 0:5311825d0590 | 35 | /** |
abougouffa | 0:5311825d0590 | 36 | * Constructor |
abougouffa | 0:5311825d0590 | 37 | */ |
abougouffa | 0:5311825d0590 | 38 | USBHostGamepad(); |
abougouffa | 0:5311825d0590 | 39 | |
abougouffa | 0:5311825d0590 | 40 | /** |
abougouffa | 0:5311825d0590 | 41 | * Try to connect a Gamepad device |
abougouffa | 0:5311825d0590 | 42 | * |
abougouffa | 0:5311825d0590 | 43 | * @return true if connection was successful |
abougouffa | 0:5311825d0590 | 44 | */ |
abougouffa | 0:5311825d0590 | 45 | bool connect(); |
abougouffa | 0:5311825d0590 | 46 | |
abougouffa | 0:5311825d0590 | 47 | /** |
abougouffa | 0:5311825d0590 | 48 | * Check if a Gamepad is connected |
abougouffa | 0:5311825d0590 | 49 | * |
abougouffa | 0:5311825d0590 | 50 | * @returns true if a Gamepad is connected |
abougouffa | 0:5311825d0590 | 51 | */ |
abougouffa | 0:5311825d0590 | 52 | bool connected(); |
abougouffa | 0:5311825d0590 | 53 | |
abougouffa | 0:5311825d0590 | 54 | /** |
abougouffa | 0:5311825d0590 | 55 | * Attach a callback called when a Gamepad event is received |
abougouffa | 0:5311825d0590 | 56 | * |
abougouffa | 0:5311825d0590 | 57 | * @param ptr function pointer |
abougouffa | 0:5311825d0590 | 58 | */ |
abougouffa | 0:5311825d0590 | 59 | inline void attachEvent(void (*ptr)(uint8_t x, uint8_t y, uint8_t z, uint8_t rz, uint16_t buttons)) |
abougouffa | 0:5311825d0590 | 60 | { |
abougouffa | 0:5311825d0590 | 61 | if (ptr != NULL) { |
abougouffa | 0:5311825d0590 | 62 | onUpdate = ptr; |
abougouffa | 0:5311825d0590 | 63 | } |
abougouffa | 0:5311825d0590 | 64 | } |
abougouffa | 0:5311825d0590 | 65 | |
abougouffa | 0:5311825d0590 | 66 | protected: |
abougouffa | 0:5311825d0590 | 67 | //From IUSBEnumerator |
abougouffa | 0:5311825d0590 | 68 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
abougouffa | 0:5311825d0590 | 69 | 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 |
abougouffa | 0:5311825d0590 | 70 | virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used |
abougouffa | 0:5311825d0590 | 71 | |
abougouffa | 0:5311825d0590 | 72 | private: |
abougouffa | 0:5311825d0590 | 73 | USBHost * host; |
abougouffa | 0:5311825d0590 | 74 | USBDeviceConnected * dev; |
abougouffa | 0:5311825d0590 | 75 | USBEndpoint * int_in; |
abougouffa | 0:5311825d0590 | 76 | uint8_t report[8]; |
abougouffa | 0:5311825d0590 | 77 | bool dev_connected; |
abougouffa | 0:5311825d0590 | 78 | bool gamepad_device_found; |
abougouffa | 0:5311825d0590 | 79 | int gamepad_intf; |
abougouffa | 0:5311825d0590 | 80 | |
abougouffa | 0:5311825d0590 | 81 | void rxHandler(); |
abougouffa | 0:5311825d0590 | 82 | void (*onUpdate)(uint8_t x, uint8_t y, uint8_t z, uint8_t rz, uint16_t buttons); |
abougouffa | 0:5311825d0590 | 83 | int report_id; |
abougouffa | 0:5311825d0590 | 84 | void init(); |
abougouffa | 0:5311825d0590 | 85 | }; |
abougouffa | 0:5311825d0590 | 86 | |
abougouffa | 0:5311825d0590 | 87 | #endif |
abougouffa | 0:5311825d0590 | 88 | |
abougouffa | 0:5311825d0590 | 89 | #endif |