From ceac834de13f199fdcca042603c080d21856271a Mon Sep 17 00:00:00 2001 From: Colin Edwards Date: Sun, 29 Oct 2023 00:13:26 -0500 Subject: [PATCH] Working on some capture refactoring --- cappy3ds/src/capture/katsukitty/mod.rs | 126 +++++++++++-------------- cappy3ds/src/capture/loopy/mod.rs | 13 +++ cappy3ds/src/capture/mod.rs | 35 +++++++ cappy3ds/src/main.rs | 6 ++ 4 files changed, 109 insertions(+), 71 deletions(-) create mode 100644 cappy3ds/src/main.rs diff --git a/cappy3ds/src/capture/katsukitty/mod.rs b/cappy3ds/src/capture/katsukitty/mod.rs index 92e514e..9f221b0 100644 --- a/cappy3ds/src/capture/katsukitty/mod.rs +++ b/cappy3ds/src/capture/katsukitty/mod.rs @@ -9,8 +9,12 @@ extern crate libusb1_sys as usbffi; use bytes::BytesMut; use simple_error::SimpleError; + + use rusb::{Device, DeviceDescriptor, DeviceHandle, UsbContext}; +use super::Capture; + mod fpga; mod fx2; mod image; @@ -18,87 +22,67 @@ mod parse; #[derive(RustEmbed)] #[folder = "resources/Katsukity/"] -struct Katsukity; +struct KatsukityResources; -pub fn connect(context: &mut T) -> Result, SimpleError> { - let firmware = Katsukity::get("firm.bin").unwrap(); - let bitstream = Katsukity::get("bitstream.bin").unwrap(); +struct Katsukity { - let vid = 0x0752; - let pid = 0x8613; +} - let mut flashed_fx2 = false; - match open_device(context, vid, pid) { - Some((mut device, device_desc, mut handle)) => { - println!("Opened {:04x}:{:04x}", vid, pid); - fx2::send_firmware(&mut handle, firmware.data.to_vec()); - flashed_fx2 = true; +impl Capture for Katsukity { + fn connect(context: &mut T) -> Result, SimpleError> { + let firmware = KatsukityResources::get("firm.bin").unwrap(); + let bitstream = KatsukityResources::get("bitstream.bin").unwrap(); + + let vid = 0x0752; + let pid = 0x8613; + + let mut flashed_fx2 = false; + match Self::open_device(context, vid, pid) { + Some((mut device, device_desc, mut handle)) => { + println!("Opened {:04x}:{:04x}", vid, pid); + fx2::send_firmware(&mut handle, firmware.data.to_vec()); + flashed_fx2 = true; + } + None => { + println!("could not find FX2 device"); + } } - None => { - println!("could not find FX2 device"); + + if flashed_fx2 { + println!("Waiting for second interface"); + let sleep_time = time::Duration::from_millis(5000); + thread::sleep(sleep_time); + // todo: loop with device check instead of sleeping } - } - - if flashed_fx2 { - println!("Waiting for second interface"); - let sleep_time = time::Duration::from_millis(5000); - thread::sleep(sleep_time); - // todo: loop with device check instead of sleeping - } - - match open_device(context, 0x0752, 0xf2c0) { - Some((mut device, device_desc, mut handle)) => { - println!("Opened secondary device"); - match handle.claim_interface(0) { - Ok(_) => {} - Err(err) => panic!("could not claim second device: {}", err), + + match Self::open_device(context, 0x0752, 0xf2c0) { + Some((mut device, device_desc, mut handle)) => { + println!("Opened secondary device"); + match handle.claim_interface(0) { + Ok(_) => {} + Err(err) => panic!("could not claim second device: {}", err), + } + + // bleh apparently relesase runs fast enough to break this + // add in some sleeps + //if fpga::check_fpga_programmed(&mut handle) { + //} else { + fpga::read_eeprom(&mut handle); + fpga::configure_fpga(&mut handle, bitstream.data.to_vec()); + fpga::configure_port(&mut handle); + //} + + fpga::fifo_start(&mut handle); + + Ok(handle) } - - // bleh apparently relesase runs fast enough to break this - // add in some sleeps - //if fpga::check_fpga_programmed(&mut handle) { - //} else { - fpga::read_eeprom(&mut handle); - fpga::configure_fpga(&mut handle, bitstream.data.to_vec()); - fpga::configure_port(&mut handle); - //} - - fpga::fifo_start(&mut handle); - - Ok(handle) + None => Err(SimpleError::new( + "secondary device missing, firmware upload failed?", + )), } - None => Err(SimpleError::new( - "secondary device missing, firmware upload failed?", - )), } } -fn open_device( - context: &mut T, - vid: u16, - pid: u16, -) -> Option<(Device, DeviceDescriptor, DeviceHandle)> { - let devices = match context.devices() { - Ok(d) => d, - Err(_) => return None, - }; - - for device in devices.iter() { - let device_desc = match device.device_descriptor() { - Ok(d) => d, - Err(_) => continue, - }; - - if device_desc.vendor_id() == vid && device_desc.product_id() == pid { - match device.open() { - Ok(handle) => return Some((device, device_desc, handle)), - Err(e) => panic!("Device found but failed to open: {}", e), - } - } - } - - None -} pub fn do_capture(handle: &mut DeviceHandle, data_callback: F) where diff --git a/cappy3ds/src/capture/loopy/mod.rs b/cappy3ds/src/capture/loopy/mod.rs index e69de29..c75d7bf 100644 --- a/cappy3ds/src/capture/loopy/mod.rs +++ b/cappy3ds/src/capture/loopy/mod.rs @@ -0,0 +1,13 @@ + + +use rusb::Context; + + +pub fn connect(context: &mut T) -> Result, SimpleError> { + + //let vid = 0x0403; + //let pid = 0x601e; + + // "N3DSXL.2" + +} \ No newline at end of file diff --git a/cappy3ds/src/capture/mod.rs b/cappy3ds/src/capture/mod.rs index bb55eb9..cb46ff7 100644 --- a/cappy3ds/src/capture/mod.rs +++ b/cappy3ds/src/capture/mod.rs @@ -1 +1,36 @@ pub mod katsukitty; + +use simple_error::SimpleError; +use rusb::{Device, DeviceDescriptor, DeviceHandle, UsbContext}; + +trait Capture { + fn connect(context: &mut T) -> Result, SimpleError>; + + fn open_device( + context: &mut T, + vid: u16, + pid: u16, + ) -> Option<(Device, DeviceDescriptor, DeviceHandle)> { + let devices = match context.devices() { + Ok(d) => d, + Err(_) => return None, + }; + + for device in devices.iter() { + let device_desc = match device.device_descriptor() { + Ok(d) => d, + Err(_) => continue, + }; + + if device_desc.vendor_id() == vid && device_desc.product_id() == pid { + match device.open() { + Ok(handle) => return Some((device, device_desc, handle)), + Err(e) => panic!("Device found but failed to open: {}", e), + } + } + } + + None + } +} + diff --git a/cappy3ds/src/main.rs b/cappy3ds/src/main.rs new file mode 100644 index 0000000..6f8e6a3 --- /dev/null +++ b/cappy3ds/src/main.rs @@ -0,0 +1,6 @@ +mod capture; + +fn main() { + //capture::katsukitty::do_capture(); + capture::loopy::do_capture(); +}