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();
90  virtual uint8_t GetAddress() { return bAddress; };
95  virtual bool isReady() { return bPollEnable; };
110  uint8_t getButtonPress(Button b);
111  bool getButtonClick(Button b);
120  int16_t getAnalogHat(AnalogHat a);
121 
123  void setAllOff() { setRumbleOn(0,0); setLedRaw(0); };
125  void setRumbleOff() { setRumbleOn(0,0); };
131  void setRumbleOn(uint8_t lValue, uint8_t rValue);
138  void setLedRaw(uint8_t value);
140  void setLedOff() { setLedRaw(0); };
145  void setLedOn(LED l);
150  void setLedBlink(LED l);
156  void setLedMode(LEDMode lm);
161 
162 protected:
166  uint8_t bAddress;
169 
170 private:
171  bool bPollEnable;
172 
173  /* Variables to store the buttons */
174  uint32_t ButtonState;
175  uint32_t OldButtonState;
176  uint16_t ButtonClickState;
177  int16_t hatValue[4];
178  uint16_t controllerStatus;
179 
180  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
181  bool R2Clicked;
182 
183  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
184  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
185 
186  void readReport(); // read incoming data
187  void printReport(); // print incoming date - Uncomment for debugging
188 
189  /* Private commands */
190  void XboxCommand(uint8_t* data, uint16_t nbytes);
191 };
192 #endif