diff --git a/allynone.c b/allynone.c index 24bd9ed..2ca2aeb 100644 --- a/allynone.c +++ b/allynone.c @@ -34,6 +34,8 @@ int main(int argc, char ** argv) { .controller_bluetooth = false, .dualsense_edge = false, .swap_y_z = false, + .gyro_to_analog_activation_treshold = 16, + .gyro_to_analog_mapping = 4, }; load_out_config(&out_settings, configuration_file); diff --git a/config.cfg.default b/config.cfg.default index 3b933c6..b327b78 100644 --- a/config.cfg.default +++ b/config.cfg.default @@ -6,6 +6,8 @@ rumble_on_mode_switch = true; gamepad_rumble_control = true; gamepad_leds_control = true; m1m2_mode = 1; +gyro_to_analog_mapping = 5; +gyro_to_analog_activation_treshold = 1; touchbar = true; controller_bluetooth = true; dualsense_edge = false; diff --git a/rogue_enemy.c b/rogue_enemy.c index 22fce04..4be29b4 100644 --- a/rogue_enemy.c +++ b/rogue_enemy.c @@ -21,3 +21,11 @@ int64_t min_max_clamp(int64_t value, int64_t min, int64_t max) { return value; } + +int64_t absolute_value(int64_t value) { + if (value < 0) { + return (int64_t)-1 * value; + } + + return value; +} \ No newline at end of file diff --git a/rogue_enemy.h b/rogue_enemy.h index a1f55e2..0c8c3d0 100644 --- a/rogue_enemy.h +++ b/rogue_enemy.h @@ -64,4 +64,6 @@ 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 +int64_t min_max_clamp(int64_t value, int64_t min, int64_t max); + +int64_t absolute_value(int64_t value); \ No newline at end of file diff --git a/settings.c b/settings.c index cce8a43..0f453fe 100644 --- a/settings.c +++ b/settings.c @@ -120,6 +120,21 @@ void load_out_config(dev_out_settings_t *const out_conf, const char* const filep fprintf(stderr, "swap_y_z (bool) configuration not found. Default value will be used.\n"); } + int gyro_to_analog_activation_treshold; + if (config_lookup_int(&cfg, "gyro_to_analog_activation_treshold", &gyro_to_analog_activation_treshold) != CONFIG_FALSE) { + out_conf->gyro_to_analog_activation_treshold = gyro_to_analog_activation_treshold; + } else { + fprintf(stderr, "gyro_to_analog_activation_treshold (int) configuration not found. Default value will be used.\n"); + } + + int gyro_to_analog_mapping; + if (config_lookup_int(&cfg, "gyro_to_analog_mapping", &gyro_to_analog_mapping) != CONFIG_FALSE) { + out_conf->gyro_to_analog_mapping = gyro_to_analog_mapping == 0 ? 1 : gyro_to_analog_mapping; + } else { + fprintf(stderr, "gyro_to_analog_mapping (int) 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 1fe2813..3063ab7 100644 --- a/settings.h +++ b/settings.h @@ -20,6 +20,8 @@ typedef struct dev_out_settings { bool controller_bluetooth; bool dualsense_edge; bool swap_y_z; + int gyro_to_analog_activation_treshold; + int gyro_to_analog_mapping; } 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 4109446..a8b288d 100644 --- a/stray_ally.c +++ b/stray_ally.c @@ -20,6 +20,8 @@ int main(int argc, char ** argv) { .controller_bluetooth = false, .dualsense_edge = false, .swap_y_z = false, + .gyro_to_analog_activation_treshold = 16, + .gyro_to_analog_mapping = 4, }; load_out_config(&out_settings, configuration_file); diff --git a/virt_ds4.c b/virt_ds4.c index c9a0f4d..9825718 100644 --- a/virt_ds4.c +++ b/virt_ds4.c @@ -429,9 +429,16 @@ static ds4_dpad_status_t ds4_dpad_from_gamepad(uint8_t dpad) { return DPAD_RELEASED; } -int virt_dualshock_init(virt_dualshock_t *const out_gamepad, bool bluetooth) { +int virt_dualshock_init( + virt_dualshock_t *const out_gamepad, + bool bluetooth, + int64_t gyro_to_analog_activation_treshold, + int64_t gyro_to_analog_mapping +) { int ret = 0; + out_gamepad->gyro_to_analog_activation_treshold = absolute_value(gyro_to_analog_activation_treshold); + out_gamepad->gyro_to_analog_mapping = gyro_to_analog_mapping; out_gamepad->dt_sum = 0; out_gamepad->dt_buffer_current = 0; memset(out_gamepad->dt_buffer, 0, sizeof(out_gamepad->dt_buffer)); @@ -781,11 +788,14 @@ void virt_dualshock_compose(virt_dualshock_t *const gamepad, gamepad_status_t *c */ const int16_t g_x = in_device_status->raw_gyro[0]; - const int16_t g_y = in_device_status->raw_gyro[1]; // Swap Y and Z - const int16_t g_z = in_device_status->raw_gyro[2]; // Swap Y and Z + const int16_t g_y = in_device_status->raw_gyro[1]; + const int16_t g_z = in_device_status->raw_gyro[2]; const int16_t a_x = in_device_status->raw_accel[0]; - const int16_t a_y = in_device_status->raw_accel[1]; // Swap Y and Z - const int16_t a_z = in_device_status->raw_accel[2]; // Swap Y and Z + const int16_t a_y = in_device_status->raw_accel[1]; + const int16_t a_z = in_device_status->raw_accel[2]; + + const int64_t contrib_x = (int64_t)127 + ((int64_t)g_y / (int64_t)gamepad->gyro_to_analog_mapping); + const int64_t contrib_y = (int64_t)127 + ((int64_t)g_x / (int64_t)gamepad->gyro_to_analog_mapping); out_buf[0] = gamepad->bluetooth ? DS4_INPUT_REPORT_BT : DS4_INPUT_REPORT_USB; // [00] report ID (0x01) @@ -811,10 +821,25 @@ void virt_dualshock_compose(virt_dualshock_t *const gamepad, gamepad_status_t *c (in_device_status->r1 ? 0x02 : 0x00) | (in_device_status->l1 ? 0x01 : 0x00); - /* - static uint8_t counter = 0; - buf[7] = (((counter++) % (uint8_t)64) << ((uint8_t)2)) | get_buttons_byte3_by_gs(&gs); - */ + if (contrib_y > gamepad->gyro_to_analog_activation_treshold) { + if (absolute_value(contrib_x) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[1] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[3] - (int64_t)127) + contrib_x), 0, 255); + } + + if (absolute_value(contrib_y) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[2] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[4] - (int64_t)127) + contrib_y), 0, 255); + } + } + + if (in_device_status->join_right_analog_and_gyroscope) { + if (absolute_value(contrib_x) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[3] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[3] - (int64_t)127) + contrib_x), 0, 255); + } + + if (absolute_value(contrib_y) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[4] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[4] - (int64_t)127) + contrib_y), 0, 255); + } + } out_shifted_buf[7] = in_device_status->center ? 0x01 : 0x00; diff --git a/virt_ds4.h b/virt_ds4.h index b9faa7f..d69a583 100644 --- a/virt_ds4.h +++ b/virt_ds4.h @@ -21,16 +21,38 @@ typedef struct virt_dualshock { uint32_t empty_reports; int64_t last_time; + + int64_t gyro_to_analog_activation_treshold; + int64_t gyro_to_analog_mapping; } virt_dualshock_t; -int virt_dualshock_init(virt_dualshock_t *const gamepad, bool bluetooth); +int virt_dualshock_init( + virt_dualshock_t *const gamepad, + bool bluetooth, + int64_t gyro_to_analog_activation_treshold, + int64_t gyro_to_analog_mapping +); -int virt_dualshock_get_fd(virt_dualshock_t *const gamepad); +int virt_dualshock_get_fd( + virt_dualshock_t *const gamepad +); -int virt_dualshock_event(virt_dualshock_t *const gamepad, gamepad_status_t *const out_device_status); +int virt_dualshock_event( + virt_dualshock_t *const gamepad, + gamepad_status_t *const out_device_status +); -void virt_dualshock_compose(virt_dualshock_t *const gamepad, gamepad_status_t *const in_device_status, uint8_t *const out_buf); +void virt_dualshock_compose( + virt_dualshock_t *const gamepad, + gamepad_status_t *const in_device_status, + uint8_t *const out_buf +); -int virt_dualshock_send(virt_dualshock_t *const gamepad, uint8_t *const out_buf); +int virt_dualshock_send( + virt_dualshock_t *const gamepad, + uint8_t *const out_buf +); -void virt_dualshock_close(virt_dualshock_t *const gamepad); +void virt_dualshock_close( + virt_dualshock_t *const gamepad +); diff --git a/virt_ds5.c b/virt_ds5.c index fa2015a..36aae8e 100644 --- a/virt_ds5.c +++ b/virt_ds5.c @@ -1102,9 +1102,17 @@ static void destroy(int fd) uhid_write(fd, &ev); } -int virt_dualsense_init(virt_dualsense_t *const out_gamepad, bool bluetooth, bool dualsense_edge) { +int virt_dualsense_init( + virt_dualsense_t *const out_gamepad, + bool bluetooth, + bool dualsense_edge, + int64_t gyro_to_analog_activation_treshold, + int64_t gyro_to_analog_mapping +) { int ret = 0; + out_gamepad->gyro_to_analog_activation_treshold = absolute_value(gyro_to_analog_activation_treshold); + out_gamepad->gyro_to_analog_mapping = gyro_to_analog_mapping; out_gamepad->edge_model = dualsense_edge; out_gamepad->bluetooth = bluetooth; out_gamepad->dt_sum = 0; @@ -1452,14 +1460,14 @@ void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *c const uint32_t timestamp = sim_time + (int)((double)gamepad->empty_reports * DS5_SPEC_DELTA_TIME); const int16_t g_x = in_device_status->raw_gyro[0]; - const int16_t g_y = in_device_status->raw_gyro[1]; // Swap Y and Z - const int16_t g_z = in_device_status->raw_gyro[2]; // Swap Y and Z + const int16_t g_y = in_device_status->raw_gyro[1]; + const int16_t g_z = in_device_status->raw_gyro[2]; const int16_t a_x = in_device_status->raw_accel[0]; - const int16_t a_y = in_device_status->raw_accel[1]; // Swap Y and Z - const int16_t a_z = in_device_status->raw_accel[2]; // Swap Y and Z + const int16_t a_y = in_device_status->raw_accel[1]; + const int16_t a_z = in_device_status->raw_accel[2]; - const int64_t contrib_x = (int64_t)127 + ((int64_t)g_y / (int64_t)4); - const int64_t contrib_y = (int64_t)127 + ((int64_t)g_x / (int64_t)4); + const int64_t contrib_x = (int64_t)127 + ((int64_t)g_y / (int64_t)gamepad->gyro_to_analog_mapping); + const int64_t contrib_y = (int64_t)127 + ((int64_t)g_x / (int64_t)gamepad->gyro_to_analog_mapping); out_buf[0] = gamepad->bluetooth ? DS_INPUT_REPORT_BT : DS_INPUT_REPORT_USB; // [00] report ID (0x01) @@ -1469,14 +1477,24 @@ void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *c out_shifted_buf[3] = ((uint64_t)((int64_t)in_device_status->joystick_positions[1][0] + (int64_t)32768) >> (uint64_t)8); // R stick, X axis 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) { - out_shifted_buf[1] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[3] - (int64_t)127) + contrib_x), 0, 255); - out_shifted_buf[2] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[4] - (int64_t)127) + contrib_y), 0, 255); + if (contrib_y > gamepad->gyro_to_analog_activation_treshold) { + if (absolute_value(contrib_x) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[1] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[3] - (int64_t)127) + contrib_x), 0, 255); + } + + if (absolute_value(contrib_y) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[2] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[4] - (int64_t)127) + contrib_y), 0, 255); + } } if (in_device_status->join_right_analog_and_gyroscope) { - out_shifted_buf[3] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[3] - (int64_t)127) + contrib_x), 0, 255); - out_shifted_buf[4] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[4] - (int64_t)127) + contrib_y), 0, 255); + if (absolute_value(contrib_x) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[3] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[3] - (int64_t)127) + contrib_x), 0, 255); + } + + if (absolute_value(contrib_y) > gamepad->gyro_to_analog_activation_treshold) { + out_shifted_buf[4] = min_max_clamp((int64_t)127 + (((int64_t)out_shifted_buf[4] - (int64_t)127) + contrib_y), 0, 255); + } } out_shifted_buf[5] = in_device_status->l2_trigger; // Z diff --git a/virt_ds5.h b/virt_ds5.h index 282f505..e4b01e3 100644 --- a/virt_ds5.h +++ b/virt_ds5.h @@ -25,12 +25,17 @@ typedef struct virt_dualsense { uint32_t empty_reports; int64_t last_time; + + int64_t gyro_to_analog_activation_treshold; + int64_t gyro_to_analog_mapping; } virt_dualsense_t; int virt_dualsense_init( virt_dualsense_t *const gamepad, bool bluetooth, - bool dualsense_edge + bool dualsense_edge, + int64_t gyro_to_analog_activation_treshold, + int64_t gyro_to_analog_mapping ); int virt_dualsense_get_fd(