Skip to content

Commit

Permalink
Merge pull request #46 from KaiseiYokoyama/dev/0.4.2
Browse files Browse the repository at this point in the history
Dev/0.4.2
  • Loading branch information
KaiseiYokoyama authored May 5, 2020
2 parents 798c079 + e2c0b30 commit 4b8a05f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "joycon-rs"
version = "0.4.1"
version = "0.4.2"
authors = ["Kaisei Yokoyama <[email protected]>"]
repository = "https://github.com/KaiseiYokoyama/joycon-rs"
edition = "2018"
Expand Down
36 changes: 35 additions & 1 deletion src/joycon/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ impl JoyConDevice {
&self.serial_number
}

/// Set blocking mode.
///
/// # Notice
/// If you are using non-blocking mode,
/// it is more likely to fail to validate the sub command reply.
pub fn set_blocking_mode(&self, blocking: bool) -> JoyConResult<()> {
if let Some(hid_device) = &self.hid_device {
Ok(hid_device.set_blocking_mode(blocking)?)
} else {
Err(JoyConError::Disconnected)
}
}

pub fn device_type(&self) -> JoyConDeviceType {
self.device_type.clone()
}
Expand Down Expand Up @@ -80,7 +93,28 @@ impl JoyConDevice {

pub fn read(&self, buf: &mut [u8]) -> JoyConResult<usize> {
if let Some(hid_device) = &self.hid_device {
Ok(hid_device.read(buf)?)
let res = hid_device.read(buf)?;

if buf.iter().all(|e| e == &0) {
Err(JoyConReportError::EmptyReport.into())
} else {
Ok(res)
}
} else {
Err(JoyConError::Disconnected)
}
}

/// * timeout - milli seconds
pub fn read_timeout(&self, buf: &mut [u8], timeout: i32) -> JoyConResult<usize> {
if let Some(hid_device) = &self.hid_device {
let res = hid_device.read_timeout(buf, timeout)?;

if buf.iter().all(|e| e == &0) {
Err(JoyConReportError::EmptyReport.into())
} else {
Ok(res)
}
} else {
Err(JoyConError::Disconnected)
}
Expand Down
8 changes: 8 additions & 0 deletions src/joycon/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ pub trait JoyConDriver {

/// Send sub-command, and data (sub-command's arguments) with u8 integers
/// This returns ACK packet for the command or Error.
///
/// # Notice
/// If you are using non-blocking mode,
/// it is more likely to fail to validate the sub command reply.
fn send_sub_command_raw(&mut self, sub_command: u8, data: &[u8]) -> JoyConResult<[u8; 362]> {
use input_report_mode::sub_command_mode::AckByte;

Expand All @@ -499,6 +503,10 @@ pub trait JoyConDriver {

/// Send command, sub-command, and data (sub-command's arguments) with `Command` and `SubCommand`
/// This returns ACK packet for the command or Error.
///
/// # Notice
/// If you are using non-blocking mode,
/// it is more likely to fail to validate the sub command reply.
fn send_command(&mut self, command: Command, sub_command: SubCommand, data: &[u8]) -> JoyConResult<usize> {
let command = command as u8;
let sub_command = sub_command as u8;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub mod result {
pub enum JoyConReportError {
InvalidSimpleHidReport(InvalidSimpleHIDReport),
InvalidStandardInputReport(InvalidStandardInputReport),
EmptyReport,
}

impl From<JoyConReportError> for JoyConError {
Expand Down

0 comments on commit 4b8a05f

Please sign in to comment.