1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
23 #include "xboxEnums.h"
25 /* Data Xbox 360 taken from descriptors */
26 #define EP_MAXPKTSIZE 32 // max size for data via USB
28 /* Names we give to the 3 Xbox360 pipes */
29 #define XBOX_CONTROL_PIPE 0
30 #define XBOX_INPUT_PIPE 1
31 #define XBOX_OUTPUT_PIPE 2
33 // PID and VID of the different devices
34 #define XBOX_VID 0x045E // Microsoft Corporation
35 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
36 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
37 #define GAMESTOP_VID 0x0E6F // Gamestop controller
39 #define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller
40 #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
41 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
42 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
43 #define MADCATZ_WIRED_PID 0xF016 // Mad Catz wired controller
44 #define JOYTECH_WIRED_PID 0xBEEF // For Joytech wired controller
45 #define GAMESTOP_WIRED_PID 0x0401 // Gamestop wired controller
46 #define AFTERGLOW_WIRED_PID 0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller
48 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
50 #define XBOX_MAX_ENDPOINTS 3
52 /** This class implements support for a Xbox wired controller via USB. */
53 class XBOXUSB : public USBDeviceConfig {
56 * Constructor for the XBOXUSB class.
57 * @param pUsb Pointer to USB class instance.
61 /** @name USBDeviceConfig implementation */
63 * Initialize the Xbox Controller.
64 * @param parent Hub number.
65 * @param port Port number on the hub.
66 * @param lowspeed Speed of the device.
67 * @return 0 on success.
69 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
71 * Release the USB device.
72 * @return 0 on success.
76 * Poll the USB Input endpoins and run the state machines.
77 * @return 0 on success.
82 * Get the device address.
83 * @return The device address.
85 virtual uint8_t GetAddress() {
90 * Used to check if the controller has been initialized.
91 * @return True if it's ready.
93 virtual bool isReady() {
98 * Used by the USB core to check what this driver support.
99 * @param vid The device's VID.
100 * @param pid The device's PID.
101 * @return Returns true if the device's VID and PID matches this driver.
103 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
104 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));
108 /** @name Xbox Controller functions */
110 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
112 * While getButtonClick(ButtonEnum b) will only return it once.
114 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
115 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
116 * @param b ::ButtonEnum to read.
117 * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
119 uint8_t getButtonPress(ButtonEnum b);
120 bool getButtonClick(ButtonEnum b);
123 /** @name Xbox Controller functions */
125 * Return the analog value from the joysticks on the controller.
126 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
127 * @return Returns a signed 16-bit integer.
129 int16_t getAnalogHat(AnalogHatEnum a);
131 /** Turn rumble off and all the LEDs on the controller. */
137 /** Turn rumble off the controller. */
138 void setRumbleOff() {
143 * @param lValue Left motor (big weight) inside the controller.
144 * @param rValue Right motor (small weight) inside the controller.
146 void setRumbleOn(uint8_t lValue, uint8_t rValue);
148 * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum.
150 * setLedOff(), setLedOn(LEDEnum l),
151 * setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm).
153 void setLedRaw(uint8_t value);
155 /** Turn all LEDs off the controller. */
160 * Turn on a LED by using ::LEDEnum.
161 * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
163 void setLedOn(LEDEnum l);
165 * Turn on a LED by using ::LEDEnum.
166 * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
168 void setLedBlink(LEDEnum l);
170 * Used to set special LED modes supported by the Xbox controller.
171 * @param lm See ::LEDModeEnum.
173 void setLedMode(LEDModeEnum lm);
176 * Used to call your own function when the controller is successfully initialized.
177 * @param funcOnInit Function to call.
179 void attachOnInit(void (*funcOnInit)(void)) {
180 pFuncOnInit = funcOnInit;
184 /** True if a Xbox 360 controller is connected. */
185 bool Xbox360Connected;
188 /** Pointer to USB class instance. */
190 /** Device address. */
192 /** Endpoint info structure. */
193 EpInfo epInfo[XBOX_MAX_ENDPOINTS];
197 * Called when the controller is successfully initialized.
198 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
199 * This is useful for instance if you want to set the LEDs in a specific way.
202 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
206 /* Variables to store the buttons */
207 uint32_t ButtonState;
208 uint32_t OldButtonState;
209 uint16_t ButtonClickState;
211 uint16_t controllerStatus;
213 bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
216 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
217 uint8_t writeBuf[8]; // General purpose buffer for output data
219 void readReport(); // read incoming data
220 void printReport(); // print incoming date - Uncomment for debugging
222 /* Private commands */
223 void XboxCommand(uint8_t* data, uint16_t nbytes);