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  setLed(Blue); // Only call this is a user function has not been set
70  };
71 
73  virtual void ResetBTHID() {
75  };
79  virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
80  uint8_t buf[79];
81  memset(buf, 0, sizeof(buf));
82 
83  buf[0] = 0x52; // HID BT Set_report (0x50) | Report Type (Output 0x02)
84  buf[1] = 0x11; // Report ID
85  buf[2] = 0x80;
86  buf[4]= 0xFF;
87 
88  buf[7] = output->smallRumble; // Small Rumble
89  buf[8] = output->bigRumble; // Big rumble
90 
91  buf[9] = output->r; // Red
92  buf[10] = output->g; // Green
93  buf[11] = output->b; // Blue
94 
95  buf[12] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
96  buf[13] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
97 
98  output->reportChanged = false;
99 
100  // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
101 
102  HID_Command(buf, sizeof(buf));
103  };
106 private:
107  void enable_sixaxis() { // Command used to make the PS4 controller send out the entire output report
108  uint8_t buf[2];
109  buf[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
110  buf[1] = 0x02; // Report ID
111 
112  HID_Command(buf, 2);
113  };
114 
115  void HID_Command(uint8_t *data, uint8_t nbytes) {
116  pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]);
117  };
118 };
119 #endif
@ Blue
Definition: AMBXEnums.h:24
Definition: BTD.h:222
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1452
Definition: BTHID.h:29
bool connected
Definition: BTHID.h:88
uint8_t control_scid[2]
Definition: BTHID.h:155
void pair(void)
Definition: BTHID.h:91
void(* pFuncOnInit)(void)
Definition: BTD.h:644
uint16_t hci_handle
Definition: BTD.h:650
BTD * pBtd
Definition: BTD.h:647
Definition: PS4BT.h:28
bool connected()
Definition: PS4BT.h:45
virtual void ParseBTHIDData(uint8_t len, uint8_t *buf)
Definition: PS4BT.h:56
virtual void ResetBTHID()
Definition: PS4BT.h:73
PS4BT(BTD *p, bool pair=false, const char *pin="0000")
Definition: PS4BT.h:36
virtual void OnInitBTHID()
Definition: PS4BT.h:65
virtual void sendOutputReport(PS4Output *output)
Definition: PS4BT.h:79
void Reset()
Definition: PS4Parser.cpp:139
void setLed(uint8_t r, uint8_t g, uint8_t b)
Definition: PS4Parser.h:320
void Parse(uint8_t len, uint8_t *buf)
Definition: PS4Parser.cpp:85
uint8_t b
Definition: PS4Parser.h:118
uint8_t flashOff
Definition: PS4Parser.h:119
uint8_t flashOn
Definition: PS4Parser.h:119
uint8_t r
Definition: PS4Parser.h:118
uint8_t smallRumble
Definition: PS4Parser.h:117
uint8_t bigRumble
Definition: PS4Parser.h:117
uint8_t g
Definition: PS4Parser.h:118
bool reportChanged
Definition: PS4Parser.h:120