USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
XBOXOLD.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 _xboxold_h_
19 #define _xboxold_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 "controllerEnums.h"
29 
30 /* Data Xbox 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 Xbox 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_OLD_PID1 0x0202 // Original Microsoft Xbox controller (US)
47 #define XBOX_OLD_PID2 0x0285 // Original Microsoft Xbox controller (Japan)
48 #define XBOX_OLD_PID3 0x0287 // Microsoft Microsoft Xbox Controller S
49 #define XBOX_OLD_PID4 0x0289 // Smaller Microsoft Xbox controller (US)
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 XBOXOLD : public USBDeviceConfig {
59 public:
64  XBOXOLD(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) && (pid == XBOX_OLD_PID1 || pid == XBOX_OLD_PID2 || pid == XBOX_OLD_PID3 || pid == XBOX_OLD_PID4));
110  };
125  uint8_t getButtonPress(Button b);
126  bool getButtonClick(Button b);
135  int16_t getAnalogHat(AnalogHat a);
136 
138  void setRumbleOff() {
139  setRumbleOn(0, 0);
140  };
146  void setRumbleOn(uint8_t lValue, uint8_t rValue);
147 
152  void attachOnInit(void (*funcOnInit)(void)) {
153  pFuncOnInit = funcOnInit;
154  };
158  bool XboxConnected;
159 
160 protected:
164  uint8_t bAddress;
167 
168 private:
174  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
175 
176  bool bPollEnable;
177 
178  /* Variables to store the digital buttons */
179  uint8_t ButtonState;
180  uint8_t OldButtonState;
181  uint8_t ButtonClickState;
182 
183  /* Variables to store the analog buttons */
184  uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
185  uint8_t oldButtonValues[8];
186  bool buttonClicked[8];
187 
188  int16_t hatValue[4]; // Joystick values
189 
190  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
191 
192  void readReport(); // Read incoming data
193  void printReport(uint16_t length); // Print incoming date
194 
195  /* Private commands */
196  void XboxCommand(uint8_t* data, uint16_t nbytes);
197 };
198 #endif