summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Narr the Reg2022-12-20 14:30:03 -0600
committerGravatar Narr the Reg2023-01-19 18:05:21 -0600
commit594b2ade6d8d829c65166aebe12f5eb3463a6fe9 (patch)
treed6d8013f6252cc9051429f39da255fe6937c8346 /src
parentinput_common: Add joycon low level functions (diff)
downloadyuzu-594b2ade6d8d829c65166aebe12f5eb3463a6fe9.tar.gz
yuzu-594b2ade6d8d829c65166aebe12f5eb3463a6fe9.tar.xz
yuzu-594b2ade6d8d829c65166aebe12f5eb3463a6fe9.zip
input_common: Add support for joycon generic functions
Diffstat (limited to '')
-rw-r--r--src/input_common/CMakeLists.txt2
-rw-r--r--src/input_common/helpers/joycon_driver.cpp54
-rw-r--r--src/input_common/helpers/joycon_driver.h2
-rw-r--r--src/input_common/helpers/joycon_protocol/generic_functions.cpp147
-rw-r--r--src/input_common/helpers/joycon_protocol/generic_functions.h108
5 files changed, 310 insertions, 3 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt
index 566be9f90..a60cecaf4 100644
--- a/src/input_common/CMakeLists.txt
+++ b/src/input_common/CMakeLists.txt
@@ -59,6 +59,8 @@ if (ENABLE_SDL2)
59 helpers/joycon_driver.h 59 helpers/joycon_driver.h
60 helpers/joycon_protocol/common_protocol.cpp 60 helpers/joycon_protocol/common_protocol.cpp
61 helpers/joycon_protocol/common_protocol.h 61 helpers/joycon_protocol/common_protocol.h
62 helpers/joycon_protocol/generic_functions.cpp
63 helpers/joycon_protocol/generic_functions.h
62 helpers/joycon_protocol/joycon_types.h 64 helpers/joycon_protocol/joycon_types.h
63 ) 65 )
64 target_link_libraries(input_common PRIVATE SDL2::SDL2) 66 target_link_libraries(input_common PRIVATE SDL2::SDL2)
diff --git a/src/input_common/helpers/joycon_driver.cpp b/src/input_common/helpers/joycon_driver.cpp
index a0a2a180b..0de55578b 100644
--- a/src/input_common/helpers/joycon_driver.cpp
+++ b/src/input_common/helpers/joycon_driver.cpp
@@ -64,13 +64,24 @@ 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 generic_protocol = std::make_unique<GenericProtocol>(hidapi_handle);
67 68
68 // Get fixed joycon info 69 // Get fixed joycon info
70 generic_protocol->GetVersionNumber(version);
71 generic_protocol->GetColor(color);
72 if (handle_device_type == ControllerType::Pro) {
73 // Some 3rd party controllers aren't pro controllers
74 generic_protocol->GetControllerType(device_type);
75 } else {
76 device_type = handle_device_type;
77 }
78 generic_protocol->GetSerialNumber(serial_number);
69 supported_features = GetSupportedFeatures(); 79 supported_features = GetSupportedFeatures();
70 80
71 // Get Calibration data 81 // Get Calibration data
72 82
73 // Set led status 83 // Set led status
84 generic_protocol->SetLedBlinkPattern(static_cast<u8>(1 + port));
74 85
75 // Apply HW configuration 86 // Apply HW configuration
76 SetPollingMode(); 87 SetPollingMode();
@@ -137,6 +148,9 @@ void JoyconDriver::OnNewData(std::span<u8> buffer) {
137 case InputReport::SIMPLE_HID_MODE: 148 case InputReport::SIMPLE_HID_MODE:
138 ReadPassiveMode(buffer); 149 ReadPassiveMode(buffer);
139 break; 150 break;
151 case InputReport::SUBCMD_REPLY:
152 LOG_DEBUG(Input, "Unhandled command reply");
153 break;
140 default: 154 default:
141 LOG_ERROR(Input, "Report mode not Implemented {}", report_mode); 155 LOG_ERROR(Input, "Report mode not Implemented {}", report_mode);
142 break; 156 break;
@@ -145,6 +159,30 @@ void JoyconDriver::OnNewData(std::span<u8> buffer) {
145 159
146void JoyconDriver::SetPollingMode() { 160void JoyconDriver::SetPollingMode() {
147 disable_input_thread = true; 161 disable_input_thread = true;
162
163 if (motion_enabled && supported_features.motion) {
164 generic_protocol->EnableImu(true);
165 generic_protocol->SetImuConfig(gyro_sensitivity, gyro_performance,
166 accelerometer_sensitivity, accelerometer_performance);
167 } else {
168 generic_protocol->EnableImu(false);
169 }
170
171 if (passive_enabled && supported_features.passive) {
172 const auto result = generic_protocol->EnablePassiveMode();
173 if (result == DriverResult::Success) {
174 disable_input_thread = false;
175 return;
176 }
177 LOG_ERROR(Input, "Error enabling passive mode");
178 }
179
180 // Default Mode
181 const auto result = generic_protocol->EnableActiveMode();
182 if (result != DriverResult::Success) {
183 LOG_ERROR(Input, "Error enabling active mode");
184 }
185
148 disable_input_thread = false; 186 disable_input_thread = false;
149} 187}
150 188
@@ -257,15 +295,22 @@ bool JoyconDriver::IsPayloadCorrect(int status, std::span<const u8> buffer) {
257 295
258DriverResult JoyconDriver::SetVibration(const VibrationValue& vibration) { 296DriverResult JoyconDriver::SetVibration(const VibrationValue& vibration) {
259 std::scoped_lock lock{mutex}; 297 std::scoped_lock lock{mutex};
298 if (disable_input_thread) {
299 return DriverResult::HandleInUse;
300 }
260 return DriverResult::NotSupported; 301 return DriverResult::NotSupported;
261} 302}
262 303
263DriverResult JoyconDriver::SetLedConfig(u8 led_pattern) { 304DriverResult JoyconDriver::SetLedConfig(u8 led_pattern) {
264 std::scoped_lock lock{mutex}; 305 std::scoped_lock lock{mutex};
265 return DriverResult::NotSupported; 306 if (disable_input_thread) {
307 return DriverResult::HandleInUse;
308 }
309 return generic_protocol->SetLedPattern(led_pattern);
266} 310}
267 311
268DriverResult JoyconDriver::SetPasiveMode() { 312DriverResult JoyconDriver::SetPasiveMode() {
313 std::scoped_lock lock{mutex};
269 motion_enabled = false; 314 motion_enabled = false;
270 hidbus_enabled = false; 315 hidbus_enabled = false;
271 nfc_enabled = false; 316 nfc_enabled = false;
@@ -275,7 +320,8 @@ DriverResult JoyconDriver::SetPasiveMode() {
275} 320}
276 321
277DriverResult JoyconDriver::SetActiveMode() { 322DriverResult JoyconDriver::SetActiveMode() {
278 motion_enabled = false; 323 std::scoped_lock lock{mutex};
324 motion_enabled = true;
279 hidbus_enabled = false; 325 hidbus_enabled = false;
280 nfc_enabled = false; 326 nfc_enabled = false;
281 passive_enabled = false; 327 passive_enabled = false;
@@ -284,6 +330,7 @@ DriverResult JoyconDriver::SetActiveMode() {
284} 330}
285 331
286DriverResult JoyconDriver::SetNfcMode() { 332DriverResult JoyconDriver::SetNfcMode() {
333 std::scoped_lock lock{mutex};
287 motion_enabled = false; 334 motion_enabled = false;
288 hidbus_enabled = false; 335 hidbus_enabled = false;
289 nfc_enabled = true; 336 nfc_enabled = true;
@@ -293,6 +340,7 @@ DriverResult JoyconDriver::SetNfcMode() {
293} 340}
294 341
295DriverResult JoyconDriver::SetRingConMode() { 342DriverResult JoyconDriver::SetRingConMode() {
343 std::scoped_lock lock{mutex};
296 motion_enabled = true; 344 motion_enabled = true;
297 hidbus_enabled = true; 345 hidbus_enabled = true;
298 nfc_enabled = false; 346 nfc_enabled = false;
@@ -328,7 +376,7 @@ std::size_t JoyconDriver::GetDevicePort() const {
328 376
329ControllerType JoyconDriver::GetDeviceType() const { 377ControllerType JoyconDriver::GetDeviceType() const {
330 std::scoped_lock lock{mutex}; 378 std::scoped_lock lock{mutex};
331 return handle_device_type; 379 return device_type;
332} 380}
333 381
334ControllerType JoyconDriver::GetHandleDeviceType() const { 382ControllerType JoyconDriver::GetHandleDeviceType() const {
diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h
index be3053a7b..deb50ec77 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/generic_functions.h"
11#include "input_common/helpers/joycon_protocol/joycon_types.h" 12#include "input_common/helpers/joycon_protocol/joycon_types.h"
12 13
13namespace InputCommon::Joycon { 14namespace InputCommon::Joycon {
@@ -94,6 +95,7 @@ private:
94 void ReadNfcIRMode(std::span<u8> buffer); 95 void ReadNfcIRMode(std::span<u8> buffer);
95 96
96 // Protocol Features 97 // Protocol Features
98 std::unique_ptr<GenericProtocol> generic_protocol = nullptr;
97 99
98 // Connection status 100 // Connection status
99 bool is_connected{}; 101 bool is_connected{};
diff --git a/src/input_common/helpers/joycon_protocol/generic_functions.cpp b/src/input_common/helpers/joycon_protocol/generic_functions.cpp
new file mode 100644
index 000000000..829f7625d
--- /dev/null
+++ b/src/input_common/helpers/joycon_protocol/generic_functions.cpp
@@ -0,0 +1,147 @@
1// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include "common/logging/log.h"
5#include "input_common/helpers/joycon_protocol/generic_functions.h"
6
7namespace InputCommon::Joycon {
8
9GenericProtocol::GenericProtocol(std::shared_ptr<JoyconHandle> handle)
10 : JoyconCommonProtocol(handle) {}
11
12DriverResult GenericProtocol::EnablePassiveMode() {
13 SetBlocking();
14 const auto result = SetReportMode(ReportMode::SIMPLE_HID_MODE);
15 SetNonBlocking();
16 return result;
17}
18
19DriverResult GenericProtocol::EnableActiveMode() {
20 SetBlocking();
21 const auto result = SetReportMode(ReportMode::STANDARD_FULL_60HZ);
22 SetNonBlocking();
23 return result;
24}
25
26DriverResult GenericProtocol::GetDeviceInfo(DeviceInfo& device_info) {
27 std::vector<u8> output;
28 SetBlocking();
29
30 const auto result = SendSubCommand(SubCommand::REQ_DEV_INFO, {}, output);
31
32 device_info = {};
33 if (result == DriverResult::Success) {
34 memcpy(&device_info, output.data(), sizeof(DeviceInfo));
35 }
36
37 SetNonBlocking();
38 return result;
39}
40
41DriverResult GenericProtocol::GetControllerType(ControllerType& controller_type) {
42 return GetDeviceType(controller_type);
43}
44
45DriverResult GenericProtocol::EnableImu(bool enable) {
46 const std::vector<u8> buffer{static_cast<u8>(enable ? 1 : 0)};
47 std::vector<u8> output;
48 SetBlocking();
49 const auto result = SendSubCommand(SubCommand::ENABLE_IMU, buffer, output);
50 SetNonBlocking();
51 return result;
52}
53
54DriverResult GenericProtocol::SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
55 AccelerometerSensitivity asen,
56 AccelerometerPerformance afrec) {
57 const std::vector<u8> buffer{static_cast<u8>(gsen), static_cast<u8>(asen),
58 static_cast<u8>(gfrec), static_cast<u8>(afrec)};
59 std::vector<u8> output;
60 SetBlocking();
61 const auto result = SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer, output);
62 SetNonBlocking();
63 return result;
64}
65
66DriverResult GenericProtocol::GetBattery(u32& battery_level) {
67 battery_level = 0;
68 return DriverResult::NotSupported;
69}
70
71DriverResult GenericProtocol::GetColor(Color& color) {
72 std::vector<u8> buffer;
73 SetBlocking();
74 const auto result = ReadSPI(CalAddr::COLOR_DATA, 12, buffer);
75 SetNonBlocking();
76
77 color = {};
78 if (result == DriverResult::Success) {
79 color.body = static_cast<u32>((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]);
80 color.buttons = static_cast<u32>((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]);
81 color.left_grip = static_cast<u32>((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]);
82 color.right_grip = static_cast<u32>((buffer[9] << 16) | (buffer[10] << 8) | buffer[11]);
83 }
84
85 return result;
86}
87
88DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
89 std::vector<u8> buffer;
90 SetBlocking();
91 const auto result = ReadSPI(CalAddr::SERIAL_NUMBER, 16, buffer);
92 SetNonBlocking();
93
94 serial_number = {};
95 if (result == DriverResult::Success) {
96 memcpy(serial_number.data(), buffer.data() + 1, sizeof(SerialNumber));
97 }
98
99 return result;
100}
101
102DriverResult GenericProtocol::GetTemperature(u32& temperature) {
103 // Not all devices have temperature sensor
104 temperature = 25;
105 return DriverResult::NotSupported;
106}
107
108DriverResult GenericProtocol::GetVersionNumber(FirmwareVersion& version) {
109 DeviceInfo device_info{};
110
111 const auto result = GetDeviceInfo(device_info);
112 version = device_info.firmware;
113
114 return result;
115}
116
117DriverResult GenericProtocol::SetHomeLight() {
118 const std::vector<u8> buffer{0x0f, 0xf0, 0x00};
119 std::vector<u8> output;
120 SetBlocking();
121
122 const auto result = SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer, output);
123
124 SetNonBlocking();
125 return result;
126}
127
128DriverResult GenericProtocol::SetLedBusy() {
129 return DriverResult::NotSupported;
130}
131
132DriverResult GenericProtocol::SetLedPattern(u8 leds) {
133 const std::vector<u8> buffer{leds};
134 std::vector<u8> output;
135 SetBlocking();
136
137 const auto result = SendSubCommand(SubCommand::SET_PLAYER_LIGHTS, buffer, output);
138
139 SetNonBlocking();
140 return result;
141}
142
143DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) {
144 return SetLedPattern(static_cast<u8>(leds << 4));
145}
146
147} // namespace InputCommon::Joycon
diff --git a/src/input_common/helpers/joycon_protocol/generic_functions.h b/src/input_common/helpers/joycon_protocol/generic_functions.h
new file mode 100644
index 000000000..c3e2ccadc
--- /dev/null
+++ b/src/input_common/helpers/joycon_protocol/generic_functions.h
@@ -0,0 +1,108 @@
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 "input_common/helpers/joycon_protocol/common_protocol.h"
12#include "input_common/helpers/joycon_protocol/joycon_types.h"
13
14namespace InputCommon::Joycon {
15
16/// Joycon driver functions that easily implemented
17class GenericProtocol final : private JoyconCommonProtocol {
18public:
19 GenericProtocol(std::shared_ptr<JoyconHandle> handle);
20
21 /// Enables passive mode. This mode only sends button data on change. Sticks will return digital
22 /// data instead of analog. Motion will be disabled
23 DriverResult EnablePassiveMode();
24
25 /// Enables active mode. This mode will return the current status every 5-15ms
26 DriverResult EnableActiveMode();
27
28 /**
29 * Sends a request to obtain the joycon firmware and mac from handle
30 * @returns controller device info
31 */
32 DriverResult GetDeviceInfo(DeviceInfo& controller_type);
33
34 /**
35 * Sends a request to obtain the joycon type from handle
36 * @returns controller type of the joycon
37 */
38 DriverResult GetControllerType(ControllerType& controller_type);
39
40 /**
41 * Enables motion input
42 * @param enable if true motion data will be enabled
43 */
44 DriverResult EnableImu(bool enable);
45
46 /**
47 * Configures the motion sensor with the specified parameters
48 * @param gsen gyroscope sensor sensitvity in degrees per second
49 * @param gfrec gyroscope sensor frequency in hertz
50 * @param asen accelerometer sensitivity in G force
51 * @param afrec accelerometer frequency in hertz
52 */
53 DriverResult SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
54 AccelerometerSensitivity asen, AccelerometerPerformance afrec);
55
56 /**
57 * Request battery level from the device
58 * @returns battery level
59 */
60 DriverResult GetBattery(u32& battery_level);
61
62 /**
63 * Request joycon colors from the device
64 * @returns colors of the body and buttons
65 */
66 DriverResult GetColor(Color& color);
67
68 /**
69 * Request joycon serial number from the device
70 * @returns 16 byte serial number
71 */
72 DriverResult GetSerialNumber(SerialNumber& serial_number);
73
74 /**
75 * Request joycon serial number from the device
76 * @returns 16 byte serial number
77 */
78 DriverResult GetTemperature(u32& temperature);
79
80 /**
81 * Request joycon serial number from the device
82 * @returns 16 byte serial number
83 */
84 DriverResult GetVersionNumber(FirmwareVersion& version);
85
86 /**
87 * Sets home led behaviour
88 */
89 DriverResult SetHomeLight();
90
91 /**
92 * Sets home led into a slow breathing state
93 */
94 DriverResult SetLedBusy();
95
96 /**
97 * Sets the 4 player leds on the joycon on a solid state
98 * @params bit flag containing the led state
99 */
100 DriverResult SetLedPattern(u8 leds);
101
102 /**
103 * Sets the 4 player leds on the joycon on a blinking state
104 * @returns bit flag containing the led state
105 */
106 DriverResult SetLedBlinkPattern(u8 leds);
107};
108} // namespace InputCommon::Joycon