Takumi Odashima / ArduinoUsbHostShield

Dependents:   USBHOST_PS5

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