Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers XBOXONE.h Source File

XBOXONE.h

00001 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
00002    Copyright (C) 2015 guruthree
00003 
00004  This software may be distributed and modified under the terms of the GNU
00005  General Public License version 2 (GPL2) as published by the Free Software
00006  Foundation and appearing in the file GPL2.TXT included in the packaging of
00007  this file. Please note that GPL2 Section 2[b] requires that all works based
00008  on this software must also be made publicly available under the terms of
00009  the GPL2 ("Copyleft").
00010 
00011  Contact information
00012  -------------------
00013 
00014  Kristian Lauszus, TKJ Electronics
00015  Web      :  http://www.tkjelectronics.com
00016  e-mail   :  kristianl@tkjelectronics.com
00017 
00018  guruthree
00019  Web      :  https://github.com/guruthree/
00020  */
00021 
00022 
00023 #ifndef _xboxone_h_
00024 #define _xboxone_h_
00025 
00026 #include "Usb.h"
00027 #include "xboxEnums.h"
00028 
00029 /* Xbox One data taken from descriptors */
00030 #define XBOX_ONE_EP_MAXPKTSIZE                  64 // Max size for data via USB
00031 
00032 /* Names we give to the 3 XboxONE pipes */
00033 #define XBOX_ONE_CONTROL_PIPE                   0
00034 #define XBOX_ONE_OUTPUT_PIPE                    1
00035 #define XBOX_ONE_INPUT_PIPE                     2
00036 
00037 #define XBOX_ONE_MAX_ENDPOINTS                  3
00038 
00039 // PID and VID of the different versions of the controller - see: https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c
00040 
00041 // Official controllers
00042 #define XBOX_VID1                               0x045E // Microsoft Corporation
00043 #define XBOX_ONE_PID1                           0x02D1 // Microsoft X-Box One pad
00044 #define XBOX_ONE_PID2                           0x02DD // Microsoft X-Box One pad (Firmware 2015)
00045 #define XBOX_ONE_PID3                           0x02E3 // Microsoft X-Box One Elite pad
00046 #define XBOX_ONE_PID4                           0x02EA // Microsoft X-Box One S pad
00047 #define XBOX_ONE_PID13                          0x0B0A // Microsoft X-Box One Adaptive Controller
00048 
00049 // Unofficial controllers
00050 #define XBOX_VID2                               0x0738 // Mad Catz
00051 #define XBOX_VID3                               0x0E6F // Afterglow
00052 #define XBOX_VID4                               0x0F0D // HORIPAD ONE
00053 #define XBOX_VID5                               0x1532 // Razer
00054 #define XBOX_VID6                               0x24C6 // PowerA
00055 
00056 #define XBOX_ONE_PID5                           0x4A01 // Mad Catz FightStick TE 2 - might have different mapping for triggers?
00057 #define XBOX_ONE_PID6                           0x0139 // Afterglow Prismatic Wired Controller
00058 #define XBOX_ONE_PID7                           0x0146 // Rock Candy Wired Controller for Xbox One
00059 #define XBOX_ONE_PID8                           0x0067 // HORIPAD ONE
00060 #define XBOX_ONE_PID9                           0x0A03 // Razer Wildcat
00061 #define XBOX_ONE_PID10                          0x541A // PowerA Xbox One Mini Wired Controller
00062 #define XBOX_ONE_PID11                          0x542A // Xbox ONE spectra
00063 #define XBOX_ONE_PID12                          0x543A // PowerA Xbox One wired controller
00064 
00065 /** This class implements support for a Xbox ONE controller connected via USB. */
00066 class XBOXONE : public USBDeviceConfig, public UsbConfigXtracter {
00067 public:
00068         /**
00069          * Constructor for the XBOXONE class.
00070          * @param  pUsb   Pointer to USB class instance.
00071          */
00072         XBOXONE(Usb *pUsb);
00073 
00074         /** @name USBDeviceConfig implementation */
00075         /**
00076          * Initialize the Xbox Controller.
00077          * @param  parent   Hub number.
00078          * @param  port     Port number on the hub.
00079          * @param  lowspeed Speed of the device.
00080          * @return          0 on success.
00081          */
00082         virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
00083         /**
00084          * Release the USB device.
00085          * @return 0 on success.
00086          */
00087         virtual uint8_t Release();
00088         /**
00089          * Poll the USB Input endpoins and run the state machines.
00090          * @return 0 on success.
00091          */
00092         virtual uint8_t Poll();
00093 
00094         /**
00095          * Get the device address.
00096          * @return The device address.
00097          */
00098         virtual uint8_t GetAddress() {
00099                 return bAddress;
00100         };
00101 
00102         /**
00103          * Used to check if the controller has been initialized.
00104          * @return True if it's ready.
00105          */
00106         virtual bool isReady() {
00107                 return bPollEnable;
00108         };
00109 
00110         /**
00111          * Read the poll interval taken from the endpoint descriptors.
00112          * @return The poll interval in ms.
00113          */
00114         uint8_t readPollInterval() {
00115                 return pollInterval;
00116         };
00117 
00118         /**
00119          * Used by the USB core to check what this driver support.
00120          * @param  vid The device's VID.
00121          * @param  pid The device's PID.
00122          * @return     Returns true if the device's VID and PID matches this driver.
00123          */
00124         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
00125                 return ((vid == XBOX_VID1 || vid == XBOX_VID2 || vid == XBOX_VID3 || vid == XBOX_VID4 || vid == XBOX_VID5 || vid == XBOX_VID6) &&
00126                     (pid == XBOX_ONE_PID1 || pid == XBOX_ONE_PID2 || pid == XBOX_ONE_PID3 || pid == XBOX_ONE_PID4 ||
00127                         pid == XBOX_ONE_PID5 || pid == XBOX_ONE_PID6 || pid == XBOX_ONE_PID7 || pid == XBOX_ONE_PID8 ||
00128                         pid == XBOX_ONE_PID9 || pid == XBOX_ONE_PID10 || pid == XBOX_ONE_PID11 || pid == XBOX_ONE_PID12 || pid == XBOX_ONE_PID13));
00129         };
00130         /**@}*/
00131 
00132         /** @name Xbox Controller functions */
00133         /**
00134          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
00135          *
00136          * While getButtonClick(ButtonEnum b) will only return it once.
00137          *
00138          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
00139          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
00140          * @param  b          ::ButtonEnum to read.
00141          * @return            getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a word if reading ::L2 or ::R2.
00142          */
00143         uint16_t getButtonPress(ButtonEnum b);
00144         bool getButtonClick(ButtonEnum b);
00145 
00146         /**
00147          * Return the analog value from the joysticks on the controller.
00148          * @param  a          Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
00149          * @return            Returns a signed 16-bit integer.
00150          */
00151         int16_t getAnalogHat(AnalogHatEnum a);
00152 
00153         /**
00154          * Used to call your own function when the controller is successfully initialized.
00155          * @param funcOnInit Function to call.
00156          */
00157         void attachOnInit(void (*funcOnInit)(void)) {
00158                 pFuncOnInit = funcOnInit;
00159         };
00160 
00161         /** Used to set the rumble off. */
00162         void setRumbleOff();
00163 
00164         /**
00165          * Used to turn on rumble continuously.
00166          * @param leftTrigger  Left trigger force.
00167          * @param rightTrigger Right trigger force.
00168          * @param leftMotor    Left motor force.
00169          * @param rightMotor   Right motor force.
00170          */
00171         void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor);
00172         /**@}*/
00173 
00174         /** True if a Xbox ONE controller is connected. */
00175         bool XboxOneConnected;
00176 
00177 protected:
00178         /** Pointer to USB class instance. */
00179         Usb *pUsb;
00180         /** Device address. */
00181         uint8_t bAddress;
00182         /** Endpoint info structure. */
00183         EpInfo epInfo[XBOX_ONE_MAX_ENDPOINTS];
00184 
00185         /** Configuration number. */
00186         uint8_t bConfNum;
00187         /** Total number of endpoints in the configuration. */
00188         uint8_t bNumEP;
00189         /** Next poll time based on poll interval taken from the USB descriptor. */
00190         uint32_t qNextPollTime;
00191 
00192         /** @name UsbConfigXtracter implementation */
00193         /**
00194          * UsbConfigXtracter implementation, used to extract endpoint information.
00195          * @param conf  Configuration value.
00196          * @param iface Interface number.
00197          * @param alt   Alternate setting.
00198          * @param proto Interface Protocol.
00199          * @param ep    Endpoint Descriptor.
00200          */
00201         void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
00202         /**@}*/
00203 
00204         /**
00205          * Used to print the USB Endpoint Descriptor.
00206          * @param ep_ptr Pointer to USB Endpoint Descriptor.
00207          */
00208         void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
00209 
00210 private:
00211         /**
00212          * Called when the controller is successfully initialized.
00213          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
00214          */
00215         void onInit();
00216         void (*pFuncOnInit)(void); // Pointer to function called in onInit()
00217 
00218         uint8_t pollInterval;
00219         bool bPollEnable;
00220 
00221         /* Variables to store the buttons */
00222         uint16_t ButtonState;
00223         uint16_t OldButtonState;
00224         uint16_t ButtonClickState;
00225         int16_t hatValue[4];
00226         uint16_t triggerValue[2];
00227         uint16_t triggerValueOld[2];
00228 
00229         bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
00230         bool R2Clicked;
00231 
00232         uint8_t readBuf[XBOX_ONE_EP_MAXPKTSIZE]; // General purpose buffer for input data
00233         uint8_t cmdCounter;
00234 
00235         void readReport(); // Used to read the incoming data
00236 
00237         /* Private commands */
00238         uint8_t XboxCommand(uint8_t* data, uint16_t nbytes);
00239 };
00240 #endif