From ae3fac630141c464b7129f1a38fab472323c746c Mon Sep 17 00:00:00 2001 From: Denis Date: Sat, 6 Jan 2024 03:32:50 +0100 Subject: [PATCH] swap y and z, improve gyro-to-joystick --- allynone.c | 1 + config.cfg.default | 3 ++- dev_out.c | 8 ++++---- rogue_enemy.c | 14 +++++++++++++- rogue_enemy.h | 2 ++ settings.c | 7 +++++++ settings.h | 1 + stray_ally.c | 1 + virt_ds5.c | 44 ++++++++++++-------------------------------- 9 files changed, 43 insertions(+), 38 deletions(-) diff --git a/allynone.c b/allynone.c index 48a08f0..24bd9ed 100644 --- a/allynone.c +++ b/allynone.c @@ -33,6 +33,7 @@ int main(int argc, char ** argv) { .gamepad_rumble_control = true, .controller_bluetooth = false, .dualsense_edge = false, + .swap_y_z = false, }; load_out_config(&out_settings, configuration_file); diff --git a/config.cfg.default b/config.cfg.default index 3bb59ba..3b933c6 100644 --- a/config.cfg.default +++ b/config.cfg.default @@ -8,4 +8,5 @@ gamepad_leds_control = true; m1m2_mode = 1; touchbar = true; controller_bluetooth = true; -dualsense_edge = false; \ No newline at end of file +dualsense_edge = false; +swap_y_z = true; \ No newline at end of file diff --git a/dev_out.c b/dev_out.c index 4a93843..f048a39 100644 --- a/dev_out.c +++ b/dev_out.c @@ -187,15 +187,15 @@ static void handle_incoming_message_gamepad_set( case GAMEPAD_GYROSCOPE: { inout_gamepad->last_gyro_motion_timestamp_ns = msg_payload->status.gyro.sample_timestamp_ns; inout_gamepad->raw_gyro[0] = msg_payload->status.gyro.x; - inout_gamepad->raw_gyro[1] = msg_payload->status.gyro.y; - inout_gamepad->raw_gyro[2] = msg_payload->status.gyro.z; + inout_gamepad->raw_gyro[1] = in_settings->swap_y_z ? msg_payload->status.gyro.z : msg_payload->status.gyro.y; + inout_gamepad->raw_gyro[2] = in_settings->swap_y_z ? msg_payload->status.gyro.y : msg_payload->status.gyro.z; break; } case GAMEPAD_ACCELEROMETER: { inout_gamepad->last_accel_motion_timestamp_ns = msg_payload->status.accel.sample_timestamp_ns; inout_gamepad->raw_accel[0] = msg_payload->status.accel.x; - inout_gamepad->raw_accel[1] = msg_payload->status.accel.y; - inout_gamepad->raw_accel[2] = msg_payload->status.accel.z; + inout_gamepad->raw_accel[1] = in_settings->swap_y_z ? msg_payload->status.accel.z : msg_payload->status.accel.y; + inout_gamepad->raw_accel[2] = in_settings->swap_y_z ? msg_payload->status.accel.y : msg_payload->status.accel.z; break; } case GAMEPAD_TOUCHPAD_TOUCH_ACTIVE: { diff --git a/rogue_enemy.c b/rogue_enemy.c index 88572a4..6d40868 100644 --- a/rogue_enemy.c +++ b/rogue_enemy.c @@ -10,4 +10,16 @@ int64_t div_round_closest_i64(int64_t x, int64_t divisor) { const int64_t __x = x; const int64_t __d = divisor; return ((__x) > 0) == ((__d) > 0) ? (((__x) + ((__d) / 2)) / (__d)) : (((__x) - ((__d) / 2)) / (__d)); -} \ No newline at end of file +} + +int64_t min_max_clamp(int64_t value, int64_t min, int64_t max) { + if (value <= min) { + return min; + } + + if (value >= max) { + return max; + } + + return value; +} diff --git a/rogue_enemy.h b/rogue_enemy.h index b868579..a1f55e2 100644 --- a/rogue_enemy.h +++ b/rogue_enemy.h @@ -63,3 +63,5 @@ int32_t div_round_closest(int32_t x, int32_t divisor); int64_t div_round_closest_i64(int64_t x, int64_t divisor); + +int64_t min_max_clamp(int64_t value, int64_t min, int64_t max); \ No newline at end of file diff --git a/settings.c b/settings.c index 57d6a1b..cce8a43 100644 --- a/settings.c +++ b/settings.c @@ -113,6 +113,13 @@ void load_out_config(dev_out_settings_t *const out_conf, const char* const filep fprintf(stderr, "dualsense_edge (bool) configuration not found. Default value will be used.\n"); } + int swap_y_z; + if (config_lookup_bool(&cfg, "swap_y_z", &swap_y_z) != CONFIG_FALSE) { + out_conf->swap_y_z = swap_y_z; + } else { + fprintf(stderr, "swap_y_z (bool) configuration not found. Default value will be used.\n"); + } + config_destroy(&cfg); load_out_config_err: diff --git a/settings.h b/settings.h index 1006db0..1fe2813 100644 --- a/settings.h +++ b/settings.h @@ -19,6 +19,7 @@ typedef struct dev_out_settings { bool gamepad_rumble_control; bool controller_bluetooth; bool dualsense_edge; + bool swap_y_z; } dev_out_settings_t; void load_out_config(dev_out_settings_t *const out_conf, const char* const filepath); \ No newline at end of file diff --git a/stray_ally.c b/stray_ally.c index 0847e52..4109446 100644 --- a/stray_ally.c +++ b/stray_ally.c @@ -19,6 +19,7 @@ int main(int argc, char ** argv) { .gamepad_rumble_control = true, .controller_bluetooth = false, .dualsense_edge = false, + .swap_y_z = false, }; load_out_config(&out_settings, configuration_file); diff --git a/virt_ds5.c b/virt_ds5.c index 33ba3fd..ab52ccc 100644 --- a/virt_ds5.c +++ b/virt_ds5.c @@ -1445,45 +1445,25 @@ void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *c out_shifted_buf[4] = ((uint64_t)((int64_t)in_device_status->joystick_positions[1][1] + (int64_t)32768) >> (uint64_t)8); // R stick, Y axis if (in_device_status->join_left_analog_and_gyroscope) { - const int64_t joint_x = /*(int64_t)in_device_status->joystick_positions[0][0]*/ (int64_t)127 + ((int64_t)g_x / (int64_t)4); - const int64_t joint_y = /*(int64_t)in_device_status->joystick_positions[0][1]*/ (int64_t)127 + ((int64_t)g_y / (int64_t)4); + const int64_t contrib_x = (int64_t)127 + ((int64_t)g_x / (int64_t)4); + const int64_t contrib_y = (int64_t)127 + ((int64_t)g_y / (int64_t)4); - if (joint_x <= (int64_t)0) { - out_shifted_buf[1] = 0x00; - } else if (joint_x >= (int64_t)255) { - out_shifted_buf[1] = 0xFF; - } else { - out_shifted_buf[1] = joint_x; - } + const uint8_t analog_x_contrib = min_max_clamp(contrib_x, 0, 255); + const uint8_t analog_y_contrib = min_max_clamp(contrib_y, 0, 255); - if (joint_y <= (int64_t)0) { - out_shifted_buf[2] = 0x00; - } else if (joint_y >= (int64_t)255) { - out_shifted_buf[2] = 0xFF; - } else { - out_shifted_buf[2] = joint_y; - } + out_shifted_buf[1] = min_max_clamp((int64_t)out_shifted_buf[1] + (int64_t)analog_x_contrib, 0, 255); + out_shifted_buf[2] = min_max_clamp((int64_t)out_shifted_buf[2] + (int64_t)analog_y_contrib, 0, 255); } if (in_device_status->join_right_analog_and_gyroscope) { - const int64_t joint_x = /*(int64_t)in_device_status->joystick_positions[1][0]*/ (int64_t)127 + ((int64_t)g_x / (int64_t)4); - const int64_t joint_y = /*(int64_t)in_device_status->joystick_positions[1][1]*/ (int64_t)127 + ((int64_t)g_y / (int64_t)4); + const int64_t contrib_x = (int64_t)127 + ((int64_t)g_x / (int64_t)4); + const int64_t contrib_y = (int64_t)127 + ((int64_t)g_y / (int64_t)4); - if (joint_x <= (int64_t)0) { - out_shifted_buf[3] = 0x00; - } else if (joint_x >= (int64_t)255) { - out_shifted_buf[3] = 0xFF; - } else { - out_shifted_buf[3] = joint_x; - } + const uint8_t analog_x_contrib = min_max_clamp(contrib_x, 0, 255); + const uint8_t analog_y_contrib = min_max_clamp(contrib_y, 0, 255); - if (joint_y <= (int64_t)0) { - out_shifted_buf[4] = 0x00; - } else if (joint_y >= (int64_t)0) { - out_shifted_buf[4] = 0xFF; - } else { - out_shifted_buf[4] = joint_y; - } + out_shifted_buf[3] = min_max_clamp((int64_t)out_shifted_buf[3] + (int64_t)analog_x_contrib, 0, 255); + out_shifted_buf[4] = min_max_clamp((int64_t)out_shifted_buf[4] + (int64_t)analog_y_contrib, 0, 255); } out_shifted_buf[5] = in_device_status->l2_trigger; // Z