diff options
| -rw-r--r-- | src/input_common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/input_common/helpers/joycon_driver.cpp | 4 | ||||
| -rw-r--r-- | src/input_common/helpers/joycon_driver.h | 2 | ||||
| -rw-r--r-- | src/input_common/helpers/joycon_protocol/calibration.cpp | 169 | ||||
| -rw-r--r-- | src/input_common/helpers/joycon_protocol/calibration.h | 54 |
5 files changed, 231 insertions, 0 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt index a60cecaf4..d4307351c 100644 --- a/src/input_common/CMakeLists.txt +++ b/src/input_common/CMakeLists.txt | |||
| @@ -57,6 +57,8 @@ if (ENABLE_SDL2) | |||
| 57 | drivers/sdl_driver.h | 57 | drivers/sdl_driver.h |
| 58 | helpers/joycon_driver.cpp | 58 | helpers/joycon_driver.cpp |
| 59 | helpers/joycon_driver.h | 59 | helpers/joycon_driver.h |
| 60 | helpers/joycon_protocol/calibration.cpp | ||
| 61 | helpers/joycon_protocol/calibration.h | ||
| 60 | helpers/joycon_protocol/common_protocol.cpp | 62 | helpers/joycon_protocol/common_protocol.cpp |
| 61 | helpers/joycon_protocol/common_protocol.h | 63 | helpers/joycon_protocol/common_protocol.h |
| 62 | helpers/joycon_protocol/generic_functions.cpp | 64 | helpers/joycon_protocol/generic_functions.cpp |
diff --git a/src/input_common/helpers/joycon_driver.cpp b/src/input_common/helpers/joycon_driver.cpp index 0de55578b..ac11be1c1 100644 --- a/src/input_common/helpers/joycon_driver.cpp +++ b/src/input_common/helpers/joycon_driver.cpp | |||
| @@ -64,6 +64,7 @@ DriverResult JoyconDriver::InitializeDevice() { | |||
| 64 | accelerometer_performance = Joycon::AccelerometerPerformance::HZ100; | 64 | accelerometer_performance = Joycon::AccelerometerPerformance::HZ100; |
| 65 | 65 | ||
| 66 | // Initialize HW Protocols | 66 | // Initialize HW Protocols |
| 67 | calibration_protocol = std::make_unique<CalibrationProtocol>(hidapi_handle); | ||
| 67 | generic_protocol = std::make_unique<GenericProtocol>(hidapi_handle); | 68 | generic_protocol = std::make_unique<GenericProtocol>(hidapi_handle); |
| 68 | 69 | ||
| 69 | // Get fixed joycon info | 70 | // Get fixed joycon info |
| @@ -79,6 +80,9 @@ DriverResult JoyconDriver::InitializeDevice() { | |||
| 79 | supported_features = GetSupportedFeatures(); | 80 | supported_features = GetSupportedFeatures(); |
| 80 | 81 | ||
| 81 | // Get Calibration data | 82 | // Get Calibration data |
| 83 | calibration_protocol->GetLeftJoyStickCalibration(left_stick_calibration); | ||
| 84 | calibration_protocol->GetRightJoyStickCalibration(right_stick_calibration); | ||
| 85 | calibration_protocol->GetImuCalibration(motion_calibration); | ||
| 82 | 86 | ||
| 83 | // Set led status | 87 | // Set led status |
| 84 | generic_protocol->SetLedBlinkPattern(static_cast<u8>(1 + port)); | 88 | generic_protocol->SetLedBlinkPattern(static_cast<u8>(1 + port)); |
diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index deb50ec77..275c97b91 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | #include <span> | 8 | #include <span> |
| 9 | #include <thread> | 9 | #include <thread> |
| 10 | 10 | ||
| 11 | #include "input_common/helpers/joycon_protocol/calibration.h" | ||
| 11 | #include "input_common/helpers/joycon_protocol/generic_functions.h" | 12 | #include "input_common/helpers/joycon_protocol/generic_functions.h" |
| 12 | #include "input_common/helpers/joycon_protocol/joycon_types.h" | 13 | #include "input_common/helpers/joycon_protocol/joycon_types.h" |
| 13 | 14 | ||
| @@ -95,6 +96,7 @@ private: | |||
| 95 | void ReadNfcIRMode(std::span<u8> buffer); | 96 | void ReadNfcIRMode(std::span<u8> buffer); |
| 96 | 97 | ||
| 97 | // Protocol Features | 98 | // Protocol Features |
| 99 | std::unique_ptr<CalibrationProtocol> calibration_protocol = nullptr; | ||
| 98 | std::unique_ptr<GenericProtocol> generic_protocol = nullptr; | 100 | std::unique_ptr<GenericProtocol> generic_protocol = nullptr; |
| 99 | 101 | ||
| 100 | // Connection status | 102 | // Connection status |
diff --git a/src/input_common/helpers/joycon_protocol/calibration.cpp b/src/input_common/helpers/joycon_protocol/calibration.cpp new file mode 100644 index 000000000..5c29af545 --- /dev/null +++ b/src/input_common/helpers/joycon_protocol/calibration.cpp | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <cstring> | ||
| 5 | |||
| 6 | #include "input_common/helpers/joycon_protocol/calibration.h" | ||
| 7 | #include "input_common/helpers/joycon_protocol/joycon_types.h" | ||
| 8 | |||
| 9 | namespace InputCommon::Joycon { | ||
| 10 | |||
| 11 | CalibrationProtocol::CalibrationProtocol(std::shared_ptr<JoyconHandle> handle) | ||
| 12 | : JoyconCommonProtocol(handle) {} | ||
| 13 | |||
| 14 | DriverResult CalibrationProtocol::GetLeftJoyStickCalibration(JoyStickCalibration& calibration) { | ||
| 15 | std::vector<u8> buffer; | ||
| 16 | DriverResult result{DriverResult::Success}; | ||
| 17 | calibration = {}; | ||
| 18 | SetBlocking(); | ||
| 19 | |||
| 20 | result = ReadSPI(CalAddr::USER_LEFT_MAGIC, sizeof(u16), buffer); | ||
| 21 | |||
| 22 | if (result == DriverResult::Success) { | ||
| 23 | const bool has_user_calibration = buffer[0] == 0xB2 && buffer[1] == 0xA1; | ||
| 24 | if (has_user_calibration) { | ||
| 25 | result = ReadSPI(CalAddr::USER_LEFT_DATA, 9, buffer); | ||
| 26 | } else { | ||
| 27 | result = ReadSPI(CalAddr::FACT_LEFT_DATA, 9, buffer); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | if (result == DriverResult::Success) { | ||
| 32 | calibration.x.max = static_cast<u16>(((buffer[1] & 0x0F) << 8) | buffer[0]); | ||
| 33 | calibration.y.max = static_cast<u16>((buffer[2] << 4) | (buffer[1] >> 4)); | ||
| 34 | calibration.x.center = static_cast<u16>(((buffer[4] & 0x0F) << 8) | buffer[3]); | ||
| 35 | calibration.y.center = static_cast<u16>((buffer[5] << 4) | (buffer[4] >> 4)); | ||
| 36 | calibration.x.min = static_cast<u16>(((buffer[7] & 0x0F) << 8) | buffer[6]); | ||
| 37 | calibration.y.min = static_cast<u16>((buffer[8] << 4) | (buffer[7] >> 4)); | ||
| 38 | } | ||
| 39 | |||
| 40 | // Nintendo fix for drifting stick | ||
| 41 | // result = ReadSPI(0x60, 0x86 ,buffer, 16); | ||
| 42 | // calibration.deadzone = (u16)((buffer[4] << 8) & 0xF00 | buffer[3]); | ||
| 43 | |||
| 44 | // Set a valid default calibration if data is missing | ||
| 45 | ValidateCalibration(calibration); | ||
| 46 | |||
| 47 | SetNonBlocking(); | ||
| 48 | return result; | ||
| 49 | } | ||
| 50 | |||
| 51 | DriverResult CalibrationProtocol::GetRightJoyStickCalibration(JoyStickCalibration& calibration) { | ||
| 52 | std::vector<u8> buffer; | ||
| 53 | DriverResult result{DriverResult::Success}; | ||
| 54 | calibration = {}; | ||
| 55 | SetBlocking(); | ||
| 56 | |||
| 57 | result = ReadSPI(CalAddr::USER_RIGHT_MAGIC, sizeof(u16), buffer); | ||
| 58 | |||
| 59 | if (result == DriverResult::Success) { | ||
| 60 | const bool has_user_calibration = buffer[0] == 0xB2 && buffer[1] == 0xA1; | ||
| 61 | if (has_user_calibration) { | ||
| 62 | result = ReadSPI(CalAddr::USER_RIGHT_DATA, 9, buffer); | ||
| 63 | } else { | ||
| 64 | result = ReadSPI(CalAddr::FACT_RIGHT_DATA, 9, buffer); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | if (result == DriverResult::Success) { | ||
| 69 | calibration.x.center = static_cast<u16>(((buffer[1] & 0x0F) << 8) | buffer[0]); | ||
| 70 | calibration.y.center = static_cast<u16>((buffer[2] << 4) | (buffer[1] >> 4)); | ||
| 71 | calibration.x.min = static_cast<u16>(((buffer[4] & 0x0F) << 8) | buffer[3]); | ||
| 72 | calibration.y.min = static_cast<u16>((buffer[5] << 4) | (buffer[4] >> 4)); | ||
| 73 | calibration.x.max = static_cast<u16>(((buffer[7] & 0x0F) << 8) | buffer[6]); | ||
| 74 | calibration.y.max = static_cast<u16>((buffer[8] << 4) | (buffer[7] >> 4)); | ||
| 75 | } | ||
| 76 | |||
| 77 | // Nintendo fix for drifting stick | ||
| 78 | // buffer = ReadSPI(0x60, 0x98 , 16); | ||
| 79 | // joystick.deadzone = (u16)((buffer[4] << 8) & 0xF00 | buffer[3]); | ||
| 80 | |||
| 81 | // Set a valid default calibration if data is missing | ||
| 82 | ValidateCalibration(calibration); | ||
| 83 | |||
| 84 | SetNonBlocking(); | ||
| 85 | return result; | ||
| 86 | } | ||
| 87 | |||
| 88 | DriverResult CalibrationProtocol::GetImuCalibration(MotionCalibration& calibration) { | ||
| 89 | std::vector<u8> buffer; | ||
| 90 | DriverResult result{DriverResult::Success}; | ||
| 91 | calibration = {}; | ||
| 92 | SetBlocking(); | ||
| 93 | |||
| 94 | result = ReadSPI(CalAddr::USER_IMU_MAGIC, sizeof(u16), buffer); | ||
| 95 | |||
| 96 | if (result == DriverResult::Success) { | ||
| 97 | const bool has_user_calibration = buffer[0] == 0xB2 && buffer[1] == 0xA1; | ||
| 98 | if (has_user_calibration) { | ||
| 99 | result = ReadSPI(CalAddr::USER_IMU_DATA, sizeof(IMUCalibration), buffer); | ||
| 100 | } else { | ||
| 101 | result = ReadSPI(CalAddr::FACT_IMU_DATA, sizeof(IMUCalibration), buffer); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | if (result == DriverResult::Success) { | ||
| 106 | IMUCalibration device_calibration{}; | ||
| 107 | memcpy(&device_calibration, buffer.data(), sizeof(IMUCalibration)); | ||
| 108 | calibration.accelerometer[0].offset = device_calibration.accelerometer_offset[0]; | ||
| 109 | calibration.accelerometer[1].offset = device_calibration.accelerometer_offset[1]; | ||
| 110 | calibration.accelerometer[2].offset = device_calibration.accelerometer_offset[2]; | ||
| 111 | |||
| 112 | calibration.accelerometer[0].scale = device_calibration.accelerometer_scale[0]; | ||
| 113 | calibration.accelerometer[1].scale = device_calibration.accelerometer_scale[1]; | ||
| 114 | calibration.accelerometer[2].scale = device_calibration.accelerometer_scale[2]; | ||
| 115 | |||
| 116 | calibration.gyro[0].offset = device_calibration.gyroscope_offset[0]; | ||
| 117 | calibration.gyro[1].offset = device_calibration.gyroscope_offset[1]; | ||
| 118 | calibration.gyro[2].offset = device_calibration.gyroscope_offset[2]; | ||
| 119 | |||
| 120 | calibration.gyro[0].scale = device_calibration.gyroscope_scale[0]; | ||
| 121 | calibration.gyro[1].scale = device_calibration.gyroscope_scale[1]; | ||
| 122 | calibration.gyro[2].scale = device_calibration.gyroscope_scale[2]; | ||
| 123 | } | ||
| 124 | |||
| 125 | ValidateCalibration(calibration); | ||
| 126 | |||
| 127 | SetNonBlocking(); | ||
| 128 | return result; | ||
| 129 | } | ||
| 130 | |||
| 131 | void CalibrationProtocol::ValidateCalibration(JoyStickCalibration& calibration) { | ||
| 132 | constexpr u16 DefaultStickCenter{2048}; | ||
| 133 | constexpr u16 DefaultStickRange{1740}; | ||
| 134 | |||
| 135 | if (calibration.x.center == 0xFFF || calibration.x.center == 0) { | ||
| 136 | calibration.x.center = DefaultStickCenter; | ||
| 137 | } | ||
| 138 | if (calibration.x.max == 0xFFF || calibration.x.max == 0) { | ||
| 139 | calibration.x.max = DefaultStickRange; | ||
| 140 | } | ||
| 141 | if (calibration.x.min == 0xFFF || calibration.x.min == 0) { | ||
| 142 | calibration.x.min = DefaultStickRange; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (calibration.y.center == 0xFFF || calibration.y.center == 0) { | ||
| 146 | calibration.y.center = DefaultStickCenter; | ||
| 147 | } | ||
| 148 | if (calibration.y.max == 0xFFF || calibration.y.max == 0) { | ||
| 149 | calibration.y.max = DefaultStickRange; | ||
| 150 | } | ||
| 151 | if (calibration.y.min == 0xFFF || calibration.y.min == 0) { | ||
| 152 | calibration.y.min = DefaultStickRange; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | void CalibrationProtocol::ValidateCalibration(MotionCalibration& calibration) { | ||
| 157 | for (auto& sensor : calibration.accelerometer) { | ||
| 158 | if (sensor.scale == 0) { | ||
| 159 | sensor.scale = 0x4000; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | for (auto& sensor : calibration.gyro) { | ||
| 163 | if (sensor.scale == 0) { | ||
| 164 | sensor.scale = 0x3be7; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | } // namespace InputCommon::Joycon | ||
diff --git a/src/input_common/helpers/joycon_protocol/calibration.h b/src/input_common/helpers/joycon_protocol/calibration.h new file mode 100644 index 000000000..38214eed4 --- /dev/null +++ b/src/input_common/helpers/joycon_protocol/calibration.h | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | // Based on dkms-hid-nintendo implementation, CTCaer joycon toolkit and dekuNukem reverse | ||
| 5 | // engineering https://github.com/nicman23/dkms-hid-nintendo/blob/master/src/hid-nintendo.c | ||
| 6 | // https://github.com/CTCaer/jc_toolkit | ||
| 7 | // https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering | ||
| 8 | |||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "input_common/helpers/joycon_protocol/common_protocol.h" | ||
| 14 | |||
| 15 | namespace InputCommon::Joycon { | ||
| 16 | enum class DriverResult; | ||
| 17 | struct JoyStickCalibration; | ||
| 18 | struct IMUCalibration; | ||
| 19 | struct JoyconHandle; | ||
| 20 | } // namespace InputCommon::Joycon | ||
| 21 | |||
| 22 | namespace InputCommon::Joycon { | ||
| 23 | |||
| 24 | /// Driver functions related to retrieving calibration data from the device | ||
| 25 | class CalibrationProtocol final : private JoyconCommonProtocol { | ||
| 26 | public: | ||
| 27 | CalibrationProtocol(std::shared_ptr<JoyconHandle> handle); | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Sends a request to obtain the left stick calibration from memory | ||
| 31 | * @param is_factory_calibration if true factory values will be returned | ||
| 32 | * @returns JoyStickCalibration of the left joystick | ||
| 33 | */ | ||
| 34 | DriverResult GetLeftJoyStickCalibration(JoyStickCalibration& calibration); | ||
| 35 | |||
| 36 | /** | ||
| 37 | * Sends a request to obtain the right stick calibration from memory | ||
| 38 | * @param is_factory_calibration if true factory values will be returned | ||
| 39 | * @returns JoyStickCalibration of the right joystick | ||
| 40 | */ | ||
| 41 | DriverResult GetRightJoyStickCalibration(JoyStickCalibration& calibration); | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Sends a request to obtain the motion calibration from memory | ||
| 45 | * @returns ImuCalibration of the motion sensor | ||
| 46 | */ | ||
| 47 | DriverResult GetImuCalibration(MotionCalibration& calibration); | ||
| 48 | |||
| 49 | private: | ||
| 50 | void ValidateCalibration(JoyStickCalibration& calibration); | ||
| 51 | void ValidateCalibration(MotionCalibration& calibration); | ||
| 52 | }; | ||
| 53 | |||
| 54 | } // namespace InputCommon::Joycon | ||