summaryrefslogtreecommitdiff
path: root/src/input_common/helpers/joycon_protocol
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/input_common/helpers/joycon_protocol
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 'src/input_common/helpers/joycon_protocol')
-rw-r--r--src/input_common/helpers/joycon_protocol/generic_functions.cpp147
-rw-r--r--src/input_common/helpers/joycon_protocol/generic_functions.h108
2 files changed, 255 insertions, 0 deletions
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