USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
53  void attachOnInit(void (*funcOnInit)(void)) {
54  pFuncOnInit = funcOnInit;
55  };
56 
57 protected:
64  virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
65  PS4Parser::Parse(len, buf);
66  };
67 
73  virtual void OnInitBTHID() {
75  enable_sixaxis(); // Make the controller send out the entire output report
76  if (pFuncOnInit)
77  pFuncOnInit(); // Call the user function
78  else
79  setLed(Blue);
80  };
81 
83  virtual void ResetBTHID() {
85  };
89  virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
90  uint8_t buf[79];
91  memset(buf, 0, sizeof(buf));
92 
93  buf[0] = 0x52; // HID BT Set_report (0x50) | Report Type (Output 0x02)
94  buf[1] = 0x11; // Report ID
95  buf[2] = 0x80;
96  buf[4]= 0xFF;
97 
98  buf[7] = output->smallRumble; // Small Rumble
99  buf[8] = output->bigRumble; // Big rumble
100 
101  buf[9] = output->r; // Red
102  buf[10] = output->g; // Green
103  buf[11] = output->b; // Blue
104 
105  buf[12] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
106  buf[13] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
107 
108  output->reportChanged = false;
109 
110  // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
111 
112  HID_Command(buf, sizeof(buf));
113  };
116 private:
117  void enable_sixaxis() { // Command used to make the PS4 controller send out the entire output report
118  uint8_t buf[2];
119  buf[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
120  buf[1] = 0x02; // Report ID
121 
122  HID_Command(buf, 2);
123  };
124 
125  void HID_Command(uint8_t *data, uint8_t nbytes) {
126  pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]);
127  };
128 
129  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
130 };
131 #endif
void Reset()
Definition: PS4Parser.h:369
Definition: BTD.h:230
uint8_t b
Definition: PS4Parser.h:118
void setLed(uint8_t r, uint8_t g, uint8_t b)
Definition: PS4Parser.h:333
bool connected()
Definition: PS4BT.h:45
bool connected
Definition: BTHID.h:92
uint8_t flashOn
Definition: PS4Parser.h:119
void attachOnInit(void(*funcOnInit)(void))
Definition: PS4BT.h:53
uint8_t control_scid[2]
Definition: BTHID.h:136
uint8_t g
Definition: PS4Parser.h:118
bool reportChanged
Definition: PS4Parser.h:120
void Parse(uint8_t len, uint8_t *buf)
Definition: PS4Parser.cpp:64
uint8_t r
Definition: PS4Parser.h:118
virtual void ParseBTHIDData(uint8_t len, uint8_t *buf)
Definition: PS4BT.h:64
uint8_t bigRumble
Definition: PS4Parser.h:117
uint8_t flashOff
Definition: PS4Parser.h:119
BTD * pBtd
Definition: BTHID.h:129
uint8_t smallRumble
Definition: PS4Parser.h:117
uint16_t hci_handle
Definition: BTHID.h:132
virtual void sendOutputReport(PS4Output *output)
Definition: PS4BT.h:89
virtual void ResetBTHID()
Definition: PS4BT.h:83
PS4BT(BTD *p, bool pair=false, const char *pin="0000")
Definition: PS4BT.h:36
virtual void OnInitBTHID()
Definition: PS4BT.h:73
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1190
Definition: BTHID.h:29
void pair(void)
Definition: BTHID.h:95
Definition: PS4BT.h:28