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 #include "Usb.h"
22 #include "xboxEnums.h"
23 
24 /* Data Xbox 360 taken from descriptors */
25 #define EP_MAXPKTSIZE 32 // max size for data via USB
26 
27 /* Endpoint types */
28 #define EP_INTERRUPT 0x03
29 
30 /* Names we give to the 3 Xbox360 pipes */
31 #define XBOX_CONTROL_PIPE 0
32 #define XBOX_INPUT_PIPE 1
33 #define XBOX_OUTPUT_PIPE 2
34 
35 // PID and VID of the different devices
36 #define XBOX_VID 0x045E // Microsoft Corporation
37 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
38 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
39 #define GAMESTOP_VID 0x0E6F // Gamestop controller
40 
41 #define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller
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 #define MADCATZ_WIRED_PID 0xF016 // Mad Catz wired controller
46 #define GAMESTOP_WIRED_PID 0x0401 // Gamestop wired controller
47 #define AFTERGLOW_WIRED_PID 0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller
48 
49 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
50 
51 // Used in control endpoint header for HID Commands
52 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
53 #define HID_REQUEST_SET_REPORT 0x09
54 
55 #define XBOX_MAX_ENDPOINTS 3
56 
58 class XBOXUSB : public USBDeviceConfig {
59 public:
64  XBOXUSB(USB *pUsb);
65 
74  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
79  virtual uint8_t Release();
84  virtual uint8_t Poll();
85 
90  virtual uint8_t GetAddress() {
91  return bAddress;
92  };
93 
98  virtual bool isReady() {
99  return bPollEnable;
100  };
101 
108  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
109  return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID || vid == GAMESTOP_VID) && (pid == XBOX_WIRED_PID || pid == MADCATZ_WIRED_PID || pid == GAMESTOP_WIRED_PID || pid == AFTERGLOW_WIRED_PID));
110  };
124  uint8_t getButtonPress(ButtonEnum b);
125  bool getButtonClick(ButtonEnum b);
134  int16_t getAnalogHat(AnalogHatEnum a);
135 
137  void setAllOff() {
138  setRumbleOn(0, 0);
139  setLedRaw(0);
140  };
141 
143  void setRumbleOff() {
144  setRumbleOn(0, 0);
145  };
151  void setRumbleOn(uint8_t lValue, uint8_t rValue);
158  void setLedRaw(uint8_t value);
159 
161  void setLedOff() {
162  setLedRaw(0);
163  };
168  void setLedOn(LEDEnum l);
173  void setLedBlink(LEDEnum l);
178  void setLedMode(LEDModeEnum lm);
179 
184  void attachOnInit(void (*funcOnInit)(void)) {
185  pFuncOnInit = funcOnInit;
186  };
190  bool Xbox360Connected;
191 
192 protected:
196  uint8_t bAddress;
199 
200 private:
206  void onInit();
207  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
208 
209  bool bPollEnable;
210 
211  /* Variables to store the buttons */
212  uint32_t ButtonState;
213  uint32_t OldButtonState;
214  uint16_t ButtonClickState;
215  int16_t hatValue[4];
216  uint16_t controllerStatus;
217 
218  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
219  bool R2Clicked;
220 
221  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
222  uint8_t writeBuf[8]; // General purpose buffer for output data
223 
224  void readReport(); // read incoming data
225  void printReport(); // print incoming date - Uncomment for debugging
226 
227  /* Private commands */
228  void XboxCommand(uint8_t* data, uint16_t nbytes);
229 };
230 #endif
#define XBOX_WIRED_PID
Definition: XBOXUSB.h:41
LEDModeEnum
Definition: xboxEnums.h:24
void setLedOn(LEDEnum l)
Definition: XBOXUSB.cpp:328
#define MADCATZ_VID
Definition: XBOXUSB.h:37
#define XBOX_MAX_ENDPOINTS
Definition: XBOXUSB.h:55
#define JOYTECH_VID
Definition: XBOXUSB.h:38
AnalogHatEnum
USB * pUsb
Definition: XBOXUSB.h:194
void setLedOff()
Definition: XBOXUSB.h:161
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: XBOXUSB.cpp:38
int16_t getAnalogHat(AnalogHatEnum a)
Definition: XBOXUSB.cpp:310
void setLedBlink(LEDEnum l)
Definition: XBOXUSB.cpp:335
virtual bool isReady()
Definition: XBOXUSB.h:98
#define AFTERGLOW_WIRED_PID
Definition: XBOXUSB.h:47
LEDEnum
void attachOnInit(void(*funcOnInit)(void))
Definition: XBOXUSB.h:184
uint8_t bAddress
Definition: XBOXUSB.h:196
uint8_t getButtonPress(ButtonEnum b)
Definition: XBOXUSB.cpp:282
Definition: address.h:32
ButtonEnum
bool Xbox360Connected
Definition: XBOXUSB.h:186
#define XBOX_VID
Definition: XBOXUSB.h:36
void setRumbleOn(uint8_t lValue, uint8_t rValue)
Definition: XBOXUSB.cpp:343
XBOXUSB(USB *pUsb)
Definition: XBOXUSB.cpp:23
virtual uint8_t GetAddress()
Definition: XBOXUSB.h:90
#define GAMESTOP_VID
Definition: XBOXUSB.h:39
bool getButtonClick(ButtonEnum b)
Definition: XBOXUSB.cpp:290
#define EP_MAXPKTSIZE
Definition: XBOXUSB.h:25
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: XBOXUSB.h:108
void setRumbleOff()
Definition: XBOXUSB.h:143
void setLedRaw(uint8_t value)
Definition: XBOXUSB.cpp:320
void setLedMode(LEDModeEnum lm)
Definition: XBOXUSB.cpp:339
#define MADCATZ_WIRED_PID
Definition: XBOXUSB.h:45
Definition: UsbCore.h:190
void setAllOff()
Definition: XBOXUSB.h:137
virtual uint8_t Release()
Definition: XBOXUSB.cpp:223
virtual uint8_t Poll()
Definition: XBOXUSB.cpp:231
EpInfo epInfo[XBOX_MAX_ENDPOINTS]
Definition: XBOXUSB.h:198
#define GAMESTOP_WIRED_PID
Definition: XBOXUSB.h:46