Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PS3BT.h Source File

PS3BT.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 _ps3bt_h_
00019 #define _ps3bt_h_
00020 
00021 #include "BTD.h"
00022 #include "PS3Enums.h"
00023 #include "mbed.h"
00024 
00025 #define HID_BUFFERSIZE 50 // Size of the buffer for the Playstation Motion Controller
00026 
00027 /**
00028  * This BluetoothService class implements support for all the official PS3 Controllers:
00029  * Dualshock 3, Navigation or a Motion controller via Bluetooth.
00030  *
00031  * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
00032  */
00033 class PS3BT : public BluetoothService {
00034 public:
00035         /**
00036          * Constructor for the PS3BT class.
00037          * @param  pBtd   Pointer to BTD class instance.
00038          * @param  btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
00039          * Pass your dongles Bluetooth address into the constructor,
00040          * This will set BTD#my_bdaddr, so you don't have to plug in the dongle before pairing with your controller.
00041          */
00042         PS3BT(BTD *pBtd, uint8_t btadr5 = 0, uint8_t btadr4 = 0, uint8_t btadr3 = 0, uint8_t btadr2 = 0, uint8_t btadr1 = 0, uint8_t btadr0 = 0);
00043 
00044         /** @name BluetoothService implementation */
00045         /** Used this to disconnect any of the controllers. */
00046         void disconnect();
00047         /**@}*/
00048 
00049         /** @name PS3 Controller functions */
00050         /**
00051          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
00052          *
00053          * While getButtonClick(ButtonEnum b) will only return it once.
00054          *
00055          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
00056          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
00057          * @param  b          ::ButtonEnum to read.
00058          * @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.
00059          */
00060         bool getButtonPress(ButtonEnum b);
00061         bool getButtonClick(ButtonEnum b);
00062         /**@}*/
00063         /** @name PS3 Controller functions */
00064         /**
00065          * Used to get the analog value from button presses.
00066          * @param  a The ::ButtonEnum to read.
00067          * The supported buttons are:
00068          * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
00069          * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
00070          * @return   Analog value in the range of 0-255.
00071          */
00072         uint8_t getAnalogButton(ButtonEnum a);
00073         /**
00074          * Used to read the analog joystick.
00075          * @param  a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
00076          * @return   Return the analog value in the range of 0-255.
00077          */
00078         uint8_t getAnalogHat(AnalogHatEnum a);
00079         /**
00080          * Used to read the sensors inside the Dualshock 3 and Move controller.
00081          * @param  a
00082          * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
00083          * The Move controller has a 3-axis accelerometer, a 3-axis gyro, a 3-axis magnetometer
00084          * and a temperature sensor inside.
00085          * @return   Return the raw sensor value.
00086          */
00087         int16_t getSensor(SensorEnum a);
00088         /**
00089          * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
00090          * @param  a Either ::Pitch or ::Roll.
00091          * @return   Return the angle in the range of 0-360.
00092          */
00093         float getAngle(AngleEnum a);
00094         /**
00095          * Read the sensors inside the Move controller.
00096          * @param  a ::aXmove, ::aYmove, ::aZmove, ::gXmove, ::gYmove, ::gZmove, ::mXmove, ::mYmove, and ::mXmove.
00097          * @return   The value in SI units.
00098          */
00099         float get9DOFValues(SensorEnum a);
00100         /**
00101          * Get the status from the controller.
00102          * @param  c The ::StatusEnum you want to read.
00103          * @return   True if correct and false if not.
00104          */
00105         bool getStatus(StatusEnum c);
00106         /** Read all the available statuses from the controller and prints it as a nice formated string. */
00107         void printStatusString();
00108         /**
00109          * Read the temperature from the Move controller.
00110          * @return The temperature in degrees Celsius.
00111          */
00112         char* getTemperature();
00113 
00114         /** Used to set all LEDs and rumble off. */
00115         void setAllOff();
00116         /** Turn off rumble. */
00117         void setRumbleOff();
00118         /**
00119          * Turn on rumble.
00120          * @param mode Either ::RumbleHigh or ::RumbleLow.
00121          */
00122         void setRumbleOn(RumbleEnum mode);
00123         /**
00124          * Turn on rumble using custom duration and power.
00125          * @param rightDuration The duration of the right/low rumble effect.
00126          * @param rightPower The intensity of the right/low rumble effect.
00127          * @param leftDuration The duration of the left/high rumble effect.
00128          * @param leftPower The intensity of the left/high rumble effect.
00129          */
00130         void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
00131 
00132         /**
00133          * Set LED value without using ::LEDEnum.
00134          * @param value See: ::LEDEnum.
00135          */
00136         void setLedRaw(uint8_t value);
00137 
00138         /** Turn all LEDs off. */
00139         void setLedOff() {
00140                 setLedRaw(0);
00141         };
00142         /**
00143          * Turn the specific LED off.
00144          * @param a The ::LEDEnum to turn off.
00145          */
00146         void setLedOff(LEDEnum a);
00147         /**
00148          * Turn the specific LED on.
00149          * @param a The ::LEDEnum to turn on.
00150          */
00151         void setLedOn(LEDEnum a);
00152         /**
00153          * Toggle the specific LED.
00154          * @param a The ::LEDEnum to toggle.
00155          */
00156         void setLedToggle(LEDEnum a);
00157 
00158         /**
00159          * Use this to set the Color using RGB values.
00160          * @param r,g,b RGB value.
00161          */
00162         void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
00163         /**
00164          * Use this to set the color using the predefined colors in ::ColorsEnum.
00165          * @param color The desired color.
00166          */
00167         void moveSetBulb(ColorsEnum color);
00168         /**
00169          * Set the rumble value inside the Move controller.
00170          * @param rumble The desired value in the range from 64-255.
00171          */
00172         void moveSetRumble(uint8_t rumble);
00173 
00174         /** Used to get the millis() of the last message */
00175         uint32_t getLastMessageTime() {
00176                 return lastMessageTime;
00177         };
00178         /**@}*/
00179 
00180         /** Variable used to indicate if the normal Playstation controller is successfully connected. */
00181         bool PS3Connected;
00182         /** Variable used to indicate if the Move controller is successfully connected. */
00183         bool PS3MoveConnected;
00184         /** Variable used to indicate if the Navigation controller is successfully connected. */
00185         bool PS3NavigationConnected;
00186 
00187 protected:
00188         /** @name BluetoothService implementation */
00189         /**
00190          * Used to pass acldata to the services.
00191          * @param ACLData Incoming acldata.
00192          */
00193         void ACLData(uint8_t* ACLData);
00194         /** Used to run part of the state machine. */
00195         void Run();
00196         /** Use this to reset the service. */
00197         void Reset();
00198         /**
00199          * Called when the controller is successfully initialized.
00200          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
00201          * This is useful for instance if you want to set the LEDs in a specific way.
00202          */
00203         void onInit();
00204         /**@}*/
00205 
00206 private:
00207 
00208         void L2CAP_task(); // L2CAP state machine
00209 
00210         /* Variables filled from HCI event management */
00211         char remote_name_first; // First letter in remote name
00212         bool activeConnection; // Used to indicate if it's already has established a connection
00213 
00214         /* Variables used by high level L2CAP task */
00215         uint8_t l2cap_state;
00216 
00217         uint32_t lastMessageTime; // Variable used to store the millis value of the last message.
00218 
00219         uint32_t ButtonState;
00220         uint32_t OldButtonState;
00221         uint32_t ButtonClickState;
00222 
00223         uint32_t timer; // Timer used to limit time between messages and also used to continuously set PS3 Move controller Bulb and rumble values
00224         uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
00225 
00226         uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
00227         uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
00228         uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
00229 
00230         /* L2CAP Channels */
00231         uint8_t control_scid[2]; // L2CAP source CID for HID_Control
00232         uint8_t control_dcid[2]; // 0x0040
00233         uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
00234         uint8_t interrupt_dcid[2]; // 0x0041
00235 
00236         /* HID Commands */
00237         void HID_Command(uint8_t* data, uint8_t nbytes);
00238         void HIDMove_Command(uint8_t* data, uint8_t nbytes);
00239         void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
00240 };
00241 #endif