USB Host Shield 2.0
XBOXONE.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2  Copyright (C) 2015 guruthree
3 
4  This software may be distributed and modified under the terms of the GNU
5  General Public License version 2 (GPL2) as published by the Free Software
6  Foundation and appearing in the file GPL2.TXT included in the packaging of
7  this file. Please note that GPL2 Section 2[b] requires that all works based
8  on this software must also be made publicly available under the terms of
9  the GPL2 ("Copyleft").
10 
11  Contact information
12  -------------------
13 
14  Kristian Lauszus, TKJ Electronics
15  Web : http://www.tkjelectronics.com
16  e-mail : kristianl@tkjelectronics.com
17 
18  guruthree
19  Web : https://github.com/guruthree/
20  */
21 
22 
23 #ifndef _xboxone_h_
24 #define _xboxone_h_
25 
26 #include "Usb.h"
27 #include "xboxEnums.h"
28 
29 /* Xbox One data taken from descriptors */
30 #define XBOX_ONE_EP_MAXPKTSIZE 64 // Max size for data via USB
31 
32 /* Names we give to the 3 XboxONE pipes */
33 #define XBOX_ONE_CONTROL_PIPE 0
34 #define XBOX_ONE_OUTPUT_PIPE 1
35 #define XBOX_ONE_INPUT_PIPE 2
36 
37 #define XBOX_ONE_MAX_ENDPOINTS 3
38 
39 // PID and VID of the different versions of the controller - see: https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c
40 
41 // Official controllers
42 #define XBOX_VID1 0x045E // Microsoft Corporation
43 #define XBOX_ONE_PID1 0x02D1 // Microsoft X-Box One pad
44 #define XBOX_ONE_PID2 0x02DD // Microsoft X-Box One pad (Firmware 2015)
45 #define XBOX_ONE_PID3 0x02E3 // Microsoft X-Box One Elite pad
46 #define XBOX_ONE_PID4 0x02EA // Microsoft X-Box One S pad
47 #define XBOX_ONE_PID13 0x0B0A // Microsoft X-Box One Adaptive Controller
48 #define XBOX_ONE_PID14 0x0B12 // Microsoft X-Box Core Controller
49 
50 // Unofficial controllers
51 #define XBOX_VID2 0x0738 // Mad Catz
52 #define XBOX_VID3 0x0E6F // Afterglow
53 #define XBOX_VID4 0x0F0D // HORIPAD ONE
54 #define XBOX_VID5 0x1532 // Razer
55 #define XBOX_VID6 0x24C6 // PowerA
56 
57 #define XBOX_ONE_PID5 0x4A01 // Mad Catz FightStick TE 2 - might have different mapping for triggers?
58 #define XBOX_ONE_PID6 0x0139 // Afterglow Prismatic Wired Controller
59 #define XBOX_ONE_PID7 0x0146 // Rock Candy Wired Controller for Xbox One
60 #define XBOX_ONE_PID8 0x0067 // HORIPAD ONE
61 #define XBOX_ONE_PID9 0x0A03 // Razer Wildcat
62 #define XBOX_ONE_PID10 0x541A // PowerA Xbox One Mini Wired Controller
63 #define XBOX_ONE_PID11 0x542A // Xbox ONE spectra
64 #define XBOX_ONE_PID12 0x543A // PowerA Xbox One wired controller
65 
67 class XBOXONE : public USBDeviceConfig, public UsbConfigXtracter {
68 public:
73  XBOXONE(USB *pUsb);
74 
83  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
88  virtual uint8_t Release();
93  virtual uint8_t Poll();
94 
99  virtual uint8_t GetAddress() {
100  return bAddress;
101  };
102 
107  virtual bool isReady() {
108  return bPollEnable;
109  };
110 
115  uint8_t readPollInterval() {
116  return pollInterval;
117  };
118 
125  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
126  return ((vid == XBOX_VID1 || vid == XBOX_VID2 || vid == XBOX_VID3 || vid == XBOX_VID4 || vid == XBOX_VID5 || vid == XBOX_VID6) &&
127  (pid == XBOX_ONE_PID1 || pid == XBOX_ONE_PID2 || pid == XBOX_ONE_PID3 || pid == XBOX_ONE_PID4 ||
128  pid == XBOX_ONE_PID5 || pid == XBOX_ONE_PID6 || pid == XBOX_ONE_PID7 || pid == XBOX_ONE_PID8 ||
129  pid == XBOX_ONE_PID9 || pid == XBOX_ONE_PID10 || pid == XBOX_ONE_PID11 || pid == XBOX_ONE_PID12 ||
130  pid == XBOX_ONE_PID13 || pid == XBOX_ONE_PID14));
131  };
145  uint16_t getButtonPress(ButtonEnum b);
146  bool getButtonClick(ButtonEnum b);
147 
153  int16_t getAnalogHat(AnalogHatEnum a);
154 
159  void attachOnInit(void (*funcOnInit)(void)) {
160  pFuncOnInit = funcOnInit;
161  };
162 
164  void setRumbleOff();
165 
173  void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor);
178 
179 protected:
183  uint8_t bAddress;
186 
188  uint8_t bConfNum;
190  uint8_t bNumEP;
192  uint32_t qNextPollTime;
193 
203  void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
211 
212 private:
217  void onInit();
218  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
219 
220  uint8_t pollInterval;
221  bool bPollEnable;
222 
223  /* Variables to store the buttons */
224  uint16_t ButtonState;
225  uint16_t OldButtonState;
226  uint16_t ButtonClickState;
227  int16_t hatValue[4];
228  uint16_t triggerValue[2];
229  uint16_t triggerValueOld[2];
230 
231  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
232  bool R2Clicked;
233 
234  bool sharePressed; // This button doesn't fit in the bitfield
235  bool shareClicked;
236 
237  uint8_t readBuf[XBOX_ONE_EP_MAXPKTSIZE]; // General purpose buffer for input data
238  uint8_t cmdCounter;
239 
240  void readReport(); // Used to read the incoming data
241 
242  /* Private commands */
243  uint8_t XboxCommand(uint8_t* data, uint16_t nbytes);
244 };
245 #endif
XBOXONE::epInfo
EpInfo epInfo[XBOX_ONE_MAX_ENDPOINTS]
Definition: XBOXONE.h:185
XBOXONE::setRumbleOn
void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor)
Definition: XBOXONE.cpp:495
USBDeviceConfig
Definition: UsbCore.h:136
XBOXONE::getAnalogHat
int16_t getAnalogHat(AnalogHatEnum a)
Definition: XBOXONE.cpp:429
XBOXONE::attachOnInit
void attachOnInit(void(*funcOnInit)(void))
Definition: XBOXONE.h:159
XBOXONE::XBOXONE
XBOXONE(USB *pUsb)
Definition: XBOXONE.cpp:27
XBOXONE::XboxOneConnected
bool XboxOneConnected
Definition: XBOXONE.h:177
XBOX_ONE_PID5
#define XBOX_ONE_PID5
Definition: XBOXONE.h:57
XBOXONE::isReady
virtual bool isReady()
Definition: XBOXONE.h:107
XBOX_ONE_EP_MAXPKTSIZE
#define XBOX_ONE_EP_MAXPKTSIZE
Definition: XBOXONE.h:30
XBOX_ONE_PID9
#define XBOX_ONE_PID9
Definition: XBOXONE.h:61
USB_ENDPOINT_DESCRIPTOR
Definition: usb_ch9.h:148
XBOXONE::qNextPollTime
uint32_t qNextPollTime
Definition: XBOXONE.h:192
XBOX_ONE_PID11
#define XBOX_ONE_PID11
Definition: XBOXONE.h:63
UsbConfigXtracter
Definition: confdescparser.h:30
XBOX_ONE_PID4
#define XBOX_ONE_PID4
Definition: XBOXONE.h:46
XBOX_ONE_PID3
#define XBOX_ONE_PID3
Definition: XBOXONE.h:45
XBOXONE::VIDPIDOK
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: XBOXONE.h:125
XBOX_ONE_PID13
#define XBOX_ONE_PID13
Definition: XBOXONE.h:47
XBOX_ONE_PID12
#define XBOX_ONE_PID12
Definition: XBOXONE.h:64
XBOXONE::Release
virtual uint8_t Release()
Definition: XBOXONE.cpp:286
XBOX_ONE_PID2
#define XBOX_ONE_PID2
Definition: XBOXONE.h:44
XBOX_ONE_PID14
#define XBOX_ONE_PID14
Definition: XBOXONE.h:48
XBOX_ONE_PID7
#define XBOX_ONE_PID7
Definition: XBOXONE.h:59
XBOX_VID2
#define XBOX_VID2
Definition: XBOXONE.h:51
XBOXONE::PrintEndpointDescriptor
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition: XBOXONE.cpp:265
XBOXONE
Definition: XBOXONE.h:67
XBOX_ONE_MAX_ENDPOINTS
#define XBOX_ONE_MAX_ENDPOINTS
Definition: XBOXONE.h:37
XBOXONE::setRumbleOff
void setRumbleOff()
Definition: XBOXONE.cpp:473
XBOXONE::getButtonClick
bool getButtonClick(ButtonEnum b)
Definition: XBOXONE.cpp:399
XBOX_VID1
#define XBOX_VID1
Definition: XBOXONE.h:42
EpInfo
Definition: address.h:39
XBOX_VID5
#define XBOX_VID5
Definition: XBOXONE.h:54
USB
Definition: UsbCore.h:212
XBOX_VID3
#define XBOX_VID3
Definition: XBOXONE.h:52
XBOX_ONE_PID10
#define XBOX_ONE_PID10
Definition: XBOXONE.h:62
XBOXONE::pUsb
USB * pUsb
Definition: XBOXONE.h:181
XBOXONE::getButtonPress
uint16_t getButtonPress(ButtonEnum b)
Definition: XBOXONE.cpp:385
XBOXONE::EndpointXtract
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition: XBOXONE.cpp:239
XBOXONE::GetAddress
virtual uint8_t GetAddress()
Definition: XBOXONE.h:99
XBOXONE::Init
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: XBOXONE.cpp:46
XBOXONE::readPollInterval
uint8_t readPollInterval()
Definition: XBOXONE.h:115
XBOXONE::Poll
virtual uint8_t Poll()
Definition: XBOXONE.cpp:300
XBOX_ONE_PID1
#define XBOX_ONE_PID1
Definition: XBOXONE.h:43
XBOXONE::bConfNum
uint8_t bConfNum
Definition: XBOXONE.h:188
Usb.h
XBOX_VID4
#define XBOX_VID4
Definition: XBOXONE.h:53
XBOX_ONE_PID6
#define XBOX_ONE_PID6
Definition: XBOXONE.h:58
XBOX_ONE_PID8
#define XBOX_ONE_PID8
Definition: XBOXONE.h:60
XBOXONE::bNumEP
uint8_t bNumEP
Definition: XBOXONE.h:190
XBOXONE::bAddress
uint8_t bAddress
Definition: XBOXONE.h:183
XBOX_VID6
#define XBOX_VID6
Definition: XBOXONE.h:55
ButtonEnum
ButtonEnum
Definition: controllerEnums.h:78
AnalogHatEnum
AnalogHatEnum
Definition: controllerEnums.h:218
xboxEnums.h