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 
29 /* Data Xbox 360 taken from descriptors */
30 #define EP_MAXPKTSIZE 32 // max size for data via USB
31 
32 /* Endpoint types */
33 #define EP_INTERRUPT 0x03
34 
35 /* Names we give to the 3 Xbox360 pipes */
36 #define XBOX_CONTROL_PIPE 0
37 #define XBOX_INPUT_PIPE 1
38 #define XBOX_OUTPUT_PIPE 2
39 
40 // PID and VID of the different devices
41 #define XBOX_VID 0x045E // Microsoft Corporation
42 #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
43 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
44 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
45 
46 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
47 
48 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
49 
50 // Used in control endpoint header for HID Commands
51 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
52 #define HID_REQUEST_SET_REPORT 0x09
53 
54 #define XBOX_MAX_ENDPOINTS 3
55 
56 enum LED {
57  ALL = 0x01, // Used to blink all LEDs
58  LED1 = 0x02,
59  LED2 = 0x03,
60  LED3 = 0x04,
61  LED4 = 0x05,
62 };
63 enum LEDMode {
64  ROTATING = 0x0A,
65  FASTBLINK = 0x0B,
66  SLOWBLINK = 0x0C,
67  ALTERNATING = 0x0D,
68 };
69 
70 enum Button {
71  // byte location | bit location
72  UP = (2 << 8) | 0x01,
73  DOWN = (2 << 8) | 0x02,
74  LEFT = (2 << 8) | 0x04,
75  RIGHT = (2 << 8) | 0x08,
76 
77  START = (2 << 8) | 0x10,
78  BACK = (2 << 8) | 0x20,
79  L3 = (2 << 8) | 0x40,
80  R3 = (2 << 8) | 0x80,
81 
82  L1 = (3 << 8) | 0x01,
83  R1 = (3 << 8) | 0x02,
84  XBOX = (3 << 8) | 0x04,
85 
86  A = (3 << 8) | 0x10,
87  B = (3 << 8) | 0x20,
88  X = (3 << 8) | 0x40,
89  Y = (3 << 8) | 0x80,
90 
91  // These buttons are analog
92  L2 = 4,
93  R2 = 5,
94 };
95 enum AnalogHat {
96  LeftHatX = 6,
97  LeftHatY = 8,
98  RightHatX = 10,
99  RightHatY = 12,
100 };
101 
102 class XBOXUSB : public USBDeviceConfig {
103 public:
104  XBOXUSB(USB *pUsb);
105 
106  // USBDeviceConfig implementation
107  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
108  virtual uint8_t Release();
109  virtual uint8_t Poll();
110  virtual uint8_t GetAddress() { return bAddress; };
111  virtual bool isReady() { return bPollEnable; };
112 
113  /* XBOX Controller Readings */
114  uint8_t getButton(Button b);
115  int16_t getAnalogHat(AnalogHat a);
116 
117  /* Commands for Dualshock 3 and Navigation controller */
118  void setAllOff() { setRumbleOn(0,0); setLedOff(); };
119  void setRumbleOff() { setRumbleOn(0,0); };
120  void setRumbleOn(uint8_t lValue, uint8_t rValue);
121  void setLedOff();
122  void setLedOn(LED l);
123  void setLedBlink(LED l);
124  void setLedMode(LEDMode lm);
125 
126  bool Xbox360Connected;// Variable used to indicate if the XBOX 360 controller is successfully connected
127  bool buttonChanged;//Indicate if a button has been changed
128  bool buttonPressed;//Indicate if a button has been pressed
129  bool buttonReleased;//Indicate if a button has been released
130 
131 protected:
132  /* mandatory members */
134  uint8_t bAddress; // device address
135  EpInfo epInfo[XBOX_MAX_ENDPOINTS]; //endpoint info structure
136 
137 private:
138  bool bPollEnable;
139 
140  uint32_t ButtonState;
141  uint32_t OldButtonState;
142 
143  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
144  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
145 
146  void readReport(); // read incoming data
147  void printReport(); // print incoming date - Uncomment for debugging
148 
149  /* Private commands */
150  void XboxCommand(uint8_t* data, uint16_t nbytes);
151 };
152 #endif