Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PS gyro for values at the boundaries #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <climits>
#include <cmath>
#include <endian.h>
#include <filesystem>
Expand Down Expand Up @@ -360,12 +361,12 @@ void PS5Joypad::set_on_rumble(const std::function<void(int, int)> &callback) {
this->_state->on_rumble = callback;
}

/**
* For a rationale behind this, see: https://github.com/LizardByte/Sunshine/issues/3247#issuecomment-2428065349
*/
static __le16 to_le_signed(float original, float value) {
auto le = htole16(value);
if (original < 0) { // adjust sign bit
le |= (1 << 15); // set the last bit (bit 15) to 1
}
return le;
value = std::clamp(value, static_cast<float>(SHRT_MIN), static_cast<float>(SHRT_MAX));
return htole16(value);
}

void PS5Joypad::set_motion(PS5Joypad::MOTION_TYPE type, float x, float y, float z) {
Expand Down
24 changes: 22 additions & 2 deletions tests/testJoypads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
// Create the controller
auto joypad = std::move(*PS5Joypad::create());

std::this_thread::sleep_for(250ms);
std::this_thread::sleep_for(50ms);

auto devices = joypad.get_nodes();
REQUIRE_THAT(devices, SizeIs(5)); // 3 eventXX and 2 jsYY
Expand All @@ -154,7 +154,7 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
{ // Test creating a second device
REQUIRE(SDL_NumJoysticks() == 1);
auto joypad2 = std::move(*PS5Joypad::create());
std::this_thread::sleep_for(250ms);
std::this_thread::sleep_for(50ms);

auto devices2 = joypad2.get_nodes();
REQUIRE_THAT(devices2, SizeIs(5)); // 3 eventXX and 2 jsYY
Expand Down Expand Up @@ -336,6 +336,26 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
REQUIRE_THAT(event.csensor.data[0], WithinAbs(gyro_data[0], 0.01f));
REQUIRE_THAT(event.csensor.data[1], WithinAbs(gyro_data[1], 0.01f));
REQUIRE_THAT(event.csensor.data[2], WithinAbs(gyro_data[2], 0.01f));
flush_sdl_events();

// Try out problematic values from https://github.com/LizardByte/Sunshine/issues/3247
gyro_data = {-32769.0f, 32769.0f, -0.0004124999977648258f};
joypad.set_motion(inputtino::PS5Joypad::GYROSCOPE, gyro_data[0], gyro_data[1], gyro_data[2]);
std::this_thread::sleep_for(10ms);

SDL_GameControllerUpdate();
SDL_SensorUpdate();
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_CONTROLLERSENSORUPDATE) {
break;
}
}
REQUIRE(event.type == SDL_CONTROLLERSENSORUPDATE);
REQUIRE(event.csensor.sensor == SDL_SENSOR_GYRO);
REQUIRE_THAT(event.csensor.data[0], WithinAbs(-28.59546f, 0.01f));
REQUIRE_THAT(event.csensor.data[1], WithinAbs(28.59546f, 0.01f));
REQUIRE_THAT(event.csensor.data[2], WithinAbs(0.0f, 0.01f));
flush_sdl_events();
}

{ // Test touchpad
Expand Down
Loading