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
USB::outTransfer
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:303
PS5Parser::rightTrigger
PS5Trigger rightTrigger
Definition: PS5Parser.h:157
USBHID::bAddress
uint8_t bAddress
Definition: usbhid.h:146
PS5USB::sendOutputReport
virtual void sendOutputReport(PS5Output *output)
Definition: PS5USB.h:93
hiduniversal.h
PS5Output
Definition: PS5Parser.h:139
PS5Output::microphoneLed
uint8_t microphoneLed
Definition: PS5Parser.h:141
PS5Parser::setLed
void setLed(uint8_t r, uint8_t g, uint8_t b)
Definition: PS5Parser.h:339
PS5_VID
#define PS5_VID
Definition: PS5USB.h:27
Red
@ Red
Definition: AMBXEnums.h:22
PS5Output::playerLeds
uint8_t playerLeds
Definition: PS5Parser.h:143
PS5Parser::Parse
void Parse(uint8_t len, uint8_t *buf)
Definition: PS5Parser.cpp:89
PS5USB::VIDPIDOK
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: PS5USB.h:149
HIDComposite::isReady
virtual bool isReady()
Definition: hidcomposite.h:99
PS5USB::OnInitSuccessful
virtual uint8_t OnInitSuccessful()
Definition: PS5USB.h:80
PS5USB::attachOnInit
void attachOnInit(void(*funcOnInit)(void))
Definition: PS5USB.h:57
PS5USB::PS5USB
PS5USB(USB *p)
Definition: PS5USB.h:40
PS5Parser
Definition: PS5Parser.h:149
PS5_PID
#define PS5_PID
Definition: PS5USB.h:28
PS5Output::reportChanged
bool reportChanged
Definition: PS5Parser.h:145
USBHID::pUsb
USB * pUsb
Definition: usbhid.h:145
HIDComposite::epInfo
EpInfo epInfo[totalEndpoints]
Definition: hidcomposite.h:63
PS5Output::bigRumble
uint8_t bigRumble
Definition: PS5Parser.h:140
USB
Definition: UsbCore.h:212
PS5Output::b
uint8_t b
Definition: PS5Parser.h:144
HIDComposite::VID
uint16_t VID
Definition: hidcomposite.h:71
PS5Parser::Reset
void Reset()
Definition: PS5Parser.cpp:149
USBHID::epInterruptOutIndex
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
PS5Output::smallRumble
uint8_t smallRumble
Definition: PS5Parser.h:140
PS5Output::disableLeds
uint8_t disableLeds
Definition: PS5Parser.h:142
PS5USB::connected
bool connected()
Definition: PS5USB.h:49
PS5Output::r
uint8_t r
Definition: PS5Parser.h:144
HIDUniversal
Definition: hiduniversal.h:23
USBHID
Definition: usbhid.h:143
HIDComposite::hidInterfaces
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hidcomposite.h:64
HIDComposite::PID
uint16_t PID
Definition: hidcomposite.h:71
PS5USB
Definition: PS5USB.h:34
PS5Trigger::processTrigger
void processTrigger(uint8_t *buffer)
Apply the trigger data to a PS5 update buffer.
Definition: PS5Trigger.cpp:34
PS5Parser.h
PS5USB::ParseHIDData
virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: PS5USB.h:70
PS5Parser::leftTrigger
PS5Trigger leftTrigger
Definition: PS5Parser.h:154
PS5Output::g
uint8_t g
Definition: PS5Parser.h:144