From 5e226075aa76d1faa5988e9a313501540fb0c66d Mon Sep 17 00:00:00 2001 From: G2-Games Date: Mon, 18 Dec 2023 12:18:47 -0600 Subject: [PATCH] Updated cli example, cleaned up code --- Cargo.toml | 2 +- minidisc-rs/src/netmd/base.rs | 12 ++++---- src/main.rs | 55 +++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 15ce85d..0875fa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ license = "AGPL-3.0" [dependencies] hex = "0.4.3" translit = "0.5.0" -webusb = "0.5.0" +yusb = "0.1.2" [dependencies.minidisc-rs] path = "minidisc-rs" diff --git a/minidisc-rs/src/netmd/base.rs b/minidisc-rs/src/netmd/base.rs index cc38c70..872e8bd 100644 --- a/minidisc-rs/src/netmd/base.rs +++ b/minidisc-rs/src/netmd/base.rs @@ -13,8 +13,8 @@ const BULK_WRITE_ENDPOINT: u8 = 0x02; const BULK_READ_ENDPOINT: u8 = 0x81; pub static DEVICE_IDS: Lazy> = Lazy::new(|| { - nofmt::pls! {Box::new( - [ + nofmt::pls! { + Box::new([ DeviceId {vendor_id: 0x04dd, product_id: 0x7202, name: Some(String::from("Sharp IM-MT899H"))}, DeviceId {vendor_id: 0x04dd, product_id: 0x9013, name: Some(String::from("Sharp IM-DR400"))}, DeviceId {vendor_id: 0x04dd, product_id: 0x9014, name: Some(String::from("Sharp IM-DR80"))}, @@ -94,7 +94,7 @@ pub struct NetMD { impl NetMD { const READ_REPLY_RETRY_INTERVAL: u32 = 10; - /// Creates a new `NetMD` struct + /// Creates a new interface to a NetMD device pub fn new(device: Device) -> Result> { let descriptor = device.device_descriptor()?; @@ -125,17 +125,17 @@ impl NetMD { }) } - /// Gets the device name from the struct + /// Gets the device name, this is limited to the devices in the list pub fn device_name(&self) -> &Option { &self.model.name } - /// Gets the vendor id from the struct + /// Gets the vendor id pub fn vendor_id(&self) -> &u16 { &self.model.vendor_id } - /// Gets the product id from the struct + /// Gets the product id pub fn product_id(&self) -> &u16 { &self.model.product_id } diff --git a/src/main.rs b/src/main.rs index 300690a..fd1c02f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,49 +1,54 @@ -use std::thread::sleep; -use std::collections::BTreeSet; - use minidisc_rs::netmd::interface; -use webusb; +use yusb; fn main() { - let webusb_context = webusb::Context::init().unwrap(); + let webusb_context = yusb::Context::new().unwrap(); - for mut device in webusb_context.devices().unwrap() { - match device.open() { - Ok(device) => device, - Err(_) => break, + for device in webusb_context.devices().unwrap() { + let handle = match device.open() { + Ok(handle) => handle, + Err(_) => continue, }; - device.claim_interface(0).unwrap(); + let descriptor = device.device_descriptor().unwrap(); println!( - "Connected to {} {}; VID: {:04x}, PID: {:04x}", - device.manufacturer_name.clone().unwrap_or("".to_string()), - device.product_name.clone().unwrap_or("".to_string()), - device.vendor_id, - device.product_id + "Connected to VID: {:04x}, PID: {:04x}", + descriptor.vendor_id(), + descriptor.product_id(), ); // Ensure the player is a minidisc player and not some other random device let mut player_controller = match interface::NetMDInterface::new(device) { Ok(player) => player, - Err(_) => continue + Err(_) => continue, }; - println!("Player Model: {}", player_controller.net_md_device.device_name().clone().unwrap()); + println!( + "Player Model: {}", + player_controller + .net_md_device + .device_name() + .clone() + .unwrap() + ); let now = std::time::Instant::now(); - println!("Disc Title: {} | {}", - player_controller.disc_title(false).unwrap_or("".to_string()), - player_controller.disc_title(true).unwrap_or("".to_string()) + println!( + "Disc Title: {} | {}", + player_controller + .disc_title(false) + .unwrap_or("".to_string()), + player_controller.disc_title(true).unwrap_or("".to_string()) ); - let track_count = player_controller.track_count().unwrap(); - let track_titles = player_controller.track_titles((0..track_count).collect(), false).unwrap(); + let track_count = player_controller.track_count().unwrap(); + let track_titles = player_controller.track_titles((0..track_count).collect(), false).unwrap(); let track_titlesw = player_controller.track_titles((0..track_count).collect(), true).unwrap(); let track_lengths = player_controller.track_lengths((0..track_count).collect()).unwrap(); for (i, track) in track_titles.iter().enumerate() { - println!("Track {i} Info:\n Title: {track} | {}\n Length: {:?}", - track_titlesw[i], - track_lengths[i] + println!( + "Track {i} Info:\n Title: {track} | {}\n Length: {:?}", + track_titlesw[i], track_lengths[i] ); } println!("{:?}", now.elapsed());