]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/XBOXUSB.h
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / XBOXUSB.h
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9
10  Contact information
11  -------------------
12
13  Kristian Lauszus, TKJ Electronics
14  Web      :  http://www.tkjelectronics.com
15  e-mail   :  kristianl@tkjelectronics.com
16  */
17
18 #ifndef _xboxusb_h_
19 #define _xboxusb_h_
20
21 #include "Usb.h"
22 #include "hid.h"
23 #include "xboxEnums.h"
24
25 /* Data Xbox 360 taken from descriptors */
26 #define EP_MAXPKTSIZE       32 // max size for data via USB
27
28 /* Names we give to the 3 Xbox360 pipes */
29 #define XBOX_CONTROL_PIPE    0
30 #define XBOX_INPUT_PIPE      1
31 #define XBOX_OUTPUT_PIPE     2
32
33 // PID and VID of the different devices
34 #define XBOX_VID                                0x045E // Microsoft Corporation
35 #define MADCATZ_VID                             0x1BAD // For unofficial Mad Catz controllers
36 #define JOYTECH_VID                             0x162E // For unofficial Joytech controllers
37 #define GAMESTOP_VID                            0x0E6F // Gamestop controller
38
39 #define XBOX_WIRED_PID                          0x028E // Microsoft 360 Wired controller
40 #define XBOX_WIRELESS_PID                       0x028F // Wireless controller only support charging
41 #define XBOX_WIRELESS_RECEIVER_PID              0x0719 // Microsoft Wireless Gaming Receiver
42 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID  0x0291 // Third party Wireless Gaming Receiver
43 #define MADCATZ_WIRED_PID                       0xF016 // Mad Catz wired controller
44 #define JOYTECH_WIRED_PID                       0xBEEF // For Joytech wired controller
45 #define GAMESTOP_WIRED_PID                      0x0401 // Gamestop wired controller
46 #define AFTERGLOW_WIRED_PID                     0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller
47
48 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
49
50 #define XBOX_MAX_ENDPOINTS   3
51
52 /** This class implements support for a Xbox wired controller via USB. */
53 class XBOXUSB : public USBDeviceConfig {
54 public:
55         /**
56          * Constructor for the XBOXUSB class.
57          * @param  pUsb   Pointer to USB class instance.
58          */
59         XBOXUSB(USB *pUsb);
60
61         /** @name USBDeviceConfig implementation */
62         /**
63          * Initialize the Xbox Controller.
64          * @param  parent   Hub number.
65          * @param  port     Port number on the hub.
66          * @param  lowspeed Speed of the device.
67          * @return          0 on success.
68          */
69         uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
70         /**
71          * Release the USB device.
72          * @return 0 on success.
73          */
74         uint8_t Release();
75         /**
76          * Poll the USB Input endpoins and run the state machines.
77          * @return 0 on success.
78          */
79         uint8_t Poll();
80
81         /**
82          * Get the device address.
83          * @return The device address.
84          */
85         virtual uint8_t GetAddress() {
86                 return bAddress;
87         };
88
89         /**
90          * Used to check if the controller has been initialized.
91          * @return True if it's ready.
92          */
93         virtual bool isReady() {
94                 return bPollEnable;
95         };
96
97         /**
98          * Used by the USB core to check what this driver support.
99          * @param  vid The device's VID.
100          * @param  pid The device's PID.
101          * @return     Returns true if the device's VID and PID matches this driver.
102          */
103         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
104                 return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID || vid == GAMESTOP_VID) && (pid == XBOX_WIRED_PID || pid == MADCATZ_WIRED_PID || pid == GAMESTOP_WIRED_PID || pid == AFTERGLOW_WIRED_PID || pid == JOYTECH_WIRED_PID));
105         };
106         /**@}*/
107
108         /** @name Xbox Controller functions */
109         /**
110          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
111          *
112          * While getButtonClick(ButtonEnum b) will only return it once.
113          *
114          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
115          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
116          * @param  b          ::ButtonEnum to read.
117          * @return            getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
118          */
119         uint8_t getButtonPress(ButtonEnum b);
120         bool getButtonClick(ButtonEnum b);
121         /**@}*/
122
123         /** @name Xbox Controller functions */
124         /**
125          * Return the analog value from the joysticks on the controller.
126          * @param  a          Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
127          * @return            Returns a signed 16-bit integer.
128          */
129         int16_t getAnalogHat(AnalogHatEnum a);
130
131         /** Turn rumble off and all the LEDs on the controller. */
132         void setAllOff() {
133                 setRumbleOn(0, 0);
134                 setLedRaw(0);
135         };
136
137         /** Turn rumble off the controller. */
138         void setRumbleOff() {
139                 setRumbleOn(0, 0);
140         };
141         /**
142          * Turn rumble on.
143          * @param lValue     Left motor (big weight) inside the controller.
144          * @param rValue     Right motor (small weight) inside the controller.
145          */
146         void setRumbleOn(uint8_t lValue, uint8_t rValue);
147         /**
148          * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum.
149          * @param value      See:
150          * setLedOff(), setLedOn(LEDEnum l),
151          * setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm).
152          */
153         void setLedRaw(uint8_t value);
154
155         /** Turn all LEDs off the controller. */
156         void setLedOff() {
157                 setLedRaw(0);
158         };
159         /**
160          * Turn on a LED by using ::LEDEnum.
161          * @param l          ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
162          */
163         void setLedOn(LEDEnum l);
164         /**
165          * Turn on a LED by using ::LEDEnum.
166          * @param l          ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
167          */
168         void setLedBlink(LEDEnum l);
169         /**
170          * Used to set special LED modes supported by the Xbox controller.
171          * @param lm         See ::LEDModeEnum.
172          */
173         void setLedMode(LEDModeEnum lm);
174
175         /**
176          * Used to call your own function when the controller is successfully initialized.
177          * @param funcOnInit Function to call.
178          */
179         void attachOnInit(void (*funcOnInit)(void)) {
180                 pFuncOnInit = funcOnInit;
181         };
182         /**@}*/
183
184         /** True if a Xbox 360 controller is connected. */
185         bool Xbox360Connected;
186
187 protected:
188         /** Pointer to USB class instance. */
189         USB *pUsb;
190         /** Device address. */
191         uint8_t bAddress;
192         /** Endpoint info structure. */
193         EpInfo epInfo[XBOX_MAX_ENDPOINTS];
194
195 private:
196         /**
197          * Called when the controller is successfully initialized.
198          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
199          * This is useful for instance if you want to set the LEDs in a specific way.
200          */
201         void onInit();
202         void (*pFuncOnInit)(void); // Pointer to function called in onInit()
203
204         bool bPollEnable;
205
206         /* Variables to store the buttons */
207         uint32_t ButtonState;
208         uint32_t OldButtonState;
209         uint16_t ButtonClickState;
210         int16_t hatValue[4];
211         uint16_t controllerStatus;
212
213         bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
214         bool R2Clicked;
215
216         uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
217         uint8_t writeBuf[8]; // General purpose buffer for output data
218
219         void readReport(); // read incoming data
220         void printReport(); // print incoming date - Uncomment for debugging
221
222         /* Private commands */
223         void XboxCommand(uint8_t* data, uint16_t nbytes);
224 };
225 #endif