USB Host Shield 2.0
PS4BT.h
Go to the documentation of this file.
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 
28 class PS4BT : public BTHID, public PS4Parser {
29 public:
36  PS4BT(BTD *p, bool pair = false, const char *pin = "0000") :
37  BTHID(p, pair, pin) {
39  };
40 
45  bool connected() {
46  return BTHID::connected;
47  };
48 
49 protected:
56  virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
57  PS4Parser::Parse(len, buf);
58  };
59 
65  virtual void OnInitBTHID() {
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 
75  virtual void ResetBTHID() {
77  };
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  };
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
void Reset()
Definition: PS4Parser.cpp:130
Definition: BTD.h:221
uint8_t b
Definition: PS4Parser.h:118
void setLed(uint8_t r, uint8_t g, uint8_t b)
Definition: PS4Parser.h:320
bool connected()
Definition: PS4BT.h:45
bool connected
Definition: BTHID.h:88
uint8_t flashOn
Definition: PS4Parser.h:119
uint8_t control_scid[2]
Definition: BTHID.h:139
uint8_t g
Definition: PS4Parser.h:118
bool reportChanged
Definition: PS4Parser.h:120
void Parse(uint8_t len, uint8_t *buf)
Definition: PS4Parser.cpp:76
uint8_t r
Definition: PS4Parser.h:118
virtual void ParseBTHIDData(uint8_t len, uint8_t *buf)
Definition: PS4BT.h:56
uint8_t bigRumble
Definition: PS4Parser.h:117
uint8_t flashOff
Definition: PS4Parser.h:119
void(* pFuncOnInit)(void)
Definition: BTD.h:643
uint8_t smallRumble
Definition: PS4Parser.h:117
virtual void sendOutputReport(PS4Output *output)
Definition: PS4BT.h:81
BTD * pBtd
Definition: BTD.h:646
virtual void ResetBTHID()
Definition: PS4BT.h:75
PS4BT(BTD *p, bool pair=false, const char *pin="0000")
Definition: PS4BT.h:36
uint16_t hci_handle
Definition: BTD.h:649
virtual void OnInitBTHID()
Definition: PS4BT.h:65
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1447
Definition: BTHID.h:29
void pair(void)
Definition: BTHID.h:91
Definition: PS4BT.h:28