]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/PS3USB.h
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
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
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
25 /* PS3 data taken from descriptors */
26 #define EP_MAXPKTSIZE 64 // max size for data via USB
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
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
39 #define PS3_MAX_ENDPOINTS 3
42 * This class implements support for all the official PS3 Controllers:
43 * Dualshock 3, Navigation or a Motion controller via USB.
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.
47 * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
49 class PS3USB
: public USBDeviceConfig
{
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.
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 );
60 /** @name USBDeviceConfig implementation */
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.
68 uint8_t Init ( uint8_t parent
, uint8_t port
, bool lowspeed
);
70 * Release the USB device.
71 * @return 0 on success.
75 * Poll the USB Input endpoins and run the state machines.
76 * @return 0 on success.
81 * Get the device address.
82 * @return The device address.
84 virtual uint8_t GetAddress () {
89 * Used to check if the controller has been initialized.
90 * @return True if it's ready.
92 virtual bool isReady () {
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.
102 virtual bool VIDPIDOK ( uint16_t vid
, uint16_t pid
) {
103 return ( vid
== PS3_VID
&& ( pid
== PS3_PID
|| pid
== PS3NAVIGATION_PID
|| pid
== PS3MOVE_PID
));
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.
112 void setBdaddr ( uint8_t * bdaddr
);
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.
118 void getBdaddr ( uint8_t * bdaddr
);
121 * Used to set the Bluetooth address inside the Move controller.
122 * Set using LSB first.
123 * @param bdaddr Your dongles Bluetooth address.
125 void setMoveBdaddr ( uint8_t * bdaddr
);
127 * Used to get the Bluetooth address inside the Move controller.
128 * Will return LSB first.
129 * @param bdaddr Your dongles Bluetooth address.
131 void getMoveBdaddr ( uint8_t * bdaddr
);
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
136 void getMoveCalibration ( uint8_t * data
);
138 /** @name PS3 Controller functions */
140 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
142 * While getButtonClick(ButtonEnum b) will only return it once.
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.
149 bool getButtonPress ( ButtonEnum b
);
150 bool getButtonClick ( ButtonEnum b
);
152 /** @name PS3 Controller functions */
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.
161 uint8_t getAnalogButton ( ButtonEnum a
);
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.
167 uint8_t getAnalogHat ( AnalogHatEnum a
);
169 * Used to read the sensors inside the Dualshock 3 controller.
171 * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
172 * @return Return the raw sensor value.
174 uint16_t getSensor ( SensorEnum a
);
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.
180 double getAngle ( AngleEnum a
);
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.
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 ();
190 /** Used to set all LEDs and rumble off. */
192 /** Turn off rumble. */
196 * @param mode Either ::RumbleHigh or ::RumbleLow.
198 void setRumbleOn ( RumbleEnum mode
);
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.
206 void setRumbleOn ( uint8_t rightDuration
, uint8_t rightPower
, uint8_t leftDuration
, uint8_t leftPower
);
209 * Set LED value without using the ::LEDEnum.
210 * @param value See: ::LEDEnum.
212 void setLedRaw ( uint8_t value
);
214 /** Turn all LEDs off. */
219 * Turn the specific ::LEDEnum off.
220 * @param a The ::LEDEnum to turn off.
222 void setLedOff ( LEDEnum a
);
224 * Turn the specific ::LEDEnum on.
225 * @param a The ::LEDEnum to turn on.
227 void setLedOn ( LEDEnum a
);
229 * Toggle the specific ::LEDEnum.
230 * @param a The ::LEDEnum to toggle.
232 void setLedToggle ( LEDEnum a
);
235 * Use this to set the Color using RGB values.
236 * @param r,g,b RGB value.
238 void moveSetBulb ( uint8_t r
, uint8_t g
, uint8_t b
);
240 * Use this to set the color using the predefined colors in ::ColorsEnum.
241 * @param color The desired color.
243 void moveSetBulb ( ColorsEnum color
);
245 * Set the rumble value inside the Move controller.
246 * @param rumble The desired value in the range from 64-255.
248 void moveSetRumble ( uint8_t rumble
);
251 * Used to call your own function when the controller is successfully initialized.
252 * @param funcOnInit Function to call.
254 void attachOnInit ( void (* funcOnInit
)( void )) {
255 pFuncOnInit
= funcOnInit
;
259 /** Variable used to indicate if the normal playstation controller is successfully connected. */
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
;
267 /** Pointer to USB class instance. */
269 /** Device address. */
271 /** Endpoint info structure. */
272 EpInfo epInfo
[ PS3_MAX_ENDPOINTS
];
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.
281 void (* pFuncOnInit
)( void ); // Pointer to function called in onInit()
285 uint32_t timer
; // used to continuously set PS3 Move controller Bulb and rumble values
287 uint32_t ButtonState
;
288 uint32_t OldButtonState
;
289 uint32_t ButtonClickState
;
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
295 void readReport (); // read incoming data
296 void printReport (); // print incoming date - Uncomment for debugging
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
);