USB Host Shield 2.0
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 "usbhid.h"
23 #include "controllerEnums.h"
24 
25 /* Data Xbox taken from descriptors */
26 #define EP_MAXPKTSIZE 32 // Max size for data via USB
27 
28 /* Names we give to the 3 Xbox pipes */
29 #define XBOX_CONTROL_PIPE 0
30 #define XBOX_INPUT_PIPE 1
31 #define XBOX_OUTPUT_PIPE 2
32 
33 // PID and VID of the different devices
34 #define XBOX_VID 0x045E // Microsoft Corporation
35 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
36 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
37 #define LOGITECH_VID 0x046D // For official Logitech controllers
38 
39 #define XBOX_OLD_PID1 0x0202 // Original Microsoft Xbox controller (US)
40 #define XBOX_OLD_PID2 0x0285 // Original Microsoft Xbox controller (Japan)
41 #define XBOX_OLD_PID3 0x0287 // Microsoft Microsoft Xbox Controller S
42 #define XBOX_OLD_PID4 0x0289 // Smaller Microsoft Xbox controller (US)
43 #define XBOX_OLD_PID5 0xCA84 // Logitech Cordless Precision controller
44 
45 #define XBOX_MAX_ENDPOINTS 3
46 
48 class XBOXOLD : public USBDeviceConfig, public UsbConfigXtracter {
49 public:
54  XBOXOLD(USB *pUsb);
55 
64  uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
69  uint8_t Release();
74  uint8_t Poll();
75 
80  virtual uint8_t GetAddress() {
81  return bAddress;
82  };
83 
88  virtual bool isReady() {
89  return bPollEnable;
90  };
91 
96  uint8_t readPollInterval() {
97  return pollInterval;
98  };
99 
106  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
107  return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID || vid == LOGITECH_VID) && (pid == XBOX_OLD_PID1 || pid == XBOX_OLD_PID2 || pid == XBOX_OLD_PID3 || pid == XBOX_OLD_PID4 || pid == XBOX_OLD_PID5));
108  };
122  uint8_t getButtonPress(ButtonEnum b);
123  bool getButtonClick(ButtonEnum b);
132  int16_t getAnalogHat(AnalogHatEnum a);
133 
135  void setRumbleOff() {
136  setRumbleOn(0, 0);
137  };
143  void setRumbleOn(uint8_t lValue, uint8_t rValue);
144 
149  void attachOnInit(void (*funcOnInit)(void)) {
150  pFuncOnInit = funcOnInit;
151  };
156 
157 protected:
161  uint8_t bAddress;
164 
166  uint8_t bConfNum;
168  uint8_t bNumEP;
170  uint32_t qNextPollTime;
171 
181  void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
189 
190 private:
191  static int8_t getAnalogIndex(ButtonEnum b);
192  static int8_t getDigitalIndex(ButtonEnum b);
193 
199  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
200 
201  uint8_t pollInterval;
202  bool bPollEnable;
203 
204  /* Variables to store the digital buttons */
205  uint8_t ButtonState;
206  uint8_t OldButtonState;
207  uint8_t ButtonClickState;
208 
209  /* Variables to store the analog buttons */
210  uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
211  uint8_t oldButtonValues[8];
212  bool buttonClicked[8];
213 
214  int16_t hatValue[4]; // Joystick values
215 
216  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
217 
218  void readReport(); // Read incoming data
219  void printReport(uint16_t length); // Print incoming date
220 
221  /* Private commands */
222  void XboxCommand(uint8_t* data, uint16_t nbytes);
223 };
224 #endif
#define MADCATZ_VID
Definition: XBOXOLD.h:35
#define XBOX_OLD_PID3
Definition: XBOXOLD.h:41
#define XBOX_OLD_PID4
Definition: XBOXOLD.h:42
#define XBOX_OLD_PID1
Definition: XBOXOLD.h:39
#define XBOX_VID
Definition: XBOXOLD.h:34
#define XBOX_OLD_PID2
Definition: XBOXOLD.h:40
#define JOYTECH_VID
Definition: XBOXOLD.h:36
#define EP_MAXPKTSIZE
Definition: XBOXOLD.h:26
#define LOGITECH_VID
Definition: XBOXOLD.h:37
#define XBOX_OLD_PID5
Definition: XBOXOLD.h:43
#define XBOX_MAX_ENDPOINTS
Definition: XBOXOLD.h:45
Definition: UsbCore.h:218
EpInfo epInfo[XBOX_MAX_ENDPOINTS]
Definition: XBOXOLD.h:163
XBOXOLD(USB *pUsb)
Definition: XBOXOLD.cpp:47
USB * pUsb
Definition: XBOXOLD.h:159
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition: XBOXOLD.cpp:254
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition: XBOXOLD.cpp:279
uint8_t Poll()
Definition: XBOXOLD.cpp:314
uint8_t readPollInterval()
Definition: XBOXOLD.h:96
bool getButtonClick(ButtonEnum b)
Definition: XBOXOLD.cpp:445
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: XBOXOLD.h:106
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: XBOXOLD.cpp:66
void attachOnInit(void(*funcOnInit)(void))
Definition: XBOXOLD.h:149
void setRumbleOff()
Definition: XBOXOLD.h:135
uint8_t getButtonPress(ButtonEnum b)
Definition: XBOXOLD.cpp:431
uint8_t Release()
Definition: XBOXOLD.cpp:300
uint8_t bConfNum
Definition: XBOXOLD.h:166
bool XboxConnected
Definition: XBOXOLD.h:151
void setRumbleOn(uint8_t lValue, uint8_t rValue)
Definition: XBOXOLD.cpp:475
virtual uint8_t GetAddress()
Definition: XBOXOLD.h:80
uint8_t bAddress
Definition: XBOXOLD.h:161
uint32_t qNextPollTime
Definition: XBOXOLD.h:170
uint8_t bNumEP
Definition: XBOXOLD.h:168
int16_t getAnalogHat(AnalogHatEnum a)
Definition: XBOXOLD.cpp:465
virtual bool isReady()
Definition: XBOXOLD.h:88
AnalogHatEnum
ButtonEnum
Definition: address.h:39