]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/XBOXONE.h
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / XBOXONE.h
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
45 /** This class implements support for a Xbox ONE controller connected via USB. */
46 class XBOXONE : public USBDeviceConfig {
47 public:
48         /**
49          * Constructor for the XBOXONE class.
50          * @param  pUsb   Pointer to USB class instance.
51          */
52         XBOXONE(USB *pUsb);
53
54         /** @name USBDeviceConfig implementation */
55         /**
56          * Initialize the Xbox Controller.
57          * @param  parent   Hub number.
58          * @param  port     Port number on the hub.
59          * @param  lowspeed Speed of the device.
60          * @return          0 on success.
61          */
62         virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
63         /**
64          * Release the USB device.
65          * @return 0 on success.
66          */
67         virtual uint8_t Release();
68         /**
69          * Poll the USB Input endpoins and run the state machines.
70          * @return 0 on success.
71          */
72         virtual uint8_t Poll();
73
74         /**
75          * Get the device address.
76          * @return The device address.
77          */
78         virtual uint8_t GetAddress() {
79                 return bAddress;
80         };
81
82         /**
83          * Used to check if the controller has been initialized.
84          * @return True if it's ready.
85          */
86         virtual bool isReady() {
87                 return bPollEnable;
88         };
89
90         /**
91          * Used by the USB core to check what this driver support.
92          * @param  vid The device's VID.
93          * @param  pid The device's PID.
94          * @return     Returns true if the device's VID and PID matches this driver.
95          */
96         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
97                 return (vid == XBOX_VID && pid == XBOX_ONE_PID);
98         };
99         /**@}*/
100
101         /** @name Xbox Controller functions */
102         /**
103          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
104          *
105          * While getButtonClick(ButtonEnum b) will only return it once.
106          *
107          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
108          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
109          * @param  b          ::ButtonEnum to read.
110          * @return            getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a word if reading ::L2 or ::R2.
111          */
112         uint16_t getButtonPress(ButtonEnum b);
113         bool getButtonClick(ButtonEnum b);
114
115         /**
116          * Return the analog value from the joysticks on the controller.
117          * @param  a          Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
118          * @return            Returns a signed 16-bit integer.
119          */
120         int16_t getAnalogHat(AnalogHatEnum a);
121
122         /**
123          * Used to call your own function when the controller is successfully initialized.
124          * @param funcOnInit Function to call.
125          */
126         void attachOnInit(void (*funcOnInit)(void)) {
127                 pFuncOnInit = funcOnInit;
128         };
129         /**@}*/
130
131         /** True if a Xbox ONE controller is connected. */
132         bool XboxOneConnected;
133
134 protected:
135         /** Pointer to USB class instance. */
136         USB *pUsb;
137         /** Device address. */
138         uint8_t bAddress;
139         /** Endpoint info structure. */
140         EpInfo epInfo[XBOX_MAX_ENDPOINTS];
141
142 private:
143         /**
144          * Called when the controller is successfully initialized.
145          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
146          */
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