2019-11-03 19:38:24 +00:00
|
|
|
#include "Controllers/Dualshock4Controller.h"
|
|
|
|
#include <cmath>
|
|
|
|
|
2019-11-06 22:20:58 +00:00
|
|
|
static ControllerConfig _dualshock4ControllerConfig{};
|
2019-11-21 22:03:31 +00:00
|
|
|
static RGBAColor _ledValue{0x00, 0x00, 0x40};
|
2019-11-06 22:20:58 +00:00
|
|
|
|
2019-11-03 19:38:24 +00:00
|
|
|
Dualshock4Controller::Dualshock4Controller(std::unique_ptr<IUSBDevice> &&interface)
|
|
|
|
: IController(std::move(interface))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Dualshock4Controller::~Dualshock4Controller()
|
|
|
|
{
|
2020-04-18 14:30:10 +00:00
|
|
|
//Exit();
|
2019-11-03 19:38:24 +00:00
|
|
|
}
|
2019-11-11 19:43:19 +00:00
|
|
|
|
2019-11-13 11:16:13 +00:00
|
|
|
Result Dualshock4Controller::SendInitBytes()
|
2019-11-11 19:43:19 +00:00
|
|
|
{
|
2019-11-21 22:03:31 +00:00
|
|
|
const uint8_t init_bytes[32] = {
|
2019-11-11 19:43:19 +00:00
|
|
|
0x05, 0x07, 0x00, 0x00,
|
2019-11-21 22:03:31 +00:00
|
|
|
0x00, 0x00, //initial strong and weak rumble
|
|
|
|
_ledValue.r, _ledValue.g, _ledValue.b, //LED color
|
2019-11-11 19:43:19 +00:00
|
|
|
0x00, 0x00};
|
2019-11-12 17:56:50 +00:00
|
|
|
|
2019-11-11 19:43:19 +00:00
|
|
|
return m_outPipe->Write(init_bytes, sizeof(init_bytes));
|
|
|
|
}
|
2019-11-03 19:38:24 +00:00
|
|
|
|
2019-11-13 11:16:13 +00:00
|
|
|
Result Dualshock4Controller::Initialize()
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-13 11:16:13 +00:00
|
|
|
Result rc;
|
2019-11-03 19:38:24 +00:00
|
|
|
|
|
|
|
rc = OpenInterfaces();
|
2019-11-13 11:16:13 +00:00
|
|
|
if (R_FAILED(rc))
|
2019-11-03 19:38:24 +00:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
rc = SendInitBytes();
|
2019-11-13 11:16:13 +00:00
|
|
|
if (R_FAILED(rc))
|
2019-11-03 19:38:24 +00:00
|
|
|
return rc;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
void Dualshock4Controller::Exit()
|
|
|
|
{
|
|
|
|
CloseInterfaces();
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:16:13 +00:00
|
|
|
Result Dualshock4Controller::OpenInterfaces()
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-13 11:16:13 +00:00
|
|
|
Result rc;
|
2019-11-03 19:38:24 +00:00
|
|
|
rc = m_device->Open();
|
2019-11-13 11:16:13 +00:00
|
|
|
if (R_FAILED(rc))
|
2019-11-03 19:38:24 +00:00
|
|
|
return rc;
|
|
|
|
|
2019-11-13 16:53:23 +00:00
|
|
|
//Open each interface, send it a setup packet and get the endpoints if it succeeds
|
2019-11-03 19:38:24 +00:00
|
|
|
std::vector<std::unique_ptr<IUSBInterface>> &interfaces = m_device->GetInterfaces();
|
|
|
|
for (auto &&interface : interfaces)
|
|
|
|
{
|
|
|
|
rc = interface->Open();
|
2019-11-13 11:16:13 +00:00
|
|
|
if (R_FAILED(rc))
|
2019-11-03 19:38:24 +00:00
|
|
|
return rc;
|
2019-11-11 19:43:19 +00:00
|
|
|
|
2019-11-15 11:03:06 +00:00
|
|
|
if (interface->GetDescriptor()->bInterfaceClass != 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (interface->GetDescriptor()->bInterfaceProtocol != 0)
|
|
|
|
continue;
|
|
|
|
|
2019-11-11 19:43:19 +00:00
|
|
|
if (interface->GetDescriptor()->bNumEndpoints < 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!m_inPipe)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-11 19:43:19 +00:00
|
|
|
for (int i = 0; i != 15; ++i)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-11 19:43:19 +00:00
|
|
|
IUSBEndpoint *inEndpoint = interface->GetEndpoint(IUSBEndpoint::USB_ENDPOINT_IN, i);
|
|
|
|
if (inEndpoint)
|
|
|
|
{
|
2019-11-13 16:53:23 +00:00
|
|
|
rc = inEndpoint->Open();
|
2019-11-13 11:16:13 +00:00
|
|
|
if (R_FAILED(rc))
|
2019-11-11 19:43:19 +00:00
|
|
|
return 61;
|
2019-11-03 19:38:24 +00:00
|
|
|
|
2019-11-11 19:43:19 +00:00
|
|
|
m_inPipe = inEndpoint;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-03 19:38:24 +00:00
|
|
|
}
|
2019-11-11 19:43:19 +00:00
|
|
|
}
|
2019-11-03 19:38:24 +00:00
|
|
|
|
2019-11-11 19:43:19 +00:00
|
|
|
if (!m_outPipe)
|
|
|
|
{
|
|
|
|
for (int i = 0; i != 15; ++i)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-11 19:43:19 +00:00
|
|
|
IUSBEndpoint *outEndpoint = interface->GetEndpoint(IUSBEndpoint::USB_ENDPOINT_OUT, i);
|
|
|
|
if (outEndpoint)
|
|
|
|
{
|
2019-11-13 16:53:23 +00:00
|
|
|
rc = outEndpoint->Open();
|
2019-11-13 11:16:13 +00:00
|
|
|
if (R_FAILED(rc))
|
2019-11-11 19:43:19 +00:00
|
|
|
return 62;
|
2019-11-03 19:38:24 +00:00
|
|
|
|
2019-11-11 19:43:19 +00:00
|
|
|
m_outPipe = outEndpoint;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-03 19:38:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_inPipe || !m_outPipe)
|
|
|
|
return 69;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
void Dualshock4Controller::CloseInterfaces()
|
|
|
|
{
|
2019-11-07 17:46:47 +00:00
|
|
|
//m_device->Reset();
|
2019-11-03 19:38:24 +00:00
|
|
|
m_device->Close();
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:16:13 +00:00
|
|
|
Result Dualshock4Controller::GetInput()
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-13 16:53:23 +00:00
|
|
|
|
2019-11-12 18:37:14 +00:00
|
|
|
uint8_t input_bytes[64];
|
2019-11-13 16:53:23 +00:00
|
|
|
|
2019-11-13 11:16:13 +00:00
|
|
|
Result rc = m_inPipe->Read(input_bytes, sizeof(input_bytes));
|
|
|
|
if (R_FAILED(rc))
|
2019-11-03 19:38:24 +00:00
|
|
|
return rc;
|
|
|
|
|
2019-11-13 16:53:23 +00:00
|
|
|
if (input_bytes[0] == 0x01)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-12 18:37:14 +00:00
|
|
|
m_buttonData = *reinterpret_cast<Dualshock4USBButtonData *>(input_bytes);
|
2019-11-03 19:38:24 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-04-22 00:54:49 +00:00
|
|
|
float Dualshock4Controller::NormalizeTrigger(uint8_t deadzonePercent, uint8_t value)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2020-04-22 00:54:49 +00:00
|
|
|
uint8_t deadzone = (UINT8_MAX * deadzonePercent) / 100;
|
2019-11-03 19:38:24 +00:00
|
|
|
//If the given value is below the trigger zone, save the calc and return 0, otherwise adjust the value to the deadzone
|
2019-11-07 17:46:47 +00:00
|
|
|
return value < deadzone
|
2019-11-03 19:38:24 +00:00
|
|
|
? 0
|
2019-11-12 18:37:14 +00:00
|
|
|
: static_cast<float>(value - deadzone) / (UINT8_MAX - deadzone);
|
2019-11-03 19:38:24 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 18:37:14 +00:00
|
|
|
void Dualshock4Controller::NormalizeAxis(uint8_t x,
|
|
|
|
uint8_t y,
|
2019-11-07 17:46:47 +00:00
|
|
|
uint8_t deadzonePercent,
|
2019-11-03 19:38:24 +00:00
|
|
|
float *x_out,
|
|
|
|
float *y_out)
|
|
|
|
{
|
2019-11-12 18:37:14 +00:00
|
|
|
float x_val = x - 127.0f;
|
|
|
|
float y_val = 127.0f - y;
|
2019-11-03 19:38:24 +00:00
|
|
|
// Determine how far the stick is pushed.
|
|
|
|
//This will never exceed 32767 because if the stick is
|
|
|
|
//horizontally maxed in one direction, vertically it must be neutral(0) and vice versa
|
|
|
|
float real_magnitude = std::sqrt(x_val * x_val + y_val * y_val);
|
2019-11-12 18:37:14 +00:00
|
|
|
float real_deadzone = (127 * deadzonePercent) / 100;
|
2019-11-03 19:38:24 +00:00
|
|
|
// Check if the controller is outside a circular dead zone.
|
2019-11-07 17:46:47 +00:00
|
|
|
if (real_magnitude > real_deadzone)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
|
|
|
// Clip the magnitude at its expected maximum value.
|
2019-11-12 18:37:14 +00:00
|
|
|
float magnitude = std::min(127.0f, real_magnitude);
|
2019-11-03 19:38:24 +00:00
|
|
|
// Adjust magnitude relative to the end of the dead zone.
|
2019-11-07 17:46:47 +00:00
|
|
|
magnitude -= real_deadzone;
|
2019-11-03 19:38:24 +00:00
|
|
|
// Normalize the magnitude with respect to its expected range giving a
|
|
|
|
// magnitude value of 0.0 to 1.0
|
|
|
|
//ratio = (currentValue / maxValue) / realValue
|
2019-11-12 18:37:14 +00:00
|
|
|
float ratio = (magnitude / (127 - real_deadzone)) / real_magnitude;
|
2019-11-03 19:38:24 +00:00
|
|
|
*x_out = x_val * ratio;
|
|
|
|
*y_out = y_val * ratio;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the controller is in the deadzone zero out the magnitude.
|
|
|
|
*x_out = *y_out = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Pass by value should hopefully be optimized away by RVO
|
|
|
|
NormalizedButtonData Dualshock4Controller::GetNormalizedButtonData()
|
|
|
|
{
|
2020-04-22 00:54:49 +00:00
|
|
|
NormalizedButtonData normalData{};
|
2019-11-03 19:38:24 +00:00
|
|
|
|
2020-04-22 00:54:49 +00:00
|
|
|
normalData.triggers[0] = NormalizeTrigger(_dualshock4ControllerConfig.triggerDeadzonePercent[0], m_buttonData.l2_pressure);
|
|
|
|
normalData.triggers[1] = NormalizeTrigger(_dualshock4ControllerConfig.triggerDeadzonePercent[1], m_buttonData.r2_pressure);
|
2019-11-11 19:43:19 +00:00
|
|
|
|
2020-04-22 00:54:49 +00:00
|
|
|
NormalizeAxis(m_buttonData.stick_left_x, m_buttonData.stick_left_y, _dualshock4ControllerConfig.stickDeadzonePercent[0],
|
2019-11-11 19:43:19 +00:00
|
|
|
&normalData.sticks[0].axis_x, &normalData.sticks[0].axis_y);
|
2020-04-22 00:54:49 +00:00
|
|
|
NormalizeAxis(m_buttonData.stick_right_x, m_buttonData.stick_right_y, _dualshock4ControllerConfig.stickDeadzonePercent[1],
|
2019-11-11 19:43:19 +00:00
|
|
|
&normalData.sticks[1].axis_x, &normalData.sticks[1].axis_y);
|
|
|
|
|
2020-04-22 00:54:49 +00:00
|
|
|
bool buttons[MAX_CONTROLLER_BUTTONS] = {
|
2019-11-11 19:43:19 +00:00
|
|
|
m_buttonData.triangle,
|
|
|
|
m_buttonData.circle,
|
|
|
|
m_buttonData.cross,
|
|
|
|
m_buttonData.square,
|
|
|
|
m_buttonData.l3,
|
|
|
|
m_buttonData.r3,
|
|
|
|
m_buttonData.l1,
|
|
|
|
m_buttonData.r1,
|
|
|
|
m_buttonData.l2,
|
|
|
|
m_buttonData.r2,
|
|
|
|
m_buttonData.share,
|
|
|
|
m_buttonData.options,
|
2019-11-12 18:37:14 +00:00
|
|
|
(m_buttonData.dpad == DS4_UP) || (m_buttonData.dpad == DS4_UPRIGHT) || (m_buttonData.dpad == DS4_UPLEFT),
|
|
|
|
(m_buttonData.dpad == DS4_RIGHT) || (m_buttonData.dpad == DS4_UPRIGHT) || (m_buttonData.dpad == DS4_DOWNRIGHT),
|
|
|
|
(m_buttonData.dpad == DS4_DOWN) || (m_buttonData.dpad == DS4_DOWNRIGHT) || (m_buttonData.dpad == DS4_DOWNLEFT),
|
|
|
|
(m_buttonData.dpad == DS4_LEFT) || (m_buttonData.dpad == DS4_UPLEFT) || (m_buttonData.dpad == DS4_DOWNLEFT),
|
2019-11-13 16:53:23 +00:00
|
|
|
m_buttonData.touchpad_press,
|
|
|
|
m_buttonData.psbutton,
|
2020-04-22 00:54:49 +00:00
|
|
|
false,
|
2019-11-15 11:03:06 +00:00
|
|
|
m_buttonData.touchpad_finger1_unpressed == false,
|
2019-11-11 19:43:19 +00:00
|
|
|
};
|
|
|
|
|
2020-04-22 00:54:49 +00:00
|
|
|
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i)
|
2019-11-11 19:43:19 +00:00
|
|
|
{
|
|
|
|
ControllerButton button = _dualshock4ControllerConfig.buttons[i];
|
2020-04-22 00:54:49 +00:00
|
|
|
if (button == NONE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
|
2019-11-11 19:43:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:38:24 +00:00
|
|
|
return normalData;
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:16:13 +00:00
|
|
|
Result Dualshock4Controller::SetRumble(uint8_t strong_magnitude, uint8_t weak_magnitude)
|
2019-11-03 19:38:24 +00:00
|
|
|
{
|
2019-11-13 16:53:23 +00:00
|
|
|
//Not implemented yet
|
2019-11-03 19:38:24 +00:00
|
|
|
return 9;
|
2019-11-06 22:20:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:03:31 +00:00
|
|
|
void Dualshock4Controller::LoadConfig(const ControllerConfig *config, RGBAColor ledValue)
|
2019-11-06 22:20:58 +00:00
|
|
|
{
|
|
|
|
_dualshock4ControllerConfig = *config;
|
2019-11-21 22:03:31 +00:00
|
|
|
_ledValue = ledValue;
|
2019-11-09 20:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ControllerConfig *Dualshock4Controller::GetConfig()
|
|
|
|
{
|
|
|
|
return &_dualshock4ControllerConfig;
|
2019-11-03 19:38:24 +00:00
|
|
|
}
|