USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
XBOXUSB.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 
18 #ifndef _xboxusb_h_
19 #define _xboxusb_h_
20 
21 #if defined(ARDUINO) && ARDUINO >= 100
22 #include "Arduino.h"
23 #else
24 #include "WProgram.h"
25 #endif
26 
27 #include "Usb.h"
28 #include "xboxEnums.h"
29 
30 /* Data Xbox 360 taken from descriptors */
31 #define EP_MAXPKTSIZE 32 // max size for data via USB
32 
33 /* Endpoint types */
34 #define EP_INTERRUPT 0x03
35 
36 /* Names we give to the 3 Xbox360 pipes */
37 #define XBOX_CONTROL_PIPE 0
38 #define XBOX_INPUT_PIPE 1
39 #define XBOX_OUTPUT_PIPE 2
40 
41 // PID and VID of the different devices
42 #define XBOX_VID 0x045E // Microsoft Corporation
43 #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
44 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
45 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
46 
47 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
48 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
49 
50 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
51 
52 // Used in control endpoint header for HID Commands
53 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
54 #define HID_REQUEST_SET_REPORT 0x09
55 
56 #define XBOX_MAX_ENDPOINTS 3
57 
59 class XBOXUSB : public USBDeviceConfig {
60 public:
65  XBOXUSB(USB *pUsb);
66 
75  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
80  virtual uint8_t Release();
85  virtual uint8_t Poll();
86 
91  virtual uint8_t GetAddress() {
92  return bAddress;
93  };
94 
99  virtual bool isReady() {
100  return bPollEnable;
101  };
116  uint8_t getButtonPress(Button b);
117  bool getButtonClick(Button b);
126  int16_t getAnalogHat(AnalogHat a);
127 
129  void setAllOff() {
130  setRumbleOn(0, 0);
131  setLedRaw(0);
132  };
133 
135  void setRumbleOff() {
136  setRumbleOn(0, 0);
137  };
143  void setRumbleOn(uint8_t lValue, uint8_t rValue);
150  void setLedRaw(uint8_t value);
151 
153  void setLedOff() {
154  setLedRaw(0);
155  };
160  void setLedOn(LED l);
165  void setLedBlink(LED l);
170  void setLedMode(LEDMode lm);
175 
176 protected:
180  uint8_t bAddress;
183 
184 private:
185  bool bPollEnable;
186 
187  /* Variables to store the buttons */
188  uint32_t ButtonState;
189  uint32_t OldButtonState;
190  uint16_t ButtonClickState;
191  int16_t hatValue[4];
192  uint16_t controllerStatus;
193 
194  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
195  bool R2Clicked;
196 
197  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
198  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
199 
200  void readReport(); // read incoming data
201  void printReport(); // print incoming date - Uncomment for debugging
202 
203  /* Private commands */
204  void XboxCommand(uint8_t* data, uint16_t nbytes);
205 };
206 #endif