summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerEmu
diff options
context:
space:
mode:
authorPierre Bourdon <delroth@gmail.com>2020-02-22 17:20:44 +0100
committerGitHub <noreply@github.com>2020-02-22 17:20:44 +0100
commit2b6a1ee4d87a80418ade913e15745d78309f7f67 (patch)
tree5989875b0c893ae1ae843a2ef6f1bad3969e39f3 /Source/Core/InputCommon/ControllerEmu
parentf3c89fd6c2e190b41e7cf3a731b1a303b78f359a (diff)
parent70ac9ad2e6faf120c8ef4859141675c33e5f33c7 (diff)
Merge pull request #8575 from jordan-woyak/ciface-wiimotes
InputCommon: Add support for Wii Remotes in ControllerInterface
Diffstat (limited to 'Source/Core/InputCommon/ControllerEmu')
-rw-r--r--Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp4
-rw-r--r--Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp130
-rw-r--r--Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h22
-rw-r--r--Source/Core/InputCommon/ControllerEmu/ControllerEmu.h101
4 files changed, 251 insertions, 6 deletions
diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp
index b5a621e43b..382bd8c2a4 100644
--- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp
+++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp
@@ -50,7 +50,7 @@ Cursor::Cursor(std::string name, std::string ui_name)
_trans("°"),
// i18n: Refers to emulated wii remote movements.
_trans("Total rotation about the yaw axis.")},
- 15, 0, 180);
+ 15, 0, 360);
AddSetting(&m_pitch_setting,
// i18n: Refers to an amount of rotational movement about the "pitch" axis.
@@ -59,7 +59,7 @@ Cursor::Cursor(std::string name, std::string ui_name)
_trans("°"),
// i18n: Refers to emulated wii remote movements.
_trans("Total rotation about the pitch axis.")},
- 15, 0, 180);
+ 15, 0, 360);
AddSetting(&m_relative_setting, {_trans("Relative Input")}, false);
AddSetting(&m_autohide_setting, {_trans("Auto-Hide")}, false);
diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp
index 84f9f20821..8837c67f13 100644
--- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp
+++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.cpp
@@ -7,6 +7,7 @@
#include <memory>
#include "Common/Common.h"
+#include "Common/MathUtil.h"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/Control/Control.h"
@@ -14,6 +15,15 @@
namespace ControllerEmu
{
+// Maximum period for calculating an average stable value.
+// Just to prevent failures due to timer overflow.
+static constexpr auto MAXIMUM_CALIBRATION_DURATION = std::chrono::hours(1);
+
+// If calibration updates do not happen at this rate, restart calibration period.
+// This prevents calibration across periods of no regular updates. (e.g. between game sessions)
+// This is made slightly lower than the UI update frequency of 30.
+static constexpr auto WORST_ACCEPTABLE_CALIBRATION_UPDATE_FREQUENCY = 25;
+
IMUGyroscope::IMUGyroscope(std::string name, std::string ui_name)
: ControlGroup(std::move(name), std::move(ui_name), GroupType::IMUGyroscope)
{
@@ -23,18 +33,130 @@ IMUGyroscope::IMUGyroscope(std::string name, std::string ui_name)
AddInput(Translate, _trans("Roll Right"));
AddInput(Translate, _trans("Yaw Left"));
AddInput(Translate, _trans("Yaw Right"));
+
+ AddSetting(&m_deadzone_setting,
+ {_trans("Dead Zone"),
+ // i18n: "°/s" is the symbol for degrees (angular measurement) divided by seconds.
+ _trans("°/s"),
+ // i18n: Refers to the dead-zone setting of gyroscope input.
+ _trans("Angular velocity to ignore.")},
+ 2, 0, 180);
+
+ AddSetting(&m_calibration_period_setting,
+ {_trans("Calibration Period"),
+ // i18n: "s" is the symbol for seconds.
+ _trans("s"),
+ // i18n: Refers to the "Calibration" setting of gyroscope input.
+ _trans("Time period of stable input to trigger calibration. (zero to disable)")},
+ 3, 0, 30);
+}
+
+void IMUGyroscope::RestartCalibration() const
+{
+ m_calibration_period_start = Clock::now();
+ m_running_calibration.Clear();
+}
+
+void IMUGyroscope::UpdateCalibration(const StateData& state) const
+{
+ const auto now = Clock::now();
+ const auto calibration_period = m_calibration_period_setting.GetValue();
+
+ // If calibration time is zero. User is choosing to not calibrate.
+ if (!calibration_period)
+ {
+ // Set calibration to zero.
+ m_calibration = {};
+ RestartCalibration();
+ return;
+ }
+
+ // If there is no running calibration a new gyro was just mapped or calibration was just enabled,
+ // apply the current state as calibration, it's often better than zeros.
+ if (!m_running_calibration.Count())
+ {
+ m_calibration = state;
+ }
+ else
+ {
+ const auto calibration_freq =
+ m_running_calibration.Count() /
+ std::chrono::duration_cast<std::chrono::duration<double>>(now - m_calibration_period_start)
+ .count();
+
+ const auto potential_calibration = m_running_calibration.Mean();
+ const auto current_difference = state - potential_calibration;
+ const auto deadzone = GetDeadzone();
+
+ // Check for required calibration update frequency
+ // and if current data is within deadzone distance of mean stable value.
+ if (calibration_freq < WORST_ACCEPTABLE_CALIBRATION_UPDATE_FREQUENCY ||
+ std::any_of(current_difference.data.begin(), current_difference.data.end(),
+ [&](auto c) { return std::abs(c) > deadzone; }))
+ {
+ RestartCalibration();
+ }
+ }
+
+ // Update running mean stable value.
+ m_running_calibration.Push(state);
+
+ // Apply calibration after configured time.
+ const auto calibration_duration = now - m_calibration_period_start;
+ if (calibration_duration >= std::chrono::duration<double>(calibration_period))
+ {
+ m_calibration = m_running_calibration.Mean();
+
+ if (calibration_duration >= MAXIMUM_CALIBRATION_DURATION)
+ {
+ RestartCalibration();
+ m_running_calibration.Push(m_calibration);
+ }
+ }
+}
+
+auto IMUGyroscope::GetRawState() const -> StateData
+{
+ return StateData(controls[1]->GetState() - controls[0]->GetState(),
+ controls[2]->GetState() - controls[3]->GetState(),
+ controls[4]->GetState() - controls[5]->GetState());
}
std::optional<IMUGyroscope::StateData> IMUGyroscope::GetState() const
{
if (controls[0]->control_ref->BoundCount() == 0)
+ {
+ // 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())
+ UpdateCalibration(state);
+
+ state -= m_calibration;
+
+ // Apply "deadzone".
+ for (auto& c : state.data)
+ c *= std::abs(c) > GetDeadzone();
- StateData state;
- state.x = (controls[1]->GetState() - controls[0]->GetState());
- state.y = (controls[2]->GetState() - controls[3]->GetState());
- state.z = (controls[4]->GetState() - controls[5]->GetState());
return state;
}
+ControlState IMUGyroscope::GetDeadzone() const
+{
+ return m_deadzone_setting.GetValue() / 360 * MathUtil::TAU;
+}
+
+bool IMUGyroscope::IsCalibrating() const
+{
+ const auto calibration_period = m_calibration_period_setting.GetValue();
+ return calibration_period && (Clock::now() - m_calibration_period_start) >=
+ std::chrono::duration<double>(calibration_period);
+}
+
} // namespace ControllerEmu
diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h
index c173c2f8b2..459f885882 100644
--- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h
+++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h
@@ -4,11 +4,14 @@
#pragma once
+#include <chrono>
#include <optional>
#include <string>
+#include "Common/MathUtil.h"
#include "Common/Matrix.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
+#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
namespace ControllerEmu
{
@@ -19,6 +22,25 @@ public:
IMUGyroscope(std::string name, std::string ui_name);
+ StateData GetRawState() const;
std::optional<StateData> GetState() const;
+
+ // Value is in rad/s.
+ ControlState GetDeadzone() const;
+
+ bool IsCalibrating() const;
+
+private:
+ using Clock = std::chrono::steady_clock;
+
+ void RestartCalibration() const;
+ void UpdateCalibration(const StateData&) const;
+
+ SettingValue<double> m_deadzone_setting;
+ SettingValue<double> m_calibration_period_setting;
+
+ mutable StateData m_calibration = {};
+ mutable MathUtil::RunningMean<StateData> m_running_calibration;
+ mutable Clock::time_point m_calibration_period_start = Clock::now();
};
} // namespace ControllerEmu
diff --git a/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h b/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h
index 2611880ee9..3f497b1f88 100644
--- a/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h
+++ b/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h
@@ -11,6 +11,7 @@
#include <type_traits>
#include <vector>
+#include "Common/BitUtils.h"
#include "Common/Common.h"
#include "Common/IniFile.h"
#include "InputCommon/ControlReference/ExpressionParser.h"
@@ -27,6 +28,106 @@ namespace ControllerEmu
{
class ControlGroup;
+// Represents calibration data found on Wii Remotes + extensions with a zero and a max value.
+// (e.g. accelerometer data)
+// Bits of precision specified to handle common situation of differing precision in the actual data.
+template <typename T, size_t Bits>
+struct TwoPointCalibration
+{
+ TwoPointCalibration() = default;
+ TwoPointCalibration(const T& zero_, const T& max_) : zero{zero_}, max{max_} {}
+
+ static constexpr size_t BITS_OF_PRECISION = Bits;
+
+ T zero;
+ T max;
+};
+
+// Represents calibration data with a min, zero, and max value. (e.g. joystick data)
+template <typename T, size_t Bits>
+struct ThreePointCalibration
+{
+ ThreePointCalibration() = default;
+ ThreePointCalibration(const T& min_, const T& zero_, const T& max_)
+ : min{min_}, zero{zero_}, max{max_}
+ {
+ }
+
+ static constexpr size_t BITS_OF_PRECISION = Bits;
+
+ T min;
+ T zero;
+ T max;
+};
+
+// Represents a raw/uncalibrated N-dimensional value of input data. (e.g. Joystick X and Y)
+// A normalized value can be calculated with a provided {Two,Three}PointCalibration.
+// Values are adjusted with mismatched bits of precision.
+// Underlying type may be an unsigned type or a a Common::TVecN<> of an unsigned type.
+template <typename T, size_t Bits>
+struct RawValue
+{
+ RawValue() = default;
+ explicit RawValue(const T& value_) : value{value_} {}
+
+ static constexpr size_t BITS_OF_PRECISION = Bits;
+
+ T value;
+
+ template <typename OtherT, size_t OtherBits>
+ auto GetNormalizedValue(const TwoPointCalibration<OtherT, OtherBits>& calibration) const
+ {
+ const auto value_expansion =
+ std::max(0, int(calibration.BITS_OF_PRECISION) - int(BITS_OF_PRECISION));
+
+ const auto calibration_expansion =
+ std::max(0, int(BITS_OF_PRECISION) - int(calibration.BITS_OF_PRECISION));
+
+ const auto calibration_zero = ExpandValue(calibration.zero, calibration_expansion) * 1.f;
+ const auto calibration_max = ExpandValue(calibration.max, calibration_expansion) * 1.f;
+
+ // Multiplication by 1.f to floatify either a scalar or a Vec.
+ return (ExpandValue(value, value_expansion) * 1.f - calibration_zero) /
+ (calibration_max - calibration_zero);
+ }
+
+ template <typename OtherT, size_t OtherBits>
+ auto GetNormalizedValue(const ThreePointCalibration<OtherT, OtherBits>& calibration) const
+ {
+ const auto value_expansion =
+ std::max(0, int(calibration.BITS_OF_PRECISION) - int(BITS_OF_PRECISION));
+
+ const auto calibration_expansion =
+ std::max(0, int(BITS_OF_PRECISION) - int(calibration.BITS_OF_PRECISION));
+
+ const auto calibration_min = ExpandValue(calibration.min, calibration_expansion) * 1.f;
+ const auto calibration_zero = ExpandValue(calibration.zero, calibration_expansion) * 1.f;
+ const auto calibration_max = ExpandValue(calibration.max, calibration_expansion) * 1.f;
+
+ const auto use_max = calibration.zero < value;
+
+ // Multiplication by 1.f to floatify either a scalar or a Vec.
+ return (ExpandValue(value, value_expansion) * 1.f - calibration_zero) /
+ (use_max * 1.f * (calibration_max - calibration_zero) +
+ !use_max * 1.f * (calibration_zero - calibration_min));
+ }
+
+ template <typename OtherT>
+ static OtherT ExpandValue(OtherT value, size_t bits)
+ {
+ if constexpr (std::is_arithmetic_v<OtherT>)
+ {
+ return Common::ExpandValue(value, bits);
+ }
+ else
+ {
+ for (size_t i = 0; i != std::size(value.data); ++i)
+ value.data[i] = Common::ExpandValue(value.data[i], bits);
+ return value;
+ }
+ }
+};
+
class EmulatedController
{
public: