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 /* Data Xbox ONE taken from descriptors */
30 #define EP_MAXPKTSIZE 32 // max size for data via USB
31 
32 /* Names we give to the 3 XboxONE pipes */
33 #define XBOX_CONTROL_PIPE 0
34 #define XBOX_OUTPUT_PIPE 1
35 #define XBOX_INPUT_PIPE 2
36 
37 // PID and VID of the different devices
38 #define XBOX_VID 0x045E // Microsoft Corporation
39 #define XBOX_ONE_PID 0x02D1 // Microsoft One Wired controller
40 
41 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
42 
43 #define XBOX_MAX_ENDPOINTS 3
44 
46 class XBOXONE : public USBDeviceConfig {
47 public:
52  XBOXONE(USB *pUsb);
53 
62  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
67  virtual uint8_t Release();
72  virtual uint8_t Poll();
73 
78  virtual uint8_t GetAddress() {
79  return bAddress;
80  };
81 
86  virtual bool isReady() {
87  return bPollEnable;
88  };
89 
96  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
97  return (vid == XBOX_VID && pid == XBOX_ONE_PID);
98  };
112  uint16_t getButtonPress(ButtonEnum b);
113  bool getButtonClick(ButtonEnum b);
114 
120  int16_t getAnalogHat(AnalogHatEnum a);
121 
126  void attachOnInit(void (*funcOnInit)(void)) {
127  pFuncOnInit = funcOnInit;
128  };
132  bool XboxOneConnected;
133 
134 protected:
138  uint8_t bAddress;
141 
142 private:
147  void onInit();
148  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
149 
150  bool bPollEnable;
151 
152  /* Variables to store the buttons */
153  uint16_t ButtonState;
154  uint16_t OldButtonState;
155  uint16_t ButtonClickState;
156  int16_t hatValue[4];
157  uint16_t triggerValue[2];
158  uint16_t triggerValueOld[2];
159 
160  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
161  bool R2Clicked;
162 
163  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
164  uint8_t writeBuf[12]; // General purpose buffer for output data
165 
166  void readReport(); // read incoming data
167  void printReport(); // print incoming date - Uncomment for debugging
168 
169  /* Private commands */
170  uint8_t XboxCommand(uint8_t* data, uint16_t nbytes);
171 };
172 #endif
virtual bool isReady()
Definition: XBOXONE.h:86
#define XBOX_VID
Definition: XBOXONE.h:38
USB * pUsb
Definition: XBOXONE.h:136
void attachOnInit(void(*funcOnInit)(void))
Definition: XBOXONE.h:126
AnalogHatEnum
uint8_t bAddress
Definition: XBOXONE.h:138
EpInfo epInfo[XBOX_MAX_ENDPOINTS]
Definition: XBOXONE.h:140
XBOXONE(USB *pUsb)
Definition: XBOXONE.cpp:27
#define XBOX_MAX_ENDPOINTS
Definition: XBOXONE.h:43
bool XboxOneConnected
Definition: XBOXONE.h:128
virtual uint8_t Release()
Definition: XBOXONE.cpp:226
Definition: address.h:32
ButtonEnum
uint16_t getButtonPress(ButtonEnum b)
Definition: XBOXONE.cpp:316
virtual uint8_t GetAddress()
Definition: XBOXONE.h:78
#define EP_MAXPKTSIZE
Definition: XBOXONE.h:30
bool getButtonClick(ButtonEnum b)
Definition: XBOXONE.cpp:324
virtual uint8_t Poll()
Definition: XBOXONE.cpp:237
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: XBOXONE.cpp:43
Definition: UsbCore.h:197
int16_t getAnalogHat(AnalogHatEnum a)
Definition: XBOXONE.cpp:344
#define XBOX_ONE_PID
Definition: XBOXONE.h:39
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: XBOXONE.h:96