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 MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
44 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
45 
46 #define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller
47 #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
48 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
49 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
50 
51 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
52 
53 // Used in control endpoint header for HID Commands
54 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
55 #define HID_REQUEST_SET_REPORT 0x09
56 
57 #define XBOX_MAX_ENDPOINTS 3
58 
60 class XBOXUSB : public USBDeviceConfig {
61 public:
66  XBOXUSB(USB *pUsb);
67 
76  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
81  virtual uint8_t Release();
86  virtual uint8_t Poll();
87 
92  virtual uint8_t GetAddress() {
93  return bAddress;
94  };
95 
100  virtual bool isReady() {
101  return bPollEnable;
102  };
103 
110  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
111  return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && pid == XBOX_WIRED_PID);
112  };
127  uint8_t getButtonPress(Button b);
128  bool getButtonClick(Button b);
137  int16_t getAnalogHat(AnalogHat a);
138 
140  void setAllOff() {
141  setRumbleOn(0, 0);
142  setLedRaw(0);
143  };
144 
146  void setRumbleOff() {
147  setRumbleOn(0, 0);
148  };
154  void setRumbleOn(uint8_t lValue, uint8_t rValue);
161  void setLedRaw(uint8_t value);
162 
164  void setLedOff() {
165  setLedRaw(0);
166  };
171  void setLedOn(LED l);
176  void setLedBlink(LED l);
181  void setLedMode(LEDMode lm);
182 
187  void attachOnInit(void (*funcOnInit)(void)) {
188  pFuncOnInit = funcOnInit;
189  };
193  bool Xbox360Connected;
194 
195 protected:
199  uint8_t bAddress;
202 
203 private:
209  void onInit();
210  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
211 
212  bool bPollEnable;
213 
214  /* Variables to store the buttons */
215  uint32_t ButtonState;
216  uint32_t OldButtonState;
217  uint16_t ButtonClickState;
218  int16_t hatValue[4];
219  uint16_t controllerStatus;
220 
221  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
222  bool R2Clicked;
223 
224  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
225  uint8_t writeBuf[8]; // General purpose buffer for output data
226 
227  void readReport(); // read incoming data
228  void printReport(); // print incoming date - Uncomment for debugging
229 
230  /* Private commands */
231  void XboxCommand(uint8_t* data, uint16_t nbytes);
232 };
233 #endif