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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2014 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 _ps4usb_h_
  15. #define _ps4usb_h_
  16. #include "hiduniversal.h"
  17. #include "PS4Parser.h"
  18. #define PS4_VID 0x054C // Sony Corporation
  19. #define PS4_PID 0x05C4 // PS4 Controller
  20. /**
  21. * This class implements support for the PS4 controller via USB.
  22. * It uses the HIDUniversal class for all the USB communication.
  23. */
  24. class PS4USB : public HIDUniversal, public PS4Parser {
  25. public:
  26. /**
  27. * Constructor for the PS4USB class.
  28. * @param p Pointer to the USB class instance.
  29. */
  30. PS4USB(USB *p) :
  31. HIDUniversal(p) {
  32. PS4Parser::Reset();
  33. };
  34. /**
  35. * Used to check if a PS4 controller is connected.
  36. * @return Returns true if it is connected.
  37. */
  38. bool connected() {
  39. return HIDUniversal::isReady() && HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID;
  40. };
  41. /**
  42. * Used to call your own function when the device is successfully initialized.
  43. * @param funcOnInit Function to call.
  44. */
  45. void attachOnInit(void (*funcOnInit)(void)) {
  46. pFuncOnInit = funcOnInit;
  47. };
  48. protected:
  49. /** @name HIDUniversal implementation */
  50. /**
  51. * Used to parse USB HID data.
  52. * @param hid Pointer to the HID class.
  53. * @param is_rpt_id Only used for Hubs.
  54. * @param len The length of the incoming data.
  55. * @param buf Pointer to the data buffer.
  56. */
  57. virtual void ParseHIDData(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  58. if (HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID)
  59. PS4Parser::Parse(len, buf);
  60. };
  61. /**
  62. * Called when a device is successfully initialized.
  63. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
  64. * This is useful for instance if you want to set the LEDs in a specific way.
  65. */
  66. virtual uint8_t OnInitSuccessful() {
  67. if (HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID) {
  68. PS4Parser::Reset();
  69. if (pFuncOnInit)
  70. pFuncOnInit(); // Call the user function
  71. else
  72. setLed(Blue);
  73. };
  74. return 0;
  75. };
  76. /**@}*/
  77. /** @name PS4Parser implementation */
  78. virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
  79. uint8_t buf[32];
  80. memset(buf, 0, sizeof(buf));
  81. buf[0] = 0x05; // Report ID
  82. buf[1]= 0xFF;
  83. buf[4] = output->smallRumble; // Small Rumble
  84. buf[5] = output->bigRumble; // Big rumble
  85. buf[6] = output->r; // Red
  86. buf[7] = output->g; // Green
  87. buf[8] = output->b; // Blue
  88. buf[9] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
  89. buf[10] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
  90. output->reportChanged = false;
  91. // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
  92. pUsb->outTransfer(bAddress, epInfo[ hidInterfaces[0].epIndex[epInterruptOutIndex] ].epAddr, sizeof(buf), buf);
  93. };
  94. /**@}*/
  95. /** @name USBDeviceConfig implementation */
  96. /**
  97. * Used by the USB core to check what this driver support.
  98. * @param vid The device's VID.
  99. * @param pid The device's PID.
  100. * @return Returns true if the device's VID and PID matches this driver.
  101. */
  102. virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
  103. return (vid == PS4_VID && pid == PS4_PID);
  104. };
  105. /**@}*/
  106. private:
  107. void (*pFuncOnInit)(void); // Pointer to function called in onInit()
  108. };
  109. #endif