early-access version 3260
This commit is contained in:
@@ -68,6 +68,8 @@ if (ENABLE_SDL2)
|
||||
helpers/joycon_protocol/generic_functions.cpp
|
||||
helpers/joycon_protocol/generic_functions.h
|
||||
helpers/joycon_protocol/joycon_types.h
|
||||
helpers/joycon_protocol/irs.cpp
|
||||
helpers/joycon_protocol/irs.h
|
||||
helpers/joycon_protocol/nfc.cpp
|
||||
helpers/joycon_protocol/nfc.h
|
||||
helpers/joycon_protocol/poller.cpp
|
||||
|
@@ -7,6 +7,7 @@
|
||||
#include "input_common/helpers/joycon_driver.h"
|
||||
#include "input_common/helpers/joycon_protocol/calibration.h"
|
||||
#include "input_common/helpers/joycon_protocol/generic_functions.h"
|
||||
#include "input_common/helpers/joycon_protocol/irs.h"
|
||||
#include "input_common/helpers/joycon_protocol/nfc.h"
|
||||
#include "input_common/helpers/joycon_protocol/poller.h"
|
||||
#include "input_common/helpers/joycon_protocol/ringcon.h"
|
||||
@@ -78,6 +79,7 @@ DriverResult JoyconDriver::InitializeDevice() {
|
||||
// Initialize HW Protocols
|
||||
calibration_protocol = std::make_unique<CalibrationProtocol>(hidapi_handle);
|
||||
generic_protocol = std::make_unique<GenericProtocol>(hidapi_handle);
|
||||
irs_protocol = std::make_unique<IrsProtocol>(hidapi_handle);
|
||||
nfc_protocol = std::make_unique<NfcProtocol>(hidapi_handle);
|
||||
ring_protocol = std::make_unique<RingConProtocol>(hidapi_handle);
|
||||
rumble_protocol = std::make_unique<RumbleProtocol>(hidapi_handle);
|
||||
@@ -251,6 +253,20 @@ DriverResult JoyconDriver::SetPollingMode() {
|
||||
generic_protocol->EnableImu(false);
|
||||
}
|
||||
|
||||
if (irs_protocol->IsEnabled()) {
|
||||
irs_protocol->DisableIrs();
|
||||
}
|
||||
|
||||
if (irs_enabled && supported_features.irs) {
|
||||
auto result = irs_protocol->EnableIrs();
|
||||
if (result == DriverResult::Success) {
|
||||
disable_input_thread = false;
|
||||
return result;
|
||||
}
|
||||
irs_protocol->DisableIrs();
|
||||
LOG_ERROR(Input, "Error enabling IRS");
|
||||
}
|
||||
|
||||
if (nfc_protocol->IsEnabled()) {
|
||||
amiibo_detected = false;
|
||||
nfc_protocol->DisableNfc();
|
||||
|
@@ -13,6 +13,7 @@
|
||||
namespace InputCommon::Joycon {
|
||||
class CalibrationProtocol;
|
||||
class GenericProtocol;
|
||||
class IrsProtocol;
|
||||
class NfcProtocol;
|
||||
class JoyconPoller;
|
||||
class RingConProtocol;
|
||||
@@ -87,6 +88,7 @@ private:
|
||||
// Protocol Features
|
||||
std::unique_ptr<CalibrationProtocol> calibration_protocol;
|
||||
std::unique_ptr<GenericProtocol> generic_protocol;
|
||||
std::unique_ptr<IrsProtocol> irs_protocol;
|
||||
std::unique_ptr<NfcProtocol> nfc_protocol;
|
||||
std::unique_ptr<JoyconPoller> joycon_poller;
|
||||
std::unique_ptr<RingConProtocol> ring_protocol;
|
||||
|
@@ -120,6 +120,19 @@ DriverResult JoyconCommonProtocol::SendSubCommand(SubCommand sc, std::span<const
|
||||
return DriverResult::Success;
|
||||
}
|
||||
|
||||
DriverResult JoyconCommonProtocol::SendMcuCommand(SubCommand sc, std::span<const u8> buffer) {
|
||||
std::vector<u8> local_buffer(MaxResponseSize);
|
||||
|
||||
local_buffer[0] = static_cast<u8>(OutputReport::MCU_DATA);
|
||||
local_buffer[1] = GetCounter();
|
||||
local_buffer[10] = static_cast<u8>(sc);
|
||||
for (std::size_t i = 0; i < buffer.size(); ++i) {
|
||||
local_buffer[11 + i] = buffer[i];
|
||||
}
|
||||
|
||||
return SendData(local_buffer);
|
||||
}
|
||||
|
||||
DriverResult JoyconCommonProtocol::SendVibrationReport(std::span<const u8> buffer) {
|
||||
std::vector<u8> local_buffer(MaxResponseSize);
|
||||
|
||||
|
@@ -74,6 +74,13 @@ public:
|
||||
*/
|
||||
DriverResult SendSubCommand(SubCommand sc, std::span<const u8> buffer, std::vector<u8>& output);
|
||||
|
||||
/**
|
||||
* Sends a mcu command to the device
|
||||
* @param sc sub command to be send
|
||||
* @param buffer data to be send
|
||||
*/
|
||||
DriverResult SendMcuCommand(SubCommand sc, std::span<const u8> buffer);
|
||||
|
||||
/**
|
||||
* Sends vibration data to the joycon
|
||||
* @param buffer data to be send
|
||||
|
209
src/input_common/helpers/joycon_protocol/irs.cpp
Executable file
209
src/input_common/helpers/joycon_protocol/irs.cpp
Executable file
@@ -0,0 +1,209 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <thread>
|
||||
#include "common/logging/log.h"
|
||||
#include "input_common/helpers/joycon_protocol/irs.h"
|
||||
|
||||
namespace InputCommon::Joycon {
|
||||
|
||||
IrsProtocol::IrsProtocol(std::shared_ptr<JoyconHandle> handle)
|
||||
: JoyconCommonProtocol(std::move(handle)) {}
|
||||
|
||||
DriverResult IrsProtocol::EnableIrs() {
|
||||
LOG_INFO(Input, "Enable IRS");
|
||||
DriverResult result{DriverResult::Success};
|
||||
SetBlocking();
|
||||
|
||||
if (result == DriverResult::Success) {
|
||||
result = SetReportMode(ReportMode::NFC_IR_MODE_60HZ);
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
result = EnableMCU(true);
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::Standby);
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
const MCUConfig config{
|
||||
.command = MCUCommand::ConfigureMCU,
|
||||
.sub_command = MCUSubCommand::SetMCUMode,
|
||||
.mode = MCUMode::IR,
|
||||
.crc = {},
|
||||
};
|
||||
|
||||
result = ConfigureMCU(config);
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
result = WaitSetMCUMode(ReportMode::NFC_IR_MODE_60HZ, MCUMode::IR);
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
result = ConfigureIrs();
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
result = WriteRegistersStep1();
|
||||
}
|
||||
if (result == DriverResult::Success) {
|
||||
result = WriteRegistersStep2();
|
||||
}
|
||||
|
||||
is_enabled = true;
|
||||
|
||||
SetNonBlocking();
|
||||
return result;
|
||||
}
|
||||
|
||||
DriverResult IrsProtocol::DisableIrs() {
|
||||
LOG_DEBUG(Input, "Disable IRS");
|
||||
DriverResult result{DriverResult::Success};
|
||||
SetBlocking();
|
||||
|
||||
if (result == DriverResult::Success) {
|
||||
result = EnableMCU(false);
|
||||
}
|
||||
|
||||
is_enabled = false;
|
||||
|
||||
SetNonBlocking();
|
||||
return result;
|
||||
}
|
||||
|
||||
DriverResult IrsProtocol::ConfigureIrs() {
|
||||
LOG_DEBUG(Input, "Configure IRS");
|
||||
constexpr std::size_t max_tries = 28;
|
||||
std::vector<u8> output;
|
||||
std::size_t tries = 0;
|
||||
|
||||
const IrsConfigure irs_configuration{
|
||||
.command = MCUCommand::ConfigureIR,
|
||||
.sub_command = MCUSubCommand::SetDeviceMode,
|
||||
.irs_mode = IrsMode::ImageTransfer,
|
||||
.number_of_fragments = 0x3,
|
||||
.mcu_major_version = 0x0500,
|
||||
.mcu_minor_version = 0x1800,
|
||||
.crc = {},
|
||||
};
|
||||
|
||||
std::vector<u8> request_data(sizeof(IrsConfigure));
|
||||
memcpy(request_data.data(), &irs_configuration, sizeof(IrsConfigure));
|
||||
request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
|
||||
do {
|
||||
const auto result = SendSubCommand(SubCommand::SET_MCU_CONFIG, request_data, output);
|
||||
|
||||
if (result != DriverResult::Success) {
|
||||
return result;
|
||||
}
|
||||
if (tries++ >= max_tries) {
|
||||
return DriverResult::WrongReply;
|
||||
}
|
||||
} while (output[15] != 0x0b);
|
||||
|
||||
return DriverResult::Success;
|
||||
}
|
||||
|
||||
DriverResult IrsProtocol::WriteRegistersStep1() {
|
||||
LOG_DEBUG(Input, "Configure IRS");
|
||||
DriverResult result{DriverResult::Success};
|
||||
constexpr std::size_t max_tries = 28;
|
||||
std::vector<u8> output;
|
||||
std::size_t tries = 0;
|
||||
|
||||
const IrsWriteRegisters irs_registers{
|
||||
.command = MCUCommand::ConfigureIR,
|
||||
.sub_command = MCUSubCommand::WriteDeviceRegisters,
|
||||
.number_of_registers = 0x9,
|
||||
.registers =
|
||||
{
|
||||
IrsRegister{0x2e00, resolution},
|
||||
{0x3001, static_cast<u8>(exposure & 0xff)},
|
||||
{0x3101, static_cast<u8>(exposure >> 8)},
|
||||
{0x3201, 0x00},
|
||||
{0x1000, leds},
|
||||
{0x2e01, static_cast<u8>((digital_gain & 0x0f) << 4)},
|
||||
{0x2f01, static_cast<u8>((digital_gain & 0xf0) >> 4)},
|
||||
{0x0e00, ex_light_filter},
|
||||
{0x4301, 0xc8},
|
||||
},
|
||||
.crc = {},
|
||||
};
|
||||
|
||||
std::vector<u8> request_data(sizeof(IrsWriteRegisters));
|
||||
memcpy(request_data.data(), &irs_registers, sizeof(IrsWriteRegisters));
|
||||
request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
|
||||
|
||||
std::array<u8, 38> mcu_request{0x02};
|
||||
mcu_request[36] = CalculateMCU_CRC8(mcu_request.data(), 36);
|
||||
mcu_request[37] = 0xFF;
|
||||
|
||||
if (result != DriverResult::Success) {
|
||||
return result;
|
||||
}
|
||||
|
||||
do {
|
||||
result = SendSubCommand(SubCommand::SET_MCU_CONFIG, request_data, output);
|
||||
|
||||
// First time we need to set the report mode
|
||||
if (result == DriverResult::Success && tries == 0) {
|
||||
result = SendMcuCommand(SubCommand::SET_REPORT_MODE, mcu_request);
|
||||
}
|
||||
if (result == DriverResult::Success && tries == 0) {
|
||||
GetSubCommandResponse(SubCommand::SET_MCU_CONFIG, output);
|
||||
}
|
||||
|
||||
if (result != DriverResult::Success) {
|
||||
return result;
|
||||
}
|
||||
if (tries++ >= max_tries) {
|
||||
return DriverResult::WrongReply;
|
||||
}
|
||||
} while (!(output[15] == 0x13 && output[17] == 0x07) && output[15] != 0x23);
|
||||
|
||||
return DriverResult::Success;
|
||||
}
|
||||
|
||||
DriverResult IrsProtocol::WriteRegistersStep2() {
|
||||
LOG_DEBUG(Input, "Configure IRS");
|
||||
constexpr std::size_t max_tries = 28;
|
||||
std::vector<u8> output;
|
||||
std::size_t tries = 0;
|
||||
|
||||
const IrsWriteRegisters irs_registers{
|
||||
.command = MCUCommand::ConfigureIR,
|
||||
.sub_command = MCUSubCommand::WriteDeviceRegisters,
|
||||
.number_of_registers = 0x8,
|
||||
.registers =
|
||||
{
|
||||
IrsRegister{0x1100, static_cast<u8>(led_intensity >> 8)},
|
||||
{0x1200, static_cast<u8>(led_intensity & 0xff)},
|
||||
{0x2d00, image_flip},
|
||||
{0x6701, static_cast<u8>((denoise >> 16) & 0xff)},
|
||||
{0x6801, static_cast<u8>((denoise >> 8) & 0xff)},
|
||||
{0x6901, static_cast<u8>(denoise & 0xff)},
|
||||
{0x0400, 0x2d},
|
||||
{0x0700, 0x01},
|
||||
},
|
||||
.crc = {},
|
||||
};
|
||||
|
||||
std::vector<u8> request_data(sizeof(IrsWriteRegisters));
|
||||
memcpy(request_data.data(), &irs_registers, sizeof(IrsWriteRegisters));
|
||||
request_data[37] = CalculateMCU_CRC8(request_data.data() + 1, 36);
|
||||
do {
|
||||
const auto result = SendSubCommand(SubCommand::SET_MCU_CONFIG, request_data, output);
|
||||
|
||||
if (result != DriverResult::Success) {
|
||||
return result;
|
||||
}
|
||||
if (tries++ >= max_tries) {
|
||||
return DriverResult::WrongReply;
|
||||
}
|
||||
} while (output[15] != 0x13 && output[15] != 0x23);
|
||||
|
||||
return DriverResult::Success;
|
||||
}
|
||||
|
||||
bool IrsProtocol::IsEnabled() const {
|
||||
return is_enabled;
|
||||
}
|
||||
|
||||
} // namespace InputCommon::Joycon
|
46
src/input_common/helpers/joycon_protocol/irs.h
Executable file
46
src/input_common/helpers/joycon_protocol/irs.h
Executable file
@@ -0,0 +1,46 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
// Based on dkms-hid-nintendo implementation, CTCaer joycon toolkit and dekuNukem reverse
|
||||
// engineering https://github.com/nicman23/dkms-hid-nintendo/blob/master/src/hid-nintendo.c
|
||||
// https://github.com/CTCaer/jc_toolkit
|
||||
// https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "input_common/helpers/joycon_protocol/common_protocol.h"
|
||||
#include "input_common/helpers/joycon_protocol/joycon_types.h"
|
||||
|
||||
namespace InputCommon::Joycon {
|
||||
|
||||
class IrsProtocol final : private JoyconCommonProtocol {
|
||||
public:
|
||||
explicit IrsProtocol(std::shared_ptr<JoyconHandle> handle);
|
||||
|
||||
DriverResult EnableIrs();
|
||||
|
||||
DriverResult DisableIrs();
|
||||
|
||||
bool IsEnabled() const;
|
||||
|
||||
private:
|
||||
DriverResult ConfigureIrs();
|
||||
|
||||
DriverResult WriteRegistersStep1();
|
||||
DriverResult WriteRegistersStep2();
|
||||
|
||||
bool is_enabled{};
|
||||
|
||||
u8 resolution = 0x69;
|
||||
u8 leds = 0x00;
|
||||
u8 ex_light_filter = 0x03;
|
||||
u8 image_flip = 0x00;
|
||||
u8 digital_gain = 0x01;
|
||||
u16 exposure = 0x2490;
|
||||
u16 led_intensity = 0x0f10;
|
||||
u32 denoise = 0x012344;
|
||||
};
|
||||
|
||||
} // namespace InputCommon::Joycon
|
@@ -273,6 +273,17 @@ enum class NFCTagType : u8 {
|
||||
Ntag215 = 0x01,
|
||||
};
|
||||
|
||||
enum class IrsMode : u8 {
|
||||
None = 0x02,
|
||||
Moment = 0x03,
|
||||
Dpd = 0x04,
|
||||
Clustering = 0x06,
|
||||
ImageTransfer = 0x07,
|
||||
Silhouette = 0x08,
|
||||
TeraImage = 0x09,
|
||||
SilhouetteTeraImage = 0x0A,
|
||||
};
|
||||
|
||||
enum class DriverResult {
|
||||
Success,
|
||||
WrongReply,
|
||||
@@ -456,6 +467,36 @@ struct NFCRequestState {
|
||||
};
|
||||
static_assert(sizeof(NFCRequestState) == 0x26, "NFCRequestState is an invalid size");
|
||||
|
||||
struct IrsConfigure {
|
||||
MCUCommand command;
|
||||
MCUSubCommand sub_command;
|
||||
IrsMode irs_mode;
|
||||
u8 number_of_fragments;
|
||||
u16 mcu_major_version;
|
||||
u16 mcu_minor_version;
|
||||
INSERT_PADDING_BYTES(0x1D);
|
||||
u8 crc;
|
||||
};
|
||||
static_assert(sizeof(IrsConfigure) == 0x26, "IrsConfigure is an invalid size");
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct IrsRegister {
|
||||
u16 address;
|
||||
u8 value;
|
||||
};
|
||||
static_assert(sizeof(IrsRegister) == 0x3, "IrsRegister is an invalid size");
|
||||
|
||||
struct IrsWriteRegisters {
|
||||
MCUCommand command;
|
||||
MCUSubCommand sub_command;
|
||||
u8 number_of_registers;
|
||||
std::array<IrsRegister, 9> registers;
|
||||
INSERT_PADDING_BYTES(0x7);
|
||||
u8 crc;
|
||||
};
|
||||
static_assert(sizeof(IrsWriteRegisters) == 0x26, "IrsWriteRegisters is an invalid size");
|
||||
#pragma pack(pop)
|
||||
|
||||
struct FirmwareVersion {
|
||||
u8 major;
|
||||
u8 minor;
|
||||
|
Reference in New Issue
Block a user