USB Host Shield 2.0
PS4USB.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 _ps4usb_h_
19 #define _ps4usb_h_
20 
21 #include "hiduniversal.h"
22 #include "PS4Parser.h"
23 
24 #define PS4_VID 0x054C // Sony Corporation
25 #define PS4_PID 0x05C4 // PS4 Controller
26 
31 class PS4USB : public HIDUniversal, public PS4Parser {
32 public:
37  PS4USB(USB *p) :
38  HIDUniversal(p) {
40  };
41 
46  bool connected() {
48  };
49 
54  void attachOnInit(void (*funcOnInit)(void)) {
55  pFuncOnInit = funcOnInit;
56  };
57 
58 protected:
67  virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
69  PS4Parser::Parse(len, buf);
70  };
71 
77  virtual uint8_t OnInitSuccessful() {
80  if (pFuncOnInit)
81  pFuncOnInit(); // Call the user function
82  else
83  setLed(Blue);
84  };
85  return 0;
86  };
90  virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
91  uint8_t buf[32];
92  memset(buf, 0, sizeof(buf));
93 
94  buf[0] = 0x05; // Report ID
95  buf[1]= 0xFF;
96 
97  buf[4] = output->smallRumble; // Small Rumble
98  buf[5] = output->bigRumble; // Big rumble
99 
100  buf[6] = output->r; // Red
101  buf[7] = output->g; // Green
102  buf[8] = output->b; // Blue
103 
104  buf[9] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
105  buf[10] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
106 
107  output->reportChanged = false;
108 
109  // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
110 
111  pUsb->outTransfer(bAddress, epInfo[ hidInterfaces[0].epIndex[epInterruptOutIndex] ].epAddr, sizeof(buf), buf);
112  };
122  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
123  return (vid == PS4_VID && pid == PS4_PID);
124  };
127 private:
128  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
129 };
130 #endif
uint16_t PID
Definition: hiduniversal.h:69
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: PS4USB.h:122
Definition: usbhid.h:143
void Reset()
Definition: PS4Parser.cpp:130
USB * pUsb
Definition: usbhid.h:145
void attachOnInit(void(*funcOnInit)(void))
Definition: PS4USB.h:54
virtual bool isReady()
Definition: hiduniversal.h:97
virtual uint8_t OnInitSuccessful()
Definition: PS4USB.h:77
uint8_t b
Definition: PS4Parser.h:118
void setLed(uint8_t r, uint8_t g, uint8_t b)
Definition: PS4Parser.h:321
Definition: PS4USB.h:31
uint8_t flashOn
Definition: PS4Parser.h:119
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hiduniversal.h:65
uint16_t VID
Definition: hiduniversal.h:69
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 outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:293
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
uint8_t bAddress
Definition: usbhid.h:146
uint8_t r
Definition: PS4Parser.h:118
uint8_t bigRumble
Definition: PS4Parser.h:117
bool connected()
Definition: PS4USB.h:46
uint8_t flashOff
Definition: PS4Parser.h:119
uint8_t smallRumble
Definition: PS4Parser.h:117
#define PS4_VID
Definition: PS4USB.h:24
#define PS4_PID
Definition: PS4USB.h:25
virtual void sendOutputReport(PS4Output *output)
Definition: PS4USB.h:90
PS4USB(USB *p)
Definition: PS4USB.h:37
Definition: UsbCore.h:197
EpInfo epInfo[totalEndpoints]
Definition: hiduniversal.h:64
virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: PS4USB.h:67