]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/PS4USB.h
1 /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
21 #include "hiduniversal.h"
22 #include "PS4Parser.h"
24 #define PS4_VID 0x054C // Sony Corporation
25 #define PS4_PID 0x05C4 // PS4 Controller
28 * This class implements support for the PS4 controller via USB.
29 * It uses the HIDUniversal class for all the USB communication.
31 class PS4USB
: public HIDUniversal
, public PS4Parser
{
34 * Constructor for the PS4USB class.
35 * @param p Pointer to the USB class instance.
43 * Used to check if a PS4 controller is connected.
44 * @return Returns true if it is connected.
47 return HIDUniversal::isReady() && HIDUniversal::VID
== PS4_VID
&& HIDUniversal::PID
== PS4_PID
;
51 * Used to call your own function when the device is successfully initialized.
52 * @param funcOnInit Function to call.
54 void attachOnInit(void (*funcOnInit
)(void)) {
55 pFuncOnInit
= funcOnInit
;
59 /** @name HIDUniversal implementation */
61 * Used to parse USB HID data.
62 * @param hid Pointer to the HID class.
63 * @param is_rpt_id Only used for Hubs.
64 * @param len The length of the incoming data.
65 * @param buf Pointer to the data buffer.
67 virtual void ParseHIDData(HID
*hid
, bool is_rpt_id
, uint8_t len
, uint8_t *buf
) {
68 if (HIDUniversal::VID
== PS4_VID
&& HIDUniversal::PID
== PS4_PID
)
69 PS4Parser::Parse(len
, buf
);
73 * Called when a device is successfully initialized.
74 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
75 * This is useful for instance if you want to set the LEDs in a specific way.
77 virtual uint8_t OnInitSuccessful() {
78 if (HIDUniversal::VID
== PS4_VID
&& HIDUniversal::PID
== PS4_PID
) {
81 pFuncOnInit(); // Call the user function
89 /** @name PS4Parser implementation */
90 virtual void sendOutputReport(PS4Output
*output
) { // Source: https://github.com/chrippa/ds4drv
92 memset(buf
, 0, sizeof(buf
));
94 buf
[0] = 0x05; // Report ID
97 buf
[4] = output
->smallRumble
; // Small Rumble
98 buf
[5] = output
->bigRumble
; // Big rumble
100 buf
[6] = output
->r
; // Red
101 buf
[7] = output
->g
; // Green
102 buf
[8] = output
->b
; // Blue
104 buf
[9] = output
->flashOn
; // Time to flash bright (255 = 2.5 seconds)
105 buf
[10] = output
->flashOff
; // Time to flash dark (255 = 2.5 seconds)
107 output
->reportChanged
= false;
109 // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
111 pUsb
->outTransfer(bAddress
, epInfo
[ hidInterfaces
[0].epIndex
[epInterruptOutIndex
] ].epAddr
, sizeof(buf
), buf
);
115 /** @name USBDeviceConfig implementation */
117 * Used by the USB core to check what this driver support.
118 * @param vid The device's VID.
119 * @param pid The device's PID.
120 * @return Returns true if the device's VID and PID matches this driver.
122 virtual bool VIDPIDOK(uint16_t vid
, uint16_t pid
) {
123 return (vid
== PS4_VID
&& pid
== PS4_PID
);
128 void (*pFuncOnInit
)(void); // Pointer to function called in onInit()