Skip to content

Commit

Permalink
Add absolute_magnitude to formula
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 14, 2024
1 parent 0ecb5fc commit 8f33ab1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
stick(stick_type stick_type)
: dispatcher_client(),
stick_type_(stick_type),
radian_(0.0),
absolute_magnitude_(0.0),
delta_magnitude_(0.0),
continued_movement_magnitude_(0.0),
previous_magnitude_(0.0),
previous_absolute_magnitude_(0.0),
event_origin_(event_origin::none),
continued_movement_absolute_magnitude_threshold_(1.0),
flicking_input_window_milliseconds_(0) {
Expand Down Expand Up @@ -181,11 +183,13 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

radian_ = std::atan2(vertical_stick_sensor_.get_value(),
horizontal_stick_sensor_.get_value());
auto magnitude = std::min(1.0,
std::sqrt(std::pow(vertical_stick_sensor_.get_value(), 2) +
std::pow(horizontal_stick_sensor_.get_value(), 2)));
// When the stick is tilted diagonally, the distance from the centre may exceed 1.0 (e.g. 1.2).
// Therefore, we correct the value <= 1.0.
absolute_magnitude_ = std::min(1.0,
std::sqrt(std::pow(vertical_stick_sensor_.get_value(), 2) +
std::pow(horizontal_stick_sensor_.get_value(), 2)));

auto dm = std::max(0.0, magnitude - previous_magnitude_);
auto dm = std::max(0.0, absolute_magnitude_ - previous_absolute_magnitude_);

//
// Update delta_magnitude_history_
Expand All @@ -203,7 +207,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
// Update delta_magnitude_
//

if (magnitude >= continued_movement_absolute_magnitude_threshold_) {
if (absolute_magnitude_ >= continued_movement_absolute_magnitude_threshold_) {
if (continued_movement_magnitude_ == 0.0) {
for (const auto& e : delta_magnitude_history_) {
continued_movement_magnitude_ += e.get_delta_magnitude();
Expand All @@ -223,7 +227,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
if (delta_magnitude_ > 0) {
auto delta_magnitude_threshold = 0.01;
if (delta_magnitude_ < delta_magnitude_threshold) {
// Return in here to prevent previous_magnitude_ from being updated.
// Return in here to prevent previous_absolute_magnitude_ from being updated.
return;
}

Expand All @@ -249,7 +253,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
// Update previous values
//

previous_magnitude_ = magnitude;
previous_absolute_magnitude_ = absolute_magnitude_;
}

std::pair<double, double> xy_hid_values(void) const {
Expand Down Expand Up @@ -335,7 +339,8 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
{},
{
{"radian", radian_},
{"magnitude", delta_magnitude_},
{"delta_magnitude", delta_magnitude_},
{"absolute_magnitude", absolute_magnitude_},
});
}

Expand All @@ -345,9 +350,10 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
stick_sensor vertical_stick_sensor_;

double radian_;
double absolute_magnitude_;
double delta_magnitude_;
double continued_movement_magnitude_;
double previous_magnitude_;
double previous_absolute_magnitude_;
std::deque<delta_magnitude_history_entry> delta_magnitude_history_;
std::atomic<event_origin> event_origin_;

Expand Down
56 changes: 42 additions & 14 deletions src/share/core_configuration/details/profile/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,59 @@ class device final {
static constexpr int game_pad_wheels_stick_flicking_input_window_milliseconds_default_value = 50;

static constexpr std::string_view game_pad_xy_stick_interval_milliseconds_formula_default_value =
"20";
"10";
static constexpr std::string_view game_pad_wheels_stick_interval_milliseconds_formula_default_value =
"50";
"20";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_x_formula_default_value =
"cos(radian) * magnitude * 32;";
"cos(radian) * delta_magnitude * 32;";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_y_formula_default_value =
"sin(radian) * magnitude * 32;";
"sin(radian) * delta_magnitude * 32;";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_vertical_wheel_formula_default_value =
"switch {\n"
" case abs(cos(radian)) > abs(sin(radian)) : 0;\n"
" default : sin(radian) * max(0.05, magnitude) * 2;\n"
"}";
static constexpr std::string_view game_pad_stick_vertical_wheel_formula_default_value = R"(
if (abs(cos(radian)) >= abs(sin(radian))) {
0;
} else {
var m := 0;
if (absolute_magnitude < 1.0) {
m := max(0.05, delta_magnitude * 5);
} else {
if (delta_magnitude > 0.3) {
m := 0.5;
} else {
m := 0.3;
}
}
sin(radian) * m;
}
)";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_horizontal_wheel_formula_default_value =
"switch {\n"
" case abs(cos(radian)) < abs(sin(radian)) : 0;\n"
" default : cos(radian) * max(0.05, magnitude) * 2;\n"
"}";
static constexpr std::string_view game_pad_stick_horizontal_wheel_formula_default_value = R"(
if (abs(cos(radian)) <= abs(sin(radian))) {
0;
} else {
var m := 0;
if (absolute_magnitude < 1.0) {
m := max(0.05, delta_magnitude * 5);
} else {
if (delta_magnitude > 0.3) {
m := 0.5;
} else {
m := 0.3;
}
}
cos(radian) * m;
}
)";

device(const nlohmann::json& json) : json_(json),
ignore_(false),
Expand Down

0 comments on commit 8f33ab1

Please sign in to comment.