]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/PS4USB.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / PS4USB.h
1 /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
2
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
8 the GPL2 ("Copyleft").
9
10 Contact information
11 -------------------
12
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
16 */
17
18 #ifndef _ps4usb_h_
19 #define _ps4usb_h_
20
21 #include "hiduniversal.h"
22 #include "PS4Parser.h"
23
24 #define PS4_VID 0x054C // Sony Corporation
25 #define PS4_PID 0x05C4 // PS4 Controller
26
27 /**
28 * This class implements support for the PS4 controller via USB.
29 * It uses the HIDUniversal class for all the USB communication.
30 */
31 class PS4USB : public HIDUniversal, public PS4Parser {
32 public:
33 /**
34 * Constructor for the PS4USB class.
35 * @param p Pointer to the USB class instance.
36 */
37 PS4USB(USB *p) :
38 HIDUniversal(p) {
39 PS4Parser::Reset();
40 };
41
42 /**
43 * Used to check if a PS4 controller is connected.
44 * @return Returns true if it is connected.
45 */
46 bool connected() {
47 return HIDUniversal::isReady() && HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID;
48 };
49
50 /**
51 * Used to call your own function when the device is successfully initialized.
52 * @param funcOnInit Function to call.
53 */
54 void attachOnInit(void (*funcOnInit)(void)) {
55 pFuncOnInit = funcOnInit;
56 };
57
58 protected:
59 /** @name HIDUniversal implementation */
60 /**
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.
66 */
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);
70 };
71
72 /**
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.
76 */
77 virtual uint8_t OnInitSuccessful() {
78 if (HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID) {
79 PS4Parser::Reset();
80 if (pFuncOnInit)
81 pFuncOnInit(); // Call the user function
82 else
83 setLed(Blue);
84 };
85 return 0;
86 };
87 /**@}*/
88
89 /** @name PS4Parser implementation */
90 virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
91 uint8_t buf[32];
92 memset(buf, 0, sizeof(buf));
93
94 buf[0] = 0x05; // Report ID
95 buf[1]= 0xFF;
96
97 buf[4] = output->smallRumble; // Small Rumble
98 buf[5] = output->bigRumble; // Big rumble
99
100 buf[6] = output->r; // Red
101 buf[7] = output->g; // Green
102 buf[8] = output->b; // Blue
103
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)
106
107 output->reportChanged = false;
108
109 // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
110
111 pUsb->outTransfer(bAddress, epInfo[ hidInterfaces[0].epIndex[epInterruptOutIndex] ].epAddr, sizeof(buf), buf);
112 };
113 /**@}*/
114
115 /** @name USBDeviceConfig implementation */
116 /**
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.
121 */
122 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
123 return (vid == PS4_VID && pid == PS4_PID);
124 };
125 /**@}*/
126
127 private:
128 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
129 };
130 #endif
Imprint / Impressum