From 026e9706153c4162da2c3fe1ab598b63addedfbe Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 3 Jan 2024 15:39:19 +0100 Subject: [PATCH] join analogs and gyroscope --- dev_out.c | 8 ++++++++ devices_status.c | 2 ++ devices_status.h | 3 +++ message.h | 2 ++ rog_ally.c | 28 ++++++++++++++++++++++++++++ rogue_enemy.c | 6 ++++++ rogue_enemy.h | 2 ++ virt_ds5.c | 39 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 90 insertions(+) diff --git a/dev_out.c b/dev_out.c index fb04007..3d1707c 100644 --- a/dev_out.c +++ b/dev_out.c @@ -134,6 +134,14 @@ static void handle_incoming_message_gamepad_set( inout_gamepad->touchpad_press = msg_payload->status.btn; break; } + case GAMEPAD_BTN_JOIN_LEFT_ANALOG_AND_GYROSCOPE: { + inout_gamepad->join_left_analog_and_gyroscope = msg_payload->status.btn; + break; + } + case GAMEPAD_BTN_JOIN_RIGHT_ANALOG_AND_GYROSCOPE: { + inout_gamepad->join_right_analog_and_gyroscope = msg_payload->status.btn; + break; + } case GAMEPAD_LEFT_JOYSTICK_X: { inout_gamepad->joystick_positions[0][0] = msg_payload->status.joystick_pos; break; diff --git a/devices_status.c b/devices_status.c index 6173659..ef12fba 100644 --- a/devices_status.c +++ b/devices_status.c @@ -98,6 +98,8 @@ void gamepad_status_init(gamepad_status_t *const stats) { stats->touchpad_touch_num = -1; stats->touchpad_x = 0; stats->touchpad_y = 0; + stats->join_left_analog_and_gyroscope = 0; + stats->join_right_analog_and_gyroscope = 0; stats->flags = 0; } diff --git a/devices_status.h b/devices_status.h index 51f8ec9..6c55136 100644 --- a/devices_status.h +++ b/devices_status.h @@ -65,6 +65,9 @@ typedef struct gamepad_status { uint64_t leds_events_count; uint8_t leds_colors[3]; // r | g | b + uint8_t join_left_analog_and_gyroscope; + uint8_t join_right_analog_and_gyroscope; + volatile uint32_t flags; } gamepad_status_t; diff --git a/message.h b/message.h index 00f2303..7f91be8 100644 --- a/message.h +++ b/message.h @@ -24,6 +24,8 @@ typedef enum in_message_gamepad_btn { GAMEPAD_BTN_L5, GAMEPAD_BTN_R5, GAMEPAD_BTN_TOUCHPAD, + GAMEPAD_BTN_JOIN_LEFT_ANALOG_AND_GYROSCOPE, + GAMEPAD_BTN_JOIN_RIGHT_ANALOG_AND_GYROSCOPE, GAMEPAD_LEFT_JOYSTICK_X, GAMEPAD_LEFT_JOYSTICK_Y, diff --git a/rog_ally.c b/rog_ally.c index 25c9b16..eab7cc6 100644 --- a/rog_ally.c +++ b/rog_ally.c @@ -224,6 +224,20 @@ static int asus_kbd_ev_map( } }; + messages[written_msg++] = current_message; + } else if (conf->m1m2_mode == 2) { + const in_message_t current_message = { + .type = GAMEPAD_SET_ELEMENT, + .data = { + .gamepad_set = { + .element = GAMEPAD_BTN_JOIN_RIGHT_ANALOG_AND_GYROSCOPE, + .status = { + .btn = e->ev[1].value, + } + } + } + }; + messages[written_msg++] = current_message; } } else if (e->ev[i].code == KEY_F15) { @@ -262,6 +276,20 @@ static int asus_kbd_ev_map( } }; + messages[written_msg++] = current_message; + } else if (conf->m1m2_mode == 2) { + const in_message_t current_message = { + .type = GAMEPAD_SET_ELEMENT, + .data = { + .gamepad_set = { + .element = GAMEPAD_BTN_JOIN_LEFT_ANALOG_AND_GYROSCOPE, + .status = { + .btn = e->ev[1].value, + } + } + } + }; + messages[written_msg++] = current_message; } } else if ((e->ev[i].code == KEY_F16) && (e->ev[i].value != 0)) { diff --git a/rogue_enemy.c b/rogue_enemy.c index 218b4ff..88572a4 100644 --- a/rogue_enemy.c +++ b/rogue_enemy.c @@ -5,3 +5,9 @@ int32_t div_round_closest(int32_t x, int32_t divisor) { const int32_t __d = divisor; return ((__x) > 0) == ((__d) > 0) ? (((__x) + ((__d) / 2)) / (__d)) : (((__x) - ((__d) / 2)) / (__d)); } + +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 diff --git a/rogue_enemy.h b/rogue_enemy.h index 5ccb243..b868579 100644 --- a/rogue_enemy.h +++ b/rogue_enemy.h @@ -61,3 +61,5 @@ // also courtesy of linux kernel int32_t div_round_closest(int32_t x, int32_t divisor); + +int64_t div_round_closest_i64(int64_t x, int64_t divisor); diff --git a/virt_ds5.c b/virt_ds5.c index 15c994b..6840260 100644 --- a/virt_ds5.c +++ b/virt_ds5.c @@ -1282,6 +1282,45 @@ void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *c out_shifted_buf[2] = ((uint64_t)((int64_t)in_device_status->joystick_positions[0][1] + (int64_t)32768) >> (uint64_t)8); // L stick, Y axis 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) { + int64_t joint_x = ((int64_t)out_shifted_buf[1] - (int64_t)128) + (div_round_closest_i64((int64_t)255 * ((int64_t)32768 + (int64_t)in_device_status->raw_gyro[0]), 0xFFFF) - (int64_t)128); + if (joint_x < (int64_t)-128) { + joint_x = -128; + } else if (joint_x < (int64_t)127) { + joint_x = 127; + } + + int64_t joint_y = ((int64_t)out_shifted_buf[2] - (int64_t)128) + (div_round_closest_i64((int64_t)255 * ((int64_t)32768 + (int64_t)in_device_status->raw_gyro[1]), 0xFFFF) - (int64_t)128); + if (joint_y < (int64_t)-128) { + joint_y = -128; + } else if (joint_x < (int64_t)127) { + joint_y = 127; + } + + out_shifted_buf[1] = joint_x + (int64_t)128; + out_shifted_buf[2] = joint_y + (int64_t)128; + } + + if (in_device_status->join_right_analog_and_gyroscope) { + int64_t joint_x = ((int64_t)out_shifted_buf[3] - (int64_t)128) + (div_round_closest_i64((int64_t)255 * ((int64_t)32768 + (int64_t)in_device_status->raw_gyro[0]), 0xFFFF) - (int64_t)128); + if (joint_x < (int64_t)-128) { + joint_x = -128; + } else if (joint_x < (int64_t)127) { + joint_x = 127; + } + + int64_t joint_y = ((int64_t)out_shifted_buf[4] - (int64_t)128) + (div_round_closest_i64((int64_t)255 * ((int64_t)32768 + (int64_t)in_device_status->raw_gyro[1]), 0xFFFF) - (int64_t)128); + if (joint_y < (int64_t)-128) { + joint_y = -128; + } else if (joint_x < (int64_t)127) { + joint_y = 127; + } + + out_shifted_buf[3] = joint_x + (int64_t)128; + out_shifted_buf[4] = joint_y + (int64_t)128; + } + out_shifted_buf[5] = in_device_status->l2_trigger; // Z out_shifted_buf[6] = in_device_status->r2_trigger; // RZ out_shifted_buf[7] = gamepad->seq_num++; // seq_number