From 435b2035256a58db56b6cf12d2e41c7c64ddab94 Mon Sep 17 00:00:00 2001 From: cathery Date: Fri, 8 Nov 2019 21:06:02 +0300 Subject: [PATCH] Fixed 360 Wireless not working --- ControllerUSB/include/ControllerTypes.h | 1 + .../Controllers/Xbox360WirelessController.h | 12 +++++- ControllerUSB/include/IController.h | 2 + ControllerUSB/source/ControllerHelpers.cpp | 4 ++ .../Controllers/Xbox360WirelessController.cpp | 42 +++++++++++-------- .../source/SwitchAbstractedPadHandler.cpp | 22 +++++++--- SwitchUSB/source/SwitchHDLHandler.cpp | 23 +++++++--- 7 files changed, 75 insertions(+), 31 deletions(-) diff --git a/ControllerUSB/include/ControllerTypes.h b/ControllerUSB/include/ControllerTypes.h index da3bc54..8d9477e 100644 --- a/ControllerUSB/include/ControllerTypes.h +++ b/ControllerUSB/include/ControllerTypes.h @@ -42,6 +42,7 @@ enum ControllerSupport { SUPPORTS_RUMBLE, SUPPORTS_BLUETOOTH, + SUPPORTS_PAIRING, SUPPORTS_SIXAXIS, SUPPORTS_SEVENAXIS, SUPPORTS_PRESSUREBUTTONS, diff --git a/ControllerUSB/include/Controllers/Xbox360WirelessController.h b/ControllerUSB/include/Controllers/Xbox360WirelessController.h index 27c01fb..c02297d 100644 --- a/ControllerUSB/include/Controllers/Xbox360WirelessController.h +++ b/ControllerUSB/include/Controllers/Xbox360WirelessController.h @@ -6,6 +6,12 @@ //References used: //https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c +struct OutputPacket +{ + const uint8_t *packet; + uint8_t length; +}; + class Xbox360WirelessController : public IController { private: @@ -16,6 +22,8 @@ private: bool m_presence; + std::vector m_outputBuffer; + public: Xbox360WirelessController(std::unique_ptr &&interface); virtual ~Xbox360WirelessController(); @@ -39,8 +47,8 @@ public: Status SetRumble(uint8_t strong_magnitude, uint8_t weak_magnitude); Status SetLED(Xbox360LEDValue value); - Status PowerOffController(); - Status ReconnectController(); static void LoadConfig(const ControllerConfig *config); + + Status OutputBuffer(); }; \ No newline at end of file diff --git a/ControllerUSB/include/IController.h b/ControllerUSB/include/IController.h index 7ae37d6..ee35a9a 100644 --- a/ControllerUSB/include/IController.h +++ b/ControllerUSB/include/IController.h @@ -32,4 +32,6 @@ public: inline IUSBDevice *GetDevice() { return m_device.get(); } virtual ControllerType GetType() = 0; virtual Status SetRumble(uint8_t strong_magnitude, uint8_t weak_magnitude) = 0; + + Status OutputBuffer() { return 1; }; }; \ No newline at end of file diff --git a/ControllerUSB/source/ControllerHelpers.cpp b/ControllerUSB/source/ControllerHelpers.cpp index 220e225..0155e41 100644 --- a/ControllerUSB/source/ControllerHelpers.cpp +++ b/ControllerUSB/source/ControllerHelpers.cpp @@ -8,6 +8,10 @@ bool DoesControllerSupport(ControllerType type, ControllerSupport supportType) if (supportType == SUPPORTS_RUMBLE) return true; return false; + case CONTROLLER_XBOX360W: + if (supportType == SUPPORTS_PAIRING) + return true; + return false; case CONTROLLER_XBOXONE: switch (supportType) { diff --git a/ControllerUSB/source/Controllers/Xbox360WirelessController.cpp b/ControllerUSB/source/Controllers/Xbox360WirelessController.cpp index 539b76f..376c11c 100644 --- a/ControllerUSB/source/Controllers/Xbox360WirelessController.cpp +++ b/ControllerUSB/source/Controllers/Xbox360WirelessController.cpp @@ -2,6 +2,9 @@ #include static ControllerConfig _xbox360WControllerConfig{}; +static const uint8_t reconnectPacket[]{0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +static const uint8_t poweroffPacket[]{0x00, 0x00, 0x08, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +static const uint8_t ledPacket[]{0x00, 0x00, 0x08, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; Xbox360WirelessController::Xbox360WirelessController(std::unique_ptr &&interface) : IController(std::move(interface)) @@ -16,12 +19,14 @@ Xbox360WirelessController::~Xbox360WirelessController() Status Xbox360WirelessController::Initialize() { Status rc; + m_outputBuffer.clear(); + m_outputBuffer.reserve(4); rc = OpenInterfaces(); if (S_FAILED(rc)) return rc; - rc = ReconnectController(); + rc = m_outPipe->Write(reconnectPacket, sizeof(reconnectPacket)); if (S_FAILED(rc)) return rc; @@ -97,7 +102,7 @@ Status Xbox360WirelessController::OpenInterfaces() void Xbox360WirelessController::CloseInterfaces() { if (m_presence) - PowerOffController(); + m_outPipe->Write(poweroffPacket, sizeof(poweroffPacket)); //m_device->Reset(); m_device->Close(); @@ -120,9 +125,12 @@ Status Xbox360WirelessController::GetInput() m_presence = newPresence; if (m_presence) - ReconnectController(); + { + m_outputBuffer.push_back(OutputPacket{reconnectPacket, sizeof(reconnectPacket)}); + m_outputBuffer.push_back(OutputPacket{ledPacket, sizeof(ledPacket)}); + } else - PowerOffController(); + m_outputBuffer.push_back(OutputPacket{poweroffPacket, sizeof(poweroffPacket)}); } } @@ -131,7 +139,7 @@ Status Xbox360WirelessController::GetInput() if (type == XBOX360INPUT_BUTTON) { - m_buttonData = *reinterpret_cast(input_bytes); + m_buttonData = *reinterpret_cast(input_bytes + 4); } return rc; @@ -237,19 +245,17 @@ Status Xbox360WirelessController::SetLED(Xbox360LEDValue value) return m_outPipe->Write(ledPacket, sizeof(ledPacket)); } -Status Xbox360WirelessController::PowerOffController() -{ - uint8_t poweroffPacket[]{0x00, 0x00, 0x08, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - return m_outPipe->Write(poweroffPacket, sizeof(poweroffPacket)); -} - -Status Xbox360WirelessController::ReconnectController() -{ - uint8_t reconnectPacket[]{0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - return m_outPipe->Write(reconnectPacket, sizeof(reconnectPacket)); -} - void Xbox360WirelessController::LoadConfig(const ControllerConfig *config) { _xbox360WControllerConfig = *config; -} \ No newline at end of file +} + +Status Xbox360WirelessController::OutputBuffer() +{ + if (m_outputBuffer.empty()) + return 1; + + Status rc = m_outPipe->Write(m_outputBuffer[0].packet, m_outputBuffer[0].length); + m_outputBuffer.erase(m_outputBuffer.begin()); + return rc; +} diff --git a/SwitchUSB/source/SwitchAbstractedPadHandler.cpp b/SwitchUSB/source/SwitchAbstractedPadHandler.cpp index 7fe7d44..c38691f 100644 --- a/SwitchUSB/source/SwitchAbstractedPadHandler.cpp +++ b/SwitchUSB/source/SwitchAbstractedPadHandler.cpp @@ -1,4 +1,5 @@ #include "SwitchAbstractedPadHandler.h" +#include "ControllerHelpers.h" #include #include @@ -51,6 +52,13 @@ Result SwitchAbstractedPadHandler::Initialize() WriteToLog("Failed to iniitalize vibration device with error ", rc2); */ + if (DoesControllerSupport(m_controllerHandler.GetController()->GetType(), SUPPORTS_PAIRING)) + { + rc = InitOutputThread(); + if (R_FAILED(rc)) + return rc; + } + rc = InitInputThread(); if (R_FAILED(rc)) return rc; @@ -164,13 +172,17 @@ void SwitchAbstractedPadHandler::UpdateInput() void SwitchAbstractedPadHandler::UpdateOutput() { - Result rc; - HidVibrationValue value; - rc = hidGetActualVibrationValue(&m_vibrationDeviceHandle, &value); - if (R_FAILED(rc)) + if (R_SUCCEEDED(m_controllerHandler.GetController()->OutputBuffer())) return; - rc = GetController()->SetRumble(static_cast(value.amp_high * 255.0f), static_cast(value.amp_low * 255.0f)); + if (DoesControllerSupport(m_controllerHandler.GetController()->GetType(), SUPPORTS_RUMBLE)) + { + Result rc; + HidVibrationValue value; + rc = hidGetActualVibrationValue(&m_vibrationDeviceHandle, &value); + if (R_SUCCEEDED(rc)) + GetController()->SetRumble(static_cast(value.amp_high * 255.0f), static_cast(value.amp_low * 255.0f)); + } svcSleepThread(1e+7L); } \ No newline at end of file diff --git a/SwitchUSB/source/SwitchHDLHandler.cpp b/SwitchUSB/source/SwitchHDLHandler.cpp index c4ba50b..819985e 100644 --- a/SwitchUSB/source/SwitchHDLHandler.cpp +++ b/SwitchUSB/source/SwitchHDLHandler.cpp @@ -1,4 +1,5 @@ #include "SwitchHDLHandler.h" +#include "ControllerHelpers.h" #include SwitchHDLHandler::SwitchHDLHandler(std::unique_ptr &&controller) @@ -49,6 +50,13 @@ Result SwitchHDLHandler::Initialize() WriteToLog("Failed to iniitalize vibration with error ", rc2); */ + if (DoesControllerSupport(m_controllerHandler.GetController()->GetType(), SUPPORTS_PAIRING)) + { + rc = InitOutputThread(); + if (R_FAILED(rc)) + return rc; + } + rc = InitInputThread(); if (R_FAILED(rc)) return rc; @@ -155,14 +163,17 @@ void SwitchHDLHandler::UpdateInput() void SwitchHDLHandler::UpdateOutput() { - //Implement rumble here - Result rc; - HidVibrationValue value; - rc = hidGetActualVibrationValue(&m_vibrationDeviceHandle, &value); - if (R_FAILED(rc)) + if (R_SUCCEEDED(m_controllerHandler.GetController()->OutputBuffer())) return; - rc = GetController()->SetRumble(static_cast(value.amp_high * 255.0f), static_cast(value.amp_low * 255.0f)); + if (DoesControllerSupport(m_controllerHandler.GetController()->GetType(), SUPPORTS_RUMBLE)) + { + Result rc; + HidVibrationValue value; + rc = hidGetActualVibrationValue(&m_vibrationDeviceHandle, &value); + if (R_SUCCEEDED(rc)) + GetController()->SetRumble(static_cast(value.amp_high * 255.0f), static_cast(value.amp_low * 255.0f)); + } svcSleepThread(1e+7L); } \ No newline at end of file