1 /* Copyright (C) 2013 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 "controllerEnums.h"
25 /* Data Xbox taken from descriptors */
26 #define EP_MAXPKTSIZE 32 // Max size for data via USB
28 /* Names we give to the 3 Xbox 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
38 #define XBOX_OLD_PID1 0x0202 // Original Microsoft Xbox controller (US)
39 #define XBOX_OLD_PID2 0x0285 // Original Microsoft Xbox controller (Japan)
40 #define XBOX_OLD_PID3 0x0287 // Microsoft Microsoft Xbox Controller S
41 #define XBOX_OLD_PID4 0x0289 // Smaller Microsoft Xbox controller (US)
43 #define XBOX_MAX_ENDPOINTS 3
45 /** This class implements support for a the original Xbox controller via USB. */
46 class XBOXOLD : public USBDeviceConfig {
49 * Constructor for the XBOXOLD class.
50 * @param pUsb Pointer to USB class instance.
54 /** @name USBDeviceConfig implementation */
56 * Initialize the Xbox Controller.
57 * @param parent Hub number.
58 * @param port Port number on the hub.
59 * @param lowspeed Speed of the device.
60 * @return 0 on success.
62 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
64 * Release the USB device.
65 * @return 0 on success.
69 * Poll the USB Input endpoins and run the state machines.
70 * @return 0 on success.
75 * Get the device address.
76 * @return The device address.
78 virtual uint8_t GetAddress() {
83 * Used to check if the controller has been initialized.
84 * @return True if it's ready.
86 virtual bool isReady() {
91 * Used by the USB core to check what this driver support.
92 * @param vid The device's VID.
93 * @param pid The device's PID.
94 * @return Returns true if the device's VID and PID matches this driver.
96 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
97 return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && (pid == XBOX_OLD_PID1 || pid == XBOX_OLD_PID2 || pid == XBOX_OLD_PID3 || pid == XBOX_OLD_PID4));
101 /** @name Xbox Controller functions */
103 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
105 * While getButtonClick(ButtonEnum b) will only return it once.
107 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
108 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
109 * @param b ::ButtonEnum to read.
110 * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
112 uint8_t getButtonPress(ButtonEnum b);
113 bool getButtonClick(ButtonEnum b);
116 /** @name Xbox Controller functions */
118 * Return the analog value from the joysticks on the controller.
119 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
120 * @return Returns a signed 16-bit integer.
122 int16_t getAnalogHat(AnalogHatEnum a);
124 /** Turn rumble off the controller. */
125 void setRumbleOff() {
130 * @param lValue Left motor (big weight) inside the controller.
131 * @param rValue Right motor (small weight) inside the controller.
133 void setRumbleOn(uint8_t lValue, uint8_t rValue);
136 * Used to call your own function when the controller is successfully initialized.
137 * @param funcOnInit Function to call.
139 void attachOnInit(void (*funcOnInit)(void)) {
140 pFuncOnInit = funcOnInit;
144 /** True if a Xbox controller is connected. */
148 /** Pointer to USB class instance. */
150 /** Device address. */
152 /** Endpoint info structure. */
153 EpInfo epInfo[XBOX_MAX_ENDPOINTS];
157 * Called when the controller is successfully initialized.
158 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
159 * This is useful for instance if you want to set the LEDs in a specific way.
161 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
165 /* Variables to store the digital buttons */
167 uint8_t OldButtonState;
168 uint8_t ButtonClickState;
170 /* Variables to store the analog buttons */
171 uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
172 uint8_t oldButtonValues[8];
173 bool buttonClicked[8];
175 int16_t hatValue[4]; // Joystick values
177 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
179 void readReport(); // Read incoming data
180 void printReport(uint16_t length); // Print incoming date
182 /* Private commands */
183 void XboxCommand(uint8_t* data, uint16_t nbytes);