PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/PS3USB.h

https://gitlab.com/joseangeljimenez/USB_Host_Shield_2.0
C Header | 303 lines | 84 code | 28 blank | 191 comment | 7 complexity | 2993495b4a811496c2dfe3aeebde78f7 MD5 | raw file
  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 : kristianl@tkjelectronics.com
  13. */
  14. #ifndef _ps3usb_h_
  15. #define _ps3usb_h_
  16. #include "Usb.h"
  17. #include "usbhid.h"
  18. #include "PS3Enums.h"
  19. /* PS3 data taken from descriptors */
  20. #define EP_MAXPKTSIZE 64 // max size for data via USB
  21. /* Names we give to the 3 ps3 pipes - this is only used for setting the bluetooth address into the ps3 controllers */
  22. #define PS3_CONTROL_PIPE 0
  23. #define PS3_OUTPUT_PIPE 1
  24. #define PS3_INPUT_PIPE 2
  25. //PID and VID of the different devices
  26. #define PS3_VID 0x054C // Sony Corporation
  27. #define PS3_PID 0x0268 // PS3 Controller DualShock 3
  28. #define PS3NAVIGATION_PID 0x042F // Navigation controller
  29. #define PS3MOVE_PID 0x03D5 // Motion controller
  30. #define PS3_MAX_ENDPOINTS 3
  31. /**
  32. * This class implements support for all the official PS3 Controllers:
  33. * Dualshock 3, Navigation or a Motion controller via USB.
  34. *
  35. * 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.
  36. *
  37. * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
  38. */
  39. class PS3USB : public USBDeviceConfig {
  40. public:
  41. /**
  42. * Constructor for the PS3USB class.
  43. * @param pUsb Pointer to USB class instance.
  44. * @param btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
  45. * Pass your dongles Bluetooth address into the constructor,
  46. * so you are able to pair the controller with a Bluetooth dongle.
  47. */
  48. 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);
  49. /** @name USBDeviceConfig implementation */
  50. /**
  51. * Initialize the PS3 Controller.
  52. * @param parent Hub number.
  53. * @param port Port number on the hub.
  54. * @param lowspeed Speed of the device.
  55. * @return 0 on success.
  56. */
  57. uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
  58. /**
  59. * Release the USB device.
  60. * @return 0 on success.
  61. */
  62. uint8_t Release();
  63. /**
  64. * Poll the USB Input endpoins and run the state machines.
  65. * @return 0 on success.
  66. */
  67. uint8_t Poll();
  68. /**
  69. * Get the device address.
  70. * @return The device address.
  71. */
  72. virtual uint8_t GetAddress() {
  73. return bAddress;
  74. };
  75. /**
  76. * Used to check if the controller has been initialized.
  77. * @return True if it's ready.
  78. */
  79. virtual bool isReady() {
  80. return bPollEnable;
  81. };
  82. /**
  83. * Used by the USB core to check what this driver support.
  84. * @param vid The device's VID.
  85. * @param pid The device's PID.
  86. * @return Returns true if the device's VID and PID matches this driver.
  87. */
  88. virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
  89. return (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID));
  90. };
  91. /**@}*/
  92. /**
  93. * Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller.
  94. * Set using LSB first.
  95. * @param bdaddr Your dongles Bluetooth address.
  96. */
  97. void setBdaddr(uint8_t *bdaddr);
  98. /**
  99. * Used to get the Bluetooth address inside the Dualshock 3 and Navigation controller.
  100. * Will return LSB first.
  101. * @param bdaddr Your dongles Bluetooth address.
  102. */
  103. void getBdaddr(uint8_t *bdaddr);
  104. /**
  105. * Used to set the Bluetooth address inside the Move controller.
  106. * Set using LSB first.
  107. * @param bdaddr Your dongles Bluetooth address.
  108. */
  109. void setMoveBdaddr(uint8_t *bdaddr);
  110. /**
  111. * Used to get the Bluetooth address inside the Move controller.
  112. * Will return LSB first.
  113. * @param bdaddr Your dongles Bluetooth address.
  114. */
  115. void getMoveBdaddr(uint8_t *bdaddr);
  116. /**
  117. * Used to get the calibration data inside the Move controller.
  118. * @param data Buffer to store data in. Must be at least 147 bytes
  119. */
  120. void getMoveCalibration(uint8_t *data);
  121. /** @name PS3 Controller functions */
  122. /**
  123. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
  124. *
  125. * While getButtonClick(ButtonEnum b) will only return it once.
  126. *
  127. * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
  128. * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
  129. * @param b ::ButtonEnum to read.
  130. * @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.
  131. */
  132. bool getButtonPress(ButtonEnum b);
  133. bool getButtonClick(ButtonEnum b);
  134. /**@}*/
  135. /** @name PS3 Controller functions */
  136. /**
  137. * Used to get the analog value from button presses.
  138. * @param a The ::ButtonEnum to read.
  139. * The supported buttons are:
  140. * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
  141. * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
  142. * @return Analog value in the range of 0-255.
  143. */
  144. uint8_t getAnalogButton(ButtonEnum a);
  145. /**
  146. * Used to read the analog joystick.
  147. * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
  148. * @return Return the analog value in the range of 0-255.
  149. */
  150. uint8_t getAnalogHat(AnalogHatEnum a);
  151. /**
  152. * Used to read the sensors inside the Dualshock 3 controller.
  153. * @param a
  154. * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
  155. * @return Return the raw sensor value.
  156. */
  157. uint16_t getSensor(SensorEnum a);
  158. /**
  159. * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
  160. * @param a Either ::Pitch or ::Roll.
  161. * @return Return the angle in the range of 0-360.
  162. */
  163. float getAngle(AngleEnum a);
  164. /**
  165. * Get the ::StatusEnum from the controller.
  166. * @param c The ::StatusEnum you want to read.
  167. * @return True if correct and false if not.
  168. */
  169. bool getStatus(StatusEnum c);
  170. /** Read all the available statuses from the controller and prints it as a nice formated string. */
  171. void printStatusString();
  172. /** Used to set all LEDs and rumble off. */
  173. void setAllOff();
  174. /** Turn off rumble. */
  175. void setRumbleOff();
  176. /**
  177. * Turn on rumble.
  178. * @param mode Either ::RumbleHigh or ::RumbleLow.
  179. */
  180. void setRumbleOn(RumbleEnum mode);
  181. /**
  182. * Turn on rumble using custom duration and power.
  183. * @param rightDuration The duration of the right/low rumble effect.
  184. * @param rightPower The intensity of the right/low rumble effect.
  185. * @param leftDuration The duration of the left/high rumble effect.
  186. * @param leftPower The intensity of the left/high rumble effect.
  187. */
  188. void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
  189. /**
  190. * Set LED value without using the ::LEDEnum.
  191. * @param value See: ::LEDEnum.
  192. */
  193. void setLedRaw(uint8_t value);
  194. /** Turn all LEDs off. */
  195. void setLedOff() {
  196. setLedRaw(0);
  197. }
  198. /**
  199. * Turn the specific ::LEDEnum off.
  200. * @param a The ::LEDEnum to turn off.
  201. */
  202. void setLedOff(LEDEnum a);
  203. /**
  204. * Turn the specific ::LEDEnum on.
  205. * @param a The ::LEDEnum to turn on.
  206. */
  207. void setLedOn(LEDEnum a);
  208. /**
  209. * Toggle the specific ::LEDEnum.
  210. * @param a The ::LEDEnum to toggle.
  211. */
  212. void setLedToggle(LEDEnum a);
  213. /**
  214. * Use this to set the Color using RGB values.
  215. * @param r,g,b RGB value.
  216. */
  217. void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
  218. /**
  219. * Use this to set the color using the predefined colors in ::ColorsEnum.
  220. * @param color The desired color.
  221. */
  222. void moveSetBulb(ColorsEnum color);
  223. /**
  224. * Set the rumble value inside the Move controller.
  225. * @param rumble The desired value in the range from 64-255.
  226. */
  227. void moveSetRumble(uint8_t rumble);
  228. /**
  229. * Used to call your own function when the controller is successfully initialized.
  230. * @param funcOnInit Function to call.
  231. */
  232. void attachOnInit(void (*funcOnInit)(void)) {
  233. pFuncOnInit = funcOnInit;
  234. };
  235. /**@}*/
  236. /** Variable used to indicate if the normal playstation controller is successfully connected. */
  237. bool PS3Connected;
  238. /** Variable used to indicate if the move controller is successfully connected. */
  239. bool PS3MoveConnected;
  240. /** Variable used to indicate if the navigation controller is successfully connected. */
  241. bool PS3NavigationConnected;
  242. protected:
  243. /** Pointer to USB class instance. */
  244. USB *pUsb;
  245. /** Device address. */
  246. uint8_t bAddress;
  247. /** Endpoint info structure. */
  248. EpInfo epInfo[PS3_MAX_ENDPOINTS];
  249. private:
  250. /**
  251. * Called when the controller is successfully initialized.
  252. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
  253. * This is useful for instance if you want to set the LEDs in a specific way.
  254. */
  255. void onInit();
  256. void (*pFuncOnInit)(void); // Pointer to function called in onInit()
  257. bool bPollEnable;
  258. uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values
  259. uint32_t ButtonState;
  260. uint32_t OldButtonState;
  261. uint32_t ButtonClickState;
  262. uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor
  263. uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
  264. uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
  265. void readReport(); // read incoming data
  266. void printReport(); // print incoming date - Uncomment for debugging
  267. /* Private commands */
  268. void PS3_Command(uint8_t *data, uint16_t nbytes);
  269. void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
  270. void Move_Command(uint8_t *data, uint16_t nbytes);
  271. };
  272. #endif