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 #define PS4_PID_SLIM 0x09CC // PS4 Slim Controller
27 
32 class PS4USB : public HIDUniversal, public PS4Parser {
33 public:
38  PS4USB(USB *p) :
39  HIDUniversal(p) {
41  };
42 
47  bool connected() {
49  };
50 
55  void attachOnInit(void (*funcOnInit)(void)) {
56  pFuncOnInit = funcOnInit;
57  };
58 
59 protected:
68  virtual void ParseHIDData(USBHID *hid __attribute__((unused)), bool is_rpt_id __attribute__((unused)), uint8_t len, uint8_t *buf) {
70  PS4Parser::Parse(len, buf);
71  };
72 
78  virtual uint8_t OnInitSuccessful() {
81  if (pFuncOnInit)
82  pFuncOnInit(); // Call the user function
83  else
84  setLed(Blue);
85  };
86  return 0;
87  };
91  virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
92  uint8_t buf[32];
93  memset(buf, 0, sizeof(buf));
94 
95  buf[0] = 0x05; // Report ID
96  buf[1]= 0xFF;
97 
98  buf[4] = output->smallRumble; // Small Rumble
99  buf[5] = output->bigRumble; // Big rumble
100 
101  buf[6] = output->r; // Red
102  buf[7] = output->g; // Green
103  buf[8] = output->b; // Blue
104 
105  buf[9] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
106  buf[10] = 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  pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, sizeof(buf), buf);
113  };
123  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
124  return (vid == PS4_VID && (pid == PS4_PID || HIDUniversal::PID == PS4_PID_SLIM));
125  };
128 private:
129  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
130 };
131 #endif
@ Blue
Definition: AMBXEnums.h:24
#define PS4_PID
Definition: PS4USB.h:25
#define PS4_PID_SLIM
Definition: PS4USB.h:26
#define PS4_VID
Definition: PS4USB.h:24
uint16_t PID
Definition: hidcomposite.h:71
virtual bool isReady()
Definition: hidcomposite.h:99
EpInfo epInfo[totalEndpoints]
Definition: hidcomposite.h:63
uint16_t VID
Definition: hidcomposite.h:71
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
Definition: PS4USB.h:32
PS4USB(USB *p)
Definition: PS4USB.h:38
bool connected()
Definition: PS4USB.h:47
virtual uint8_t OnInitSuccessful()
Definition: PS4USB.h:78
virtual void sendOutputReport(PS4Output *output)
Definition: PS4USB.h:91
virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: PS4USB.h:68
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: PS4USB.h:123
void attachOnInit(void(*funcOnInit)(void))
Definition: PS4USB.h:55
Definition: usbhid.h:143
uint8_t bAddress
Definition: usbhid.h:146
USB * pUsb
Definition: usbhid.h:145
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
Definition: UsbCore.h:212
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:303
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