diff --git a/Cargo.toml b/Cargo.toml index dda97e2..c1d86bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "joycon-rs" -version = "0.4.1" +version = "0.4.2" authors = ["Kaisei Yokoyama "] repository = "https://github.com/KaiseiYokoyama/joycon-rs" edition = "2018" diff --git a/src/joycon/device.rs b/src/joycon/device.rs index a94416f..e77f24c 100644 --- a/src/joycon/device.rs +++ b/src/joycon/device.rs @@ -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() } @@ -80,7 +93,28 @@ impl JoyConDevice { pub fn read(&self, buf: &mut [u8]) -> JoyConResult { 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 { + 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) } diff --git a/src/joycon/driver.rs b/src/joycon/driver.rs index 579073b..a87aff8 100644 --- a/src/joycon/driver.rs +++ b/src/joycon/driver.rs @@ -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; @@ -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 { let command = command as u8; let sub_command = sub_command as u8; diff --git a/src/lib.rs b/src/lib.rs index 487c926..e185b5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,6 +198,7 @@ pub mod result { pub enum JoyConReportError { InvalidSimpleHidReport(InvalidSimpleHIDReport), InvalidStandardInputReport(InvalidStandardInputReport), + EmptyReport, } impl From for JoyConError {