]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/XBOXOLD.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / XBOXOLD.h
1 /* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
2
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
8 the GPL2 ("Copyleft").
9
10 Contact information
11 -------------------
12
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
16 */
17
18 #ifndef _xboxold_h_
19 #define _xboxold_h_
20
21 #include "Usb.h"
22 #include "hid.h"
23 #include "controllerEnums.h"
24
25 /* Data Xbox taken from descriptors */
26 #define EP_MAXPKTSIZE 32 // Max size for data via USB
27
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
32
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
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)
42
43 #define XBOX_MAX_ENDPOINTS 3
44
45 /** This class implements support for a the original Xbox controller via USB. */
46 class XBOXOLD : public USBDeviceConfig {
47 public:
48 /**
49 * Constructor for the XBOXOLD class.
50 * @param pUsb Pointer to USB class instance.
51 */
52 XBOXOLD(USB *pUsb);
53
54 /** @name USBDeviceConfig implementation */
55 /**
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.
61 */
62 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
63 /**
64 * Release the USB device.
65 * @return 0 on success.
66 */
67 uint8_t Release();
68 /**
69 * Poll the USB Input endpoins and run the state machines.
70 * @return 0 on success.
71 */
72 uint8_t Poll();
73
74 /**
75 * Get the device address.
76 * @return The device address.
77 */
78 virtual uint8_t GetAddress() {
79 return bAddress;
80 };
81
82 /**
83 * Used to check if the controller has been initialized.
84 * @return True if it's ready.
85 */
86 virtual bool isReady() {
87 return bPollEnable;
88 };
89
90 /**
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.
95 */
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));
98 };
99 /**@}*/
100
101 /** @name Xbox Controller functions */
102 /**
103 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
104 *
105 * While getButtonClick(ButtonEnum b) will only return it once.
106 *
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.
111 */
112 uint8_t getButtonPress(ButtonEnum b);
113 bool getButtonClick(ButtonEnum b);
114 /**@}*/
115
116 /** @name Xbox Controller functions */
117 /**
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.
121 */
122 int16_t getAnalogHat(AnalogHatEnum a);
123
124 /** Turn rumble off the controller. */
125 void setRumbleOff() {
126 setRumbleOn(0, 0);
127 };
128 /**
129 * Turn rumble on.
130 * @param lValue Left motor (big weight) inside the controller.
131 * @param rValue Right motor (small weight) inside the controller.
132 */
133 void setRumbleOn(uint8_t lValue, uint8_t rValue);
134
135 /**
136 * Used to call your own function when the controller is successfully initialized.
137 * @param funcOnInit Function to call.
138 */
139 void attachOnInit(void (*funcOnInit)(void)) {
140 pFuncOnInit = funcOnInit;
141 };
142 /**@}*/
143
144 /** True if a Xbox controller is connected. */
145 bool XboxConnected;
146
147 protected:
148 /** Pointer to USB class instance. */
149 USB *pUsb;
150 /** Device address. */
151 uint8_t bAddress;
152 /** Endpoint info structure. */
153 EpInfo epInfo[XBOX_MAX_ENDPOINTS];
154
155 private:
156 /**
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.
160 */
161 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
162
163 bool bPollEnable;
164
165 /* Variables to store the digital buttons */
166 uint8_t ButtonState;
167 uint8_t OldButtonState;
168 uint8_t ButtonClickState;
169
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];
174
175 int16_t hatValue[4]; // Joystick values
176
177 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
178
179 void readReport(); // Read incoming data
180 void printReport(uint16_t length); // Print incoming date
181
182 /* Private commands */
183 void XboxCommand(uint8_t* data, uint16_t nbytes);
184 };
185 #endif
Imprint / Impressum