Compare commits

...

2 Commits

Author SHA1 Message Date
german
4057af1871 Address Comments 2020-10-03 22:15:53 -05:00
german
9336dec9dc Enable GC vibration 2020-10-03 15:24:00 -05:00
3 changed files with 53 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
#include "common/logging/log.h"
#include "common/param_package.h"
#include "core/settings.h"
#include "input_common/gcadapter/gc_adapter.h"
#include "input_common/settings.h"
@@ -129,6 +130,23 @@ void Adapter::Read() {
adapter_thread_running = false; // error reading from adapter, stop reading.
break;
}
if (Settings::values.vibration_enabled) {
// Modulate vibrations to 8 states
vibration_counter = (vibration_counter + 1) % 8;
const bool p1 = rumble[0] > vibration_counter;
const bool p2 = rumble[1] > vibration_counter;
const bool p3 = rumble[2] > vibration_counter;
const bool p4 = rumble[3] > vibration_counter;
Vibrate(p1, p2, p3, p4);
} else {
// stop all onging vibrations
if (vibration_counter != 8) {
vibration_counter = 8;
Vibrate(false, false, false, false);
}
}
for (std::size_t port = 0; port < pads.size(); ++port) {
pads[port] = GetPadStatus(port, adapter_payload);
if (DeviceConnected(port) && configuring) {
@@ -160,6 +178,8 @@ void Adapter::Setup() {
adapter_controllers_status.fill(ControllerTypes::None);
// Initialize all ports to store axis origin values
get_origin.fill(true);
// Initialize all rumble to no vibrate
rumble.fill(0);
// pointer to list of connected usb devices
libusb_device** devices{};
@@ -380,6 +400,23 @@ InputCommon::AnalogMapping Adapter::GetAnalogMappingForDevice(
return mapping;
}
void Adapter::Vibrate(bool p1, bool p2, bool p3, bool p4) {
int size = 0;
std::array<u8, 5> payload = {0x11, p1, p2, p3, p4};
const int err = libusb_interrupt_transfer(usb_adapter_handle, output_endpoint, payload.data(),
sizeof(payload), &size, 16);
if (err) {
LOG_ERROR(Input, "Adapter libusb write failed: {}", libusb_error_name(err));
}
}
bool Adapter::RumblePlay(int port, f32 amplitude) {
const u8 raw_amp = static_cast<u8>(amplitude * 0x8);
rumble[port] = raw_amp;
return false;
}
bool Adapter::DeviceConnected(std::size_t port) const {
return adapter_controllers_status[port] != ControllerTypes::None;
}

View File

@@ -91,6 +91,8 @@ public:
int GetOriginValue(int port, int axis) const;
bool RumblePlay(int port, f32 amplitude);
private:
GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload);
@@ -113,6 +115,9 @@ private:
/// For use in initialization, querying devices to find the adapter
void Setup();
// Updates vibration state of all controllers
void Vibrate(bool p1, bool p2, bool p3, bool p4);
libusb_device_handle* usb_adapter_handle = nullptr;
std::thread adapter_input_thread;
@@ -122,10 +127,12 @@ private:
u8 input_endpoint = 0;
u8 output_endpoint = 0;
u8 vibration_counter = 0;
bool configuring = false;
std::array<GCState, 4> state;
std::array<u8, 4> rumble;
std::array<bool, 4> get_origin;
std::array<GCPadStatus, 4> origin_status;
std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;

View File

@@ -15,7 +15,7 @@ namespace InputCommon {
class GCButton final : public Input::ButtonDevice {
public:
explicit GCButton(int port_, int button_, const GCAdapter::Adapter* adapter)
explicit GCButton(int port_, int button_, GCAdapter::Adapter* adapter)
: port(port_), button(button_), gcadapter(adapter) {}
~GCButton() override;
@@ -27,10 +27,17 @@ public:
return false;
}
bool SetRumblePlay(f32 amp_high, f32 amp_low, f32 freq_high, f32 freq_low) const override {
const f32 amplitude = amp_high + amp_low > 2 ? 1.0f : (amp_high + amp_low) * 0.5f;
const f32 new_amp = pow(amplitude, 0.5f) * (3.0f - 2.0f * pow(amplitude, 0.15f));
return gcadapter->RumblePlay(port, new_amp);
}
private:
const int port;
const int button;
const GCAdapter::Adapter* gcadapter;
GCAdapter::Adapter* gcadapter;
};
class GCAxisButton final : public Input::ButtonDevice {