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
22 #include "PS4Parser.h"
25 * This class implements support for the PS4 controller via Bluetooth.
26 * It uses the BTHID class for all the Bluetooth communication.
28 class PS4BT : public BTHID, public PS4Parser {
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.
36 PS4BT(BTD *p, bool pair = false, const char *pin = "0000") :
42 * Used to check if a PS4 controller is connected.
43 * @return Returns true if it is connected.
46 return BTHID::connected;
50 /** @name BTHID implementation */
52 * Used to parse Bluetooth HID data.
53 * @param len The length of the incoming data.
54 * @param buf Pointer to the data buffer.
56 virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
57 PS4Parser::Parse(len, buf);
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.
65 virtual void OnInitBTHID() {
67 enable_sixaxis(); // Make the controller send out the entire output report
69 pFuncOnInit(); // Call the user function
74 /** Used to reset the different buffers to there default values */
75 virtual void ResetBTHID() {
80 /** @name PS4Parser implementation */
81 virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
83 memset(buf, 0, sizeof(buf));
85 buf[0] = 0x52; // HID BT Set_report (0x50) | Report Type (Output 0x02)
86 buf[1] = 0x11; // Report ID
90 buf[7] = output->smallRumble; // Small Rumble
91 buf[8] = output->bigRumble; // Big rumble
93 buf[9] = output->r; // Red
94 buf[10] = output->g; // Green
95 buf[11] = output->b; // Blue
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)
100 output->reportChanged = false;
102 // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
104 HID_Command(buf, sizeof(buf));
109 void enable_sixaxis() { // Command used to make the PS4 controller send out the entire output report
111 buf[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
112 buf[1] = 0x02; // Report ID
117 void HID_Command(uint8_t *data, uint8_t nbytes) {
118 pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]);