shared_ptr for the GC adapter class, constexpr constants

This commit is contained in:
Ameer 2020-06-21 21:50:58 -04:00
parent 968d631aa5
commit 46b4461fbb
5 changed files with 55 additions and 69 deletions

View file

@ -6,7 +6,6 @@
#include "input_common/gcadapter/gc_adapter.h"
namespace GCAdapter {
Adapter* Adapter::adapter_instance{nullptr};
Adapter::Adapter() {
if (usb_adapter_handle != nullptr) {
@ -20,13 +19,6 @@ Adapter::Adapter() {
StartScanThread();
}
Adapter* Adapter::GetInstance() {
if (!adapter_instance) {
adapter_instance = new Adapter;
}
return adapter_instance;
}
GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) {
GCPadStatus pad = {};
bool get_origin = false;
@ -151,34 +143,26 @@ void Adapter::Read() {
}
// Accounting for a threshold here because of some controller variance
if (pad[port].stick_x >
pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD ||
pad[port].stick_x <
pad_constants.MAIN_STICK_CENTER_X - pad_constants.THRESHOLD) {
if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD ||
pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::StickX;
pad[port].axis_value = pad[port].stick_x;
pad_queue[port].Push(pad[port]);
}
if (pad[port].stick_y >
pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD ||
pad[port].stick_y <
pad_constants.MAIN_STICK_CENTER_Y - pad_constants.THRESHOLD) {
if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD ||
pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::StickY;
pad[port].axis_value = pad[port].stick_y;
pad_queue[port].Push(pad[port]);
}
if (pad[port].substick_x >
pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD ||
pad[port].substick_x <
pad_constants.C_STICK_CENTER_X - pad_constants.THRESHOLD) {
if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD ||
pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::SubstickX;
pad[port].axis_value = pad[port].substick_x;
pad_queue[port].Push(pad[port]);
}
if (pad[port].substick_y >
pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD ||
pad[port].substick_y <
pad_constants.C_STICK_CENTER_Y - pad_constants.THRESHOLD) {
if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD ||
pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::SubstickY;
pad[port].axis_value = pad[port].substick_y;
pad_queue[port].Push(pad[port]);
@ -367,8 +351,16 @@ std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
return pad_queue;
}
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
return pad_queue;
}
std::array<GCState, 4>& Adapter::GetPadState() {
return state;
}
const std::array<GCState, 4>& Adapter::GetPadState() const {
return state;
}
} // end of namespace GCAdapter

View file

@ -46,17 +46,6 @@ enum class PadAxes : u8 {
TriggerRight,
Undefined,
};
const struct GCPadConstants {
const u8 MAIN_STICK_CENTER_X = 0x80;
const u8 MAIN_STICK_CENTER_Y = 0x80;
const u8 MAIN_STICK_RADIUS = 0x7f;
const u8 C_STICK_CENTER_X = 0x80;
const u8 C_STICK_CENTER_Y = 0x80;
const u8 C_STICK_RADIUS = 0x7f;
const u8 TRIGGER_CENTER = 20;
const u8 THRESHOLD = 10;
} pad_constants;
struct GCPadStatus {
u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
@ -67,6 +56,15 @@ struct GCPadStatus {
u8 trigger_left; // 0 <= trigger_left <= 255
u8 trigger_right; // 0 <= trigger_right <= 255
static constexpr u8 MAIN_STICK_CENTER_X = 0x80;
static constexpr u8 MAIN_STICK_CENTER_Y = 0x80;
static constexpr u8 MAIN_STICK_RADIUS = 0x7f;
static constexpr u8 C_STICK_CENTER_X = 0x80;
static constexpr u8 C_STICK_CENTER_Y = 0x80;
static constexpr u8 C_STICK_RADIUS = 0x7f;
static constexpr u8 TRIGGER_CENTER = 20;
static constexpr u8 THRESHOLD = 10;
u8 port;
PadAxes axis = PadAxes::Undefined;
u8 axis_value = 255;
@ -87,28 +85,22 @@ enum {
/// Singleton Adapter class
class Adapter {
public:
/// For retreiving the singleton instance
static Adapter* GetInstance();
/// Used for polling
void BeginConfiguration();
void EndConfiguration();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
std::array<GCState, 4>& GetPadState();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const;
std::array<GCState, 4>& GetPadState() const;
private:
/// Singleton instance.
static Adapter* adapter_instance;
/// Initialize the GC Adapter capture and read sequence
Adapter();
/// Close the adapter read thread and release the adapter
~Adapter();
/// Used for polling
void BeginConfiguration();
void EndConfiguration();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const;
std::array<GCState, 4>& GetPadState();
const std::array<GCState, 4>& GetPadState() const;
private:
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
void PadToState(GCPadStatus pad, GCState& state);

View file

@ -14,7 +14,8 @@ namespace InputCommon {
class GCButton final : public Input::ButtonDevice {
public:
explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter)
explicit GCButton(int port_, int button_, int axis_,
std::shared_ptr<GCAdapter::Adapter> adapter)
: port(port_), button(button_), gcadapter(adapter) {}
~GCButton() override;
@ -26,13 +27,13 @@ public:
private:
const int port;
const int button;
GCAdapter::Adapter* gcadapter;
std::shared_ptr<GCAdapter::Adapter> gcadapter;
};
class GCAxisButton final : public Input::ButtonDevice {
public:
explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_,
GCAdapter::Adapter* adapter)
std::shared_ptr<GCAdapter::Adapter> adapter)
: port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
gcadapter(adapter) {}
@ -49,12 +50,11 @@ private:
const int axis;
float threshold;
bool trigger_if_greater;
GCAdapter::Adapter* gcadapter;
std::shared_ptr<GCAdapter::Adapter> gcadapter;
};
GCButtonFactory::GCButtonFactory() {
adapter = GCAdapter::Adapter::GetInstance();
}
GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
: adapter(adapter_) {}
GCButton::~GCButton() = default;
@ -171,7 +171,8 @@ void GCButtonFactory::EndConfiguration() {
class GCAnalog final : public Input::AnalogDevice {
public:
GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter)
GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_,
std::shared_ptr<GCAdapter::Adapter> adapter)
: port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {}
float GetAxis(int axis) const {
@ -230,13 +231,12 @@ private:
const int axis_y;
const float deadzone;
mutable std::mutex mutex;
GCAdapter::Adapter* gcadapter;
std::shared_ptr<GCAdapter::Adapter> gcadapter;
};
/// An analog device factory that creates analog devices from GC Adapter
GCAnalogFactory::GCAnalogFactory() {
adapter = GCAdapter::Adapter::GetInstance();
};
GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
: adapter(adapter_) {}
/**
* Creates analog device from joystick axes

View file

@ -15,7 +15,7 @@ namespace InputCommon {
*/
class GCButtonFactory final : public Input::Factory<Input::ButtonDevice> {
public:
GCButtonFactory();
GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_);
/**
* Creates a button device from a button press
@ -35,14 +35,14 @@ public:
}
private:
GCAdapter::Adapter* adapter;
std::shared_ptr<GCAdapter::Adapter> adapter;
bool polling = false;
};
/// An analog device factory that creates analog devices from GC Adapter
class GCAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
public:
GCAnalogFactory();
GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_);
std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override;
Common::ParamPackage GetNextInput();
@ -55,7 +55,7 @@ public:
}
private:
GCAdapter::Adapter* adapter;
std::shared_ptr<GCAdapter::Adapter> adapter;
int analog_x_axis = -1;
int analog_y_axis = -1;
int controller_number = -1;

View file

@ -25,13 +25,15 @@ static std::shared_ptr<MotionEmu> motion_emu;
static std::unique_ptr<SDL::State> sdl;
#endif
static std::unique_ptr<CemuhookUDP::State> udp;
static std::shared_ptr<GCAdapter::Adapter> gcadapter;
static std::shared_ptr<GCButtonFactory> gcbuttons;
static std::shared_ptr<GCAnalogFactory> gcanalog;
void Init() {
gcbuttons = std::make_shared<GCButtonFactory>();
gcadapter = std::make_shared<GCAdapter::Adapter>();
gcbuttons = std::make_shared<GCButtonFactory>(gcadapter);
Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons);
gcanalog = std::make_shared<GCAnalogFactory>();
gcanalog = std::make_shared<GCAnalogFactory>(gcadapter);
Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog);
keyboard = std::make_shared<Keyboard>();