Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: UsbHostMAX3421E_Hello
XBOXONE.h@0:84353c479782, 2020-07-12 (annotated)
- Committer:
- hudakz
- Date:
- Sun Jul 12 20:39:26 2020 +0000
- Revision:
- 0:84353c479782
- Child:
- 1:2263e77400e9
MAX3421E-based USB Host Shield Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:84353c479782 | 1 | /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. |
hudakz | 0:84353c479782 | 2 | Copyright (C) 2015 guruthree |
hudakz | 0:84353c479782 | 3 | |
hudakz | 0:84353c479782 | 4 | This software may be distributed and modified under the terms of the GNU |
hudakz | 0:84353c479782 | 5 | General Public License version 2 (GPL2) as published by the Free Software |
hudakz | 0:84353c479782 | 6 | Foundation and appearing in the file GPL2.TXT included in the packaging of |
hudakz | 0:84353c479782 | 7 | this file. Please note that GPL2 Section 2[b] requires that all works based |
hudakz | 0:84353c479782 | 8 | on this software must also be made publicly available under the terms of |
hudakz | 0:84353c479782 | 9 | the GPL2 ("Copyleft"). |
hudakz | 0:84353c479782 | 10 | |
hudakz | 0:84353c479782 | 11 | Contact information |
hudakz | 0:84353c479782 | 12 | ------------------- |
hudakz | 0:84353c479782 | 13 | |
hudakz | 0:84353c479782 | 14 | Kristian Lauszus, TKJ Electronics |
hudakz | 0:84353c479782 | 15 | Web : http://www.tkjelectronics.com |
hudakz | 0:84353c479782 | 16 | e-mail : kristianl@tkjelectronics.com |
hudakz | 0:84353c479782 | 17 | |
hudakz | 0:84353c479782 | 18 | guruthree |
hudakz | 0:84353c479782 | 19 | Web : https://github.com/guruthree/ |
hudakz | 0:84353c479782 | 20 | */ |
hudakz | 0:84353c479782 | 21 | |
hudakz | 0:84353c479782 | 22 | |
hudakz | 0:84353c479782 | 23 | #ifndef _xboxone_h_ |
hudakz | 0:84353c479782 | 24 | #define _xboxone_h_ |
hudakz | 0:84353c479782 | 25 | |
hudakz | 0:84353c479782 | 26 | #include "Usb.h" |
hudakz | 0:84353c479782 | 27 | #include "xboxEnums.h" |
hudakz | 0:84353c479782 | 28 | |
hudakz | 0:84353c479782 | 29 | /* Xbox One data taken from descriptors */ |
hudakz | 0:84353c479782 | 30 | #define XBOX_ONE_EP_MAXPKTSIZE 64 // Max size for data via USB |
hudakz | 0:84353c479782 | 31 | |
hudakz | 0:84353c479782 | 32 | /* Names we give to the 3 XboxONE pipes */ |
hudakz | 0:84353c479782 | 33 | #define XBOX_ONE_CONTROL_PIPE 0 |
hudakz | 0:84353c479782 | 34 | #define XBOX_ONE_OUTPUT_PIPE 1 |
hudakz | 0:84353c479782 | 35 | #define XBOX_ONE_INPUT_PIPE 2 |
hudakz | 0:84353c479782 | 36 | |
hudakz | 0:84353c479782 | 37 | #define XBOX_ONE_MAX_ENDPOINTS 3 |
hudakz | 0:84353c479782 | 38 | |
hudakz | 0:84353c479782 | 39 | // PID and VID of the different versions of the controller - see: https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c |
hudakz | 0:84353c479782 | 40 | |
hudakz | 0:84353c479782 | 41 | // Official controllers |
hudakz | 0:84353c479782 | 42 | #define XBOX_VID1 0x045E // Microsoft Corporation |
hudakz | 0:84353c479782 | 43 | #define XBOX_ONE_PID1 0x02D1 // Microsoft X-Box One pad |
hudakz | 0:84353c479782 | 44 | #define XBOX_ONE_PID2 0x02DD // Microsoft X-Box One pad (Firmware 2015) |
hudakz | 0:84353c479782 | 45 | #define XBOX_ONE_PID3 0x02E3 // Microsoft X-Box One Elite pad |
hudakz | 0:84353c479782 | 46 | #define XBOX_ONE_PID4 0x02EA // Microsoft X-Box One S pad |
hudakz | 0:84353c479782 | 47 | #define XBOX_ONE_PID13 0x0B0A // Microsoft X-Box One Adaptive Controller |
hudakz | 0:84353c479782 | 48 | |
hudakz | 0:84353c479782 | 49 | // Unofficial controllers |
hudakz | 0:84353c479782 | 50 | #define XBOX_VID2 0x0738 // Mad Catz |
hudakz | 0:84353c479782 | 51 | #define XBOX_VID3 0x0E6F // Afterglow |
hudakz | 0:84353c479782 | 52 | #define XBOX_VID4 0x0F0D // HORIPAD ONE |
hudakz | 0:84353c479782 | 53 | #define XBOX_VID5 0x1532 // Razer |
hudakz | 0:84353c479782 | 54 | #define XBOX_VID6 0x24C6 // PowerA |
hudakz | 0:84353c479782 | 55 | |
hudakz | 0:84353c479782 | 56 | #define XBOX_ONE_PID5 0x4A01 // Mad Catz FightStick TE 2 - might have different mapping for triggers? |
hudakz | 0:84353c479782 | 57 | #define XBOX_ONE_PID6 0x0139 // Afterglow Prismatic Wired Controller |
hudakz | 0:84353c479782 | 58 | #define XBOX_ONE_PID7 0x0146 // Rock Candy Wired Controller for Xbox One |
hudakz | 0:84353c479782 | 59 | #define XBOX_ONE_PID8 0x0067 // HORIPAD ONE |
hudakz | 0:84353c479782 | 60 | #define XBOX_ONE_PID9 0x0A03 // Razer Wildcat |
hudakz | 0:84353c479782 | 61 | #define XBOX_ONE_PID10 0x541A // PowerA Xbox One Mini Wired Controller |
hudakz | 0:84353c479782 | 62 | #define XBOX_ONE_PID11 0x542A // Xbox ONE spectra |
hudakz | 0:84353c479782 | 63 | #define XBOX_ONE_PID12 0x543A // PowerA Xbox One wired controller |
hudakz | 0:84353c479782 | 64 | |
hudakz | 0:84353c479782 | 65 | /** This class implements support for a Xbox ONE controller connected via USB. */ |
hudakz | 0:84353c479782 | 66 | class XBOXONE : public USBDeviceConfig, public UsbConfigXtracter { |
hudakz | 0:84353c479782 | 67 | public: |
hudakz | 0:84353c479782 | 68 | /** |
hudakz | 0:84353c479782 | 69 | * Constructor for the XBOXONE class. |
hudakz | 0:84353c479782 | 70 | * @param pUsb Pointer to USB class instance. |
hudakz | 0:84353c479782 | 71 | */ |
hudakz | 0:84353c479782 | 72 | XBOXONE(USB *pUsb); |
hudakz | 0:84353c479782 | 73 | |
hudakz | 0:84353c479782 | 74 | /** @name USBDeviceConfig implementation */ |
hudakz | 0:84353c479782 | 75 | /** |
hudakz | 0:84353c479782 | 76 | * Initialize the Xbox Controller. |
hudakz | 0:84353c479782 | 77 | * @param parent Hub number. |
hudakz | 0:84353c479782 | 78 | * @param port Port number on the hub. |
hudakz | 0:84353c479782 | 79 | * @param lowspeed Speed of the device. |
hudakz | 0:84353c479782 | 80 | * @return 0 on success. |
hudakz | 0:84353c479782 | 81 | */ |
hudakz | 0:84353c479782 | 82 | virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); |
hudakz | 0:84353c479782 | 83 | /** |
hudakz | 0:84353c479782 | 84 | * Release the USB device. |
hudakz | 0:84353c479782 | 85 | * @return 0 on success. |
hudakz | 0:84353c479782 | 86 | */ |
hudakz | 0:84353c479782 | 87 | virtual uint8_t Release(); |
hudakz | 0:84353c479782 | 88 | /** |
hudakz | 0:84353c479782 | 89 | * Poll the USB Input endpoins and run the state machines. |
hudakz | 0:84353c479782 | 90 | * @return 0 on success. |
hudakz | 0:84353c479782 | 91 | */ |
hudakz | 0:84353c479782 | 92 | virtual uint8_t Poll(); |
hudakz | 0:84353c479782 | 93 | |
hudakz | 0:84353c479782 | 94 | /** |
hudakz | 0:84353c479782 | 95 | * Get the device address. |
hudakz | 0:84353c479782 | 96 | * @return The device address. |
hudakz | 0:84353c479782 | 97 | */ |
hudakz | 0:84353c479782 | 98 | virtual uint8_t GetAddress() { |
hudakz | 0:84353c479782 | 99 | return bAddress; |
hudakz | 0:84353c479782 | 100 | }; |
hudakz | 0:84353c479782 | 101 | |
hudakz | 0:84353c479782 | 102 | /** |
hudakz | 0:84353c479782 | 103 | * Used to check if the controller has been initialized. |
hudakz | 0:84353c479782 | 104 | * @return True if it's ready. |
hudakz | 0:84353c479782 | 105 | */ |
hudakz | 0:84353c479782 | 106 | virtual bool isReady() { |
hudakz | 0:84353c479782 | 107 | return bPollEnable; |
hudakz | 0:84353c479782 | 108 | }; |
hudakz | 0:84353c479782 | 109 | |
hudakz | 0:84353c479782 | 110 | /** |
hudakz | 0:84353c479782 | 111 | * Read the poll interval taken from the endpoint descriptors. |
hudakz | 0:84353c479782 | 112 | * @return The poll interval in ms. |
hudakz | 0:84353c479782 | 113 | */ |
hudakz | 0:84353c479782 | 114 | uint8_t readPollInterval() { |
hudakz | 0:84353c479782 | 115 | return pollInterval; |
hudakz | 0:84353c479782 | 116 | }; |
hudakz | 0:84353c479782 | 117 | |
hudakz | 0:84353c479782 | 118 | /** |
hudakz | 0:84353c479782 | 119 | * Used by the USB core to check what this driver support. |
hudakz | 0:84353c479782 | 120 | * @param vid The device's VID. |
hudakz | 0:84353c479782 | 121 | * @param pid The device's PID. |
hudakz | 0:84353c479782 | 122 | * @return Returns true if the device's VID and PID matches this driver. |
hudakz | 0:84353c479782 | 123 | */ |
hudakz | 0:84353c479782 | 124 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { |
hudakz | 0:84353c479782 | 125 | return ((vid == XBOX_VID1 || vid == XBOX_VID2 || vid == XBOX_VID3 || vid == XBOX_VID4 || vid == XBOX_VID5 || vid == XBOX_VID6) && |
hudakz | 0:84353c479782 | 126 | (pid == XBOX_ONE_PID1 || pid == XBOX_ONE_PID2 || pid == XBOX_ONE_PID3 || pid == XBOX_ONE_PID4 || |
hudakz | 0:84353c479782 | 127 | pid == XBOX_ONE_PID5 || pid == XBOX_ONE_PID6 || pid == XBOX_ONE_PID7 || pid == XBOX_ONE_PID8 || |
hudakz | 0:84353c479782 | 128 | pid == XBOX_ONE_PID9 || pid == XBOX_ONE_PID10 || pid == XBOX_ONE_PID11 || pid == XBOX_ONE_PID12 || pid == XBOX_ONE_PID13)); |
hudakz | 0:84353c479782 | 129 | }; |
hudakz | 0:84353c479782 | 130 | /**@}*/ |
hudakz | 0:84353c479782 | 131 | |
hudakz | 0:84353c479782 | 132 | /** @name Xbox Controller functions */ |
hudakz | 0:84353c479782 | 133 | /** |
hudakz | 0:84353c479782 | 134 | * getButtonPress(ButtonEnum b) will return true as long as the button is held down. |
hudakz | 0:84353c479782 | 135 | * |
hudakz | 0:84353c479782 | 136 | * While getButtonClick(ButtonEnum b) will only return it once. |
hudakz | 0:84353c479782 | 137 | * |
hudakz | 0:84353c479782 | 138 | * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), |
hudakz | 0:84353c479782 | 139 | * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). |
hudakz | 0:84353c479782 | 140 | * @param b ::ButtonEnum to read. |
hudakz | 0:84353c479782 | 141 | * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a word if reading ::L2 or ::R2. |
hudakz | 0:84353c479782 | 142 | */ |
hudakz | 0:84353c479782 | 143 | uint16_t getButtonPress(ButtonEnum b); |
hudakz | 0:84353c479782 | 144 | bool getButtonClick(ButtonEnum b); |
hudakz | 0:84353c479782 | 145 | |
hudakz | 0:84353c479782 | 146 | /** |
hudakz | 0:84353c479782 | 147 | * Return the analog value from the joysticks on the controller. |
hudakz | 0:84353c479782 | 148 | * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. |
hudakz | 0:84353c479782 | 149 | * @return Returns a signed 16-bit integer. |
hudakz | 0:84353c479782 | 150 | */ |
hudakz | 0:84353c479782 | 151 | int16_t getAnalogHat(AnalogHatEnum a); |
hudakz | 0:84353c479782 | 152 | |
hudakz | 0:84353c479782 | 153 | /** |
hudakz | 0:84353c479782 | 154 | * Used to call your own function when the controller is successfully initialized. |
hudakz | 0:84353c479782 | 155 | * @param funcOnInit Function to call. |
hudakz | 0:84353c479782 | 156 | */ |
hudakz | 0:84353c479782 | 157 | void attachOnInit(void (*funcOnInit)(void)) { |
hudakz | 0:84353c479782 | 158 | pFuncOnInit = funcOnInit; |
hudakz | 0:84353c479782 | 159 | }; |
hudakz | 0:84353c479782 | 160 | |
hudakz | 0:84353c479782 | 161 | /** Used to set the rumble off. */ |
hudakz | 0:84353c479782 | 162 | void setRumbleOff(); |
hudakz | 0:84353c479782 | 163 | |
hudakz | 0:84353c479782 | 164 | /** |
hudakz | 0:84353c479782 | 165 | * Used to turn on rumble continuously. |
hudakz | 0:84353c479782 | 166 | * @param leftTrigger Left trigger force. |
hudakz | 0:84353c479782 | 167 | * @param rightTrigger Right trigger force. |
hudakz | 0:84353c479782 | 168 | * @param leftMotor Left motor force. |
hudakz | 0:84353c479782 | 169 | * @param rightMotor Right motor force. |
hudakz | 0:84353c479782 | 170 | */ |
hudakz | 0:84353c479782 | 171 | void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor); |
hudakz | 0:84353c479782 | 172 | /**@}*/ |
hudakz | 0:84353c479782 | 173 | |
hudakz | 0:84353c479782 | 174 | /** True if a Xbox ONE controller is connected. */ |
hudakz | 0:84353c479782 | 175 | bool XboxOneConnected; |
hudakz | 0:84353c479782 | 176 | |
hudakz | 0:84353c479782 | 177 | protected: |
hudakz | 0:84353c479782 | 178 | /** Pointer to USB class instance. */ |
hudakz | 0:84353c479782 | 179 | USB *pUsb; |
hudakz | 0:84353c479782 | 180 | /** Device address. */ |
hudakz | 0:84353c479782 | 181 | uint8_t bAddress; |
hudakz | 0:84353c479782 | 182 | /** Endpoint info structure. */ |
hudakz | 0:84353c479782 | 183 | EpInfo epInfo[XBOX_ONE_MAX_ENDPOINTS]; |
hudakz | 0:84353c479782 | 184 | |
hudakz | 0:84353c479782 | 185 | /** Configuration number. */ |
hudakz | 0:84353c479782 | 186 | uint8_t bConfNum; |
hudakz | 0:84353c479782 | 187 | /** Total number of endpoints in the configuration. */ |
hudakz | 0:84353c479782 | 188 | uint8_t bNumEP; |
hudakz | 0:84353c479782 | 189 | /** Next poll time based on poll interval taken from the USB descriptor. */ |
hudakz | 0:84353c479782 | 190 | uint32_t qNextPollTime; |
hudakz | 0:84353c479782 | 191 | |
hudakz | 0:84353c479782 | 192 | /** @name UsbConfigXtracter implementation */ |
hudakz | 0:84353c479782 | 193 | /** |
hudakz | 0:84353c479782 | 194 | * UsbConfigXtracter implementation, used to extract endpoint information. |
hudakz | 0:84353c479782 | 195 | * @param conf Configuration value. |
hudakz | 0:84353c479782 | 196 | * @param iface Interface number. |
hudakz | 0:84353c479782 | 197 | * @param alt Alternate setting. |
hudakz | 0:84353c479782 | 198 | * @param proto Interface Protocol. |
hudakz | 0:84353c479782 | 199 | * @param ep Endpoint Descriptor. |
hudakz | 0:84353c479782 | 200 | */ |
hudakz | 0:84353c479782 | 201 | void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); |
hudakz | 0:84353c479782 | 202 | /**@}*/ |
hudakz | 0:84353c479782 | 203 | |
hudakz | 0:84353c479782 | 204 | /** |
hudakz | 0:84353c479782 | 205 | * Used to print the USB Endpoint Descriptor. |
hudakz | 0:84353c479782 | 206 | * @param ep_ptr Pointer to USB Endpoint Descriptor. |
hudakz | 0:84353c479782 | 207 | */ |
hudakz | 0:84353c479782 | 208 | void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr); |
hudakz | 0:84353c479782 | 209 | |
hudakz | 0:84353c479782 | 210 | private: |
hudakz | 0:84353c479782 | 211 | /** |
hudakz | 0:84353c479782 | 212 | * Called when the controller is successfully initialized. |
hudakz | 0:84353c479782 | 213 | * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. |
hudakz | 0:84353c479782 | 214 | */ |
hudakz | 0:84353c479782 | 215 | void onInit(); |
hudakz | 0:84353c479782 | 216 | void (*pFuncOnInit)(void); // Pointer to function called in onInit() |
hudakz | 0:84353c479782 | 217 | |
hudakz | 0:84353c479782 | 218 | uint8_t pollInterval; |
hudakz | 0:84353c479782 | 219 | bool bPollEnable; |
hudakz | 0:84353c479782 | 220 | |
hudakz | 0:84353c479782 | 221 | /* Variables to store the buttons */ |
hudakz | 0:84353c479782 | 222 | uint16_t ButtonState; |
hudakz | 0:84353c479782 | 223 | uint16_t OldButtonState; |
hudakz | 0:84353c479782 | 224 | uint16_t ButtonClickState; |
hudakz | 0:84353c479782 | 225 | int16_t hatValue[4]; |
hudakz | 0:84353c479782 | 226 | uint16_t triggerValue[2]; |
hudakz | 0:84353c479782 | 227 | uint16_t triggerValueOld[2]; |
hudakz | 0:84353c479782 | 228 | |
hudakz | 0:84353c479782 | 229 | bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not |
hudakz | 0:84353c479782 | 230 | bool R2Clicked; |
hudakz | 0:84353c479782 | 231 | |
hudakz | 0:84353c479782 | 232 | uint8_t readBuf[XBOX_ONE_EP_MAXPKTSIZE]; // General purpose buffer for input data |
hudakz | 0:84353c479782 | 233 | uint8_t cmdCounter; |
hudakz | 0:84353c479782 | 234 | |
hudakz | 0:84353c479782 | 235 | void readReport(); // Used to read the incoming data |
hudakz | 0:84353c479782 | 236 | |
hudakz | 0:84353c479782 | 237 | /* Private commands */ |
hudakz | 0:84353c479782 | 238 | uint8_t XboxCommand(uint8_t* data, uint16_t nbytes); |
hudakz | 0:84353c479782 | 239 | }; |
hudakz | 0:84353c479782 | 240 | #endif |