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();
90 
95  virtual uint8_t GetAddress() {
96  return bAddress;
97  };
98 
103  virtual bool isReady() {
104  return bPollEnable;
105  };
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);
133 
138  void setAllOff(uint8_t controller) {
139  setRumbleOn(controller, 0, 0);
140  setLedOff(controller);
141  };
142 
147  void setRumbleOff(uint8_t controller) {
148  setRumbleOn(controller, 0, 0);
149  };
156  void setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue);
164  void setLedRaw(uint8_t controller, uint8_t value);
165 
170  void setLedOff(uint8_t controller) {
171  setLedRaw(controller, 0);
172  };
178  void setLedOn(uint8_t controller, LED l);
184  void setLedBlink(uint8_t controller, LED l);
190  void setLedMode(uint8_t controller, LEDMode lm);
196  uint8_t getBatteryLevel(uint8_t controller);
202  bool buttonChanged(uint8_t controller);
208  uint8_t Xbox360Connected[4];
209 
210 protected:
214  uint8_t bAddress;
217 
218 private:
219  bool bPollEnable;
220 
221  /* Variables to store the buttons */
222  uint32_t ButtonState[4];
223  uint32_t OldButtonState[4];
224  uint16_t ButtonClickState[4];
225  int16_t hatValue[4][4];
226  uint16_t controllerStatus[4];
227  bool buttonStateChanged[4]; // True if a button has changed
228 
229  bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
230  bool R2Clicked[4];
231 
232  unsigned long timer; // Timing for checkStatus() signals
233 
234  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
235  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
236 
237  void readReport(uint8_t controller); // read incoming data
238  void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging
239 
240  /* Private commands */
241  void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes);
242  void checkStatus();
243 };
244 #endif