From e0c5acefb78a846ec8f09120572a18737e171afd Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Fri, 24 Jul 2020 20:50:59 +0200 Subject: [PATCH] add test device --- src/main.rs | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index eadf233..e4331af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,6 +138,12 @@ impl Drop for Handle { } impl Handle { + fn from_name(name: &str) -> Result { + let name = std::ffi::CString::new(name).unwrap(); + let mut handle = std::ptr::null_mut(); + unsafe { checked(|| sane_open(name.as_ptr(), &mut handle))? }; + Ok(Self(handle)) + } fn descriptors(&self) -> impl ExactSizeIterator + '_ { // Guaranteed to exist let first_desc = self.get_descriptor(0).unwrap(); @@ -346,7 +352,7 @@ impl<'a> Acquisition<'a> { let mut image = Vec::::with_capacity(bytesize as _); let mut len = 0; - unsafe { checked(|| sane_set_io_mode(self.handle.0, SANE_FALSE as _))? }; + // unsafe { checked(|| sane_set_io_mode(self.handle.0, SANE_FALSE as _))? }; unsafe { 'read_loop: loop { @@ -383,6 +389,8 @@ impl Drop for Acquisition<'_> { } } +const TESTDEVICE: bool = true; + fn main() { let (context, version) = Context::init().unwrap(); println!( @@ -391,19 +399,23 @@ fn main() { version.minor(), version.build() ); - let mut chosen_device = None; - for device in context.devices(true).unwrap() { - println!("Device:"); - let name = device.name(); - println!("\tname: {}", name); - println!("\tvendor: {}", device.vendor()); - println!("\tmodel: {}", device.model()); - println!("\ttype: {}", device.type_()); - chosen_device = Some(device); - } + let handle = if TESTDEVICE { + Handle::from_name("test").unwrap() + } else { + let mut chosen_device = None; + for device in context.devices(true).unwrap() { + println!("Device:"); + let name = device.name(); + println!("\tname: {}", name); + println!("\tvendor: {}", device.vendor()); + println!("\tmodel: {}", device.model()); + println!("\ttype: {}", device.type_()); + chosen_device = Some(device); + } - let device = chosen_device.unwrap(); - let handle = device.open().unwrap(); + let device = chosen_device.unwrap(); + device.open().unwrap() + }; println!("Options:"); for option in handle.options() { @@ -412,7 +424,9 @@ fn main() { continue; } println!("\t{}", optname); - println!("\t\t{}", option.desc()); + for line in option.desc().lines() { + println!("\t\t{}", line); + } match optname { "mode" => { print!("\t\t"); @@ -426,6 +440,9 @@ fn main() { "depth" => { println!("\t\tCurrent depth: {}", option.get_int().unwrap()); } + "test-picture" => { + option.set_string("Color pattern"); + } _ => {} } } @@ -433,6 +450,6 @@ fn main() { let parameters = handle.parameters().unwrap(); println!("{:?}", parameters); - // let acq = handle.start().unwrap(); - // let image = acq.get_image().unwrap(); + let acq = handle.start().unwrap(); + let image = acq.get_image().unwrap(); }