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.
USB_Host/PS3BT.h@1:da31140f2a1c, 2020-05-02 (annotated)
- Committer:
- robo_ichinoseki_a
- Date:
- Sat May 02 05:56:48 2020 +0000
- Revision:
- 1:da31140f2a1c
- Parent:
- 0:b1ce54272580
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotakku | 0:b1ce54272580 | 1 | /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. |
kotakku | 0:b1ce54272580 | 2 | |
kotakku | 0:b1ce54272580 | 3 | This software may be distributed and modified under the terms of the GNU |
kotakku | 0:b1ce54272580 | 4 | General Public License version 2 (GPL2) as published by the Free Software |
kotakku | 0:b1ce54272580 | 5 | Foundation and appearing in the file GPL2.TXT included in the packaging of |
kotakku | 0:b1ce54272580 | 6 | this file. Please note that GPL2 Section 2[b] requires that all works based |
kotakku | 0:b1ce54272580 | 7 | on this software must also be made publicly available under the terms of |
kotakku | 0:b1ce54272580 | 8 | the GPL2 ("Copyleft"). |
kotakku | 0:b1ce54272580 | 9 | |
kotakku | 0:b1ce54272580 | 10 | Contact information |
kotakku | 0:b1ce54272580 | 11 | ------------------- |
kotakku | 0:b1ce54272580 | 12 | |
kotakku | 0:b1ce54272580 | 13 | Kristian Lauszus, TKJ Electronics |
kotakku | 0:b1ce54272580 | 14 | Web : http://www.tkjelectronics.com |
kotakku | 0:b1ce54272580 | 15 | e-mail : kristianl@tkjelectronics.com |
kotakku | 0:b1ce54272580 | 16 | */ |
kotakku | 0:b1ce54272580 | 17 | |
kotakku | 0:b1ce54272580 | 18 | #ifndef _ps3bt_h_ |
kotakku | 0:b1ce54272580 | 19 | #define _ps3bt_h_ |
kotakku | 0:b1ce54272580 | 20 | |
kotakku | 0:b1ce54272580 | 21 | #include "BTD.h" |
kotakku | 0:b1ce54272580 | 22 | #include "PS3Enums.h" |
kotakku | 0:b1ce54272580 | 23 | |
kotakku | 0:b1ce54272580 | 24 | #define HID_BUFFERSIZE 50 // Size of the buffer for the Playstation Motion Controller |
kotakku | 0:b1ce54272580 | 25 | |
kotakku | 0:b1ce54272580 | 26 | /** |
kotakku | 0:b1ce54272580 | 27 | * This BluetoothService class implements support for all the official PS3 Controllers: |
kotakku | 0:b1ce54272580 | 28 | * Dualshock 3, Navigation or a Motion controller via Bluetooth. |
kotakku | 0:b1ce54272580 | 29 | * |
kotakku | 0:b1ce54272580 | 30 | * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information. |
kotakku | 0:b1ce54272580 | 31 | */ |
kotakku | 0:b1ce54272580 | 32 | class PS3BT : public BluetoothService { |
kotakku | 0:b1ce54272580 | 33 | public: |
kotakku | 0:b1ce54272580 | 34 | /** |
kotakku | 0:b1ce54272580 | 35 | * Constructor for the PS3BT class. |
kotakku | 0:b1ce54272580 | 36 | * @param pBtd Pointer to BTD class instance. |
kotakku | 0:b1ce54272580 | 37 | * @param btadr5,btadr4,btadr3,btadr2,btadr1,btadr0 |
kotakku | 0:b1ce54272580 | 38 | * Pass your dongles Bluetooth address into the constructor, |
kotakku | 0:b1ce54272580 | 39 | * This will set BTD#my_bdaddr, so you don't have to plug in the dongle before pairing with your controller. |
kotakku | 0:b1ce54272580 | 40 | */ |
kotakku | 0:b1ce54272580 | 41 | 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); |
kotakku | 0:b1ce54272580 | 42 | |
kotakku | 0:b1ce54272580 | 43 | /** @name BluetoothService implementation */ |
kotakku | 0:b1ce54272580 | 44 | /** Used this to disconnect any of the controllers. */ |
kotakku | 0:b1ce54272580 | 45 | void disconnect(); |
kotakku | 0:b1ce54272580 | 46 | /**@}*/ |
kotakku | 0:b1ce54272580 | 47 | |
kotakku | 0:b1ce54272580 | 48 | /** @name PS3 Controller functions */ |
kotakku | 0:b1ce54272580 | 49 | /** |
kotakku | 0:b1ce54272580 | 50 | * getButtonPress(ButtonEnum b) will return true as long as the button is held down. |
kotakku | 0:b1ce54272580 | 51 | * |
kotakku | 0:b1ce54272580 | 52 | * While getButtonClick(ButtonEnum b) will only return it once. |
kotakku | 0:b1ce54272580 | 53 | * |
kotakku | 0:b1ce54272580 | 54 | * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), |
kotakku | 0:b1ce54272580 | 55 | * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). |
kotakku | 0:b1ce54272580 | 56 | * @param b ::ButtonEnum to read. |
kotakku | 0:b1ce54272580 | 57 | * @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. |
kotakku | 0:b1ce54272580 | 58 | */ |
kotakku | 0:b1ce54272580 | 59 | bool getButtonPress(ButtonEnum b); |
kotakku | 0:b1ce54272580 | 60 | bool getButtonClick(ButtonEnum b); |
kotakku | 0:b1ce54272580 | 61 | /**@}*/ |
kotakku | 0:b1ce54272580 | 62 | /** @name PS3 Controller functions */ |
kotakku | 0:b1ce54272580 | 63 | /** |
kotakku | 0:b1ce54272580 | 64 | * Used to get the analog value from button presses. |
kotakku | 0:b1ce54272580 | 65 | * @param a The ::ButtonEnum to read. |
kotakku | 0:b1ce54272580 | 66 | * The supported buttons are: |
kotakku | 0:b1ce54272580 | 67 | * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2, |
kotakku | 0:b1ce54272580 | 68 | * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T. |
kotakku | 0:b1ce54272580 | 69 | * @return Analog value in the range of 0-255. |
kotakku | 0:b1ce54272580 | 70 | */ |
kotakku | 0:b1ce54272580 | 71 | uint8_t getAnalogButton(ButtonEnum a); |
kotakku | 0:b1ce54272580 | 72 | /** |
kotakku | 0:b1ce54272580 | 73 | * Used to read the analog joystick. |
kotakku | 0:b1ce54272580 | 74 | * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY. |
kotakku | 0:b1ce54272580 | 75 | * @return Return the analog value in the range of 0-255. |
kotakku | 0:b1ce54272580 | 76 | */ |
kotakku | 0:b1ce54272580 | 77 | uint8_t getAnalogHat(AnalogHatEnum a); |
kotakku | 0:b1ce54272580 | 78 | /** |
kotakku | 0:b1ce54272580 | 79 | * Used to read the sensors inside the Dualshock 3 and Move controller. |
kotakku | 0:b1ce54272580 | 80 | * @param a |
kotakku | 0:b1ce54272580 | 81 | * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside. |
kotakku | 0:b1ce54272580 | 82 | * The Move controller has a 3-axis accelerometer, a 3-axis gyro, a 3-axis magnetometer |
kotakku | 0:b1ce54272580 | 83 | * and a temperature sensor inside. |
kotakku | 0:b1ce54272580 | 84 | * @return Return the raw sensor value. |
kotakku | 0:b1ce54272580 | 85 | */ |
kotakku | 0:b1ce54272580 | 86 | int16_t getSensor(SensorEnum a); |
kotakku | 0:b1ce54272580 | 87 | /** |
kotakku | 0:b1ce54272580 | 88 | * Use this to get ::Pitch and ::Roll calculated using the accelerometer. |
kotakku | 0:b1ce54272580 | 89 | * @param a Either ::Pitch or ::Roll. |
kotakku | 0:b1ce54272580 | 90 | * @return Return the angle in the range of 0-360. |
kotakku | 0:b1ce54272580 | 91 | */ |
kotakku | 0:b1ce54272580 | 92 | float getAngle(AngleEnum a); |
kotakku | 0:b1ce54272580 | 93 | /** |
kotakku | 0:b1ce54272580 | 94 | * Read the sensors inside the Move controller. |
kotakku | 0:b1ce54272580 | 95 | * @param a ::aXmove, ::aYmove, ::aZmove, ::gXmove, ::gYmove, ::gZmove, ::mXmove, ::mYmove, and ::mXmove. |
kotakku | 0:b1ce54272580 | 96 | * @return The value in SI units. |
kotakku | 0:b1ce54272580 | 97 | */ |
kotakku | 0:b1ce54272580 | 98 | float get9DOFValues(SensorEnum a); |
kotakku | 0:b1ce54272580 | 99 | /** |
kotakku | 0:b1ce54272580 | 100 | * Get the status from the controller. |
kotakku | 0:b1ce54272580 | 101 | * @param c The ::StatusEnum you want to read. |
kotakku | 0:b1ce54272580 | 102 | * @return True if correct and false if not. |
kotakku | 0:b1ce54272580 | 103 | */ |
kotakku | 0:b1ce54272580 | 104 | bool getStatus(StatusEnum c); |
kotakku | 0:b1ce54272580 | 105 | /** Read all the available statuses from the controller and prints it as a nice formated string. */ |
kotakku | 0:b1ce54272580 | 106 | void printStatusString(); |
kotakku | 0:b1ce54272580 | 107 | /** |
kotakku | 0:b1ce54272580 | 108 | * Read the temperature from the Move controller. |
kotakku | 0:b1ce54272580 | 109 | * @return The temperature in degrees Celsius. |
kotakku | 0:b1ce54272580 | 110 | */ |
kotakku | 0:b1ce54272580 | 111 | String getTemperature(); |
kotakku | 0:b1ce54272580 | 112 | |
kotakku | 0:b1ce54272580 | 113 | /** Used to set all LEDs and rumble off. */ |
kotakku | 0:b1ce54272580 | 114 | void setAllOff(); |
kotakku | 0:b1ce54272580 | 115 | /** Turn off rumble. */ |
kotakku | 0:b1ce54272580 | 116 | void setRumbleOff(); |
kotakku | 0:b1ce54272580 | 117 | /** |
kotakku | 0:b1ce54272580 | 118 | * Turn on rumble. |
kotakku | 0:b1ce54272580 | 119 | * @param mode Either ::RumbleHigh or ::RumbleLow. |
kotakku | 0:b1ce54272580 | 120 | */ |
kotakku | 0:b1ce54272580 | 121 | void setRumbleOn(RumbleEnum mode); |
kotakku | 0:b1ce54272580 | 122 | /** |
kotakku | 0:b1ce54272580 | 123 | * Turn on rumble using custom duration and power. |
kotakku | 0:b1ce54272580 | 124 | * @param rightDuration The duration of the right/low rumble effect. |
kotakku | 0:b1ce54272580 | 125 | * @param rightPower The intensity of the right/low rumble effect. |
kotakku | 0:b1ce54272580 | 126 | * @param leftDuration The duration of the left/high rumble effect. |
kotakku | 0:b1ce54272580 | 127 | * @param leftPower The intensity of the left/high rumble effect. |
kotakku | 0:b1ce54272580 | 128 | */ |
kotakku | 0:b1ce54272580 | 129 | void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower); |
kotakku | 0:b1ce54272580 | 130 | |
kotakku | 0:b1ce54272580 | 131 | /** |
kotakku | 0:b1ce54272580 | 132 | * Set LED value without using ::LEDEnum. |
kotakku | 0:b1ce54272580 | 133 | * @param value See: ::LEDEnum. |
kotakku | 0:b1ce54272580 | 134 | */ |
kotakku | 0:b1ce54272580 | 135 | void setLedRaw(uint8_t value); |
kotakku | 0:b1ce54272580 | 136 | |
kotakku | 0:b1ce54272580 | 137 | /** Turn all LEDs off. */ |
kotakku | 0:b1ce54272580 | 138 | void setLedOff() { |
kotakku | 0:b1ce54272580 | 139 | setLedRaw(0); |
kotakku | 0:b1ce54272580 | 140 | }; |
kotakku | 0:b1ce54272580 | 141 | /** |
kotakku | 0:b1ce54272580 | 142 | * Turn the specific LED off. |
kotakku | 0:b1ce54272580 | 143 | * @param a The ::LEDEnum to turn off. |
kotakku | 0:b1ce54272580 | 144 | */ |
kotakku | 0:b1ce54272580 | 145 | void setLedOff(LEDEnum a); |
kotakku | 0:b1ce54272580 | 146 | /** |
kotakku | 0:b1ce54272580 | 147 | * Turn the specific LED on. |
kotakku | 0:b1ce54272580 | 148 | * @param a The ::LEDEnum to turn on. |
kotakku | 0:b1ce54272580 | 149 | */ |
kotakku | 0:b1ce54272580 | 150 | void setLedOn(LEDEnum a); |
kotakku | 0:b1ce54272580 | 151 | /** |
kotakku | 0:b1ce54272580 | 152 | * Toggle the specific LED. |
kotakku | 0:b1ce54272580 | 153 | * @param a The ::LEDEnum to toggle. |
kotakku | 0:b1ce54272580 | 154 | */ |
kotakku | 0:b1ce54272580 | 155 | void setLedToggle(LEDEnum a); |
kotakku | 0:b1ce54272580 | 156 | |
kotakku | 0:b1ce54272580 | 157 | /** |
kotakku | 0:b1ce54272580 | 158 | * Use this to set the Color using RGB values. |
kotakku | 0:b1ce54272580 | 159 | * @param r,g,b RGB value. |
kotakku | 0:b1ce54272580 | 160 | */ |
kotakku | 0:b1ce54272580 | 161 | void moveSetBulb(uint8_t r, uint8_t g, uint8_t b); |
kotakku | 0:b1ce54272580 | 162 | /** |
kotakku | 0:b1ce54272580 | 163 | * Use this to set the color using the predefined colors in ::ColorsEnum. |
kotakku | 0:b1ce54272580 | 164 | * @param color The desired color. |
kotakku | 0:b1ce54272580 | 165 | */ |
kotakku | 0:b1ce54272580 | 166 | void moveSetBulb(ColorsEnum color); |
kotakku | 0:b1ce54272580 | 167 | /** |
kotakku | 0:b1ce54272580 | 168 | * Set the rumble value inside the Move controller. |
kotakku | 0:b1ce54272580 | 169 | * @param rumble The desired value in the range from 64-255. |
kotakku | 0:b1ce54272580 | 170 | */ |
kotakku | 0:b1ce54272580 | 171 | void moveSetRumble(uint8_t rumble); |
kotakku | 0:b1ce54272580 | 172 | |
kotakku | 0:b1ce54272580 | 173 | /** Used to get the millis() of the last message */ |
kotakku | 0:b1ce54272580 | 174 | uint32_t getLastMessageTime() { |
kotakku | 0:b1ce54272580 | 175 | return lastMessageTime; |
kotakku | 0:b1ce54272580 | 176 | }; |
kotakku | 0:b1ce54272580 | 177 | /**@}*/ |
kotakku | 0:b1ce54272580 | 178 | |
kotakku | 0:b1ce54272580 | 179 | /** Variable used to indicate if the normal Playstation controller is successfully connected. */ |
kotakku | 0:b1ce54272580 | 180 | bool PS3Connected; |
kotakku | 0:b1ce54272580 | 181 | /** Variable used to indicate if the Move controller is successfully connected. */ |
kotakku | 0:b1ce54272580 | 182 | bool PS3MoveConnected; |
kotakku | 0:b1ce54272580 | 183 | /** Variable used to indicate if the Navigation controller is successfully connected. */ |
kotakku | 0:b1ce54272580 | 184 | bool PS3NavigationConnected; |
kotakku | 0:b1ce54272580 | 185 | |
kotakku | 0:b1ce54272580 | 186 | protected: |
kotakku | 0:b1ce54272580 | 187 | /** @name BluetoothService implementation */ |
kotakku | 0:b1ce54272580 | 188 | /** |
kotakku | 0:b1ce54272580 | 189 | * Used to pass acldata to the services. |
kotakku | 0:b1ce54272580 | 190 | * @param ACLData Incoming acldata. |
kotakku | 0:b1ce54272580 | 191 | */ |
kotakku | 0:b1ce54272580 | 192 | void ACLData(uint8_t* ACLData); |
kotakku | 0:b1ce54272580 | 193 | /** Used to run part of the state machine. */ |
kotakku | 0:b1ce54272580 | 194 | void Run(); |
kotakku | 0:b1ce54272580 | 195 | /** Use this to reset the service. */ |
kotakku | 0:b1ce54272580 | 196 | void Reset(); |
kotakku | 0:b1ce54272580 | 197 | /** |
kotakku | 0:b1ce54272580 | 198 | * Called when the controller is successfully initialized. |
kotakku | 0:b1ce54272580 | 199 | * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. |
kotakku | 0:b1ce54272580 | 200 | * This is useful for instance if you want to set the LEDs in a specific way. |
kotakku | 0:b1ce54272580 | 201 | */ |
kotakku | 0:b1ce54272580 | 202 | void onInit(); |
kotakku | 0:b1ce54272580 | 203 | /**@}*/ |
kotakku | 0:b1ce54272580 | 204 | |
kotakku | 0:b1ce54272580 | 205 | private: |
kotakku | 0:b1ce54272580 | 206 | |
kotakku | 0:b1ce54272580 | 207 | void L2CAP_task(); // L2CAP state machine |
kotakku | 0:b1ce54272580 | 208 | |
kotakku | 0:b1ce54272580 | 209 | /* Variables filled from HCI event management */ |
kotakku | 0:b1ce54272580 | 210 | char remote_name_first; // First letter in remote name |
kotakku | 0:b1ce54272580 | 211 | bool activeConnection; // Used to indicate if it's already has established a connection |
kotakku | 0:b1ce54272580 | 212 | |
kotakku | 0:b1ce54272580 | 213 | /* Variables used by high level L2CAP task */ |
kotakku | 0:b1ce54272580 | 214 | uint8_t l2cap_state; |
kotakku | 0:b1ce54272580 | 215 | |
kotakku | 0:b1ce54272580 | 216 | uint32_t lastMessageTime; // Variable used to store the millis value of the last message. |
kotakku | 0:b1ce54272580 | 217 | |
kotakku | 0:b1ce54272580 | 218 | uint32_t ButtonState; |
kotakku | 0:b1ce54272580 | 219 | uint32_t OldButtonState; |
kotakku | 0:b1ce54272580 | 220 | uint32_t ButtonClickState; |
kotakku | 0:b1ce54272580 | 221 | |
kotakku | 0:b1ce54272580 | 222 | uint32_t timer; // Timer used to limit time between messages and also used to continuously set PS3 Move controller Bulb and rumble values |
kotakku | 0:b1ce54272580 | 223 | uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command |
kotakku | 0:b1ce54272580 | 224 | |
kotakku | 0:b1ce54272580 | 225 | uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data |
kotakku | 0:b1ce54272580 | 226 | uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands |
kotakku | 0:b1ce54272580 | 227 | uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller |
kotakku | 0:b1ce54272580 | 228 | |
kotakku | 0:b1ce54272580 | 229 | /* L2CAP Channels */ |
kotakku | 0:b1ce54272580 | 230 | uint8_t control_scid[2]; // L2CAP source CID for HID_Control |
kotakku | 0:b1ce54272580 | 231 | uint8_t control_dcid[2]; // 0x0040 |
kotakku | 0:b1ce54272580 | 232 | uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt |
kotakku | 0:b1ce54272580 | 233 | uint8_t interrupt_dcid[2]; // 0x0041 |
kotakku | 0:b1ce54272580 | 234 | |
kotakku | 0:b1ce54272580 | 235 | /* HID Commands */ |
kotakku | 0:b1ce54272580 | 236 | void HID_Command(uint8_t* data, uint8_t nbytes); |
kotakku | 0:b1ce54272580 | 237 | void HIDMove_Command(uint8_t* data, uint8_t nbytes); |
kotakku | 0:b1ce54272580 | 238 | void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth |
kotakku | 0:b1ce54272580 | 239 | }; |
kotakku | 0:b1ce54272580 | 240 | #endif |
kotakku | 0:b1ce54272580 | 241 |