diff --git a/examples/iio_free_scan.rs b/examples/iio_free_scan.rs new file mode 100644 index 0000000..5d0bddc --- /dev/null +++ b/examples/iio_free_scan.rs @@ -0,0 +1,53 @@ +// industrial-io/examples/iio_free_scan.rs +// +// Simple Rust IIO example. +// This does buffered reading without using a trigger (free scan). +// +// Copyright (c) 2018, Frank Pagliughi +// +// Licensed under the MIT license: +// +// This file may not be copied, modified, or distributed except according +// to those terms. +// + +extern crate industrial_io as iio; +use std::process; + +const DFLT_DEV_NAME: &'static str = "44e0d000.tscadc:adc"; + + +fn main() { + let dev_name = DFLT_DEV_NAME; + + let ctx = iio::Context::new().unwrap_or_else(|_err| { + println!("Couldn't open default IIO context"); + process::exit(1); + }); + + let dev = ctx.find_device(dev_name).unwrap_or_else(|| { + println!("No IIO device named '{}'", dev_name); + process::exit(2); + }); + + for mut chan in dev.channels() { + chan.enable(); + } + + let mut buf = dev.create_buffer(8, false).unwrap_or_else(|err| { + eprintln!("Unable to create buffer: {}", err); + process::exit(3); + }); + + println!("Capturing a buffer..."); + if let Err(err) = buf.refill() { + eprintln!("Error filling the buffer: {}", err); + process::exit(4); + } + + for mut chan in dev.channels() { + let data: Vec = buf.channel_iter::(&chan).collect(); + println!("{}: {:?}", chan.id().unwrap_or_default(), data); + } +} + diff --git a/src/device.rs b/src/device.rs index 9d5d48d..958ceb3 100644 --- a/src/device.rs +++ b/src/device.rs @@ -192,6 +192,15 @@ impl Device { } } + /// Creates a buffer for the device. + /// + /// `sample_count` The number of samples the buffer should hold + /// `cyclic` Whether to enable cyclic mode. + pub fn create_buffer(&self, sample_count: usize, cyclic: bool) -> Result { + let buf = unsafe { ffi::iio_device_create_buffer(self.dev, sample_count, cyclic) }; + if buf.is_null() { bail!(SysError(Errno::last())); } + Ok(Buffer { buf, }) + } } impl PartialEq for Device {