From 7f5b6ed788afce17622b8d61dad8a267ac170b53 Mon Sep 17 00:00:00 2001 From: Filoppi Date: Tue, 4 May 2021 23:57:25 +0300 Subject: [PATCH 1/4] Guitar: consistency of BoundCount check only control[0] was checked, and not one, which seems random. Either both or none should be checked. --- Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp index 88a14c3400..23bc8f09c9 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp @@ -107,7 +107,8 @@ void Guitar::Update() } // slider bar - if (m_slider_bar->controls[0]->control_ref->BoundCount()) + if (m_slider_bar->controls[0]->control_ref->BoundCount() && + m_slider_bar->controls[1]->control_ref->BoundCount()) { const ControllerEmu::Slider::StateData slider_data = m_slider_bar->GetState(); From d43a06ff6ad713f25b6b80ad6dc4bba8e003db04 Mon Sep 17 00:00:00 2001 From: Filoppi Date: Tue, 4 May 2021 23:58:08 +0300 Subject: [PATCH 2/4] IMUAccelerometer: consistency of BoundCount checks Similar to the guitar, only control[0] was checked, and that felt random. --- .../ControllerEmu/ControlGroup/IMUAccelerometer.cpp | 11 ++++++++--- .../ControllerEmu/ControlGroup/IMUAccelerometer.h | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.cpp index 1e23c5dffe..ef9f519633 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.cpp @@ -4,13 +4,12 @@ #include "InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.h" +#include #include #include "Common/Common.h" #include "Core/HW/WiimoteEmu/WiimoteEmu.h" -#include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControllerEmu/Control/Control.h" -#include "InputCommon/ControllerEmu/Control/Input.h" namespace ControllerEmu { @@ -25,9 +24,15 @@ IMUAccelerometer::IMUAccelerometer(std::string name_, std::string ui_name_) AddInput(Translate, _trans("Backward")); } +bool IMUAccelerometer::AreInputsBound() const +{ + return std::all_of(controls.begin(), controls.end(), + [](const auto& control) { return control->control_ref->BoundCount() > 0; }); +} + std::optional IMUAccelerometer::GetState() const { - if (controls[0]->control_ref->BoundCount() == 0) + if (!AreInputsBound()) return std::nullopt; StateData state; diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.h index b5d8e0c935..57fa4ea0b6 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.h @@ -20,5 +20,7 @@ public: IMUAccelerometer(std::string name, std::string ui_name); std::optional GetState() const; + + bool AreInputsBound() const; }; } // namespace ControllerEmu From a19a0096db4bbee3b7ae6411c0b6e801c091783e Mon Sep 17 00:00:00 2001 From: Filoppi Date: Wed, 5 May 2021 00:01:20 +0300 Subject: [PATCH 3/4] InputCommon: improve code that returns a controller attachment index casting a value to a u32 when it's originally an int, and it's exposed as int to users, could end up in cases where a negative number would result as a positive one. This doesn't really affect the value range of the attachment enum, still I think the code was wrong. Heavily tested. --- .../InputCommon/ControllerEmu/ControlGroup/Attachments.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp index 6f980d8c60..18ccbc1966 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp @@ -17,10 +17,11 @@ void Attachments::AddAttachment(std::unique_ptr att) u32 Attachments::GetSelectedAttachment() const { - const u32 value = m_selection_value.GetValue(); + // This is originally an int, treat it as such + const int value = m_selection_value.GetValue(); - if (value < m_attachments.size()) - return value; + if (value > 0 && static_cast(value) < m_attachments.size()) + return u32(value); return 0; } From 379ffc268d30c66c9cc43a15998369de769efaf0 Mon Sep 17 00:00:00 2001 From: Filoppi Date: Wed, 5 May 2021 00:04:47 +0300 Subject: [PATCH 4/4] IMUGyroscope: make GetState update optional (on by default), fix const, clean code My future PRs will split the UI state from the Emulation State of some of these emulated controller values and this readies the code for it. --- .../ControlGroup/IMUGyroscope.cpp | 36 +++++++++++++------ .../ControllerEmu/ControlGroup/IMUGyroscope.h | 15 ++++---- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp index 95c74b1896..212e2e0f8d 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp @@ -12,7 +12,6 @@ #include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControllerEmu/Control/Control.h" -#include "InputCommon/ControllerEmu/Control/Input.h" namespace ControllerEmu { @@ -52,13 +51,13 @@ IMUGyroscope::IMUGyroscope(std::string name_, std::string ui_name_) 3, 0, 30); } -void IMUGyroscope::RestartCalibration() const +void IMUGyroscope::RestartCalibration() { m_calibration_period_start = Clock::now(); m_running_calibration.Clear(); } -void IMUGyroscope::UpdateCalibration(const StateData& state) const +void IMUGyroscope::UpdateCalibration(const StateData& state) { const auto now = Clock::now(); const auto calibration_period = m_calibration_period_setting.GetValue(); @@ -123,21 +122,36 @@ auto IMUGyroscope::GetRawState() const -> StateData controls[4]->GetState() - controls[5]->GetState()); } -std::optional IMUGyroscope::GetState() const +bool IMUGyroscope::AreInputsBound() const { - if (std::all_of(controls.begin(), controls.end(), - [](const auto& control) { return control->control_ref->BoundCount() == 0; })) + return std::all_of(controls.begin(), controls.end(), + [](const auto& control) { return control->control_ref->BoundCount() > 0; }); +} + +bool IMUGyroscope::CanCalibrate() const +{ + // If the input gate is disabled, miscalibration to zero values would occur. + return ControlReference::GetInputGate(); +} + +std::optional IMUGyroscope::GetState(bool update) +{ + if (!AreInputsBound()) { - // Set calibration to zero. - m_calibration = {}; - RestartCalibration(); + if (update) + { + // Set calibration to zero. + m_calibration = {}; + RestartCalibration(); + } return std::nullopt; } auto state = GetRawState(); - // If the input gate is disabled, miscalibration to zero values would occur. - if (ControlReference::GetInputGate()) + // Alternatively we could open the control gate around GetRawState() while calibrating, + // but that would imply background input would temporarily be treated differently for our controls + if (update && CanCalibrate()) UpdateCalibration(state); state -= m_calibration; diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h index 459f885882..c040e11781 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h @@ -23,7 +23,8 @@ public: IMUGyroscope(std::string name, std::string ui_name); StateData GetRawState() const; - std::optional GetState() const; + // Also updates the state by default + std::optional GetState(bool update = true); // Value is in rad/s. ControlState GetDeadzone() const; @@ -33,14 +34,16 @@ public: private: using Clock = std::chrono::steady_clock; - void RestartCalibration() const; - void UpdateCalibration(const StateData&) const; + bool AreInputsBound() const; + bool CanCalibrate() const; + void RestartCalibration(); + void UpdateCalibration(const StateData&); SettingValue m_deadzone_setting; SettingValue m_calibration_period_setting; - mutable StateData m_calibration = {}; - mutable MathUtil::RunningMean m_running_calibration; - mutable Clock::time_point m_calibration_period_start = Clock::now(); + StateData m_calibration = {}; + MathUtil::RunningMean m_running_calibration; + Clock::time_point m_calibration_period_start = Clock::now(); }; } // namespace ControllerEmu