diff options
Diffstat (limited to 'src/input_common/helpers/joycon_protocol/calibration.cpp')
| -rw-r--r-- | src/input_common/helpers/joycon_protocol/calibration.cpp | 169 |
1 files changed, 169 insertions, 0 deletions
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 | ||