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
XBOXUSB.h
00001 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. 00002 00003 This software may be distributed and modified under the terms of the GNU 00004 General Public License version 2 (GPL2) as published by the Free Software 00005 Foundation and appearing in the file GPL2.TXT included in the packaging of 00006 this file. Please note that GPL2 Section 2[b] requires that all works based 00007 on this software must also be made publicly available under the terms of 00008 the GPL2 ("Copyleft"). 00009 00010 Contact information 00011 ------------------- 00012 00013 Kristian Lauszus, TKJ Electronics 00014 Web : http://www.tkjelectronics.com 00015 e-mail : kristianl@tkjelectronics.com 00016 */ 00017 00018 #ifndef _xboxusb_h_ 00019 #define _xboxusb_h_ 00020 00021 #include "Usb.h" 00022 #include "usbhid.h" 00023 #include "xboxEnums.h" 00024 00025 /* Data Xbox 360 taken from descriptors */ 00026 #define EP_MAXPKTSIZE 32 // max size for data via USB 00027 00028 /* Names we give to the 3 Xbox360 pipes */ 00029 #define XBOX_CONTROL_PIPE 0 00030 #define XBOX_INPUT_PIPE 1 00031 #define XBOX_OUTPUT_PIPE 2 00032 00033 // PID and VID of the different devices 00034 #define XBOX_VID 0x045E // Microsoft Corporation 00035 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers 00036 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers 00037 #define GAMESTOP_VID 0x0E6F // Gamestop controller 00038 00039 #define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller 00040 #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging 00041 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver 00042 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver 00043 #define MADCATZ_WIRED_PID 0xF016 // Mad Catz wired controller 00044 #define JOYTECH_WIRED_PID 0xBEEF // For Joytech wired controller 00045 #define GAMESTOP_WIRED_PID 0x0401 // Gamestop wired controller 00046 #define AFTERGLOW_WIRED_PID 0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller 00047 00048 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer 00049 00050 #define XBOX_MAX_ENDPOINTS 3 00051 00052 /** This class implements support for a Xbox wired controller via USB. */ 00053 class XBOXUSB : public USBDeviceConfig { 00054 public: 00055 /** 00056 * Constructor for the XBOXUSB class. 00057 * @param pUsb Pointer to USB class instance. 00058 */ 00059 XBOXUSB(Usb *pUsb); 00060 00061 /** @name USBDeviceConfig implementation */ 00062 /** 00063 * Initialize the Xbox Controller. 00064 * @param parent Hub number. 00065 * @param port Port number on the hub. 00066 * @param lowspeed Speed of the device. 00067 * @return 0 on success. 00068 */ 00069 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); 00070 /** 00071 * Release the USB device. 00072 * @return 0 on success. 00073 */ 00074 uint8_t Release(); 00075 /** 00076 * Poll the USB Input endpoins and run the state machines. 00077 * @return 0 on success. 00078 */ 00079 uint8_t Poll(); 00080 00081 /** 00082 * Get the device address. 00083 * @return The device address. 00084 */ 00085 virtual uint8_t GetAddress() { 00086 return bAddress; 00087 }; 00088 00089 /** 00090 * Used to check if the controller has been initialized. 00091 * @return True if it's ready. 00092 */ 00093 virtual bool isReady() { 00094 return bPollEnable; 00095 }; 00096 00097 /** 00098 * Used by the USB core to check what this driver support. 00099 * @param vid The device's VID. 00100 * @param pid The device's PID. 00101 * @return Returns true if the device's VID and PID matches this driver. 00102 */ 00103 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { 00104 return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID || vid == GAMESTOP_VID) && (pid == XBOX_WIRED_PID || pid == MADCATZ_WIRED_PID || pid == GAMESTOP_WIRED_PID || pid == AFTERGLOW_WIRED_PID || pid == JOYTECH_WIRED_PID)); 00105 }; 00106 /**@}*/ 00107 00108 /** @name Xbox Controller functions */ 00109 /** 00110 * getButtonPress(ButtonEnum b) will return true as long as the button is held down. 00111 * 00112 * While getButtonClick(ButtonEnum b) will only return it once. 00113 * 00114 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), 00115 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). 00116 * @param b ::ButtonEnum to read. 00117 * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2. 00118 */ 00119 uint8_t getButtonPress(ButtonEnum b); 00120 bool getButtonClick(ButtonEnum b); 00121 /**@}*/ 00122 00123 /** @name Xbox Controller functions */ 00124 /** 00125 * Return the analog value from the joysticks on the controller. 00126 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. 00127 * @return Returns a signed 16-bit integer. 00128 */ 00129 int16_t getAnalogHat(AnalogHatEnum a); 00130 00131 /** Turn rumble off and all the LEDs on the controller. */ 00132 void setAllOff() { 00133 setRumbleOn(0, 0); 00134 setLedRaw(0); 00135 }; 00136 00137 /** Turn rumble off the controller. */ 00138 void setRumbleOff() { 00139 setRumbleOn(0, 0); 00140 }; 00141 /** 00142 * Turn rumble on. 00143 * @param lValue Left motor (big weight) inside the controller. 00144 * @param rValue Right motor (small weight) inside the controller. 00145 */ 00146 void setRumbleOn(uint8_t lValue, uint8_t rValue); 00147 /** 00148 * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum. 00149 * @param value See: 00150 * setLedOff(), setLedOn(LEDEnum l), 00151 * setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm). 00152 */ 00153 void setLedRaw(uint8_t value); 00154 00155 /** Turn all LEDs off the controller. */ 00156 void setLedOff() { 00157 setLedRaw(0); 00158 }; 00159 /** 00160 * Turn on a LED by using ::LEDEnum. 00161 * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. 00162 */ 00163 void setLedOn(LEDEnum l); 00164 /** 00165 * Turn on a LED by using ::LEDEnum. 00166 * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. 00167 */ 00168 void setLedBlink(LEDEnum l); 00169 /** 00170 * Used to set special LED modes supported by the Xbox controller. 00171 * @param lm See ::LEDModeEnum. 00172 */ 00173 void setLedMode(LEDModeEnum lm); 00174 00175 /** 00176 * Used to call your own function when the controller is successfully initialized. 00177 * @param funcOnInit Function to call. 00178 */ 00179 void attachOnInit(void (*funcOnInit)(void)) { 00180 pFuncOnInit = funcOnInit; 00181 }; 00182 /**@}*/ 00183 00184 /** True if a Xbox 360 controller is connected. */ 00185 bool Xbox360Connected; 00186 00187 protected: 00188 /** Pointer to USB class instance. */ 00189 Usb *pUsb; 00190 /** Device address. */ 00191 uint8_t bAddress; 00192 /** Endpoint info structure. */ 00193 EpInfo epInfo[XBOX_MAX_ENDPOINTS]; 00194 00195 private: 00196 /** 00197 * Called when the controller is successfully initialized. 00198 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. 00199 * This is useful for instance if you want to set the LEDs in a specific way. 00200 */ 00201 void onInit(); 00202 void (*pFuncOnInit)(void); // Pointer to function called in onInit() 00203 00204 bool bPollEnable; 00205 00206 /* Variables to store the buttons */ 00207 uint32_t ButtonState; 00208 uint32_t OldButtonState; 00209 uint16_t ButtonClickState; 00210 int16_t hatValue[4]; 00211 uint16_t controllerStatus; 00212 00213 bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not 00214 bool R2Clicked; 00215 00216 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data 00217 uint8_t writeBuf[8]; // General purpose buffer for output data 00218 00219 void readReport(); // read incoming data 00220 void printReport(); // print incoming date - Uncomment for debugging 00221 00222 /* Private commands */ 00223 void XboxCommand(uint8_t* data, uint16_t nbytes); 00224 }; 00225 #endif
Generated on Tue Jul 12 2022 18:12:05 by
