1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-11-16 14:09:51 +00:00
sys-con/source/ControllerLib/Controllers/Xbox360Controller.cpp

235 lines
6.8 KiB
C++
Raw Normal View History

2019-10-31 18:00:42 +00:00
#include "Controllers/Xbox360Controller.h"
#include <cmath>
2019-11-06 22:20:58 +00:00
static ControllerConfig _xbox360ControllerConfig{};
2019-10-31 18:00:42 +00:00
Xbox360Controller::Xbox360Controller(std::unique_ptr<IUSBDevice> &&interface)
: IController(std::move(interface))
{
}
Xbox360Controller::~Xbox360Controller()
{
2020-04-18 14:30:10 +00:00
//Exit();
2019-10-31 18:00:42 +00:00
}
2019-11-13 11:16:13 +00:00
Result Xbox360Controller::Initialize()
2019-10-31 18:00:42 +00:00
{
2019-11-13 11:16:13 +00:00
Result rc;
2019-10-31 18:00:42 +00:00
rc = OpenInterfaces();
2019-11-13 11:16:13 +00:00
if (R_FAILED(rc))
2019-10-31 18:00:42 +00:00
return rc;
SetLED(XBOX360LED_TOPLEFT);
2019-10-31 18:00:42 +00:00
return rc;
}
void Xbox360Controller::Exit()
{
CloseInterfaces();
}
2019-11-13 11:16:13 +00:00
Result Xbox360Controller::OpenInterfaces()
2019-10-31 18:00:42 +00:00
{
2019-11-13 11:16:13 +00:00
Result rc;
2019-10-31 18:00:42 +00:00
rc = m_device->Open();
2019-11-13 11:16:13 +00:00
if (R_FAILED(rc))
2019-10-31 18:00:42 +00:00
return rc;
//This will open each interface and try to acquire Xbox One controller's in and out endpoints, if it hasn't already
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-10-31 18:00:42 +00:00
return rc;
if (interface->GetDescriptor()->bInterfaceProtocol != 1)
continue;
if (interface->GetDescriptor()->bNumEndpoints < 2)
continue;
2019-10-31 18:00:42 +00:00
if (!m_inPipe)
{
for (int i = 0; i != 15; ++i)
2019-10-31 18:00:42 +00:00
{
IUSBEndpoint *inEndpoint = interface->GetEndpoint(IUSBEndpoint::USB_ENDPOINT_IN, i);
if (inEndpoint)
{
rc = inEndpoint->Open();
2019-11-13 11:16:13 +00:00
if (R_FAILED(rc))
return 55555;
m_inPipe = inEndpoint;
break;
}
2019-10-31 18:00:42 +00:00
}
}
if (!m_outPipe)
{
for (int i = 0; i != 15; ++i)
2019-10-31 18:00:42 +00:00
{
IUSBEndpoint *outEndpoint = interface->GetEndpoint(IUSBEndpoint::USB_ENDPOINT_OUT, i);
if (outEndpoint)
{
rc = outEndpoint->Open();
2019-11-13 11:16:13 +00:00
if (R_FAILED(rc))
return 66666;
m_outPipe = outEndpoint;
break;
}
2019-10-31 18:00:42 +00:00
}
}
}
if (!m_inPipe || !m_outPipe)
return 369;
return rc;
}
void Xbox360Controller::CloseInterfaces()
{
2019-11-07 17:46:47 +00:00
//m_device->Reset();
2019-10-31 18:00:42 +00:00
m_device->Close();
}
2019-11-13 11:16:13 +00:00
Result Xbox360Controller::GetInput()
2019-10-31 18:00:42 +00:00
{
uint8_t input_bytes[64];
2019-11-13 11:16:13 +00:00
Result rc = m_inPipe->Read(input_bytes, sizeof(input_bytes));
2019-10-31 18:00:42 +00:00
uint8_t type = input_bytes[0];
if (type == XBOX360INPUT_BUTTON) //Button data
{
m_buttonData = *reinterpret_cast<Xbox360ButtonData *>(input_bytes);
}
return rc;
}
2019-11-13 11:16:13 +00:00
Result Xbox360Controller::SendInitBytes()
2019-10-31 18:00:42 +00:00
{
uint8_t init_bytes[]{
0x05,
0x20, 0x00, 0x01, 0x00};
2019-11-13 11:16:13 +00:00
Result rc = m_outPipe->Write(init_bytes, sizeof(init_bytes));
2019-10-31 18:00:42 +00:00
return rc;
}
float Xbox360Controller::NormalizeTrigger(uint8_t deadzonePercent, uint8_t value)
2019-10-31 18:00:42 +00:00
{
uint16_t deadzone = (UINT8_MAX * deadzonePercent) / 100;
2019-10-31 18:00:42 +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-10-31 18:00:42 +00:00
? 0
2019-11-07 17:46:47 +00:00
: static_cast<float>(value - deadzone) / (UINT8_MAX - deadzone);
2019-10-31 18:00:42 +00:00
}
void Xbox360Controller::NormalizeAxis(int16_t x,
int16_t y,
2019-11-07 17:46:47 +00:00
uint8_t deadzonePercent,
2019-10-31 18:00:42 +00:00
float *x_out,
float *y_out)
{
float x_val = x;
float y_val = y;
// 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-07 17:46:47 +00:00
float real_deadzone = (32767 * deadzonePercent) / 100;
2019-10-31 18:00:42 +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-10-31 18:00:42 +00:00
{
// Clip the magnitude at its expected maximum value.
float magnitude = std::min(32767.0f, real_magnitude);
// Adjust magnitude relative to the end of the dead zone.
2019-11-07 17:46:47 +00:00
magnitude -= real_deadzone;
2019-10-31 18:00:42 +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-07 17:46:47 +00:00
float ratio = (magnitude / (32767 - real_deadzone)) / real_magnitude;
2019-11-09 20:24:29 +00:00
2019-10-31 18:00:42 +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 Xbox360Controller::GetNormalizedButtonData()
{
NormalizedButtonData normalData{};
2019-10-31 18:00:42 +00:00
normalData.triggers[0] = NormalizeTrigger(_xbox360ControllerConfig.triggerDeadzonePercent[0], m_buttonData.trigger_left);
normalData.triggers[1] = NormalizeTrigger(_xbox360ControllerConfig.triggerDeadzonePercent[1], m_buttonData.trigger_right);
2019-11-07 17:46:47 +00:00
NormalizeAxis(m_buttonData.stick_left_x, m_buttonData.stick_left_y, _xbox360ControllerConfig.stickDeadzonePercent[0],
2019-11-07 17:46:47 +00:00
&normalData.sticks[0].axis_x, &normalData.sticks[0].axis_y);
NormalizeAxis(m_buttonData.stick_right_x, m_buttonData.stick_right_y, _xbox360ControllerConfig.stickDeadzonePercent[1],
2019-11-07 17:46:47 +00:00
&normalData.sticks[1].axis_x, &normalData.sticks[1].axis_y);
bool buttons[MAX_CONTROLLER_BUTTONS]{
2019-11-07 17:46:47 +00:00
m_buttonData.y,
m_buttonData.b,
m_buttonData.a,
m_buttonData.x,
m_buttonData.stick_left_click,
m_buttonData.stick_right_click,
m_buttonData.bumper_left,
m_buttonData.bumper_right,
normalData.triggers[0] > 0,
normalData.triggers[1] > 0,
m_buttonData.back,
m_buttonData.start,
m_buttonData.dpad_up,
m_buttonData.dpad_right,
m_buttonData.dpad_down,
m_buttonData.dpad_left,
false,
m_buttonData.guide,
};
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i)
2019-11-07 17:46:47 +00:00
{
ControllerButton button = _xbox360ControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
2019-11-07 17:46:47 +00:00
}
2019-10-31 18:00:42 +00:00
return normalData;
}
2019-11-13 11:16:13 +00:00
Result Xbox360Controller::SetRumble(uint8_t strong_magnitude, uint8_t weak_magnitude)
2019-10-31 18:00:42 +00:00
{
uint8_t rumbleData[]{0x00, sizeof(Xbox360RumbleData), 0x00, strong_magnitude, weak_magnitude, 0x00, 0x00, 0x00};
2019-10-31 18:00:42 +00:00
return m_outPipe->Write(rumbleData, sizeof(rumbleData));
2019-11-06 22:20:58 +00:00
}
2019-11-13 11:16:13 +00:00
Result Xbox360Controller::SetLED(Xbox360LEDValue value)
{
uint8_t ledPacket[]{0x01, 0x03, static_cast<uint8_t>(value)};
return m_outPipe->Write(ledPacket, sizeof(ledPacket));
}
2019-11-06 22:20:58 +00:00
void Xbox360Controller::LoadConfig(const ControllerConfig *config)
{
_xbox360ControllerConfig = *config;
2019-11-09 20:26:47 +00:00
}
ControllerConfig *Xbox360Controller::GetConfig()
{
return &_xbox360ControllerConfig;
2019-10-31 18:00:42 +00:00
}