Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: UsbHostMAX3421E_Hello
PS3USB.h
00001 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. 00002 00003 This software may be distributed and modified under the terms of the GNU 00004 General Public License version 2 (GPL2) as published by the Free Software 00005 Foundation and appearing in the file GPL2.TXT included in the packaging of 00006 this file. Please note that GPL2 Section 2[b] requires that all works based 00007 on this software must also be made publicly available under the terms of 00008 the GPL2 ("Copyleft"). 00009 00010 Contact information 00011 ------------------- 00012 00013 Kristian Lauszus, TKJ Electronics 00014 Web : http://www.tkjelectronics.com 00015 e-mail : kristianl@tkjelectronics.com 00016 */ 00017 00018 #ifndef _ps3usb_h_ 00019 #define _ps3usb_h_ 00020 00021 #include "Usb.h" 00022 #include "usbhid.h" 00023 #include "PS3Enums.h" 00024 00025 /* PS3 data taken from descriptors */ 00026 #define EP_MAXPKTSIZE 64 // max size for data via USB 00027 00028 /* Names we give to the 3 ps3 pipes - this is only used for setting the bluetooth address into the ps3 controllers */ 00029 #define PS3_CONTROL_PIPE 0 00030 #define PS3_OUTPUT_PIPE 1 00031 #define PS3_INPUT_PIPE 2 00032 00033 //PID and VID of the different devices 00034 #define PS3_VID 0x054C // Sony Corporation 00035 #define PS3_PID 0x0268 // PS3 Controller DualShock 3 00036 #define PS3NAVIGATION_PID 0x042F // Navigation controller 00037 #define PS3MOVE_PID 0x03D5 // Motion controller 00038 00039 #define PS3_MAX_ENDPOINTS 3 00040 00041 /** 00042 * This class implements support for all the official PS3 Controllers: 00043 * Dualshock 3, Navigation or a Motion controller via USB. 00044 * 00045 * 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. 00046 * 00047 * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information. 00048 */ 00049 class PS3USB : public USBDeviceConfig { 00050 public: 00051 /** 00052 * Constructor for the PS3USB class. 00053 * @param pUsb Pointer to USB class instance. 00054 * @param btadr5,btadr4,btadr3,btadr2,btadr1,btadr0 00055 * Pass your dongles Bluetooth address into the constructor, 00056 * so you are able to pair the controller with a Bluetooth dongle. 00057 */ 00058 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); 00059 00060 /** @name USBDeviceConfig implementation */ 00061 /** 00062 * Initialize the PS3 Controller. 00063 * @param parent Hub number. 00064 * @param port Port number on the hub. 00065 * @param lowspeed Speed of the device. 00066 * @return 0 on success. 00067 */ 00068 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); 00069 /** 00070 * Release the USB device. 00071 * @return 0 on success. 00072 */ 00073 uint8_t Release(); 00074 /** 00075 * Poll the USB Input endpoins and run the state machines. 00076 * @return 0 on success. 00077 */ 00078 uint8_t Poll(); 00079 00080 /** 00081 * Get the device address. 00082 * @return The device address. 00083 */ 00084 virtual uint8_t GetAddress() { 00085 return bAddress; 00086 }; 00087 00088 /** 00089 * Used to check if the controller has been initialized. 00090 * @return True if it's ready. 00091 */ 00092 virtual bool isReady() { 00093 return bPollEnable; 00094 }; 00095 00096 /** 00097 * Used by the USB core to check what this driver support. 00098 * @param vid The device's VID. 00099 * @param pid The device's PID. 00100 * @return Returns true if the device's VID and PID matches this driver. 00101 */ 00102 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { 00103 return (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID)); 00104 }; 00105 /**@}*/ 00106 00107 /** 00108 * Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller. 00109 * Set using LSB first. 00110 * @param bdaddr Your dongles Bluetooth address. 00111 */ 00112 void setBdaddr(uint8_t *bdaddr); 00113 /** 00114 * Used to get the Bluetooth address inside the Dualshock 3 and Navigation controller. 00115 * Will return LSB first. 00116 * @param bdaddr Your dongles Bluetooth address. 00117 */ 00118 void getBdaddr(uint8_t *bdaddr); 00119 00120 /** 00121 * Used to set the Bluetooth address inside the Move controller. 00122 * Set using LSB first. 00123 * @param bdaddr Your dongles Bluetooth address. 00124 */ 00125 void setMoveBdaddr(uint8_t *bdaddr); 00126 /** 00127 * Used to get the Bluetooth address inside the Move controller. 00128 * Will return LSB first. 00129 * @param bdaddr Your dongles Bluetooth address. 00130 */ 00131 void getMoveBdaddr(uint8_t *bdaddr); 00132 /** 00133 * Used to get the calibration data inside the Move controller. 00134 * @param data Buffer to store data in. Must be at least 147 bytes 00135 */ 00136 void getMoveCalibration(uint8_t *data); 00137 00138 /** @name PS3 Controller functions */ 00139 /** 00140 * getButtonPress(ButtonEnum b) will return true as long as the button is held down. 00141 * 00142 * While getButtonClick(ButtonEnum b) will only return it once. 00143 * 00144 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), 00145 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). 00146 * @param b ::ButtonEnum to read. 00147 * @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. 00148 */ 00149 bool getButtonPress(ButtonEnum b); 00150 bool getButtonClick(ButtonEnum b); 00151 /**@}*/ 00152 /** @name PS3 Controller functions */ 00153 /** 00154 * Used to get the analog value from button presses. 00155 * @param a The ::ButtonEnum to read. 00156 * The supported buttons are: 00157 * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2, 00158 * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T. 00159 * @return Analog value in the range of 0-255. 00160 */ 00161 uint8_t getAnalogButton(ButtonEnum a); 00162 /** 00163 * Used to read the analog joystick. 00164 * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY. 00165 * @return Return the analog value in the range of 0-255. 00166 */ 00167 uint8_t getAnalogHat(AnalogHatEnum a); 00168 /** 00169 * Used to read the sensors inside the Dualshock 3 controller. 00170 * @param a 00171 * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside. 00172 * @return Return the raw sensor value. 00173 */ 00174 uint16_t getSensor(SensorEnum a); 00175 /** 00176 * Use this to get ::Pitch and ::Roll calculated using the accelerometer. 00177 * @param a Either ::Pitch or ::Roll. 00178 * @return Return the angle in the range of 0-360. 00179 */ 00180 float getAngle(AngleEnum a); 00181 /** 00182 * Get the ::StatusEnum from the controller. 00183 * @param c The ::StatusEnum you want to read. 00184 * @return True if correct and false if not. 00185 */ 00186 bool getStatus(StatusEnum c); 00187 /** Read all the available statuses from the controller and prints it as a nice formated string. */ 00188 void printStatusString(); 00189 00190 /** Used to set all LEDs and rumble off. */ 00191 void setAllOff(); 00192 /** Turn off rumble. */ 00193 void setRumbleOff(); 00194 /** 00195 * Turn on rumble. 00196 * @param mode Either ::RumbleHigh or ::RumbleLow. 00197 */ 00198 void setRumbleOn(RumbleEnum mode); 00199 /** 00200 * Turn on rumble using custom duration and power. 00201 * @param rightDuration The duration of the right/low rumble effect. 00202 * @param rightPower The intensity of the right/low rumble effect. 00203 * @param leftDuration The duration of the left/high rumble effect. 00204 * @param leftPower The intensity of the left/high rumble effect. 00205 */ 00206 void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower); 00207 00208 /** 00209 * Set LED value without using the ::LEDEnum. 00210 * @param value See: ::LEDEnum. 00211 */ 00212 void setLedRaw(uint8_t value); 00213 00214 /** Turn all LEDs off. */ 00215 void setLedOff() { 00216 setLedRaw(0); 00217 } 00218 /** 00219 * Turn the specific ::LEDEnum off. 00220 * @param a The ::LEDEnum to turn off. 00221 */ 00222 void setLedOff(LEDEnum a); 00223 /** 00224 * Turn the specific ::LEDEnum on. 00225 * @param a The ::LEDEnum to turn on. 00226 */ 00227 void setLedOn(LEDEnum a); 00228 /** 00229 * Toggle the specific ::LEDEnum. 00230 * @param a The ::LEDEnum to toggle. 00231 */ 00232 void setLedToggle(LEDEnum a); 00233 00234 /** 00235 * Use this to set the Color using RGB values. 00236 * @param r,g,b RGB value. 00237 */ 00238 void moveSetBulb(uint8_t r, uint8_t g, uint8_t b); 00239 /** 00240 * Use this to set the color using the predefined colors in ::ColorsEnum. 00241 * @param color The desired color. 00242 */ 00243 void moveSetBulb(ColorsEnum color); 00244 /** 00245 * Set the rumble value inside the Move controller. 00246 * @param rumble The desired value in the range from 64-255. 00247 */ 00248 void moveSetRumble(uint8_t rumble); 00249 00250 /** 00251 * Used to call your own function when the controller is successfully initialized. 00252 * @param funcOnInit Function to call. 00253 */ 00254 void attachOnInit(void (*funcOnInit)(void)) { 00255 pFuncOnInit = funcOnInit; 00256 }; 00257 /**@}*/ 00258 00259 /** Variable used to indicate if the normal playstation controller is successfully connected. */ 00260 bool PS3Connected; 00261 /** Variable used to indicate if the move controller is successfully connected. */ 00262 bool PS3MoveConnected; 00263 /** Variable used to indicate if the navigation controller is successfully connected. */ 00264 bool PS3NavigationConnected; 00265 00266 protected: 00267 /** Pointer to USB class instance. */ 00268 Usb *pUsb; 00269 /** Device address. */ 00270 uint8_t bAddress; 00271 /** Endpoint info structure. */ 00272 EpInfo epInfo[PS3_MAX_ENDPOINTS]; 00273 00274 private: 00275 /** 00276 * Called when the controller is successfully initialized. 00277 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. 00278 * This is useful for instance if you want to set the LEDs in a specific way. 00279 */ 00280 void onInit(); 00281 void (*pFuncOnInit)(void); // Pointer to function called in onInit() 00282 00283 bool bPollEnable; 00284 00285 uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values 00286 00287 uint32_t ButtonState; 00288 uint32_t OldButtonState; 00289 uint32_t ButtonClickState; 00290 00291 uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor 00292 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data 00293 uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data 00294 00295 void readReport(); // read incoming data 00296 void printReport(); // print incoming date - Uncomment for debugging 00297 00298 /* Private commands */ 00299 void PS3_Command(uint8_t *data, uint16_t nbytes); 00300 void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB 00301 void Move_Command(uint8_t *data, uint16_t nbytes); 00302 }; 00303 #endif
Generated on Tue Jul 12 2022 18:12:05 by
1.7.2