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
XBOXRECV.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 getBatteryLevel and checkStatus functions made by timstamp.co.uk found using BusHound from Perisoft.net 00018 */ 00019 00020 #ifndef _xboxrecv_h_ 00021 #define _xboxrecv_h_ 00022 00023 #include "Usb.h" 00024 #include "xboxEnums.h" 00025 00026 /* Data Xbox 360 taken from descriptors */ 00027 #define EP_MAXPKTSIZE 32 // max size for data via USB 00028 00029 /* Names we give to the 9 Xbox360 pipes */ 00030 #define XBOX_CONTROL_PIPE 0 00031 #define XBOX_INPUT_PIPE_1 1 00032 #define XBOX_OUTPUT_PIPE_1 2 00033 #define XBOX_INPUT_PIPE_2 3 00034 #define XBOX_OUTPUT_PIPE_2 4 00035 #define XBOX_INPUT_PIPE_3 5 00036 #define XBOX_OUTPUT_PIPE_3 6 00037 #define XBOX_INPUT_PIPE_4 7 00038 #define XBOX_OUTPUT_PIPE_4 8 00039 00040 // PID and VID of the different devices 00041 #define XBOX_VID 0x045E // Microsoft Corporation 00042 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz receivers 00043 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers 00044 00045 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver 00046 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver 00047 00048 #define XBOX_MAX_ENDPOINTS 9 00049 00050 /** 00051 * This class implements support for a Xbox Wireless receiver. 00052 * 00053 * Up to four controllers can connect to one receiver, if more is needed one can use a second receiver via the USBHub class. 00054 */ 00055 class XBOXRECV : public USBDeviceConfig { 00056 public: 00057 /** 00058 * Constructor for the XBOXRECV class. 00059 * @param pUsb Pointer to USB class instance. 00060 */ 00061 XBOXRECV(Usb *pUsb); 00062 00063 /** @name USBDeviceConfig implementation */ 00064 /** 00065 * Address assignment and basic initilization is done here. 00066 * @param parent Hub number. 00067 * @param port Port number on the hub. 00068 * @param lowspeed Speed of the device. 00069 * @return 0 on success. 00070 */ 00071 uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed); 00072 /** 00073 * Initialize the Xbox wireless receiver. 00074 * @param parent Hub number. 00075 * @param port Port number on the hub. 00076 * @param lowspeed Speed of the device. 00077 * @return 0 on success. 00078 */ 00079 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); 00080 /** 00081 * Release the USB device. 00082 * @return 0 on success. 00083 */ 00084 uint8_t Release(); 00085 /** 00086 * Poll the USB Input endpoins and run the state machines. 00087 * @return 0 on success. 00088 */ 00089 uint8_t Poll(); 00090 00091 /** 00092 * Get the device address. 00093 * @return The device address. 00094 */ 00095 virtual uint8_t GetAddress() { 00096 return bAddress; 00097 }; 00098 00099 /** 00100 * Used to check if the controller has been initialized. 00101 * @return True if it's ready. 00102 */ 00103 virtual bool isReady() { 00104 return bPollEnable; 00105 }; 00106 00107 /** 00108 * Used by the USB core to check what this driver support. 00109 * @param vid The device's VID. 00110 * @param pid The device's PID. 00111 * @return Returns true if the device's VID and PID matches this driver. 00112 */ 00113 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { 00114 return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && (pid == XBOX_WIRELESS_RECEIVER_PID || pid == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID)); 00115 }; 00116 /**@}*/ 00117 00118 /** @name Xbox Controller functions */ 00119 /** 00120 * getButtonPress(uint8_t controller, ButtonEnum b) will return true as long as the button is held down. 00121 * 00122 * While getButtonClick(uint8_t controller, ButtonEnum b) will only return it once. 00123 * 00124 * So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, ButtonEnum b), 00125 * but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, ButtonEnum b). 00126 * @param b ::ButtonEnum to read. 00127 * @param controller The controller to read from. Default to 0. 00128 * @return getButtonClick(uint8_t controller, ButtonEnum b) will return a bool, while getButtonPress(uint8_t controller, ButtonEnum b) will return a byte if reading ::L2 or ::R2. 00129 */ 00130 uint8_t getButtonPress(ButtonEnum b, uint8_t controller = 0); 00131 bool getButtonClick(ButtonEnum b, uint8_t controller = 0); 00132 /**@}*/ 00133 00134 /** @name Xbox Controller functions */ 00135 /** 00136 * Return the analog value from the joysticks on the controller. 00137 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. 00138 * @param controller The controller to read from. Default to 0. 00139 * @return Returns a signed 16-bit integer. 00140 */ 00141 int16_t getAnalogHat(AnalogHatEnum a, uint8_t controller = 0); 00142 00143 /** 00144 * Used to disconnect any of the controllers. 00145 * @param controller The controller to disconnect. Default to 0. 00146 */ 00147 void disconnect(uint8_t controller = 0); 00148 00149 /** 00150 * Turn rumble off and all the LEDs on the specific controller. 00151 * @param controller The controller to write to. Default to 0. 00152 */ 00153 void setAllOff(uint8_t controller = 0) { 00154 setRumbleOn(0, 0, controller); 00155 setLedOff(controller); 00156 }; 00157 00158 /** 00159 * Turn rumble off the specific controller. 00160 * @param controller The controller to write to. Default to 0. 00161 */ 00162 void setRumbleOff(uint8_t controller = 0) { 00163 setRumbleOn(0, 0, controller); 00164 }; 00165 /** 00166 * Turn rumble on. 00167 * @param lValue Left motor (big weight) inside the controller. 00168 * @param rValue Right motor (small weight) inside the controller. 00169 * @param controller The controller to write to. Default to 0. 00170 */ 00171 void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0); 00172 /** 00173 * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum. 00174 * @param value See: 00175 * setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l), 00176 * setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm). 00177 * @param controller The controller to write to. Default to 0. 00178 */ 00179 void setLedRaw(uint8_t value, uint8_t controller = 0); 00180 00181 /** 00182 * Turn all LEDs off the specific controller. 00183 * @param controller The controller to write to. Default to 0. 00184 */ 00185 void setLedOff(uint8_t controller = 0) { 00186 setLedRaw(0, controller); 00187 }; 00188 /** 00189 * Turn on a LED by using ::LEDEnum. 00190 * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. 00191 * @param controller The controller to write to. Default to 0. 00192 */ 00193 void setLedOn(LEDEnum l, uint8_t controller = 0); 00194 /** 00195 * Turn on a LED by using ::LEDEnum. 00196 * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. 00197 * @param controller The controller to write to. Default to 0. 00198 */ 00199 void setLedBlink(LEDEnum l, uint8_t controller = 0); 00200 /** 00201 * Used to set special LED modes supported by the Xbox controller. 00202 * @param lm See ::LEDModeEnum. 00203 * @param controller The controller to write to. Default to 0. 00204 */ 00205 void setLedMode(LEDModeEnum lm, uint8_t controller = 0); 00206 /** 00207 * Used to get the battery level from the controller. 00208 * @param controller The controller to read from. Default to 0. 00209 * @return Returns the battery level as an integer in the range of 0-3. 00210 */ 00211 uint8_t getBatteryLevel(uint8_t controller = 0); 00212 /** 00213 * Used to check if a button has changed. 00214 * @param controller The controller to read from. Default to 0. 00215 * @return True if a button has changed. 00216 */ 00217 bool buttonChanged(uint8_t controller = 0); 00218 00219 /** 00220 * Used to call your own function when the controller is successfully initialized. 00221 * @param funcOnInit Function to call. 00222 */ 00223 void attachOnInit(void (*funcOnInit)(void)) { 00224 pFuncOnInit = funcOnInit; 00225 }; 00226 /**@}*/ 00227 00228 /** True if a wireless receiver is connected. */ 00229 bool XboxReceiverConnected; 00230 /** Variable used to indicate if the XBOX 360 controller is successfully connected. */ 00231 uint8_t Xbox360Connected[4]; 00232 00233 protected: 00234 /** Pointer to USB class instance. */ 00235 Usb *pUsb; 00236 /** Device address. */ 00237 uint8_t bAddress; 00238 /** Endpoint info structure. */ 00239 EpInfo epInfo[XBOX_MAX_ENDPOINTS]; 00240 00241 private: 00242 /** 00243 * Called when the controller is successfully initialized. 00244 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. 00245 * This is useful for instance if you want to set the LEDs in a specific way. 00246 * @param controller The initialized controller. 00247 */ 00248 void onInit(uint8_t controller); 00249 void (*pFuncOnInit)(void); // Pointer to function called in onInit() 00250 00251 bool bPollEnable; 00252 00253 /* Variables to store the buttons */ 00254 uint32_t ButtonState[4]; 00255 uint32_t OldButtonState[4]; 00256 uint16_t ButtonClickState[4]; 00257 int16_t hatValue[4][4]; 00258 uint16_t controllerStatus[4]; 00259 bool buttonStateChanged[4]; // True if a button has changed 00260 00261 bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not 00262 bool R2Clicked[4]; 00263 00264 uint32_t checkStatusTimer; // Timing for checkStatus() signals 00265 00266 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data 00267 uint8_t writeBuf[7]; // General purpose buffer for output data 00268 00269 void readReport(uint8_t controller); // read incoming data 00270 void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging 00271 00272 /* Private commands */ 00273 void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes); 00274 void checkStatus(); 00275 }; 00276 #endif
Generated on Tue Jul 12 2022 18:12:05 by
1.7.2