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.
This commit is contained in:
Filoppi 2021-05-05 00:04:47 +03:00
parent a19a0096db
commit 379ffc268d
2 changed files with 34 additions and 17 deletions

View File

@ -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::StateData> 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::StateData> 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;

View File

@ -23,7 +23,8 @@ public:
IMUGyroscope(std::string name, std::string ui_name);
StateData GetRawState() const;
std::optional<StateData> GetState() const;
// Also updates the state by default
std::optional<StateData> 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<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();
StateData m_calibration = {};
MathUtil::RunningMean<StateData> m_running_calibration;
Clock::time_point m_calibration_period_start = Clock::now();
};
} // namespace ControllerEmu