From 3f21458d480d6d13a972056c1db2277158c62157 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Mon, 20 Nov 2023 08:41:11 +0900 Subject: [PATCH] Improve deadzone handling --- .../game_pad_stick_converter.hpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/grabber/include/grabber/device_grabber_details/game_pad_stick_converter.hpp b/src/core/grabber/include/grabber/device_grabber_details/game_pad_stick_converter.hpp index 70688ed87..b5d266f6f 100644 --- a/src/core/grabber/include/grabber/device_grabber_details/game_pad_stick_converter.hpp +++ b/src/core/grabber/include/grabber/device_grabber_details/game_pad_stick_converter.hpp @@ -105,10 +105,16 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche } void set_update_timer(double deadzone) { + // The deadzone may be changed even while update_timer is active. + // Therefore, we always update regardless of the update_timer status. + enqueue_to_dispatcher([this, deadzone] { + deadzone_ = deadzone; + }); + if (!update_timer_.enabled()) { update_timer_.start( - [this, deadzone] { - update_values(deadzone); + [this] { + update_values(); }, std::chrono::milliseconds(update_timer_interval_milliseconds)); } @@ -206,7 +212,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche } private: - void update_values(double deadzone) { + void update_values(void) { auto now = pqrs::osx::chrono::mach_absolute_time_point(); auto delta_vertical = vertical_stick_sensor_.get_value() - previous_vertical_value_; @@ -228,8 +234,8 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche // Update stroke_acceleration_ // - if (std::abs(vertical_stick_sensor_.get_value()) < deadzone && - std::abs(horizontal_stick_sensor_.get_value()) < deadzone) { + if (std::abs(vertical_stick_sensor_.get_value()) < deadzone_ && + std::abs(horizontal_stick_sensor_.get_value()) < deadzone_) { deadzone_magnitude_ = magnitude_; delta_magnitude = 0.0; @@ -323,6 +329,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche stick_sensor horizontal_stick_sensor_; stick_sensor vertical_stick_sensor_; + double deadzone_; double radian_; double magnitude_; double stroke_acceleration_destination_value_; @@ -552,7 +559,9 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche } ~game_pad_stick_converter(void) { - detach_from_dispatcher(); + detach_from_dispatcher([this] { + states_.clear(); + }); } void set_core_configuration(std::weak_ptr core_configuration) {