Publish LumaOps source
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| RGBController_SonyDS4.cpp |
|
||||
| |
|
||||
| RGBController for Sony Dualshock 4 |
|
||||
| |
|
||||
| Pol Rius (alpemwarrior) 24 Sep 2020 |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#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* controller_ptr)
|
||||
{
|
||||
controller = controller_ptr;
|
||||
|
||||
name = "Sony DualShock 4";
|
||||
vendor = "Sony";
|
||||
type = DEVICE_TYPE_GAMEPAD;
|
||||
description = "Sony DualShock 4 Device";
|
||||
location = controller->GetLocation();
|
||||
serial = controller->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 controller;
|
||||
}
|
||||
|
||||
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]));
|
||||
controller->SetColors(red, green, blue);
|
||||
}
|
||||
|
||||
void RGBController_SonyDS4::UpdateZoneLEDs(int /*zone*/)
|
||||
{
|
||||
DeviceUpdateLEDs();
|
||||
}
|
||||
|
||||
void RGBController_SonyDS4::UpdateSingleLED(int /*led*/)
|
||||
{
|
||||
DeviceUpdateLEDs();
|
||||
}
|
||||
|
||||
void RGBController_SonyDS4::DeviceUpdateMode()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| RGBController_SonyDS4.h |
|
||||
| |
|
||||
| RGBController for Sony Dualshock 4 |
|
||||
| |
|
||||
| Pol Rius (alpemwarrior) 24 Sep 2020 |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RGBController.h"
|
||||
#include "SonyDS4Controller.h"
|
||||
|
||||
class RGBController_SonyDS4 : public RGBController
|
||||
{
|
||||
public:
|
||||
RGBController_SonyDS4(SonyDS4Controller* controller_ptr);
|
||||
~RGBController_SonyDS4();
|
||||
|
||||
void SetupZones();
|
||||
void ResizeZone(int zone, int new_size);
|
||||
|
||||
void DeviceUpdateLEDs();
|
||||
void UpdateZoneLEDs(int zone);
|
||||
void UpdateSingleLED(int led);
|
||||
|
||||
void DeviceUpdateMode();
|
||||
|
||||
private:
|
||||
SonyDS4Controller* controller;
|
||||
};
|
||||
@@ -0,0 +1,126 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| SonyDS4Controller.cpp |
|
||||
| |
|
||||
| Driver for Sony Dualshock 4 |
|
||||
| |
|
||||
| Pol Rius (alpemwarrior) 24 Sep 2020 |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <CRC.h>
|
||||
#include <hidapi.h>
|
||||
#include "SonyDS4Controller.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
SonyDS4Controller::SonyDS4Controller(hid_device * device_handle, const char * device_path)
|
||||
{
|
||||
this->dev = device_handle;
|
||||
unsigned char readBuffer[64];
|
||||
unsigned char reportBuffer[64];
|
||||
reportBuffer[0] = 0x02;
|
||||
|
||||
hid_get_feature_report(dev, reportBuffer, 64);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
hid_read(dev, readBuffer, 64);
|
||||
if (readBuffer[0] == 17)
|
||||
{
|
||||
is_bluetooth = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
location = device_path;
|
||||
}
|
||||
|
||||
SonyDS4Controller::~SonyDS4Controller()
|
||||
{
|
||||
hid_close(dev);
|
||||
}
|
||||
|
||||
std::string SonyDS4Controller::GetLocation()
|
||||
{
|
||||
return("HID: " + location);
|
||||
}
|
||||
|
||||
std::string SonyDS4Controller::GetSerialString()
|
||||
{
|
||||
wchar_t serial_string[128];
|
||||
int ret = hid_get_serial_number_string(dev, serial_string, 128);
|
||||
|
||||
if(ret != 0)
|
||||
{
|
||||
return("");
|
||||
}
|
||||
|
||||
return(StringUtils::wstring_to_string(serial_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(dev, 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(dev, buffer, 11);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| SonyDS4Controller.h |
|
||||
| |
|
||||
| Driver for Sony Dualshock 4 |
|
||||
| |
|
||||
| Pol Rius (alpemwarrior) 24 Sep 2020 |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <hidapi.h>
|
||||
|
||||
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* dev;
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user