USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
XBOXRECV.h
Go to the documentation of this file.
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 #if defined(ARDUINO) && ARDUINO >= 100
24 #include "Arduino.h"
25 #else
26 #include "WProgram.h"
27 #endif
28 
29 #include "Usb.h"
30 #include "xboxEnums.h"
31 
32 /* Data Xbox 360 taken from descriptors */
33 #define EP_MAXPKTSIZE 32 // max size for data via USB
34 
35 /* Endpoint types */
36 #define EP_INTERRUPT 0x03
37 
38 /* Names we give to the 9 Xbox360 pipes */
39 #define XBOX_CONTROL_PIPE 0
40 #define XBOX_INPUT_PIPE_1 1
41 #define XBOX_OUTPUT_PIPE_1 2
42 #define XBOX_INPUT_PIPE_2 3
43 #define XBOX_OUTPUT_PIPE_2 4
44 #define XBOX_INPUT_PIPE_3 5
45 #define XBOX_OUTPUT_PIPE_3 6
46 #define XBOX_INPUT_PIPE_4 7
47 #define XBOX_OUTPUT_PIPE_4 8
48 
49 // PID and VID of the different devices
50 #define XBOX_VID 0x045E // Microsoft Corporation
51 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz receivers
52 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
53 
54 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
55 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
56 
57 #define XBOX_MAX_ENDPOINTS 9
58 
64 class XBOXRECV : public USBDeviceConfig {
65 public:
70  XBOXRECV(USB *pUsb);
71 
80  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
85  virtual uint8_t Release();
90  virtual uint8_t Poll();
91 
96  virtual uint8_t GetAddress() {
97  return bAddress;
98  };
99 
104  virtual bool isReady() {
105  return bPollEnable;
106  };
107 
114  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
115  return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && (pid == XBOX_WIRELESS_RECEIVER_PID || pid == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID));
116  };
132  uint8_t getButtonPress(Button b, uint8_t controller = 0);
133  bool getButtonClick(Button b, uint8_t controller = 0);
143  int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0);
144 
149  void setAllOff(uint8_t controller = 0) {
150  setRumbleOn(0, 0, controller);
151  setLedOff(controller);
152  };
153 
158  void setRumbleOff(uint8_t controller = 0) {
159  setRumbleOn(0, 0, controller);
160  };
167  void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0);
175  void setLedRaw(uint8_t value, uint8_t controller = 0);
176 
181  void setLedOff(uint8_t controller = 0) {
182  setLedRaw(0, controller);
183  };
189  void setLedOn(LED l, uint8_t controller = 0);
195  void setLedBlink(LED l, uint8_t controller = 0);
201  void setLedMode(LEDMode lm, uint8_t controller = 0);
207  uint8_t getBatteryLevel(uint8_t controller = 0);
213  bool buttonChanged(uint8_t controller = 0);
214 
219  void attachOnInit(void (*funcOnInit)(void)) {
220  pFuncOnInit = funcOnInit;
221  };
227  uint8_t Xbox360Connected[4];
228 
229 protected:
233  uint8_t bAddress;
236 
237 private:
244  void onInit(uint8_t controller);
245  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
246 
247  bool bPollEnable;
248 
249  /* Variables to store the buttons */
250  uint32_t ButtonState[4];
251  uint32_t OldButtonState[4];
252  uint16_t ButtonClickState[4];
253  int16_t hatValue[4][4];
254  uint16_t controllerStatus[4];
255  bool buttonStateChanged[4]; // True if a button has changed
256 
257  bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
258  bool R2Clicked[4];
259 
260  unsigned long timer; // Timing for checkStatus() signals
261 
262  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
263  uint8_t writeBuf[7]; // General purpose buffer for output data
264 
265  void readReport(uint8_t controller); // read incoming data
266  void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging
267 
268  /* Private commands */
269  void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes);
270  void checkStatus();
271 };
272 #endif