]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/PS3USB.h
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / PS3USB.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 _ps3usb_h_
19 #define _ps3usb_h_
20
21 #include "Usb.h"
22 #include "hid.h"
23 #include "PS3Enums.h"
24
25 /* PS3 data taken from descriptors */
26 #define EP_MAXPKTSIZE           64 // max size for data via USB
27
28 /* Names we give to the 3 ps3 pipes - this is only used for setting the bluetooth address into the ps3 controllers */
29 #define PS3_CONTROL_PIPE        0
30 #define PS3_OUTPUT_PIPE         1
31 #define PS3_INPUT_PIPE          2
32
33 //PID and VID of the different devices
34 #define PS3_VID                 0x054C  // Sony Corporation
35 #define PS3_PID                 0x0268  // PS3 Controller DualShock 3
36 #define PS3NAVIGATION_PID       0x042F  // Navigation controller
37 #define PS3MOVE_PID             0x03D5  // Motion controller
38
39 #define PS3_MAX_ENDPOINTS       3
40
41 /**
42  * This class implements support for all the official PS3 Controllers:
43  * Dualshock 3, Navigation or a Motion controller via USB.
44  *
45  * One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB on the Move controller.
46  *
47  * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
48  */
49 class PS3USB : public USBDeviceConfig {
50 public:
51         /**
52          * Constructor for the PS3USB class.
53          * @param  pUsb   Pointer to USB class instance.
54          * @param  btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
55          * Pass your dongles Bluetooth address into the constructor,
56          * so you are able to pair the controller with a Bluetooth dongle.
57          */
58         PS3USB(USB *pUsb, uint8_t btadr5 = 0, uint8_t btadr4 = 0, uint8_t btadr3 = 0, uint8_t btadr2 = 0, uint8_t btadr1 = 0, uint8_t btadr0 = 0);
59
60         /** @name USBDeviceConfig implementation */
61         /**
62          * Initialize the PS3 Controller.
63          * @param  parent   Hub number.
64          * @param  port     Port number on the hub.
65          * @param  lowspeed Speed of the device.
66          * @return          0 on success.
67          */
68         uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
69         /**
70          * Release the USB device.
71          * @return 0 on success.
72          */
73         uint8_t Release();
74         /**
75          * Poll the USB Input endpoins and run the state machines.
76          * @return 0 on success.
77          */
78         uint8_t Poll();
79
80         /**
81          * Get the device address.
82          * @return The device address.
83          */
84         virtual uint8_t GetAddress() {
85                 return bAddress;
86         };
87
88         /**
89          * Used to check if the controller has been initialized.
90          * @return True if it's ready.
91          */
92         virtual bool isReady() {
93                 return bPollEnable;
94         };
95
96         /**
97          * Used by the USB core to check what this driver support.
98          * @param  vid The device's VID.
99          * @param  pid The device's PID.
100          * @return     Returns true if the device's VID and PID matches this driver.
101          */
102         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
103                 return (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID));
104         };
105         /**@}*/
106
107         /**
108          * Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller.
109          * Set using LSB first.
110          * @param bdaddr Your dongles Bluetooth address.
111          */
112         void setBdaddr(uint8_t *bdaddr);
113         /**
114          * Used to get the Bluetooth address inside the Dualshock 3 and Navigation controller.
115          * Will return LSB first.
116          * @param bdaddr Your dongles Bluetooth address.
117          */
118         void getBdaddr(uint8_t *bdaddr);
119
120         /**
121          * Used to set the Bluetooth address inside the Move controller.
122          * Set using LSB first.
123          * @param bdaddr Your dongles Bluetooth address.
124          */
125         void setMoveBdaddr(uint8_t *bdaddr);
126         /**
127          * Used to get the Bluetooth address inside the Move controller.
128          * Will return LSB first.
129          * @param bdaddr Your dongles Bluetooth address.
130          */
131         void getMoveBdaddr(uint8_t *bdaddr);
132         /**
133          * Used to get the calibration data inside the Move controller.
134          * @param data Buffer to store data in. Must be at least 147 bytes
135          */
136         void getMoveCalibration(uint8_t *data);
137
138         /** @name PS3 Controller functions */
139         /**
140          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
141          *
142          * While getButtonClick(ButtonEnum b) will only return it once.
143          *
144          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
145          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
146          * @param  b          ::ButtonEnum to read.
147          * @return            getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
148          */
149         bool getButtonPress(ButtonEnum b);
150         bool getButtonClick(ButtonEnum b);
151         /**@}*/
152         /** @name PS3 Controller functions */
153         /**
154          * Used to get the analog value from button presses.
155          * @param  a The ::ButtonEnum to read.
156          * The supported buttons are:
157          * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
158          * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
159          * @return   Analog value in the range of 0-255.
160          */
161         uint8_t getAnalogButton(ButtonEnum a);
162         /**
163          * Used to read the analog joystick.
164          * @param  a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
165          * @return   Return the analog value in the range of 0-255.
166          */
167         uint8_t getAnalogHat(AnalogHatEnum a);
168         /**
169          * Used to read the sensors inside the Dualshock 3 controller.
170          * @param  a
171          * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
172          * @return   Return the raw sensor value.
173          */
174         uint16_t getSensor(SensorEnum a);
175         /**
176          * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
177          * @param  a Either ::Pitch or ::Roll.
178          * @return   Return the angle in the range of 0-360.
179          */
180         double getAngle(AngleEnum a);
181         /**
182          * Get the ::StatusEnum from the controller.
183          * @param  c The ::StatusEnum you want to read.
184          * @return   True if correct and false if not.
185          */
186         bool getStatus(StatusEnum c);
187         /** Read all the available statuses from the controller and prints it as a nice formated string. */
188         void printStatusString();
189
190         /** Used to set all LEDs and rumble off. */
191         void setAllOff();
192         /** Turn off rumble. */
193         void setRumbleOff();
194         /**
195          * Turn on rumble.
196          * @param mode Either ::RumbleHigh or ::RumbleLow.
197          */
198         void setRumbleOn(RumbleEnum mode);
199         /**
200          * Turn on rumble using custom duration and power.
201          * @param rightDuration The duration of the right/low rumble effect.
202          * @param rightPower The intensity of the right/low rumble effect.
203          * @param leftDuration The duration of the left/high rumble effect.
204          * @param leftPower The intensity of the left/high rumble effect.
205          */
206         void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
207
208         /**
209          * Set LED value without using the ::LEDEnum.
210          * @param value See: ::LEDEnum.
211          */
212         void setLedRaw(uint8_t value);
213
214         /** Turn all LEDs off. */
215         void setLedOff() {
216                 setLedRaw(0);
217         }
218         /**
219          * Turn the specific ::LEDEnum off.
220          * @param a The ::LEDEnum to turn off.
221          */
222         void setLedOff(LEDEnum a);
223         /**
224          * Turn the specific ::LEDEnum on.
225          * @param a The ::LEDEnum to turn on.
226          */
227         void setLedOn(LEDEnum a);
228         /**
229          * Toggle the specific ::LEDEnum.
230          * @param a The ::LEDEnum to toggle.
231          */
232         void setLedToggle(LEDEnum a);
233
234         /**
235          * Use this to set the Color using RGB values.
236          * @param r,g,b RGB value.
237          */
238         void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
239         /**
240          * Use this to set the color using the predefined colors in ::ColorsEnum.
241          * @param color The desired color.
242          */
243         void moveSetBulb(ColorsEnum color);
244         /**
245          * Set the rumble value inside the Move controller.
246          * @param rumble The desired value in the range from 64-255.
247          */
248         void moveSetRumble(uint8_t rumble);
249
250         /**
251          * Used to call your own function when the controller is successfully initialized.
252          * @param funcOnInit Function to call.
253          */
254         void attachOnInit(void (*funcOnInit)(void)) {
255                 pFuncOnInit = funcOnInit;
256         };
257         /**@}*/
258
259         /** Variable used to indicate if the normal playstation controller is successfully connected. */
260         bool PS3Connected;
261         /** Variable used to indicate if the move controller is successfully connected. */
262         bool PS3MoveConnected;
263         /** Variable used to indicate if the navigation controller is successfully connected. */
264         bool PS3NavigationConnected;
265
266 protected:
267         /** Pointer to USB class instance. */
268         USB *pUsb;
269         /** Device address. */
270         uint8_t bAddress;
271         /** Endpoint info structure. */
272         EpInfo epInfo[PS3_MAX_ENDPOINTS];
273
274 private:
275         /**
276          * Called when the controller is successfully initialized.
277          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
278          * This is useful for instance if you want to set the LEDs in a specific way.
279          */
280         void onInit();
281         void (*pFuncOnInit)(void); // Pointer to function called in onInit()
282
283         bool bPollEnable;
284
285         uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values
286
287         uint32_t ButtonState;
288         uint32_t OldButtonState;
289         uint32_t ButtonClickState;
290
291         uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor
292         uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
293         uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
294
295         void readReport(); // read incoming data
296         void printReport(); // print incoming date - Uncomment for debugging
297
298         /* Private commands */
299         void PS3_Command(uint8_t *data, uint16_t nbytes);
300         void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
301         void Move_Command(uint8_t *data, uint16_t nbytes);
302 };
303 #endif