On Fri, Oct 14, 2022 at 04:17:21PM +0530, Viresh Kumar wrote: > Add examples for the usage of the rust bindings, quite similar to the > ones in cxx bindings. > > Signed-off-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx> > --- > bindings/rust/libgpiod/examples/gpiodetect.rs | 30 ++++++ > bindings/rust/libgpiod/examples/gpiofind.rs | 35 +++++++ > bindings/rust/libgpiod/examples/gpioget.rs | 42 ++++++++ > bindings/rust/libgpiod/examples/gpioinfo.rs | 95 +++++++++++++++++++ > bindings/rust/libgpiod/examples/gpiomon.rs | 73 ++++++++++++++ > bindings/rust/libgpiod/examples/gpioset.rs | 60 ++++++++++++ > bindings/rust/libgpiod/examples/gpiowatch.rs | 54 +++++++++++ > 7 files changed, 389 insertions(+) > create mode 100644 bindings/rust/libgpiod/examples/gpiodetect.rs > create mode 100644 bindings/rust/libgpiod/examples/gpiofind.rs > create mode 100644 bindings/rust/libgpiod/examples/gpioget.rs > create mode 100644 bindings/rust/libgpiod/examples/gpioinfo.rs > create mode 100644 bindings/rust/libgpiod/examples/gpiomon.rs > create mode 100644 bindings/rust/libgpiod/examples/gpioset.rs > create mode 100644 bindings/rust/libgpiod/examples/gpiowatch.rs > Add an example that exercises the event buffer, demonstrating that its is not possible to read new events while the previous set are still in use. I added something along those lines, based on gpiomon, in my mods branch mentioned in patch 3. If multi-threading is supported, perhaps some examples demonstrating that as well. Even if there are tests that already cover it - major features should have explicit examples, not require rooting around through the test suite. > diff --git a/bindings/rust/libgpiod/examples/gpiodetect.rs b/bindings/rust/libgpiod/examples/gpiodetect.rs > new file mode 100644 > index 000000000000..f24ac72e2d48 > --- /dev/null > +++ b/bindings/rust/libgpiod/examples/gpiodetect.rs > @@ -0,0 +1,30 @@ > +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause > +// > +// Copyright 2022 Linaro Ltd. All Rights Reserved. > +// Viresh Kumar <viresh.kumar@xxxxxxxxxx> > +// > +// Simplified Rust implementation of gpiodetect tool. > + > +use std::env; > +use std::path::Path; > + > +use libgpiod::{gpiochip_devices, Error, Result}; > + > +fn main() -> Result<()> { > + let args: Vec<String> = env::args().collect(); > + if args.len() > 1 { > + println!("Usage: {}", args[0]); > + return Err(Error::InvalidArguments); > + } > + > + for chip in gpiochip_devices(&Path::new("/dev"))? { > + println!( > + "{} [{}] ({})", > + chip.name()?, > + chip.label()?, > + chip.num_lines(), > + ); > + } > + > + Ok(()) > +} > diff --git a/bindings/rust/libgpiod/examples/gpiofind.rs b/bindings/rust/libgpiod/examples/gpiofind.rs > new file mode 100644 > index 000000000000..07e886bc3896 > --- /dev/null > +++ b/bindings/rust/libgpiod/examples/gpiofind.rs > @@ -0,0 +1,35 @@ > +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause > +// > +// Copyright 2022 Linaro Ltd. All Rights Reserved. > +// Viresh Kumar <viresh.kumar@xxxxxxxxxx> > +// > +// Simplified Rust implementation of gpiofind tool. > + > +use std::env; > +use std::path::Path; > + > +use libgpiod::{gpiochip_devices, Error, Result}; > + > +fn main() -> Result<()> { > + let args: Vec<String> = env::args().collect(); > + if args.len() != 2 { > + println!("Usage: {} <line-name>", args[0]); > + return Err(Error::InvalidArguments); > + } > + > + for chip in gpiochip_devices(&Path::new("/dev"))? { > + let offset = chip.line_offset_from_name(&args[1]); > + if offset.is_ok() { > + println!( > + "Line {} found: Chip: {}, offset: {}", > + args[1], > + chip.name()?, > + offset? > + ); > + return Ok(()); > + } > + } > + > + println!("Failed to find line: {}", args[1]); > + Ok(()) > +} > diff --git a/bindings/rust/libgpiod/examples/gpioget.rs b/bindings/rust/libgpiod/examples/gpioget.rs > new file mode 100644 > index 000000000000..f4c111987d96 > --- /dev/null > +++ b/bindings/rust/libgpiod/examples/gpioget.rs > @@ -0,0 +1,42 @@ > +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause > +// > +// Copyright 2022 Linaro Ltd. All Rights Reserved. > +// Viresh Kumar <viresh.kumar@xxxxxxxxxx> > +// > +// Simplified Rust implementation of gpioget tool. > + > +use std::env; > + > +use libgpiod::{chip::Chip, line, request, Direction, Error, Offset, Result, SettingVal}; > + > +fn main() -> Result<()> { > + let args: Vec<String> = env::args().collect(); > + if args.len() < 3 { > + println!("Usage: {} <chip> <line_offset0> ...", args[0]); > + return Err(Error::InvalidArguments); > + } > + > + let mut lsettings = line::Settings::new()?; > + let lconfig = line::Config::new()?; > + let mut offsets = Vec::<Offset>::new(); > + > + for arg in &args[2..] { > + let offset = arg.parse::<Offset>().map_err(|_| Error::InvalidArguments)?; > + offsets.push(offset); > + } > + > + lsettings.set_prop(&[SettingVal::Direction(Direction::Input)])?; This is an example of where lsettings.set_direction(Direction::Input)?; would be simpler and clearer. Cheers, Kent.