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 "controllerEnums.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 XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
52 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
53 
54 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
55 
56 #define XBOX_MAX_ENDPOINTS 9
57 
59 enum LEDMode {
60  ROTATING = 0x0A,
61  FASTBLINK = 0x0B,
62  SLOWBLINK = 0x0C,
63  ALTERNATING = 0x0D,
64 };
65 
71 class XBOXRECV : public USBDeviceConfig {
72 public:
77  XBOXRECV(USB *pUsb);
78 
87  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
92  virtual uint8_t Release();
97  virtual uint8_t Poll();
102  virtual uint8_t GetAddress() { return bAddress; };
107  virtual bool isReady() { return bPollEnable; };
121  uint8_t getButtonPress(uint8_t controller, Button b);
122  bool getButtonClick(uint8_t controller, Button b);
132  int16_t getAnalogHat(uint8_t controller, AnalogHat a);
137  void setAllOff(uint8_t controller) { setRumbleOn(controller,0,0); setLedOff(controller); };
142  void setRumbleOff(uint8_t controller) { setRumbleOn(controller,0,0); };
149  void setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue);
157  void setLedRaw(uint8_t controller, uint8_t value);
162  void setLedOff(uint8_t controller) { setLedRaw(controller,0); };
168  void setLedOn(uint8_t controller, LED l);
174  void setLedBlink(uint8_t controller, LED l);
180  void setLedMode(uint8_t controller, LEDMode lm);
186  uint8_t getBatteryLevel(uint8_t controller);
192  bool buttonChanged(uint8_t controller);
198  uint8_t Xbox360Connected[4];
199 
200 protected:
204  uint8_t bAddress;
207 
208 private:
209  bool bPollEnable;
210 
211  /* Variables to store the buttons */
212  uint32_t ButtonState[4];
213  uint32_t OldButtonState[4];
214  uint16_t ButtonClickState[4];
215  int16_t hatValue[4][4];
216  uint16_t controllerStatus[4];
217  bool buttonStateChanged[4]; // True if a button has changed
218 
219  bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
220  bool R2Clicked[4];
221 
222  unsigned long timer; // Timing for checkStatus() signals
223 
224  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
225  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
226 
227  void readReport(uint8_t controller); // read incoming data
228  void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging
229 
230  /* Private commands */
231  void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes);
232  void checkStatus();
233 };
234 #endif