1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-08 03:58:43 +00:00

Make dualshock 4 input at least functional

This commit is contained in:
cathery 2019-11-12 21:37:14 +03:00
parent 7c1757bef8
commit 76baf30713
2 changed files with 30 additions and 42 deletions

View File

@ -90,14 +90,14 @@ struct Dualshock4USBButtonData
enum Dualshock4Dpad enum Dualshock4Dpad
{ {
DS4_UP = 0x000, DS4_UP,
DS4_UPRIGHT = 0x001, DS4_UPRIGHT,
DS4_RIGHT = 0x010, DS4_RIGHT,
DS4_DOWNRIGHT = 0x011, DS4_DOWNRIGHT,
DS4_DOWN = 0x100, DS4_DOWN,
DS4_DOWNLEFT = 0x101, DS4_DOWNLEFT,
DS4_LEFT = 0x110, DS4_LEFT,
DS4_UPLEFT = 0x111, DS4_UPLEFT,
}; };
class Dualshock4Controller : public IController class Dualshock4Controller : public IController
@ -107,7 +107,7 @@ private:
IUSBEndpoint *m_outPipe = nullptr; IUSBEndpoint *m_outPipe = nullptr;
IUSBInterface *m_interface = nullptr; IUSBInterface *m_interface = nullptr;
Dualshock4ButtonData m_buttonData; Dualshock4USBButtonData m_buttonData;
public: public:
Dualshock4Controller(std::unique_ptr<IUSBDevice> &&interface); Dualshock4Controller(std::unique_ptr<IUSBDevice> &&interface);
@ -125,10 +125,10 @@ public:
virtual ControllerType GetType() { return CONTROLLER_DUALSHOCK4; } virtual ControllerType GetType() { return CONTROLLER_DUALSHOCK4; }
inline const Dualshock4ButtonData &GetButtonData() { return m_buttonData; }; inline const Dualshock4USBButtonData &GetButtonData() { return m_buttonData; };
float NormalizeTrigger(uint16_t value); float NormalizeTrigger(uint8_t value);
void NormalizeAxis(int16_t x, int16_t y, uint8_t deadzonePercent, float *x_out, float *y_out); void NormalizeAxis(uint8_t x, uint8_t y, uint8_t deadzonePercent, float *x_out, float *y_out);
Status SendInitBytes(); Status SendInitBytes();
Status SetRumble(uint8_t strong_magnitude, uint8_t weak_magnitude); Status SetRumble(uint8_t strong_magnitude, uint8_t weak_magnitude);

View File

@ -4,10 +4,6 @@
static ControllerConfig _dualshock4ControllerConfig{}; static ControllerConfig _dualshock4ControllerConfig{};
const uint8_t kRumbleMagnitudeMax = 0xff;
const float kAxisMax = 255.0f;
const float kDpadMax = 7.0f;
Dualshock4Controller::Dualshock4Controller(std::unique_ptr<IUSBDevice> &&interface) Dualshock4Controller::Dualshock4Controller(std::unique_ptr<IUSBDevice> &&interface)
: IController(std::move(interface)) : IController(std::move(interface))
{ {
@ -133,64 +129,56 @@ void Dualshock4Controller::CloseInterfaces()
Status Dualshock4Controller::GetInput() Status Dualshock4Controller::GetInput()
{ {
uint8_t input_bytes[65]; uint8_t input_bytes[64];
Status rc = m_inPipe->Read(input_bytes, sizeof(input_bytes)); Status rc = m_inPipe->Read(input_bytes, sizeof(input_bytes));
if (S_FAILED(rc)) if (S_FAILED(rc))
{ {
m_inputData[0] = static_cast<uint8_t>(rc); m_inputData[0] = static_cast<uint8_t>(rc);
return rc; return rc;
} }
for (int i = 0; i != 65; ++i)
{
m_inputData[i] = input_bytes[i];
}
m_UpdateCalled = true;
uint8_t type = input_bytes[0]; uint8_t type = input_bytes[0];
if (type == 0x01) if (type == 0x01)
{ {
m_buttonData = *reinterpret_cast<Dualshock4ButtonData *>(input_bytes); m_buttonData = *reinterpret_cast<Dualshock4USBButtonData *>(input_bytes);
} }
return rc; return rc;
} }
float Dualshock4Controller::NormalizeTrigger(uint16_t value) float Dualshock4Controller::NormalizeTrigger(uint8_t value)
{ {
uint16_t deadzone = (UINT16_MAX * _dualshock4ControllerConfig.triggerDeadzonePercent) / 100; uint16_t deadzone = (UINT8_MAX * _dualshock4ControllerConfig.triggerDeadzonePercent) / 100;
//If the given value is below the trigger zone, save the calc and return 0, otherwise adjust the value to the deadzone //If the given value is below the trigger zone, save the calc and return 0, otherwise adjust the value to the deadzone
return value < deadzone return value < deadzone
? 0 ? 0
: static_cast<float>(value - deadzone) / (UINT16_MAX - deadzone); : static_cast<float>(value - deadzone) / (UINT8_MAX - deadzone);
} }
void Dualshock4Controller::NormalizeAxis(int16_t x, void Dualshock4Controller::NormalizeAxis(uint8_t x,
int16_t y, uint8_t y,
uint8_t deadzonePercent, uint8_t deadzonePercent,
float *x_out, float *x_out,
float *y_out) float *y_out)
{ {
float x_val = x; float x_val = x - 127.0f;
float y_val = y; float y_val = 127.0f - y;
// Determine how far the stick is pushed. // Determine how far the stick is pushed.
//This will never exceed 32767 because if the stick is //This will never exceed 32767 because if the stick is
//horizontally maxed in one direction, vertically it must be neutral(0) and vice versa //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); float real_magnitude = std::sqrt(x_val * x_val + y_val * y_val);
float real_deadzone = (32767 * deadzonePercent) / 100; float real_deadzone = (127 * deadzonePercent) / 100;
// Check if the controller is outside a circular dead zone. // Check if the controller is outside a circular dead zone.
if (real_magnitude > real_deadzone) if (real_magnitude > real_deadzone)
{ {
// Clip the magnitude at its expected maximum value. // Clip the magnitude at its expected maximum value.
float magnitude = std::min(32767.0f, real_magnitude); float magnitude = std::min(127.0f, real_magnitude);
// Adjust magnitude relative to the end of the dead zone. // Adjust magnitude relative to the end of the dead zone.
magnitude -= real_deadzone; magnitude -= real_deadzone;
// Normalize the magnitude with respect to its expected range giving a // Normalize the magnitude with respect to its expected range giving a
// magnitude value of 0.0 to 1.0 // magnitude value of 0.0 to 1.0
//ratio = (currentValue / maxValue) / realValue //ratio = (currentValue / maxValue) / realValue
float ratio = (magnitude / (32767 - real_deadzone)) / real_magnitude; float ratio = (magnitude / (127 - real_deadzone)) / real_magnitude;
*x_out = x_val * ratio; *x_out = x_val * ratio;
*y_out = y_val * ratio; *y_out = y_val * ratio;
} }
@ -227,12 +215,12 @@ NormalizedButtonData Dualshock4Controller::GetNormalizedButtonData()
m_buttonData.r2, m_buttonData.r2,
m_buttonData.share, m_buttonData.share,
m_buttonData.options, m_buttonData.options,
m_buttonData.dpad & DS4_UP, (m_buttonData.dpad == DS4_UP) || (m_buttonData.dpad == DS4_UPRIGHT) || (m_buttonData.dpad == DS4_UPLEFT),
m_buttonData.dpad & DS4_RIGHT, (m_buttonData.dpad == DS4_RIGHT) || (m_buttonData.dpad == DS4_UPRIGHT) || (m_buttonData.dpad == DS4_DOWNRIGHT),
m_buttonData.dpad & DS4_DOWN, (m_buttonData.dpad == DS4_DOWN) || (m_buttonData.dpad == DS4_DOWNRIGHT) || (m_buttonData.dpad == DS4_DOWNLEFT),
m_buttonData.dpad & DS4_LEFT, (m_buttonData.dpad == DS4_LEFT) || (m_buttonData.dpad == DS4_UPLEFT) || (m_buttonData.dpad == DS4_DOWNLEFT),
m_buttonData.touchpad_press, false, //m_buttonData.touchpad_press,
m_buttonData.psbutton, false, //m_buttonData.psbutton,
}; };
for (int i = 0; i != NUM_CONTROLLERBUTTONS; ++i) for (int i = 0; i != NUM_CONTROLLERBUTTONS; ++i)