Skip to content

Commit

Permalink
Added free-running (non-triggered) buffered input example
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Oct 30, 2018
1 parent 55ce7e7 commit e8c094a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/iio_free_scan.rs
Original file line number Diff line number Diff line change
@@ -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:
// <LICENSE or http://opensource.org/licenses/MIT>
// 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<u16> = buf.channel_iter::<u16>(&chan).collect();
println!("{}: {:?}", chan.id().unwrap_or_default(), data);
}
}

9 changes: 9 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer> {
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 {
Expand Down

0 comments on commit e8c094a

Please sign in to comment.