]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/XBOXRECV.h
ps2_usb: Fix for VUSB configuration
[tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / XBOXRECV.h
1 /* Copyright (C) 2012 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 getBatteryLevel and checkStatus functions made by timstamp.co.uk found using BusHound from Perisoft.net
18 */
19
20 #ifndef _xboxrecv_h_
21 #define _xboxrecv_h_
22
23 #include "Usb.h"
24 #include "xboxEnums.h"
25
26 /* Data Xbox 360 taken from descriptors */
27 #define EP_MAXPKTSIZE 32 // max size for data via USB
28
29 /* Names we give to the 9 Xbox360 pipes */
30 #define XBOX_CONTROL_PIPE 0
31 #define XBOX_INPUT_PIPE_1 1
32 #define XBOX_OUTPUT_PIPE_1 2
33 #define XBOX_INPUT_PIPE_2 3
34 #define XBOX_OUTPUT_PIPE_2 4
35 #define XBOX_INPUT_PIPE_3 5
36 #define XBOX_OUTPUT_PIPE_3 6
37 #define XBOX_INPUT_PIPE_4 7
38 #define XBOX_OUTPUT_PIPE_4 8
39
40 // PID and VID of the different devices
41 #define XBOX_VID 0x045E // Microsoft Corporation
42 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz receivers
43 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
44
45 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
46 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
47
48 #define XBOX_MAX_ENDPOINTS 9
49
50 /**
51 * This class implements support for a Xbox Wireless receiver.
52 *
53 * Up to four controllers can connect to one receiver, if more is needed one can use a second receiver via the USBHub class.
54 */
55 class XBOXRECV : public USBDeviceConfig {
56 public:
57 /**
58 * Constructor for the XBOXRECV class.
59 * @param pUsb Pointer to USB class instance.
60 */
61 XBOXRECV(USB *pUsb);
62
63 /** @name USBDeviceConfig implementation */
64 /**
65 * Address assignment and basic initilization is done here.
66 * @param parent Hub number.
67 * @param port Port number on the hub.
68 * @param lowspeed Speed of the device.
69 * @return 0 on success.
70 */
71 uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
72 /**
73 * Initialize the Xbox wireless receiver.
74 * @param parent Hub number.
75 * @param port Port number on the hub.
76 * @param lowspeed Speed of the device.
77 * @return 0 on success.
78 */
79 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
80 /**
81 * Release the USB device.
82 * @return 0 on success.
83 */
84 uint8_t Release();
85 /**
86 * Poll the USB Input endpoins and run the state machines.
87 * @return 0 on success.
88 */
89 uint8_t Poll();
90
91 /**
92 * Get the device address.
93 * @return The device address.
94 */
95 virtual uint8_t GetAddress() {
96 return bAddress;
97 };
98
99 /**
100 * Used to check if the controller has been initialized.
101 * @return True if it's ready.
102 */
103 virtual bool isReady() {
104 return bPollEnable;
105 };
106
107 /**
108 * Used by the USB core to check what this driver support.
109 * @param vid The device's VID.
110 * @param pid The device's PID.
111 * @return Returns true if the device's VID and PID matches this driver.
112 */
113 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
114 return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && (pid == XBOX_WIRELESS_RECEIVER_PID || pid == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID));
115 };
116 /**@}*/
117
118 /** @name Xbox Controller functions */
119 /**
120 * getButtonPress(uint8_t controller, ButtonEnum b) will return true as long as the button is held down.
121 *
122 * While getButtonClick(uint8_t controller, ButtonEnum b) will only return it once.
123 *
124 * So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, ButtonEnum b),
125 * but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, ButtonEnum b).
126 * @param b ::ButtonEnum to read.
127 * @param controller The controller to read from. Default to 0.
128 * @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.
129 */
130 uint8_t getButtonPress(ButtonEnum b, uint8_t controller = 0);
131 bool getButtonClick(ButtonEnum b, uint8_t controller = 0);
132 /**@}*/
133
134 /** @name Xbox Controller functions */
135 /**
136 * Return the analog value from the joysticks on the controller.
137 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
138 * @param controller The controller to read from. Default to 0.
139 * @return Returns a signed 16-bit integer.
140 */
141 int16_t getAnalogHat(AnalogHatEnum a, uint8_t controller = 0);
142
143 /**
144 * Used to disconnect any of the controllers.
145 * @param controller The controller to disconnect. Default to 0.
146 */
147 void disconnect(uint8_t controller = 0);
148
149 /**
150 * Turn rumble off and all the LEDs on the specific controller.
151 * @param controller The controller to write to. Default to 0.
152 */
153 void setAllOff(uint8_t controller = 0) {
154 setRumbleOn(0, 0, controller);
155 setLedOff(controller);
156 };
157
158 /**
159 * Turn rumble off the specific controller.
160 * @param controller The controller to write to. Default to 0.
161 */
162 void setRumbleOff(uint8_t controller = 0) {
163 setRumbleOn(0, 0, controller);
164 };
165 /**
166 * Turn rumble on.
167 * @param lValue Left motor (big weight) inside the controller.
168 * @param rValue Right motor (small weight) inside the controller.
169 * @param controller The controller to write to. Default to 0.
170 */
171 void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0);
172 /**
173 * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum.
174 * @param value See:
175 * setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l),
176 * setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm).
177 * @param controller The controller to write to. Default to 0.
178 */
179 void setLedRaw(uint8_t value, uint8_t controller = 0);
180
181 /**
182 * Turn all LEDs off the specific controller.
183 * @param controller The controller to write to. Default to 0.
184 */
185 void setLedOff(uint8_t controller = 0) {
186 setLedRaw(0, controller);
187 };
188 /**
189 * Turn on a LED by using ::LEDEnum.
190 * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
191 * @param controller The controller to write to. Default to 0.
192 */
193 void setLedOn(LEDEnum l, uint8_t controller = 0);
194 /**
195 * Turn on a LED by using ::LEDEnum.
196 * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
197 * @param controller The controller to write to. Default to 0.
198 */
199 void setLedBlink(LEDEnum l, uint8_t controller = 0);
200 /**
201 * Used to set special LED modes supported by the Xbox controller.
202 * @param lm See ::LEDModeEnum.
203 * @param controller The controller to write to. Default to 0.
204 */
205 void setLedMode(LEDModeEnum lm, uint8_t controller = 0);
206 /**
207 * Used to get the battery level from the controller.
208 * @param controller The controller to read from. Default to 0.
209 * @return Returns the battery level as an integer in the range of 0-3.
210 */
211 uint8_t getBatteryLevel(uint8_t controller = 0);
212 /**
213 * Used to check if a button has changed.
214 * @param controller The controller to read from. Default to 0.
215 * @return True if a button has changed.
216 */
217 bool buttonChanged(uint8_t controller = 0);
218
219 /**
220 * Used to call your own function when the controller is successfully initialized.
221 * @param funcOnInit Function to call.
222 */
223 void attachOnInit(void (*funcOnInit)(void)) {
224 pFuncOnInit = funcOnInit;
225 };
226 /**@}*/
227
228 /** True if a wireless receiver is connected. */
229 bool XboxReceiverConnected;
230 /** Variable used to indicate if the XBOX 360 controller is successfully connected. */
231 uint8_t Xbox360Connected[4];
232
233 protected:
234 /** Pointer to USB class instance. */
235 USB *pUsb;
236 /** Device address. */
237 uint8_t bAddress;
238 /** Endpoint info structure. */
239 EpInfo epInfo[XBOX_MAX_ENDPOINTS];
240
241 private:
242 /**
243 * Called when the controller is successfully initialized.
244 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
245 * This is useful for instance if you want to set the LEDs in a specific way.
246 * @param controller The initialized controller.
247 */
248 void onInit(uint8_t controller);
249 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
250
251 bool bPollEnable;
252
253 /* Variables to store the buttons */
254 uint32_t ButtonState[4];
255 uint32_t OldButtonState[4];
256 uint16_t ButtonClickState[4];
257 int16_t hatValue[4][4];
258 uint16_t controllerStatus[4];
259 bool buttonStateChanged[4]; // True if a button has changed
260
261 bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
262 bool R2Clicked[4];
263
264 uint32_t checkStatusTimer; // Timing for checkStatus() signals
265
266 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
267 uint8_t writeBuf[7]; // General purpose buffer for output data
268
269 void readReport(uint8_t controller); // read incoming data
270 void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging
271
272 /* Private commands */
273 void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes);
274 void checkStatus();
275 };
276 #endif
Imprint / Impressum