From 012abca3ff66996c4ad6d03898ad0b85c0f7c29a Mon Sep 17 00:00:00 2001 From: Kaisei Yokoyama Date: Sun, 31 May 2020 00:29:18 +0900 Subject: [PATCH 1/3] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ca94797..ccd4a23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "joycon-rs" -version = "0.5.1" +version = "0.5.2" authors = ["Kaisei Yokoyama "] repository = "https://github.com/KaiseiYokoyama/joycon-rs" edition = "2018" From 680e82b850b5de06d97535d07707ff21a47bd509 Mon Sep 17 00:00:00 2001 From: Kaisei Yokoyama Date: Sun, 31 May 2020 00:29:39 +0900 Subject: [PATCH 2/3] For windows --- src/joycon/manager.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/joycon/manager.rs b/src/joycon/manager.rs index 5ae8422..98a2db6 100644 --- a/src/joycon/manager.rs +++ b/src/joycon/manager.rs @@ -82,26 +82,30 @@ impl JoyConManager { std::thread::spawn(move || { while let Some(manager) = manager.upgrade() { - // Get manager - let mut manager = match manager.lock() { - Ok(m) => m, - Err(m) => m.into_inner(), - }; + let interval = { + // Get manager + let mut manager = match manager.lock() { + Ok(m) => m, + Err(m) => m.into_inner(), + }; - // Send new devices - if let Ok(new_devices) = manager.scan() { - // If mpsc channel is disconnected, end this thread. - let send_result = new_devices.into_iter() - .try_for_each::<_, Result<(), crossbeam_channel::SendError<_>>>(|new_device| { - tx.send(new_device) - }); - if send_result.is_err() { - return; + // Send new devices + if let Ok(new_devices) = manager.scan() { + // If mpsc channel is disconnected, end this thread. + let send_result = new_devices.into_iter() + .try_for_each::<_, Result<(), crossbeam_channel::SendError<_>>>(|new_device| { + tx.send(new_device) + }); + if send_result.is_err() { + return; + } } - } + + manager.scan_interval.clone() + }; // Sleep - std::thread::sleep(manager.scan_interval) + std::thread::sleep(interval) } }) }; From b8c339ce1046797db58ce870521518a283c24edd Mon Sep 17 00:00:00 2001 From: Kaisei Yokoyama Date: Sun, 31 May 2020 01:20:24 +0900 Subject: [PATCH 3/3] Add static receiver --- Cargo.toml | 1 + src/joycon/manager.rs | 10 ++++++++++ src/joycon/mod.rs | 2 +- src/lib.rs | 2 ++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ccd4a23..5774a76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ exclude = ["/images/*"] [dependencies] hidapi-alt-for-hidapi-issue-127 = "1.2.1" crossbeam-channel = "0.4.2" +lazy_static = "1.4.0" [dev-dependencies] doc-comment = "0.3.3" diff --git a/src/joycon/manager.rs b/src/joycon/manager.rs index 98a2db6..cd76f14 100644 --- a/src/joycon/manager.rs +++ b/src/joycon/manager.rs @@ -291,3 +291,13 @@ impl JoyConManager { } } +lazy_static! { + pub static ref JOYCON_RECEIVER: crossbeam_channel::Receiver>> = { + let manager = JoyConManager::get_instance(); + let manager = match manager.lock() { + Ok(manager) => manager, + Err(e) => e.into_inner(), + }; + manager.new_devices() + }; +} diff --git a/src/joycon/mod.rs b/src/joycon/mod.rs index 79222ce..fad99eb 100644 --- a/src/joycon/mod.rs +++ b/src/joycon/mod.rs @@ -15,7 +15,7 @@ pub use driver::{ lights, device_info, }; -pub use manager::JoyConManager; +pub use manager::{JoyConManager, JOYCON_RECEIVER}; use std::sync::Arc; use std::fmt::{Debug, Formatter}; diff --git a/src/lib.rs b/src/lib.rs index 630d49b..3e8392e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,6 +145,8 @@ //! [Deal with LED (Player lights)]: joycon/lights/index.html //! [Vibration (Rumble)]:joycon/struct.Rumble.html extern crate hidapi_alt_for_hidapi_issue_127 as hidapi; +#[macro_use] +extern crate lazy_static; pub mod joycon;