From 1d188c335df7c10e0bf8a03c1bbf9a57b533a8f1 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Mon, 29 Jan 2024 19:44:10 -0600 Subject: [PATCH] Finished main async porting --- minidisc-rs/src/netmd/base.rs | 37 ++++++++++++------------------ minidisc-rs/src/netmd/interface.rs | 2 +- src/main.rs | 5 +--- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/minidisc-rs/src/netmd/base.rs b/minidisc-rs/src/netmd/base.rs index 9c85925..36bd570 100644 --- a/minidisc-rs/src/netmd/base.rs +++ b/minidisc-rs/src/netmd/base.rs @@ -4,11 +4,10 @@ use std::error::Error; use std::time::Duration; // USB stuff -use nusb::transfer::{Control, ControlType, Recipient, RequestBuffer, ControlIn}; +use nusb::transfer::{Control, ControlType, Recipient, RequestBuffer, ControlIn, ControlOut}; use nusb::{Device, DeviceInfo, Interface}; const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0); - const BULK_WRITE_ENDPOINT: u8 = 0x02; const BULK_READ_ENDPOINT: u8 = 0x81; @@ -199,17 +198,16 @@ impl NetMD { true => 0xff, }; - match self.usb_interface.control_out_blocking( - Control { + match self.usb_interface.control_out( + ControlOut { control_type: ControlType::Vendor, recipient: Recipient::Interface, request, value: 0, index: 0, - }, - &command, - DEFAULT_TIMEOUT, - ) { + data: &command, + } + ).await.into_result() { Ok(_) => Ok(()), Err(error) => Err(error.into()), } @@ -226,8 +224,7 @@ impl NetMD { self._read_reply(true, override_length).await } - /// Poll to see if a message is ready, - /// and if so, recieve it + /// Poll to see if a message is ready, and once it is, retrieve it async fn _read_reply( &mut self, use_factory_command: bool, @@ -237,6 +234,7 @@ impl NetMD { let mut current_attempt = 0; while length == 0 { + // Back off while trying again let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64 * (u64::pow(2, current_attempt as u32 / 10) - 1); @@ -255,23 +253,18 @@ impl NetMD { }; // Create a buffer to fill with the result - let mut buf: Vec = vec![0; length as usize]; - - // Create a buffer to fill with the result - match self.usb_interface.control_in_blocking( - Control { + let reply = self.usb_interface.control_in( + ControlIn { control_type: ControlType::Vendor, recipient: Recipient::Interface, request, value: 0, index: 0, - }, - &mut buf, - DEFAULT_TIMEOUT, - ) { - Ok(_) => Ok(buf), - Err(error) => Err(error.into()), - } + length, + } + ).await.into_result()?; + + Ok(reply) } // Default chunksize should be 0x10000 diff --git a/minidisc-rs/src/netmd/interface.rs b/minidisc-rs/src/netmd/interface.rs index 6d46b53..5546ea1 100644 --- a/minidisc-rs/src/netmd/interface.rs +++ b/minidisc-rs/src/netmd/interface.rs @@ -955,7 +955,7 @@ impl NetMDInterface { Ok(track_titles) } - /// Gets the title of a track at an index + /// Gets the title of a single track at an index pub async fn track_title(&mut self, track: u16, wchar: bool) -> Result> { let title = match self.track_titles([track].into(), wchar).await { Ok(titles) => titles[0].clone(), diff --git a/src/main.rs b/src/main.rs index 3ac00cb..e3b196b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,7 @@ async fn main() { // Ensure the player is a minidisc player and not some other random device let mut player_controller = match interface::NetMDInterface::new(&device).await { Ok(player) => player, - Err(err) => { - dbg!(err); - continue; - } + Err(err) => continue, }; println!(