1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 Copyright (C) 2015 guruthree
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
14 Kristian Lauszus, TKJ Electronics
15 Web : http://www.tkjelectronics.com
16 e-mail : kristianl@tkjelectronics.com
19 Web : https://github.com/guruthree/
27 #include "xboxEnums.h"
29 /* Data Xbox ONE taken from descriptors */
30 #define EP_MAXPKTSIZE 32 // max size for data via USB
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
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
41 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
43 #define XBOX_MAX_ENDPOINTS 3
45 /** This class implements support for a Xbox ONE controller connected via USB. */
46 class XBOXONE : public USBDeviceConfig {
49 * Constructor for the XBOXONE class.
50 * @param pUsb Pointer to USB class instance.
54 /** @name USBDeviceConfig implementation */
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.
62 virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
64 * Release the USB device.
65 * @return 0 on success.
67 virtual uint8_t Release();
69 * Poll the USB Input endpoins and run the state machines.
70 * @return 0 on success.
72 virtual uint8_t Poll();
75 * Get the device address.
76 * @return The device address.
78 virtual uint8_t GetAddress() {
83 * Used to check if the controller has been initialized.
84 * @return True if it's ready.
86 virtual bool isReady() {
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.
96 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
97 return (vid == XBOX_VID && pid == XBOX_ONE_PID);
101 /** @name Xbox Controller functions */
103 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
105 * While getButtonClick(ButtonEnum b) will only return it once.
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.
112 uint16_t getButtonPress(ButtonEnum b);
113 bool getButtonClick(ButtonEnum b);
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.
120 int16_t getAnalogHat(AnalogHatEnum a);
123 * Used to call your own function when the controller is successfully initialized.
124 * @param funcOnInit Function to call.
126 void attachOnInit(void (*funcOnInit)(void)) {
127 pFuncOnInit = funcOnInit;
131 /** True if a Xbox ONE controller is connected. */
132 bool XboxOneConnected;
135 /** Pointer to USB class instance. */
137 /** Device address. */
139 /** Endpoint info structure. */
140 EpInfo epInfo[XBOX_MAX_ENDPOINTS];
144 * Called when the controller is successfully initialized.
145 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
148 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
152 /* Variables to store the buttons */
153 uint16_t ButtonState;
154 uint16_t OldButtonState;
155 uint16_t ButtonClickState;
157 uint16_t triggerValue[2];
158 uint16_t triggerValueOld[2];
160 bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
163 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
164 uint8_t writeBuf[12]; // General purpose buffer for output data
166 void readReport(); // read incoming data
167 void printReport(); // print incoming date - Uncomment for debugging
169 /* Private commands */
170 uint8_t XboxCommand(uint8_t* data, uint16_t nbytes);