-
Notifications
You must be signed in to change notification settings - Fork 779
/
XBOXOLD.h
224 lines (190 loc) · 8.16 KB
/
XBOXOLD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").
Contact information
-------------------
Kristian Lauszus, TKJ Electronics
Web : http://www.tkjelectronics.com
e-mail : [email protected]
*/
#ifndef _xboxold_h_
#define _xboxold_h_
#include "Usb.h"
#include "usbhid.h"
#include "controllerEnums.h"
/* Data Xbox taken from descriptors */
#define EP_MAXPKTSIZE 32 // Max size for data via USB
/* Names we give to the 3 Xbox pipes */
#define XBOX_CONTROL_PIPE 0
#define XBOX_INPUT_PIPE 1
#define XBOX_OUTPUT_PIPE 2
// PID and VID of the different devices
#define XBOX_VID 0x045E // Microsoft Corporation
#define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
#define JOYTECH_VID 0x162E // For unofficial Joytech controllers
#define LOGITECH_VID 0x046D // For official Logitech controllers
#define XBOX_OLD_PID1 0x0202 // Original Microsoft Xbox controller (US)
#define XBOX_OLD_PID2 0x0285 // Original Microsoft Xbox controller (Japan)
#define XBOX_OLD_PID3 0x0287 // Microsoft Microsoft Xbox Controller S
#define XBOX_OLD_PID4 0x0289 // Smaller Microsoft Xbox controller (US)
#define XBOX_OLD_PID5 0xCA84 // Logitech Cordless Precision controller
#define XBOX_MAX_ENDPOINTS 3
/** This class implements support for a the original Xbox controller via USB. */
class XBOXOLD : public USBDeviceConfig, public UsbConfigXtracter {
public:
/**
* Constructor for the XBOXOLD class.
* @param pUsb Pointer to USB class instance.
*/
XBOXOLD(USB *pUsb);
/** @name USBDeviceConfig implementation */
/**
* Initialize the Xbox Controller.
* @param parent Hub number.
* @param port Port number on the hub.
* @param lowspeed Speed of the device.
* @return 0 on success.
*/
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
/**
* Release the USB device.
* @return 0 on success.
*/
uint8_t Release();
/**
* Poll the USB Input endpoins and run the state machines.
* @return 0 on success.
*/
uint8_t Poll();
/**
* Get the device address.
* @return The device address.
*/
virtual uint8_t GetAddress() {
return bAddress;
};
/**
* Used to check if the controller has been initialized.
* @return True if it's ready.
*/
virtual bool isReady() {
return bPollEnable;
};
/**
* Read the poll interval taken from the endpoint descriptors.
* @return The poll interval in ms.
*/
uint8_t readPollInterval() {
return pollInterval;
};
/**
* Used by the USB core to check what this driver support.
* @param vid The device's VID.
* @param pid The device's PID.
* @return Returns true if the device's VID and PID matches this driver.
*/
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
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));
};
/**@}*/
/** @name Xbox Controller functions */
/**
* getButtonPress(ButtonEnum b) will return true as long as the button is held down.
*
* While getButtonClick(ButtonEnum b) will only return it once.
*
* So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::ButtonEnum to read.
* @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
*/
uint8_t getButtonPress(ButtonEnum b);
bool getButtonClick(ButtonEnum b);
/**@}*/
/** @name Xbox Controller functions */
/**
* Return the analog value from the joysticks on the controller.
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
* @return Returns a signed 16-bit integer.
*/
int16_t getAnalogHat(AnalogHatEnum a);
/** Turn rumble off the controller. */
void setRumbleOff() {
setRumbleOn(0, 0);
};
/**
* Turn rumble on.
* @param lValue Left motor (big weight) inside the controller.
* @param rValue Right motor (small weight) inside the controller.
*/
void setRumbleOn(uint8_t lValue, uint8_t rValue);
/**
* Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call.
*/
void attachOnInit(void (*funcOnInit)(void)) {
pFuncOnInit = funcOnInit;
};
/**@}*/
/** True if a Xbox controller is connected. */
bool XboxConnected;
protected:
/** Pointer to USB class instance. */
USB *pUsb;
/** Device address. */
uint8_t bAddress;
/** Endpoint info structure. */
EpInfo epInfo[XBOX_MAX_ENDPOINTS];
/** Configuration number. */
uint8_t bConfNum;
/** Total number of endpoints in the configuration. */
uint8_t bNumEP;
/** Next poll time based on poll interval taken from the USB descriptor. */
uint32_t qNextPollTime;
/** @name UsbConfigXtracter implementation */
/**
* UsbConfigXtracter implementation, used to extract endpoint information.
* @param conf Configuration value.
* @param iface Interface number.
* @param alt Alternate setting.
* @param proto Interface Protocol.
* @param ep Endpoint Descriptor.
*/
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
/**@}*/
/**
* Used to print the USB Endpoint Descriptor.
* @param ep_ptr Pointer to USB Endpoint Descriptor.
*/
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
private:
static int8_t getAnalogIndex(ButtonEnum b);
static int8_t getDigitalIndex(ButtonEnum b);
/**
* Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
* This is useful for instance if you want to set the LEDs in a specific way.
*/
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
uint8_t pollInterval;
bool bPollEnable;
/* Variables to store the digital buttons */
uint8_t ButtonState;
uint8_t OldButtonState;
uint8_t ButtonClickState;
/* Variables to store the analog buttons */
uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
uint8_t oldButtonValues[8];
bool buttonClicked[8];
int16_t hatValue[4]; // Joystick values
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
void readReport(); // Read incoming data
void printReport(uint16_t length); // Print incoming date
/* Private commands */
void XboxCommand(uint8_t* data, uint16_t nbytes);
};
#endif