Skip to content

Commit

Permalink
Merge pull request #57 from KaiseiYokoyama/dev/0.5.2
Browse files Browse the repository at this point in the history
Dev/0.5.2
  • Loading branch information
KaiseiYokoyama authored May 30, 2020
2 parents 16dbf27 + b8c339c commit 04ffc89
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "joycon-rs"
version = "0.5.1"
version = "0.5.2"
authors = ["Kaisei Yokoyama <[email protected]>"]
repository = "https://github.com/KaiseiYokoyama/joycon-rs"
edition = "2018"
Expand All @@ -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"
Expand Down
46 changes: 30 additions & 16 deletions src/joycon/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
};
Expand Down Expand Up @@ -287,3 +291,13 @@ impl JoyConManager {
}
}

lazy_static! {
pub static ref JOYCON_RECEIVER: crossbeam_channel::Receiver<Arc<Mutex<JoyConDevice>>> = {
let manager = JoyConManager::get_instance();
let manager = match manager.lock() {
Ok(manager) => manager,
Err(e) => e.into_inner(),
};
manager.new_devices()
};
}
2 changes: 1 addition & 1 deletion src/joycon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 04ffc89

Please sign in to comment.