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 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 receivers
55 
56 #define XBOX_MAX_ENDPOINTS 9
57 
63 class XBOXRECV : public USBDeviceConfig {
64 public:
69  XBOXRECV(USB *pUsb);
70 
79  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
84  virtual uint8_t Release();
89  virtual uint8_t Poll();
94  virtual uint8_t GetAddress() { return bAddress; };
99  virtual bool isReady() { return bPollEnable; };
115  uint8_t getButtonPress(uint8_t controller, Button b);
116  bool getButtonClick(uint8_t controller, Button b);
126  int16_t getAnalogHat(uint8_t controller, AnalogHat a);
131  void setAllOff(uint8_t controller) { setRumbleOn(controller,0,0); setLedOff(controller); };
136  void setRumbleOff(uint8_t controller) { setRumbleOn(controller,0,0); };
143  void setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue);
151  void setLedRaw(uint8_t controller, uint8_t value);
156  void setLedOff(uint8_t controller) { setLedRaw(controller,0); };
162  void setLedOn(uint8_t controller, LED l);
168  void setLedBlink(uint8_t controller, LED l);
174  void setLedMode(uint8_t controller, LEDMode lm);
180  uint8_t getBatteryLevel(uint8_t controller);
186  bool buttonChanged(uint8_t controller);
192  uint8_t Xbox360Connected[4];
193 
194 protected:
198  uint8_t bAddress;
201 
202 private:
203  bool bPollEnable;
204 
205  /* Variables to store the buttons */
206  uint32_t ButtonState[4];
207  uint32_t OldButtonState[4];
208  uint16_t ButtonClickState[4];
209  int16_t hatValue[4][4];
210  uint16_t controllerStatus[4];
211  bool buttonStateChanged[4]; // True if a button has changed
212 
213  bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
214  bool R2Clicked[4];
215 
216  unsigned long timer; // Timing for checkStatus() signals
217 
218  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
219  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
220 
221  void readReport(uint8_t controller); // read incoming data
222  void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging
223 
224  /* Private commands */
225  void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes);
226  void checkStatus();
227 };
228 #endif