input_common: Add option to configure gyro threshold

This commit is contained in:
german77 2022-01-23 21:54:33 -06:00
parent 2136ebccd6
commit ebf19616f4
4 changed files with 24 additions and 6 deletions

View File

@ -749,6 +749,7 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback
raw_status.gyro.y.value, raw_status.gyro.y.value,
raw_status.gyro.z.value, raw_status.gyro.z.value,
}); });
emulated.SetGyroThreshold(raw_status.gyro.x.properties.threshold);
emulated.UpdateRotation(raw_status.delta_timestamp); emulated.UpdateRotation(raw_status.delta_timestamp);
emulated.UpdateOrientation(raw_status.delta_timestamp); emulated.UpdateOrientation(raw_status.delta_timestamp);
force_update_motion = raw_status.force_update; force_update_motion = raw_status.force_update;

View File

@ -10,7 +10,7 @@ namespace Core::HID {
MotionInput::MotionInput() { MotionInput::MotionInput() {
// Initialize PID constants with default values // Initialize PID constants with default values
SetPID(0.3f, 0.005f, 0.0f); SetPID(0.3f, 0.005f, 0.0f);
SetGyroThreshold(0.00005f); SetGyroThreshold(0.007f);
} }
void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) {
@ -31,7 +31,7 @@ void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f); gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f);
} }
if (gyro.Length2() < gyro_threshold) { if (gyro.Length() < gyro_threshold) {
gyro = {}; gyro = {};
} else { } else {
only_accelerometer = false; only_accelerometer = false;

View File

@ -504,9 +504,10 @@ private:
class InputFromMotion final : public Common::Input::InputDevice { class InputFromMotion final : public Common::Input::InputDevice {
public: public:
explicit InputFromMotion(PadIdentifier identifier_, int motion_sensor_, explicit InputFromMotion(PadIdentifier identifier_, int motion_sensor_, float gyro_threshold_,
InputEngine* input_engine_) InputEngine* input_engine_)
: identifier(identifier_), motion_sensor(motion_sensor_), input_engine(input_engine_) { : identifier(identifier_), motion_sensor(motion_sensor_), gyro_threshold(gyro_threshold_),
input_engine(input_engine_) {
UpdateCallback engine_callback{[this]() { OnChange(); }}; UpdateCallback engine_callback{[this]() { OnChange(); }};
const InputIdentifier input_identifier{ const InputIdentifier input_identifier{
.identifier = identifier, .identifier = identifier,
@ -525,8 +526,9 @@ public:
const auto basic_motion = input_engine->GetMotion(identifier, motion_sensor); const auto basic_motion = input_engine->GetMotion(identifier, motion_sensor);
Common::Input::MotionStatus status{}; Common::Input::MotionStatus status{};
const Common::Input::AnalogProperties properties = { const Common::Input::AnalogProperties properties = {
.deadzone = 0.001f, .deadzone = 0.0f,
.range = 1.0f, .range = 1.0f,
.threshold = gyro_threshold,
.offset = 0.0f, .offset = 0.0f,
}; };
status.accel.x = {.raw_value = basic_motion.accel_x, .properties = properties}; status.accel.x = {.raw_value = basic_motion.accel_x, .properties = properties};
@ -551,6 +553,7 @@ public:
private: private:
const PadIdentifier identifier; const PadIdentifier identifier;
const int motion_sensor; const int motion_sensor;
const float gyro_threshold;
int callback_key; int callback_key;
InputEngine* input_engine; InputEngine* input_engine;
}; };
@ -873,9 +876,11 @@ std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateMotionDevice(
if (params.Has("motion")) { if (params.Has("motion")) {
const auto motion_sensor = params.Get("motion", 0); const auto motion_sensor = params.Get("motion", 0);
const auto gyro_threshold = params.Get("threshold", 0.007f);
input_engine->PreSetController(identifier); input_engine->PreSetController(identifier);
input_engine->PreSetMotion(identifier, motion_sensor); input_engine->PreSetMotion(identifier, motion_sensor);
return std::make_unique<InputFromMotion>(identifier, motion_sensor, input_engine.get()); return std::make_unique<InputFromMotion>(identifier, motion_sensor, gyro_threshold,
input_engine.get());
} }
const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f); const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);

View File

@ -403,10 +403,22 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
connect(button, &QPushButton::customContextMenuRequested, connect(button, &QPushButton::customContextMenuRequested,
[=, this](const QPoint& menu_location) { [=, this](const QPoint& menu_location) {
QMenu context_menu; QMenu context_menu;
Common::ParamPackage param = emulated_controller->GetMotionParam(motion_id);
context_menu.addAction(tr("Clear"), [&] { context_menu.addAction(tr("Clear"), [&] {
emulated_controller->SetMotionParam(motion_id, {}); emulated_controller->SetMotionParam(motion_id, {});
motion_map[motion_id]->setText(tr("[not set]")); motion_map[motion_id]->setText(tr("[not set]"));
}); });
if (param.Has("motion")) {
context_menu.addAction(tr("Set gyro threshold"), [&] {
const int gyro_threshold =
static_cast<int>(param.Get("threshold", 0.007f) * 1000.0f);
const int new_threshold = QInputDialog::getInt(
this, tr("Set threshold"), tr("Choose a value between 0% and 100%"),
gyro_threshold, 0, 100);
param.Set("threshold", new_threshold / 1000.0f);
emulated_controller->SetMotionParam(motion_id, param);
});
}
context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location)); context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location));
}); });
} }