USB Host Shield 2.0
PS5USB.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Kristian Sloth Lauszus. 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 Sloth Lauszus
14  Web : https://lauszus.com
15  e-mail : lauszus@gmail.com
16 
17  Thanks to Joseph Duchesne for the initial port. Data structure mapping partially based
18  on values from Ludwig Füchsl's https://github.com/Ohjurot/DualSense-Windows
19  */
20 
21 #ifndef _ps5usb_h_
22 #define _ps5usb_h_
23 
24 #include "hiduniversal.h"
25 #include "PS5Parser.h"
26 
27 #define PS5_VID 0x054C // Sony Corporation
28 #define PS5_PID 0x0CE6 // PS5 Controller
29 
34 class PS5USB : public HIDUniversal, public PS5Parser {
35 public:
40  PS5USB(USB *p) :
41  HIDUniversal(p) {
43  };
44 
49  bool connected() {
51  };
52 
57  void attachOnInit(void (*funcOnInit)(void)) {
58  pFuncOnInit = funcOnInit;
59  };
60 
61 protected:
70  virtual void ParseHIDData(USBHID *hid __attribute__((unused)), bool is_rpt_id __attribute__((unused)), uint8_t len, uint8_t *buf) {
72  PS5Parser::Parse(len, buf);
73  };
74 
80  virtual uint8_t OnInitSuccessful() {
83  if (pFuncOnInit)
84  pFuncOnInit(); // Call the user function
85  else
86  setLed(Red); // Set the LED to red, so it is consistent with the PS5BT driver
87  };
88  return 0;
89  };
93  virtual void sendOutputReport(PS5Output *output) { // Source: https://github.com/chrippa/ds4drv
94  // PS4 Source: https://github.com/chrippa/ds4drv
95  // PS5 values from https://www.reddit.com/r/gamedev/comments/jumvi5/dualsense_haptics_leds_and_more_hid_output_report/,
96  // Ludwig Füchsl's https://github.com/Ohjurot/DualSense-Windows and
97  // the series of patches found here: https://patchwork.kernel.org/project/linux-input/cover/20201219062336.72568-1-roderick@gaikai.com/
98  uint8_t buf[1 /* report id */ + 47 /* common */];
99  memset(buf, 0, sizeof(buf));
100 
101  buf[0x00] = 0x02; // Report ID
102 
103  buf[0x01] = 0xFF; // feature flags 1
104  buf[0x02]= 0xF7; // feature flags 2
105 
106  buf[0x03] = output->smallRumble; // Small Rumble
107  buf[0x04] = output->bigRumble; // Big rumble
108 
109  // 5-7 headphone, speaker, mic volume, audio flags
110 
111  buf[0x09] = (uint8_t)output->microphoneLed;
112 
113  // 0x0A mute flags
114 
115  // Adaptive Triggers: 0x0B-0x14 right, 0x15 unknown, 0x16-0x1F left
116  rightTrigger.processTrigger(&buf[0x0B]); // right
117  leftTrigger.processTrigger(&buf[0x16]); // left
118 
119  // 0x20-0x24 unknown
120  // 0x25 trigger motor effect strengths
121  // 0x26 speaker volume
122 
123  // player LEDs
124  buf[0x27] = 0x03; // led brightness, pulse
125  buf[0x2A] = output->disableLeds ? 0x01 : 0x2; // led pulse option
126  // buf[0x2B] LED brightness, 0 = full, 1= medium, 2 = low
127  buf[0x2C] = output->playerLeds; // 5 white player LEDs
128 
129  // lightbar
130  buf[0x2D] = output->r; // Red
131  buf[0x2E] = output->g; // Green
132  buf[0x2F] = output->b; // Blue
133 
134  output->reportChanged = false;
135 
136  // There is no need to calculate a crc32 when the controller is connected via USB
137 
138  pUsb->outTransfer(bAddress, epInfo[ hidInterfaces[0].epIndex[epInterruptOutIndex] ].epAddr, sizeof(buf), buf);
139  };
149  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
150  return (vid == PS5_VID && pid == PS5_PID);
151  };
154 private:
155  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
156 };
157 #endif
@ Red
Definition: AMBXEnums.h:22
#define PS5_PID
Definition: PS5USB.h:28
#define PS5_VID
Definition: PS5USB.h:27
uint16_t PID
Definition: hidcomposite.h:71
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hidcomposite.h:64
virtual bool isReady()
Definition: hidcomposite.h:99
EpInfo epInfo[totalEndpoints]
Definition: hidcomposite.h:63
uint16_t VID
Definition: hidcomposite.h:71
void setLed(uint8_t r, uint8_t g, uint8_t b)
Definition: PS5Parser.h:339
void Reset()
Definition: PS5Parser.cpp:149
void Parse(uint8_t len, uint8_t *buf)
Definition: PS5Parser.cpp:89
PS5Trigger rightTrigger
Definition: PS5Parser.h:157
PS5Trigger leftTrigger
Definition: PS5Parser.h:154
void processTrigger(uint8_t *buffer)
Apply the trigger data to a PS5 update buffer.
Definition: PS5Trigger.cpp:34
Definition: PS5USB.h:34
PS5USB(USB *p)
Definition: PS5USB.h:40
virtual void sendOutputReport(PS5Output *output)
Definition: PS5USB.h:93
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: PS5USB.h:149
bool connected()
Definition: PS5USB.h:49
virtual uint8_t OnInitSuccessful()
Definition: PS5USB.h:80
void attachOnInit(void(*funcOnInit)(void))
Definition: PS5USB.h:57
virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: PS5USB.h:70
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 smallRumble
Definition: PS5Parser.h:140
uint8_t b
Definition: PS5Parser.h:144
uint8_t microphoneLed
Definition: PS5Parser.h:141
uint8_t r
Definition: PS5Parser.h:144
bool reportChanged
Definition: PS5Parser.h:145
uint8_t playerLeds
Definition: PS5Parser.h:143
uint8_t disableLeds
Definition: PS5Parser.h:142
uint8_t bigRumble
Definition: PS5Parser.h:140
uint8_t g
Definition: PS5Parser.h:144