]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/PS4BT.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[qmk_firmware.git] / protocol / usb_hid / USB_Host_Shield_2.0 / PS4BT.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 _ps4bt_h_
19 #define _ps4bt_h_
20
21 #include "BTHID.h"
22 #include "PS4Parser.h"
23
24 /**
25  * This class implements support for the PS4 controller via Bluetooth.
26  * It uses the BTHID class for all the Bluetooth communication.
27  */
28 class PS4BT : public BTHID, public PS4Parser {
29 public:
30         /**
31          * Constructor for the PS4BT class.
32          * @param  p     Pointer to the BTD class instance.
33          * @param  pair  Set this to true in order to pair with the device. If the argument is omitted then it will not pair with it. One can use ::PAIR to set it to true.
34          * @param  pin   Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
35          */
36         PS4BT(BTD *p, bool pair = false, const char *pin = "0000") :
37         BTHID(p, pair, pin) {
38                 PS4Parser::Reset();
39         };
40
41         /**
42          * Used to check if a PS4 controller is connected.
43          * @return Returns true if it is connected.
44          */
45         bool connected() {
46                 return BTHID::connected;
47         };
48
49 protected:
50         /** @name BTHID implementation */
51         /**
52          * Used to parse Bluetooth HID data.
53          * @param len The length of the incoming data.
54          * @param buf Pointer to the data buffer.
55          */
56         virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
57                 PS4Parser::Parse(len, buf);
58         };
59
60         /**
61          * Called when a device is successfully initialized.
62          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
63          * This is useful for instance if you want to set the LEDs in a specific way.
64          */
65         virtual void OnInitBTHID() {
66                 PS4Parser::Reset();
67                 enable_sixaxis(); // Make the controller send out the entire output report
68                 if (pFuncOnInit)
69                         pFuncOnInit(); // Call the user function
70                 else
71                         setLed(Blue);
72         };
73
74         /** Used to reset the different buffers to there default values */
75         virtual void ResetBTHID() {
76                 PS4Parser::Reset();
77         };
78         /**@}*/
79
80         /** @name PS4Parser implementation */
81         virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
82                 uint8_t buf[79];
83                 memset(buf, 0, sizeof(buf));
84
85                 buf[0] = 0x52; // HID BT Set_report (0x50) | Report Type (Output 0x02)
86                 buf[1] = 0x11; // Report ID
87                 buf[2] = 0x80;
88                 buf[4]= 0xFF;
89
90                 buf[7] = output->smallRumble; // Small Rumble
91                 buf[8] = output->bigRumble; // Big rumble
92
93                 buf[9] = output->r; // Red
94                 buf[10] = output->g; // Green
95                 buf[11] = output->b; // Blue
96
97                 buf[12] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
98                 buf[13] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
99
100                 output->reportChanged = false;
101
102                 // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
103
104                 HID_Command(buf, sizeof(buf));
105         };
106         /**@}*/
107
108 private:
109         void enable_sixaxis() { // Command used to make the PS4 controller send out the entire output report
110                 uint8_t buf[2];
111                 buf[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
112                 buf[1] = 0x02; // Report ID
113
114                 HID_Command(buf, 2);
115         };
116
117         void HID_Command(uint8_t *data, uint8_t nbytes) {
118                 pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]);
119         };
120 };
121 #endif