Add support for Sony DualSense closes #2013

This commit is contained in:
Flora Aubry
2022-07-08 23:31:07 +00:00
committed by Adam Honse
parent da6e790ee6
commit 3168212214
10 changed files with 384 additions and 12 deletions

View File

@@ -0,0 +1,107 @@
/*-----------------------------------------*\
| RGBController_SonyDS4.cpp |
| |
| Controller for Sony Dualshock 4 |
| |
| Pol Rius (alpemwarrior) 24/09/2020 |
\*-----------------------------------------*/
#include <iostream>
#include "RGBController.h"
#include "RGBController_SonyDS4.h"
/**------------------------------------------------------------------*\
@name Sony Dual Shock 4 controller
@category Gamepad
@type USB
@save :x:
@direct :white_check_mark:
@effects :x:
@detectors DetectSonyDS4Controllers
@comment
\*-------------------------------------------------------------------*/
RGBController_SonyDS4::RGBController_SonyDS4(SonyDS4Controller* dualshock)
{
this->dualshock = dualshock;
name = "Sony DualShock 4";
vendor = "Sony";
type = DEVICE_TYPE_GAMEPAD;
description = "Sony DualShock 4 Device";
location = dualshock->GetLocation();
serial = dualshock->GetSerialString();
mode Direct;
Direct.value = 0;
Direct.name = "Direct";
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR;
Direct.color_mode = MODE_COLORS_PER_LED;
modes.push_back(Direct);
SetupZones();
}
RGBController_SonyDS4::~RGBController_SonyDS4()
{
delete dualshock;
}
void RGBController_SonyDS4::SetupZones()
{
/*---------------------------------------------------------*\
| This device only has one LED, so create a single zone and |
| LED for it |
\*---------------------------------------------------------*/
zone* new_zone = new zone();
led* new_led = new led();
new_zone->name = "Controller Zone";
new_zone->type = ZONE_TYPE_SINGLE;
new_zone->leds_min = 1;
new_zone->leds_max = 1;
new_zone->leds_count = 1;
new_zone->matrix_map = NULL;
new_led->name = "Controller LED";
/*---------------------------------------------------------*\
| Push the zone and LED on to device vectors |
\*---------------------------------------------------------*/
leds.push_back(*new_led);
zones.push_back(*new_zone);
SetupColors();
}
void RGBController_SonyDS4::ResizeZone(int /*zone*/, int /*new_size*/)
{
/*---------------------------------------------------------*\
| This device does not support resizing zones |
\*---------------------------------------------------------*/
}
void RGBController_SonyDS4::DeviceUpdateLEDs()
{
unsigned char red = char(RGBGetRValue(colors[0]));
unsigned char green = char(RGBGetGValue(colors[0]));
unsigned char blue = char(RGBGetBValue(colors[0]));
dualshock->SetColors(red, green, blue);
}
void RGBController_SonyDS4::UpdateZoneLEDs(int /*zone*/)
{
DeviceUpdateLEDs();
}
void RGBController_SonyDS4::UpdateSingleLED(int /*led*/)
{
DeviceUpdateLEDs();
}
void RGBController_SonyDS4::SetCustomMode()
{
}
void RGBController_SonyDS4::DeviceUpdateMode()
{
}

View File

@@ -0,0 +1,31 @@
/*-----------------------------------------*\
| RGBController_SonyDS4.h |
| |
| Controller for Sony Dualshock 4 |
| |
| Pol Rius (alpemwarrior) 24/09/2020 |
\*-----------------------------------------*/
#include "RGBController.h"
#include "SonyDS4Controller.h"
class RGBController_SonyDS4 : public RGBController
{
public:
RGBController_SonyDS4(SonyDS4Controller* dualshock);
~RGBController_SonyDS4();
void SetupZones();
void ResizeZone(int zone, int new_size);
void DeviceUpdateLEDs();
void UpdateZoneLEDs(int zone);
void UpdateSingleLED(int led);
void SetCustomMode();
void DeviceUpdateMode();
private:
SonyDS4Controller* dualshock;
};

View File

@@ -0,0 +1,143 @@
/*-----------------------------------------*\
| RGBController_SonyDualSense.h |
| |
| Controller for Sony DualSense |
| |
| by flora 01/07/2022 |
\*-----------------------------------------*/
#include <iostream>
#include "RGBController_SonyDualSense.h"
/**------------------------------------------------------------------*\
@name Sony Dual Sense controller
@category Gamepad
@type USB
@save :x:
@direct :white_check_mark:
@effects :x:
@detectors DetectSonyDualSenseControllers
@comment
\*-------------------------------------------------------------------*/
RGBController_SonyDualSense::RGBController_SonyDualSense(SonyDualSenseController* controller_ptr)
{
controller = controller_ptr;
name = "Sony DualSense";
if(controller->IsBluetooth())
{
name.append(" (BT)");
}
vendor = "Sony";
type = DEVICE_TYPE_GAMEPAD;
description = "Sony DualSense Device";
location = controller->GetLocation();
serial = controller->GetSerialString();
mode Direct;
Direct.value = SONY_DUALSENSE_DIRECT_MODE_VALUE;
Direct.name = "Direct";
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR | MODE_FLAG_HAS_BRIGHTNESS;
Direct.color_mode = MODE_COLORS_PER_LED;
Direct.brightness_min = SONY_DUALSENSE_BRIGHTNESS_MIN;
Direct.brightness_max = SONY_DUALSENSE_BRIGHTNESS_MAX;
Direct.brightness = SONY_DUALSENSE_DEFAULT_BRIGHTNESS;
modes.push_back(Direct);
mode Micoff;
Micoff.value = SONY_DUALSENSE_MIC_OFF_MODE_VALUE;
Micoff.name = "Mic Off (Direct)";
Micoff.flags = MODE_FLAG_HAS_PER_LED_COLOR | MODE_FLAG_HAS_BRIGHTNESS;
Micoff.color_mode = MODE_COLORS_PER_LED;
Micoff.brightness_min = SONY_DUALSENSE_BRIGHTNESS_MIN;
Micoff.brightness_max = SONY_DUALSENSE_BRIGHTNESS_MAX;
Micoff.brightness = SONY_DUALSENSE_DEFAULT_BRIGHTNESS;
modes.push_back(Micoff);
mode Micpulse;
Micpulse.value = SONY_DUALSENSE_MIC_PULSE_MODE_VALUE;
Micpulse.name = "Mic Pulse (Direct)";
Micpulse.flags = MODE_FLAG_HAS_PER_LED_COLOR | MODE_FLAG_HAS_BRIGHTNESS;
Micpulse.color_mode = MODE_COLORS_PER_LED;
Micpulse.brightness_min = SONY_DUALSENSE_BRIGHTNESS_MIN;
Micpulse.brightness_max = SONY_DUALSENSE_BRIGHTNESS_MAX;
Micpulse.brightness = SONY_DUALSENSE_DEFAULT_BRIGHTNESS;
modes.push_back(Micpulse);
SetupZones();
}
RGBController_SonyDualSense::~RGBController_SonyDualSense()
{
delete controller;
}
void RGBController_SonyDualSense::SetupZones()
{
/*---------------------------------------------------------*\
| This device only has one LED, so create a single zone and |
| LED for it |
\*---------------------------------------------------------*/
zone lightbar;
lightbar.name = "Lightbar";
lightbar.type = ZONE_TYPE_SINGLE;
lightbar.leds_min = SONY_DUALSENSE_LIGHTBAR_LED_COUNT;
lightbar.leds_max = SONY_DUALSENSE_LIGHTBAR_LED_COUNT;
lightbar.leds_count = SONY_DUALSENSE_LIGHTBAR_LED_COUNT;
lightbar.matrix_map = NULL;
zones.push_back(lightbar);
zone playerleds;
playerleds.name = "Player LEDs";
playerleds.type = ZONE_TYPE_LINEAR;
playerleds.leds_min = SONY_DUALSENSE_PLAYER_LED_COUNT;
playerleds.leds_max = SONY_DUALSENSE_PLAYER_LED_COUNT;
playerleds.leds_count = SONY_DUALSENSE_PLAYER_LED_COUNT;
playerleds.matrix_map = NULL;
zones.push_back(playerleds);
leds.resize(SONY_DUALSENSE_LIGHTBAR_LED_COUNT + SONY_DUALSENSE_PLAYER_LED_COUNT);
leds[0].name = "LED 1";
for(unsigned int i = 0 ; i < SONY_DUALSENSE_PLAYER_LED_COUNT; i++)
{
leds[i + 1].name = "Player " + std::to_string(i + 1);
}
SetupColors();
}
void RGBController_SonyDualSense::ResizeZone(int /*zone*/, int /*new_size*/)
{
/*---------------------------------------------------------*\
| This device does not support resizing zones |
\*---------------------------------------------------------*/
}
void RGBController_SonyDualSense::DeviceUpdateLEDs()
{
controller->SetColors(colors, modes[active_mode].brightness, modes[active_mode].value);
}
void RGBController_SonyDualSense::UpdateZoneLEDs(int /*zone*/)
{
DeviceUpdateLEDs();
}
void RGBController_SonyDualSense::UpdateSingleLED(int /*led*/)
{
DeviceUpdateLEDs();
}
void RGBController_SonyDualSense::SetCustomMode()
{
active_mode = 0;
}
void RGBController_SonyDualSense::DeviceUpdateMode()
{
}

View File

@@ -0,0 +1,29 @@
/*-----------------------------------------*\
| RGBController_SonyDualSense.h |
| |
| Controller for Sony DualSense |
| |
| by flora 01/07/2022 |
\*-----------------------------------------*/
#include "RGBController.h"
#include "SonyDualSenseController.h"
class RGBController_SonyDualSense : public RGBController
{
public:
RGBController_SonyDualSense(SonyDualSenseController* controller_ptr);
~RGBController_SonyDualSense();
void SetupZones();
void ResizeZone(int zone, int new_size);
void DeviceUpdateLEDs();
void UpdateZoneLEDs(int zone);
void UpdateSingleLED(int led);
void SetCustomMode();
void DeviceUpdateMode();
private:
SonyDualSenseController* controller;
};

View File

@@ -0,0 +1,124 @@
/*-----------------------------------------*\
| SonyDS4Controller.cpp |
| |
| Controller for Sony Dualshock 4 |
| |
| Pol Rius (alpemwarrior) 24/09/2020 |
\*-----------------------------------------*/
#include <CRC.h>
#include <hidapi/hidapi.h>
#include "SonyDS4Controller.h"
SonyDS4Controller::SonyDS4Controller(hid_device * device_handle, const char * device_path)
{
this->device_handle = device_handle;
unsigned char readBuffer[64];
unsigned char reportBuffer[64];
reportBuffer[0] = 0x02;
hid_get_feature_report(device_handle, reportBuffer, 64);
for (int i = 0; i < 5; i++)
{
hid_read(device_handle, readBuffer, 64);
if (readBuffer[0] == 17)
{
is_bluetooth = true;
break;
}
}
location = device_path;
}
SonyDS4Controller::~SonyDS4Controller()
{
hid_close(device_handle);
}
std::string SonyDS4Controller::GetLocation()
{
return("HID: " + location);
}
std::string SonyDS4Controller::GetSerialString()
{
wchar_t serial_string[128];
int ret = hid_get_serial_number_string(device_handle, serial_string, 128);
if(ret != 0)
{
return("");
}
std::wstring return_wstring = serial_string;
std::string return_string(return_wstring.begin(), return_wstring.end());
return(return_string);
}
void SonyDS4Controller::SetColors(unsigned char red, unsigned char green, unsigned char blue)
{
if(is_bluetooth)
{
sendReportBT(red, green, blue);
}
else
{
sendReportUSB(red, green, blue);
}
}
void SonyDS4Controller::sendReportBT(unsigned char red, unsigned char green, unsigned char blue)
{
unsigned char buffer[79] =
{
0xa2, 0x11, 0xC0, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, red, green, blue, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
uint32_t crc = CRCPP::CRC::Calculate(buffer, 75, CRCPP::CRC::CRC_32());
unsigned char outbuffer[78];
/*-------------------------------------------------*\
| The report has to be signed with the byte 0xa2. |
| However, hidapi already adds 0xa2 to the report, |
| so we need to remove it from the buffer. |
\*-------------------------------------------------*/
for(unsigned int i = 1; i < 79; i++)
{
outbuffer[i - 1] = buffer[i];
}
/*-------------------------------------------------*\
| Add the crc32 to the end of the buffer |
\*-------------------------------------------------*/
outbuffer[74] = (0x000000FF & crc);
outbuffer[75] = (0x0000FF00 & crc) >> 8;
outbuffer[76] = (0x00FF0000 & crc) >> 16;
outbuffer[77] = (0xFF000000 & crc) >> 24;
hid_write(device_handle, outbuffer, 78);
}
void SonyDS4Controller::sendReportUSB(unsigned char red, unsigned char green, unsigned char blue)
{
uint8_t buffer[11] =
{
0x05,
0x07,
0x00,
0x00,
0x00,
0x00,
red,
green,
blue,
0x00,
0x00
};
hid_write(device_handle, buffer, 11);
}

View File

@@ -0,0 +1,31 @@
/*-----------------------------------------*\
| SonyDS4Controller.h |
| |
| Controller for Sony Dualshock 4 |
| |
| Pol Rius (alpemwarrior) 24/09/2020 |
\*-----------------------------------------*/
#include <hidapi/hidapi.h>
#include <string>
class SonyDS4Controller
{
public:
SonyDS4Controller(hid_device * device_handle, const char * device_path);
~SonyDS4Controller();
std::string GetLocation();
std::string GetSerialString();
void SetColors(unsigned char red, unsigned char green, unsigned char blue);
private:
hid_device* device_handle;
bool is_bluetooth = false;
std::string location;
void sendReportUSB(unsigned char red, unsigned char green, unsigned char blue);
void sendReportBT(unsigned char red, unsigned char green, unsigned char blue);
};

View File

@@ -0,0 +1,133 @@
/*-----------------------------------------*\
| RGBController_SonyDualSense.h |
| |
| Controller for Sony DualSense |
| |
| by flora 01/07/2022 |
\*-----------------------------------------*/
#include <CRC.h>
#include <cstring>
#include <hidapi/hidapi.h>
#include "SonyDualSenseController.h"
SonyDualSenseController::SonyDualSenseController(hid_device * device_handle, const char * device_path)
{
dev = device_handle;
location = device_path;
unsigned char usb_buf[SONY_DUALSENSE_BT_PACKET_SIZE];
memset(usb_buf, 0x00, SONY_DUALSENSE_BT_PACKET_SIZE);
usb_buf[0] = 0x31;
int bytes = hid_write(dev, usb_buf, SONY_DUALSENSE_BT_PACKET_SIZE);
is_bluetooth = bytes > 0;
}
SonyDualSenseController::~SonyDualSenseController()
{
hid_close(dev);
}
std::string SonyDualSenseController::GetLocation()
{
return("HID: " + location);
}
std::string SonyDualSenseController::GetSerialString()
{
wchar_t serial_string[128];
int ret = hid_get_serial_number_string(dev, serial_string, 128);
if(ret != 0)
{
return("");
}
std::wstring return_wstring = serial_string;
std::string return_string(return_wstring.begin(), return_wstring.end());
return(return_string);
}
void SonyDualSenseController::SetColors(std::vector<RGBColor> colors, unsigned char brightness, unsigned char mode_value)
{
if(is_bluetooth)
{
unsigned char buffer[SONY_DUALSENSE_BT_PACKET_SIZE + 1];
memset(buffer, 0x00, SONY_DUALSENSE_BT_PACKET_SIZE + 1);
buffer[0] = 0xA2;
buffer[1] = 0x31;
buffer[2] = 0x02;
buffer[3] = 0x0F;
buffer[4] = 0x55;
buffer[11] = mode_value;
buffer[41] = 0xFF; // Must be > 0x00 to control birghtness
buffer[44] = 0x02; // bypass default blue color when connected to bluetooth
buffer[45] = 0x02 - brightness;
buffer[46] = 0x20
+ ((colors[1] > 0) )
+ ((colors[2] > 0) << 1)
+ ((colors[3] > 0) << 2)
+ ((colors[4] > 0) << 3)
+ ((colors[5] > 0) << 4);
buffer[47] = RGBGetRValue(colors[0]);
buffer[48] = RGBGetGValue(colors[0]);
buffer[49] = RGBGetBValue(colors[0]);
uint32_t crc = CRCPP::CRC::Calculate(buffer, SONY_DUALSENSE_BT_PACKET_SIZE - 3, CRCPP::CRC::CRC_32());
unsigned char outbuffer[SONY_DUALSENSE_BT_PACKET_SIZE];
/*-------------------------------------------------*\
| The report has to be signed with the byte 0xa2. |
| However, hidapi already adds 0xa2 to the report, |
| so we need to remove it from the buffer. |
\*-------------------------------------------------*/
for(unsigned int i = 1; i < SONY_DUALSENSE_BT_PACKET_SIZE + 1; i++)
{
outbuffer[i - 1] = buffer[i];
}
/*-------------------------------------------------*\
| Add the crc32 to the end of the buffer |
\*-------------------------------------------------*/
outbuffer[74] = (0x000000FF & crc);
outbuffer[75] = (0x0000FF00 & crc) >> 8;
outbuffer[76] = (0x00FF0000 & crc) >> 16;
outbuffer[77] = (0xFF000000 & crc) >> 24;
hid_write(dev, outbuffer, SONY_DUALSENSE_BT_PACKET_SIZE);
}
else
{
unsigned char usb_buf[SONY_DUALSENSE_USB_PACKET_SIZE];
memset(usb_buf, 0x00, SONY_DUALSENSE_USB_PACKET_SIZE);
usb_buf[0] = 0x02;
usb_buf[1] = 0x0F;
usb_buf[2] = 0x55;
usb_buf[9] = mode_value;
usb_buf[39] = 0xFF; // Must be > 0x00 to control birghtness
usb_buf[43] = 0x02 - brightness;
usb_buf[44] = 0x20
+ ((colors[1] > 0) )
+ ((colors[2] > 0) << 1)
+ ((colors[3] > 0) << 2)
+ ((colors[4] > 0) << 3)
+ ((colors[5] > 0) << 4);
usb_buf[45] = RGBGetRValue(colors[0]);
usb_buf[46] = RGBGetGValue(colors[0]);
usb_buf[47] = RGBGetBValue(colors[0]);
hid_write(dev, usb_buf, SONY_DUALSENSE_USB_PACKET_SIZE);
}
}
bool SonyDualSenseController::IsBluetooth()
{
return(is_bluetooth);
}

View File

@@ -0,0 +1,47 @@
/*-----------------------------------------*\
| RGBController_SonyDualSense.h |
| |
| Controller for Sony DualSense |
| |
| by flora 01/07/2022 |
\*-----------------------------------------*/
#include <hidapi/hidapi.h>
#include "RGBController.h"
#include <string>
#include <vector>
#define SONY_DUALSENSE_LIGHTBAR_LED_COUNT 1
#define SONY_DUALSENSE_PLAYER_LED_COUNT 5
#define SONY_DUALSENSE_BT_PACKET_SIZE 78
#define SONY_DUALSENSE_USB_PACKET_SIZE 48
enum
{
SONY_DUALSENSE_DIRECT_MODE_VALUE = 0x01,
SONY_DUALSENSE_MIC_OFF_MODE_VALUE = 0x00,
SONY_DUALSENSE_MIC_PULSE_MODE_VALUE = 0x02,
SONY_DUALSENSE_BRIGHTNESS_MIN = 0x00,
SONY_DUALSENSE_BRIGHTNESS_MAX = 0x02,
SONY_DUALSENSE_DEFAULT_BRIGHTNESS = 0x01
};
class SonyDualSenseController
{
public:
SonyDualSenseController(hid_device * device_handle, const char * device_path);
~SonyDualSenseController();
std::string GetLocation();
std::string GetSerialString();
void SetColors(std::vector<RGBColor> colors, unsigned char brightness, unsigned char mode_value);
bool IsBluetooth();
private:
hid_device* dev;
std::string location;
bool is_bluetooth;
};

View File

@@ -0,0 +1,50 @@
/*-----------------------------------------*\
| SonyDS4Controller.h |
| |
| Detector for Sony Dualshock 4 |
| |
| Pol Rius (alpemwarrior) 24/09/2020 |
\*-----------------------------------------*/
#include <vector>
#include <hidapi/hidapi.h>
#include "RGBController_SonyDS4.h"
#include "RGBController_SonyDualSense.h"
#include "Detector.h"
#define SONY_VID 0x054C
#define SONY_DS4_V1_PID 0x05C4
#define SONY_DS4_V2_PID 0x09CC
#define SONY_DS4_RECEIVER_PID 0x0BA0
#define SONY_DUALSENSE_PID 0x0CE6
void DetectSonyDS4Controllers(hid_device_info* info, const std::string&)
{
hid_device* dev = hid_open_path(info->path);
if(dev)
{
SonyDS4Controller* controller = new SonyDS4Controller(dev, info->path);
RGBController_SonyDS4* rgb_controller = new RGBController_SonyDS4(controller);
// Constructor sets the name
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
}
void DetectSonyDualSenseControllers(hid_device_info* info, const std::string&)
{
hid_device* dev = hid_open_path(info->path);
if(dev)
{
SonyDualSenseController* controller = new SonyDualSenseController(dev, info->path);
RGBController_SonyDualSense* rgb_controller = new RGBController_SonyDualSense(controller);
// Constructor sets the name
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
}
REGISTER_HID_DETECTOR("Sony DualShock 4", DetectSonyDS4Controllers, SONY_VID, SONY_DS4_V1_PID);
REGISTER_HID_DETECTOR("Sony DualShock 4", DetectSonyDS4Controllers, SONY_VID, SONY_DS4_V2_PID);
REGISTER_HID_DETECTOR("Sony DualShock 4", DetectSonyDS4Controllers, SONY_VID, SONY_DS4_RECEIVER_PID);
REGISTER_HID_DETECTOR("Sony DualSense" , DetectSonyDualSenseControllers, SONY_VID, SONY_DUALSENSE_PID);