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 #include "Usb.h"
22 #include "controllerEnums.h"
23 
24 /* Data Xbox 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 Xbox 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 
40 #define XBOX_OLD_PID1 0x0202 // Original Microsoft Xbox controller (US)
41 #define XBOX_OLD_PID2 0x0285 // Original Microsoft Xbox controller (Japan)
42 #define XBOX_OLD_PID3 0x0287 // Microsoft Microsoft Xbox Controller S
43 #define XBOX_OLD_PID4 0x0289 // Smaller Microsoft Xbox controller (US)
44 
45 // Used in control endpoint header for HID Commands
46 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
47 #define HID_REQUEST_SET_REPORT 0x09
48 
49 #define XBOX_MAX_ENDPOINTS 3
50 
52 class XBOXOLD : public USBDeviceConfig {
53 public:
58  XBOXOLD(USB *pUsb);
59 
68  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
73  virtual uint8_t Release();
78  virtual uint8_t Poll();
79 
84  virtual uint8_t GetAddress() {
85  return bAddress;
86  };
87 
92  virtual bool isReady() {
93  return bPollEnable;
94  };
95 
102  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
103  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));
104  };
118  uint8_t getButtonPress(ButtonEnum b);
119  bool getButtonClick(ButtonEnum b);
128  int16_t getAnalogHat(AnalogHatEnum a);
129 
131  void setRumbleOff() {
132  setRumbleOn(0, 0);
133  };
139  void setRumbleOn(uint8_t lValue, uint8_t rValue);
140 
145  void attachOnInit(void (*funcOnInit)(void)) {
146  pFuncOnInit = funcOnInit;
147  };
151  bool XboxConnected;
152 
153 protected:
157  uint8_t bAddress;
160 
161 private:
167  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
168 
169  bool bPollEnable;
170 
171  /* Variables to store the digital buttons */
172  uint8_t ButtonState;
173  uint8_t OldButtonState;
174  uint8_t ButtonClickState;
175 
176  /* Variables to store the analog buttons */
177  uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
178  uint8_t oldButtonValues[8];
179  bool buttonClicked[8];
180 
181  int16_t hatValue[4]; // Joystick values
182 
183  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
184 
185  void readReport(); // Read incoming data
186  void printReport(uint16_t length); // Print incoming date
187 
188  /* Private commands */
189  void XboxCommand(uint8_t* data, uint16_t nbytes);
190 };
191 #endif
virtual uint8_t Release()
Definition: XBOXOLD.cpp:236
void attachOnInit(void(*funcOnInit)(void))
Definition: XBOXOLD.h:145
AnalogHatEnum
virtual uint8_t GetAddress()
Definition: XBOXOLD.h:84
void setRumbleOn(uint8_t lValue, uint8_t rValue)
Definition: XBOXOLD.cpp:326
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: XBOXOLD.h:102
#define XBOX_OLD_PID4
Definition: XBOXOLD.h:43
virtual bool isReady()
Definition: XBOXOLD.h:92
EpInfo epInfo[XBOX_MAX_ENDPOINTS]
Definition: XBOXOLD.h:159
int16_t getAnalogHat(AnalogHatEnum a)
Definition: XBOXOLD.cpp:316
#define XBOX_VID
Definition: XBOXOLD.h:36
XBOXOLD(USB *pUsb)
Definition: XBOXOLD.cpp:47
#define XBOX_OLD_PID1
Definition: XBOXOLD.h:40
Definition: address.h:32
ButtonEnum
#define JOYTECH_VID
Definition: XBOXOLD.h:38
USB * pUsb
Definition: XBOXOLD.h:155
virtual uint8_t Poll()
Definition: XBOXOLD.cpp:244
#define XBOX_OLD_PID2
Definition: XBOXOLD.h:41
#define MADCATZ_VID
Definition: XBOXOLD.h:37
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: XBOXOLD.cpp:62
#define XBOX_MAX_ENDPOINTS
Definition: XBOXOLD.h:49
#define XBOX_OLD_PID3
Definition: XBOXOLD.h:42
bool XboxConnected
Definition: XBOXOLD.h:147
Definition: UsbCore.h:176
bool getButtonClick(ButtonEnum b)
Definition: XBOXOLD.cpp:301
void setRumbleOff()
Definition: XBOXOLD.h:131
uint8_t getButtonPress(ButtonEnum b)
Definition: XBOXOLD.cpp:294
uint8_t bAddress
Definition: XBOXOLD.h:157
#define EP_MAXPKTSIZE
Definition: XBOXOLD.h:25