You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

PS3BT.h 9.5KB

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