summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp66
-rw-r--r--src/input_common/gcadapter/gc_adapter.h10
-rw-r--r--src/input_common/gcadapter/gc_poller.cpp23
3 files changed, 64 insertions, 35 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp
index 6d9f4d9eb..38cf02f7e 100644
--- a/src/input_common/gcadapter/gc_adapter.cpp
+++ b/src/input_common/gcadapter/gc_adapter.cpp
@@ -24,6 +24,7 @@ Adapter::Adapter() {
24 LOG_INFO(Input, "GC Adapter Initialization started"); 24 LOG_INFO(Input, "GC Adapter Initialization started");
25 25
26 current_status = NO_ADAPTER_DETECTED; 26 current_status = NO_ADAPTER_DETECTED;
27 get_origin.fill(true);
27 28
28 const int init_res = libusb_init(&libusb_ctx); 29 const int init_res = libusb_init(&libusb_ctx);
29 if (init_res == LIBUSB_SUCCESS) { 30 if (init_res == LIBUSB_SUCCESS) {
@@ -35,13 +36,8 @@ Adapter::Adapter() {
35 36
36GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { 37GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) {
37 GCPadStatus pad = {}; 38 GCPadStatus pad = {};
38 bool get_origin = false;
39 39
40 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); 40 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4);
41 if (type != ControllerTypes::None) {
42 get_origin = true;
43 }
44
45 adapter_controllers_status[port] = type; 41 adapter_controllers_status[port] = type;
46 42
47 static constexpr std::array<PadButton, 8> b1_buttons{ 43 static constexpr std::array<PadButton, 8> b1_buttons{
@@ -57,6 +53,11 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_pa
57 PadButton::PAD_TRIGGER_L, 53 PadButton::PAD_TRIGGER_L,
58 }; 54 };
59 55
56 if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) {
57 // Controller may have been disconnected, recalibrate if reconnected.
58 get_origin[port] = true;
59 }
60
60 if (adapter_controllers_status[port] != ControllerTypes::None) { 61 if (adapter_controllers_status[port] != ControllerTypes::None) {
61 const u8 b1 = adapter_payload[1 + (9 * port) + 1]; 62 const u8 b1 = adapter_payload[1 + (9 * port) + 1];
62 const u8 b2 = adapter_payload[1 + (9 * port) + 2]; 63 const u8 b2 = adapter_payload[1 + (9 * port) + 2];
@@ -73,16 +74,22 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_pa
73 } 74 }
74 } 75 }
75 76
76 if (get_origin) {
77 pad.button |= PAD_GET_ORIGIN;
78 }
79
80 pad.stick_x = adapter_payload[1 + (9 * port) + 3]; 77 pad.stick_x = adapter_payload[1 + (9 * port) + 3];
81 pad.stick_y = adapter_payload[1 + (9 * port) + 4]; 78 pad.stick_y = adapter_payload[1 + (9 * port) + 4];
82 pad.substick_x = adapter_payload[1 + (9 * port) + 5]; 79 pad.substick_x = adapter_payload[1 + (9 * port) + 5];
83 pad.substick_y = adapter_payload[1 + (9 * port) + 6]; 80 pad.substick_y = adapter_payload[1 + (9 * port) + 6];
84 pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; 81 pad.trigger_left = adapter_payload[1 + (9 * port) + 7];
85 pad.trigger_right = adapter_payload[1 + (9 * port) + 8]; 82 pad.trigger_right = adapter_payload[1 + (9 * port) + 8];
83
84 if (get_origin[port]) {
85 origin_status[port].stick_x = pad.stick_x;
86 origin_status[port].stick_y = pad.stick_y;
87 origin_status[port].substick_x = pad.substick_x;
88 origin_status[port].substick_y = pad.substick_y;
89 origin_status[port].trigger_left = pad.trigger_left;
90 origin_status[port].trigger_right = pad.trigger_right;
91 get_origin[port] = false;
92 }
86 } 93 }
87 return pad; 94 return pad;
88} 95}
@@ -131,31 +138,31 @@ void Adapter::Read() {
131 for (std::size_t port = 0; port < pads.size(); ++port) { 138 for (std::size_t port = 0; port < pads.size(); ++port) {
132 pads[port] = GetPadStatus(port, adapter_payload_copy); 139 pads[port] = GetPadStatus(port, adapter_payload_copy);
133 if (DeviceConnected(port) && configuring) { 140 if (DeviceConnected(port) && configuring) {
134 if (pads[port].button != PAD_GET_ORIGIN) { 141 if (pads[port].button != 0) {
135 pad_queue[port].Push(pads[port]); 142 pad_queue[port].Push(pads[port]);
136 } 143 }
137 144
138 // Accounting for a threshold here because of some controller variance 145 // Accounting for a threshold here because of some controller variance
139 if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD || 146 if (pads[port].stick_x > origin_status[port].stick_x + pads[port].THRESHOLD ||
140 pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) { 147 pads[port].stick_x < origin_status[port].stick_x - pads[port].THRESHOLD) {
141 pads[port].axis = GCAdapter::PadAxes::StickX; 148 pads[port].axis = GCAdapter::PadAxes::StickX;
142 pads[port].axis_value = pads[port].stick_x; 149 pads[port].axis_value = pads[port].stick_x;
143 pad_queue[port].Push(pads[port]); 150 pad_queue[port].Push(pads[port]);
144 } 151 }
145 if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD || 152 if (pads[port].stick_y > origin_status[port].stick_y + pads[port].THRESHOLD ||
146 pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) { 153 pads[port].stick_y < origin_status[port].stick_y - pads[port].THRESHOLD) {
147 pads[port].axis = GCAdapter::PadAxes::StickY; 154 pads[port].axis = GCAdapter::PadAxes::StickY;
148 pads[port].axis_value = pads[port].stick_y; 155 pads[port].axis_value = pads[port].stick_y;
149 pad_queue[port].Push(pads[port]); 156 pad_queue[port].Push(pads[port]);
150 } 157 }
151 if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD || 158 if (pads[port].substick_x > origin_status[port].substick_x + pads[port].THRESHOLD ||
152 pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) { 159 pads[port].substick_x < origin_status[port].substick_x - pads[port].THRESHOLD) {
153 pads[port].axis = GCAdapter::PadAxes::SubstickX; 160 pads[port].axis = GCAdapter::PadAxes::SubstickX;
154 pads[port].axis_value = pads[port].substick_x; 161 pads[port].axis_value = pads[port].substick_x;
155 pad_queue[port].Push(pads[port]); 162 pad_queue[port].Push(pads[port]);
156 } 163 }
157 if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD || 164 if (pads[port].substick_y > origin_status[port].substick_y + pads[port].THRESHOLD ||
158 pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) { 165 pads[port].substick_y < origin_status[port].substick_y - pads[port].THRESHOLD) {
159 pads[port].axis = GCAdapter::PadAxes::SubstickY; 166 pads[port].axis = GCAdapter::PadAxes::SubstickY;
160 pads[port].axis_value = pads[port].substick_y; 167 pads[port].axis_value = pads[port].substick_y;
161 pad_queue[port].Push(pads[port]); 168 pad_queue[port].Push(pads[port]);
@@ -344,6 +351,7 @@ void Adapter::Reset() {
344 adapter_input_thread.join(); 351 adapter_input_thread.join();
345 352
346 adapter_controllers_status.fill(ControllerTypes::None); 353 adapter_controllers_status.fill(ControllerTypes::None);
354 get_origin.fill(true);
347 current_status = NO_ADAPTER_DETECTED; 355 current_status = NO_ADAPTER_DETECTED;
348 356
349 if (usb_adapter_handle) { 357 if (usb_adapter_handle) {
@@ -366,6 +374,7 @@ void Adapter::ResetDeviceType(int port) {
366} 374}
367 375
368void Adapter::BeginConfiguration() { 376void Adapter::BeginConfiguration() {
377 get_origin.fill(true);
369 for (auto& pq : pad_queue) { 378 for (auto& pq : pad_queue) {
370 pq.Clear(); 379 pq.Clear();
371 } 380 }
@@ -395,4 +404,25 @@ const std::array<GCState, 4>& Adapter::GetPadState() const {
395 return state; 404 return state;
396} 405}
397 406
407int Adapter::GetOriginValue(int port, int axis) const {
408 const auto& status = origin_status[port];
409
410 switch (static_cast<PadAxes>(axis)) {
411 case PadAxes::StickX:
412 return status.stick_x;
413 case PadAxes::StickY:
414 return status.stick_y;
415 case PadAxes::SubstickX:
416 return status.substick_x;
417 case PadAxes::SubstickY:
418 return status.substick_y;
419 case PadAxes::TriggerLeft:
420 return status.trigger_left;
421 case PadAxes::TriggerRight:
422 return status.trigger_right;
423 default:
424 return 0;
425 }
426}
427
398} // namespace GCAdapter 428} // namespace GCAdapter
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h
index b1c2a1958..a67485586 100644
--- a/src/input_common/gcadapter/gc_adapter.h
+++ b/src/input_common/gcadapter/gc_adapter.h
@@ -14,12 +14,6 @@
14 14
15namespace GCAdapter { 15namespace GCAdapter {
16 16
17enum {
18 PAD_USE_ORIGIN = 0x0080,
19 PAD_GET_ORIGIN = 0x2000,
20 PAD_ERR_STATUS = 0x8000,
21};
22
23enum class PadButton { 17enum class PadButton {
24 PAD_BUTTON_LEFT = 0x0001, 18 PAD_BUTTON_LEFT = 0x0001,
25 PAD_BUTTON_RIGHT = 0x0002, 19 PAD_BUTTON_RIGHT = 0x0002,
@@ -103,6 +97,8 @@ public:
103 std::array<GCState, 4>& GetPadState(); 97 std::array<GCState, 4>& GetPadState();
104 const std::array<GCState, 4>& GetPadState() const; 98 const std::array<GCState, 4>& GetPadState() const;
105 99
100 int GetOriginValue(int port, int axis) const;
101
106private: 102private:
107 GCPadStatus GetPadStatus(int port, const std::array<u8, 37>& adapter_payload); 103 GCPadStatus GetPadStatus(int port, const std::array<u8, 37>& adapter_payload);
108 104
@@ -156,6 +152,8 @@ private:
156 152
157 std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue; 153 std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;
158 std::array<GCState, 4> state; 154 std::array<GCState, 4> state;
155 std::array<bool, 4> get_origin;
156 std::array<GCPadStatus, 4> origin_status;
159}; 157};
160 158
161} // namespace GCAdapter 159} // namespace GCAdapter
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp
index 385ce8430..ad321e933 100644
--- a/src/input_common/gcadapter/gc_poller.cpp
+++ b/src/input_common/gcadapter/gc_poller.cpp
@@ -34,16 +34,11 @@ public:
34 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, 34 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_,
35 GCAdapter::Adapter* adapter) 35 GCAdapter::Adapter* adapter)
36 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), 36 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
37 gcadapter(adapter) { 37 gcadapter(adapter), origin_value(adapter->GetOriginValue(port_, axis_)) {}
38 // L/R triggers range is only in positive direction beginning near 0
39 // 0.0 threshold equates to near half trigger press, but threshold accounts for variability.
40 if (axis > 3) {
41 threshold *= -0.5;
42 }
43 }
44 38
45 bool GetStatus() const override { 39 bool GetStatus() const override {
46 const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; 40 const float current_axis_value = gcadapter->GetPadState()[port].axes.at(axis);
41 const float axis_value = (current_axis_value - origin_value) / 128.0f;
47 if (trigger_if_greater) { 42 if (trigger_if_greater) {
48 // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently 43 // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently
49 // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick 44 // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick
@@ -58,6 +53,7 @@ private:
58 float threshold; 53 float threshold;
59 bool trigger_if_greater; 54 bool trigger_if_greater;
60 GCAdapter::Adapter* gcadapter; 55 GCAdapter::Adapter* gcadapter;
56 const float origin_value;
61}; 57};
62 58
63GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) 59GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
@@ -144,14 +140,17 @@ void GCButtonFactory::EndConfiguration() {
144class GCAnalog final : public Input::AnalogDevice { 140class GCAnalog final : public Input::AnalogDevice {
145public: 141public:
146 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) 142 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter)
147 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} 143 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter),
144 origin_value_x(adapter->GetOriginValue(port_, axis_x_)),
145 origin_value_y(adapter->GetOriginValue(port_, axis_y_)) {}
148 146
149 float GetAxis(int axis) const { 147 float GetAxis(int axis) const {
150 std::lock_guard lock{mutex}; 148 std::lock_guard lock{mutex};
149 const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y;
151 // division is not by a perfect 128 to account for some variance in center location 150 // division is not by a perfect 128 to account for some variance in center location
152 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range 151 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range
153 // [20-230] 152 // [20-230]
154 return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; 153 return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / 95.0f;
155 } 154 }
156 155
157 std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { 156 std::pair<float, float> GetAnalog(int axis_x, int axis_y) const {
@@ -201,8 +200,10 @@ private:
201 const int axis_x; 200 const int axis_x;
202 const int axis_y; 201 const int axis_y;
203 const float deadzone; 202 const float deadzone;
204 mutable std::mutex mutex;
205 GCAdapter::Adapter* gcadapter; 203 GCAdapter::Adapter* gcadapter;
204 const float origin_value_x;
205 const float origin_value_y;
206 mutable std::mutex mutex;
206}; 207};
207 208
208/// An analog device factory that creates analog devices from GC Adapter 209/// An analog device factory that creates analog devices from GC Adapter