From d80e6c399bf8196646cca5ac1265d122638bb96b Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Tue, 20 Dec 2022 11:34:33 -0600 Subject: input_common: Initial skeleton for custom joycon driver --- src/input_common/helpers/joycon_driver.h | 146 +++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/input_common/helpers/joycon_driver.h (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h new file mode 100644 index 000000000..be3053a7b --- /dev/null +++ b/src/input_common/helpers/joycon_driver.h @@ -0,0 +1,146 @@ +// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include +#include + +#include "input_common/helpers/joycon_protocol/joycon_types.h" + +namespace InputCommon::Joycon { + +class JoyconDriver final { +public: + explicit JoyconDriver(std::size_t port_); + + ~JoyconDriver(); + + DriverResult RequestDeviceAccess(SDL_hid_device_info* device_info); + DriverResult InitializeDevice(); + void Stop(); + + bool IsConnected() const; + bool IsVibrationEnabled() const; + + FirmwareVersion GetDeviceVersion() const; + Color GetDeviceColor() const; + std::size_t GetDevicePort() const; + ControllerType GetDeviceType() const; + ControllerType GetHandleDeviceType() const; + SerialNumber GetSerialNumber() const; + SerialNumber GetHandleSerialNumber() const; + + DriverResult SetVibration(const VibrationValue& vibration); + DriverResult SetLedConfig(u8 led_pattern); + DriverResult SetPasiveMode(); + DriverResult SetActiveMode(); + DriverResult SetNfcMode(); + DriverResult SetRingConMode(); + + // Returns device type from hidapi handle + static Joycon::DriverResult GetDeviceType(SDL_hid_device_info* device_info, + Joycon::ControllerType& controller_type); + + // Returns serial number from hidapi handle + static Joycon::DriverResult GetSerialNumber(SDL_hid_device_info* device_info, + Joycon::SerialNumber& serial_number); + + std::function on_battery_data; + std::function on_color_data; + std::function on_button_data; + std::function on_stick_data; + std::function on_motion_data; + std::function on_ring_data; + std::function&)> on_amiibo_data; + +private: + struct SupportedFeatures { + bool passive{}; + bool hidbus{}; + bool irs{}; + bool motion{}; + bool nfc{}; + bool vibration{}; + }; + + /// Main thread, actively request new data from the handle + void InputThread(std::stop_token stop_token); + + /// Called everytime a valid package arrives + void OnNewData(std::span buffer); + + /// Updates device configuration to enable or disable features + void SetPollingMode(); + + /// Returns true if input thread is valid and doesn't need to be stopped + bool IsInputThreadValid() const; + + /// Returns true if the data should be interpreted. Otherwise the error counter is incremented + bool IsPayloadCorrect(int status, std::span buffer); + + /// Returns a list of supported features that can be enabled on this device + SupportedFeatures GetSupportedFeatures(); + + /// Handles data from passive packages + void ReadPassiveMode(std::span buffer); + + /// Handles data from active packages + void ReadActiveMode(std::span buffer); + + /// Handles data from nfc or ir packages + void ReadNfcIRMode(std::span buffer); + + // Protocol Features + + // Connection status + bool is_connected{}; + u64 delta_time; + std::size_t error_counter{}; + std::shared_ptr hidapi_handle = nullptr; + std::chrono::time_point last_update; + + // External device status + bool starlink_connected{}; + bool ring_connected{}; + bool amiibo_detected{}; + + // Harware configuration + u8 leds{}; + ReportMode mode{}; + bool passive_enabled{}; // Low power mode, Ideal for multiple controllers at the same time + bool hidbus_enabled{}; // External device support + bool irs_enabled{}; // Infrared camera input + bool motion_enabled{}; // Enables motion input + bool nfc_enabled{}; // Enables Amiibo detection + bool vibration_enabled{}; // Allows vibrations + + // Calibration data + GyroSensitivity gyro_sensitivity{}; + GyroPerformance gyro_performance{}; + AccelerometerSensitivity accelerometer_sensitivity{}; + AccelerometerPerformance accelerometer_performance{}; + JoyStickCalibration left_stick_calibration{}; + JoyStickCalibration right_stick_calibration{}; + MotionCalibration motion_calibration{}; + + // Fixed joycon info + FirmwareVersion version{}; + Color color{}; + std::size_t port{}; + ControllerType device_type{}; // Device type reported by controller + ControllerType handle_device_type{}; // Device type reported by hidapi + SerialNumber serial_number{}; // Serial number reported by controller + SerialNumber handle_serial_number{}; // Serial number type reported by hidapi + SupportedFeatures supported_features{}; + + // Thread related + mutable std::mutex mutex; + std::jthread input_thread; + bool input_thread_running{}; + bool disable_input_thread{}; +}; + +} // namespace InputCommon::Joycon -- cgit v1.2.3 From 594b2ade6d8d829c65166aebe12f5eb3463a6fe9 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Tue, 20 Dec 2022 14:30:03 -0600 Subject: input_common: Add support for joycon generic functions --- src/input_common/helpers/joycon_driver.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/input_common/helpers/joycon_driver.h') 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 @@ #include #include +#include "input_common/helpers/joycon_protocol/generic_functions.h" #include "input_common/helpers/joycon_protocol/joycon_types.h" namespace InputCommon::Joycon { @@ -94,6 +95,7 @@ private: void ReadNfcIRMode(std::span buffer); // Protocol Features + std::unique_ptr generic_protocol = nullptr; // Connection status bool is_connected{}; -- cgit v1.2.3 From 5676c2e17fe895e450e185029991fc20bdf56ec5 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Tue, 20 Dec 2022 18:09:59 -0600 Subject: input_common: Use calibration from joycon --- src/input_common/helpers/joycon_driver.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index deb50ec77..275c97b91 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -8,6 +8,7 @@ #include #include +#include "input_common/helpers/joycon_protocol/calibration.h" #include "input_common/helpers/joycon_protocol/generic_functions.h" #include "input_common/helpers/joycon_protocol/joycon_types.h" @@ -95,6 +96,7 @@ private: void ReadNfcIRMode(std::span buffer); // Protocol Features + std::unique_ptr calibration_protocol = nullptr; std::unique_ptr generic_protocol = nullptr; // Connection status -- cgit v1.2.3 From f09a023292e659af46d551b9b134d94d000a57c7 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Tue, 20 Dec 2022 20:27:34 -0600 Subject: input_common: Add support for joycon input reports --- src/input_common/helpers/joycon_driver.h | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index 275c97b91..48ba859f4 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -11,6 +11,8 @@ #include "input_common/helpers/joycon_protocol/calibration.h" #include "input_common/helpers/joycon_protocol/generic_functions.h" #include "input_common/helpers/joycon_protocol/joycon_types.h" +#include "input_common/helpers/joycon_protocol/poller.h" +#include "input_common/helpers/joycon_protocol/rumble.h" namespace InputCommon::Joycon { @@ -42,6 +44,8 @@ public: DriverResult SetNfcMode(); DriverResult SetRingConMode(); + void SetCallbacks(const Joycon::JoyconCallbacks& callbacks); + // Returns device type from hidapi handle static Joycon::DriverResult GetDeviceType(SDL_hid_device_info* device_info, Joycon::ControllerType& controller_type); @@ -50,14 +54,6 @@ public: static Joycon::DriverResult GetSerialNumber(SDL_hid_device_info* device_info, Joycon::SerialNumber& serial_number); - std::function on_battery_data; - std::function on_color_data; - std::function on_button_data; - std::function on_stick_data; - std::function on_motion_data; - std::function on_ring_data; - std::function&)> on_amiibo_data; - private: struct SupportedFeatures { bool passive{}; @@ -86,18 +82,11 @@ private: /// Returns a list of supported features that can be enabled on this device SupportedFeatures GetSupportedFeatures(); - /// Handles data from passive packages - void ReadPassiveMode(std::span buffer); - - /// Handles data from active packages - void ReadActiveMode(std::span buffer); - - /// Handles data from nfc or ir packages - void ReadNfcIRMode(std::span buffer); - // Protocol Features std::unique_ptr calibration_protocol = nullptr; std::unique_ptr generic_protocol = nullptr; + std::unique_ptr joycon_poller = nullptr; + std::unique_ptr rumble_protocol = nullptr; // Connection status bool is_connected{}; -- cgit v1.2.3 From 751d36e7392b0b1637f17988cfc1ef0d7cd95753 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Tue, 20 Dec 2022 19:10:42 -0600 Subject: input_common: Add support for joycon ring controller --- src/input_common/helpers/joycon_driver.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index 48ba859f4..dc5d60221 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -12,6 +12,7 @@ #include "input_common/helpers/joycon_protocol/generic_functions.h" #include "input_common/helpers/joycon_protocol/joycon_types.h" #include "input_common/helpers/joycon_protocol/poller.h" +#include "input_common/helpers/joycon_protocol/ringcon.h" #include "input_common/helpers/joycon_protocol/rumble.h" namespace InputCommon::Joycon { @@ -86,6 +87,7 @@ private: std::unique_ptr calibration_protocol = nullptr; std::unique_ptr generic_protocol = nullptr; std::unique_ptr joycon_poller = nullptr; + std::unique_ptr ring_protocol = nullptr; std::unique_ptr rumble_protocol = nullptr; // Connection status @@ -118,6 +120,7 @@ private: JoyStickCalibration left_stick_calibration{}; JoyStickCalibration right_stick_calibration{}; MotionCalibration motion_calibration{}; + RingCalibration ring_calibration{}; // Fixed joycon info FirmwareVersion version{}; -- cgit v1.2.3 From 6d6b7bdbc327528d155f0422ef096846559844c0 Mon Sep 17 00:00:00 2001 From: german77 Date: Thu, 22 Dec 2022 01:07:46 -0600 Subject: input_common: Implement joycon nfc --- src/input_common/helpers/joycon_driver.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index dc5d60221..c9118ee93 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -8,14 +8,15 @@ #include #include -#include "input_common/helpers/joycon_protocol/calibration.h" -#include "input_common/helpers/joycon_protocol/generic_functions.h" #include "input_common/helpers/joycon_protocol/joycon_types.h" -#include "input_common/helpers/joycon_protocol/poller.h" -#include "input_common/helpers/joycon_protocol/ringcon.h" -#include "input_common/helpers/joycon_protocol/rumble.h" namespace InputCommon::Joycon { +class CalibrationProtocol; +class GenericProtocol; +class NfcProtocol; +class JoyconPoller; +class RingConProtocol; +class RumbleProtocol; class JoyconDriver final { public: @@ -84,17 +85,18 @@ private: SupportedFeatures GetSupportedFeatures(); // Protocol Features - std::unique_ptr calibration_protocol = nullptr; - std::unique_ptr generic_protocol = nullptr; - std::unique_ptr joycon_poller = nullptr; - std::unique_ptr ring_protocol = nullptr; - std::unique_ptr rumble_protocol = nullptr; + std::unique_ptr calibration_protocol; + std::unique_ptr generic_protocol; + std::unique_ptr nfc_protocol; + std::unique_ptr joycon_poller; + std::unique_ptr ring_protocol; + std::unique_ptr rumble_protocol; // Connection status bool is_connected{}; u64 delta_time; std::size_t error_counter{}; - std::shared_ptr hidapi_handle = nullptr; + std::shared_ptr hidapi_handle; std::chrono::time_point last_update; // External device status -- cgit v1.2.3 From 527dad70976a158e94defc51707347e064a31099 Mon Sep 17 00:00:00 2001 From: german77 Date: Mon, 26 Dec 2022 11:11:01 -0600 Subject: input_common: Use DriverResult on all engines --- src/input_common/helpers/joycon_driver.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index c9118ee93..bf38a3009 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -46,15 +46,15 @@ public: DriverResult SetNfcMode(); DriverResult SetRingConMode(); - void SetCallbacks(const Joycon::JoyconCallbacks& callbacks); + void SetCallbacks(const JoyconCallbacks& callbacks); // Returns device type from hidapi handle - static Joycon::DriverResult GetDeviceType(SDL_hid_device_info* device_info, - Joycon::ControllerType& controller_type); + static DriverResult GetDeviceType(SDL_hid_device_info* device_info, + ControllerType& controller_type); // Returns serial number from hidapi handle - static Joycon::DriverResult GetSerialNumber(SDL_hid_device_info* device_info, - Joycon::SerialNumber& serial_number); + static DriverResult GetSerialNumber(SDL_hid_device_info* device_info, + SerialNumber& serial_number); private: struct SupportedFeatures { -- cgit v1.2.3 From 5cb437703fa441a08db295f8a916caedc3a581f2 Mon Sep 17 00:00:00 2001 From: german77 Date: Mon, 26 Dec 2022 12:49:49 -0600 Subject: yuzu: Add ring controller test button --- src/input_common/helpers/joycon_driver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index bf38a3009..5ff15c784 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -73,7 +73,7 @@ private: void OnNewData(std::span buffer); /// Updates device configuration to enable or disable features - void SetPollingMode(); + DriverResult SetPollingMode(); /// Returns true if input thread is valid and doesn't need to be stopped bool IsInputThreadValid() const; -- cgit v1.2.3 From 459fb2b21337bae60194a2a99ce68c87aaed522d Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Wed, 28 Dec 2022 15:21:12 -0600 Subject: input_common: Implement joycon ir camera --- src/input_common/helpers/joycon_driver.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index 5ff15c784..61ecf4a6c 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -13,6 +13,7 @@ namespace InputCommon::Joycon { class CalibrationProtocol; class GenericProtocol; +class IrsProtocol; class NfcProtocol; class JoyconPoller; class RingConProtocol; @@ -41,8 +42,10 @@ public: DriverResult SetVibration(const VibrationValue& vibration); DriverResult SetLedConfig(u8 led_pattern); + DriverResult SetIrsConfig(IrsMode mode_, IrsResolution format_); DriverResult SetPasiveMode(); DriverResult SetActiveMode(); + DriverResult SetIrMode(); DriverResult SetNfcMode(); DriverResult SetRingConMode(); @@ -87,6 +90,7 @@ private: // Protocol Features std::unique_ptr calibration_protocol; std::unique_ptr generic_protocol; + std::unique_ptr irs_protocol; std::unique_ptr nfc_protocol; std::unique_ptr joycon_poller; std::unique_ptr ring_protocol; -- cgit v1.2.3 From d05ea2f3ebdf62e328d2edbfc5b9bc01e3453569 Mon Sep 17 00:00:00 2001 From: german77 Date: Mon, 2 Jan 2023 22:11:03 -0600 Subject: input_common: Fix issue where ring and irs are enabled at the same time --- src/input_common/helpers/joycon_driver.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index 61ecf4a6c..e8e65e133 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -107,6 +107,7 @@ private: bool starlink_connected{}; bool ring_connected{}; bool amiibo_detected{}; + bool is_ring_disabled_by_irs{}; // Harware configuration u8 leds{}; -- cgit v1.2.3 From 340f15d1fa79594dbe12a6e19140ba012751b533 Mon Sep 17 00:00:00 2001 From: german77 Date: Fri, 13 Jan 2023 23:29:05 -0600 Subject: input_common: Address byte review --- src/input_common/helpers/joycon_driver.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/input_common/helpers/joycon_driver.h') diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h index e8e65e133..c1e189fa5 100644 --- a/src/input_common/helpers/joycon_driver.h +++ b/src/input_common/helpers/joycon_driver.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include #include @@ -97,7 +98,7 @@ private: std::unique_ptr rumble_protocol; // Connection status - bool is_connected{}; + std::atomic is_connected{}; u64 delta_time; std::size_t error_counter{}; std::shared_ptr hidapi_handle; -- cgit v1.2.3