diff options
| author | 2022-12-20 14:30:03 -0600 | |
|---|---|---|
| committer | 2023-01-19 18:05:21 -0600 | |
| commit | 594b2ade6d8d829c65166aebe12f5eb3463a6fe9 (patch) | |
| tree | d6d8013f6252cc9051429f39da255fe6937c8346 /src/input_common/helpers/joycon_protocol/generic_functions.cpp | |
| parent | input_common: Add joycon low level functions (diff) | |
| download | yuzu-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/generic_functions.cpp')
| -rw-r--r-- | src/input_common/helpers/joycon_protocol/generic_functions.cpp | 147 |
1 files changed, 147 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 | |||
| 7 | namespace InputCommon::Joycon { | ||
| 8 | |||
| 9 | GenericProtocol::GenericProtocol(std::shared_ptr<JoyconHandle> handle) | ||
| 10 | : JoyconCommonProtocol(handle) {} | ||
| 11 | |||
| 12 | DriverResult GenericProtocol::EnablePassiveMode() { | ||
| 13 | SetBlocking(); | ||
| 14 | const auto result = SetReportMode(ReportMode::SIMPLE_HID_MODE); | ||
| 15 | SetNonBlocking(); | ||
| 16 | return result; | ||
| 17 | } | ||
| 18 | |||
| 19 | DriverResult GenericProtocol::EnableActiveMode() { | ||
| 20 | SetBlocking(); | ||
| 21 | const auto result = SetReportMode(ReportMode::STANDARD_FULL_60HZ); | ||
| 22 | SetNonBlocking(); | ||
| 23 | return result; | ||
| 24 | } | ||
| 25 | |||
| 26 | DriverResult 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 | |||
| 41 | DriverResult GenericProtocol::GetControllerType(ControllerType& controller_type) { | ||
| 42 | return GetDeviceType(controller_type); | ||
| 43 | } | ||
| 44 | |||
| 45 | DriverResult 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 | |||
| 54 | DriverResult 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 | |||
| 66 | DriverResult GenericProtocol::GetBattery(u32& battery_level) { | ||
| 67 | battery_level = 0; | ||
| 68 | return DriverResult::NotSupported; | ||
| 69 | } | ||
| 70 | |||
| 71 | DriverResult 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 | |||
| 88 | DriverResult 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 | |||
| 102 | DriverResult GenericProtocol::GetTemperature(u32& temperature) { | ||
| 103 | // Not all devices have temperature sensor | ||
| 104 | temperature = 25; | ||
| 105 | return DriverResult::NotSupported; | ||
| 106 | } | ||
| 107 | |||
| 108 | DriverResult 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 | |||
| 117 | DriverResult 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 | |||
| 128 | DriverResult GenericProtocol::SetLedBusy() { | ||
| 129 | return DriverResult::NotSupported; | ||
| 130 | } | ||
| 131 | |||
| 132 | DriverResult 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 | |||
| 143 | DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) { | ||
| 144 | return SetLedPattern(static_cast<u8>(leds << 4)); | ||
| 145 | } | ||
| 146 | |||
| 147 | } // namespace InputCommon::Joycon | ||