Finished main async porting

This commit is contained in:
G2-Games 2024-01-29 19:44:10 -06:00
parent 0e666b1207
commit 1d188c335d
3 changed files with 17 additions and 27 deletions

View file

@ -4,11 +4,10 @@ use std::error::Error;
use std::time::Duration; use std::time::Duration;
// USB stuff // USB stuff
use nusb::transfer::{Control, ControlType, Recipient, RequestBuffer, ControlIn}; use nusb::transfer::{Control, ControlType, Recipient, RequestBuffer, ControlIn, ControlOut};
use nusb::{Device, DeviceInfo, Interface}; use nusb::{Device, DeviceInfo, Interface};
const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0); const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0);
const BULK_WRITE_ENDPOINT: u8 = 0x02; const BULK_WRITE_ENDPOINT: u8 = 0x02;
const BULK_READ_ENDPOINT: u8 = 0x81; const BULK_READ_ENDPOINT: u8 = 0x81;
@ -199,17 +198,16 @@ impl NetMD {
true => 0xff, true => 0xff,
}; };
match self.usb_interface.control_out_blocking( match self.usb_interface.control_out(
Control { ControlOut {
control_type: ControlType::Vendor, control_type: ControlType::Vendor,
recipient: Recipient::Interface, recipient: Recipient::Interface,
request, request,
value: 0, value: 0,
index: 0, index: 0,
}, data: &command,
&command, }
DEFAULT_TIMEOUT, ).await.into_result() {
) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(error) => Err(error.into()), Err(error) => Err(error.into()),
} }
@ -226,8 +224,7 @@ impl NetMD {
self._read_reply(true, override_length).await self._read_reply(true, override_length).await
} }
/// Poll to see if a message is ready, /// Poll to see if a message is ready, and once it is, retrieve it
/// and if so, recieve it
async fn _read_reply( async fn _read_reply(
&mut self, &mut self,
use_factory_command: bool, use_factory_command: bool,
@ -237,6 +234,7 @@ impl NetMD {
let mut current_attempt = 0; let mut current_attempt = 0;
while length == 0 { while length == 0 {
// Back off while trying again
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64 let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64
* (u64::pow(2, current_attempt as u32 / 10) - 1); * (u64::pow(2, current_attempt as u32 / 10) - 1);
@ -255,23 +253,18 @@ impl NetMD {
}; };
// Create a buffer to fill with the result // Create a buffer to fill with the result
let mut buf: Vec<u8> = vec![0; length as usize]; let reply = self.usb_interface.control_in(
ControlIn {
// Create a buffer to fill with the result
match self.usb_interface.control_in_blocking(
Control {
control_type: ControlType::Vendor, control_type: ControlType::Vendor,
recipient: Recipient::Interface, recipient: Recipient::Interface,
request, request,
value: 0, value: 0,
index: 0, index: 0,
}, length,
&mut buf,
DEFAULT_TIMEOUT,
) {
Ok(_) => Ok(buf),
Err(error) => Err(error.into()),
} }
).await.into_result()?;
Ok(reply)
} }
// Default chunksize should be 0x10000 // Default chunksize should be 0x10000

View file

@ -955,7 +955,7 @@ impl NetMDInterface {
Ok(track_titles) 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<String, Box<dyn Error>> { pub async fn track_title(&mut self, track: u16, wchar: bool) -> Result<String, Box<dyn Error>> {
let title = match self.track_titles([track].into(), wchar).await { let title = match self.track_titles([track].into(), wchar).await {
Ok(titles) => titles[0].clone(), Ok(titles) => titles[0].clone(),

View file

@ -9,10 +9,7 @@ async fn main() {
// Ensure the player is a minidisc player and not some other random device // Ensure the player is a minidisc player and not some other random device
let mut player_controller = match interface::NetMDInterface::new(&device).await { let mut player_controller = match interface::NetMDInterface::new(&device).await {
Ok(player) => player, Ok(player) => player,
Err(err) => { Err(err) => continue,
dbg!(err);
continue;
}
}; };
println!( println!(