From: Kent Gibson <warthog618@gmail.com>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: "Linus Walleij" <linus.walleij@linaro.org>,
"Bartosz Golaszewski" <brgl@bgdev.pl>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
linux-gpio@vger.kernel.org,
"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@google.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
stratos-dev@op-lists.linaro.org,
"Gerard Ryan" <g.m0n3y.2503@gmail.com>
Subject: Re: [PATCH V7 4/8] libgpiod: Add rust examples
Date: Mon, 17 Oct 2022 21:00:00 +0800 [thread overview]
Message-ID: <Y01R0A0wrVR2Osyv@sol> (raw)
In-Reply-To: <2d5fe872fcbf3e817cf09944494252a0e1bc8979.1665744170.git.viresh.kumar@linaro.org>
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@linaro.org>
> ---
> 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@linaro.org>
> +//
> +// 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@linaro.org>
> +//
> +// 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@linaro.org>
> +//
> +// 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.
next prev parent reply other threads:[~2022-10-17 13:00 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-14 10:47 [PATCH V7 0/8] libgpiod: Add Rust bindings Viresh Kumar
2022-10-14 10:47 ` [PATCH V7 1/8] libgpiod: Add libgpiod-sys rust crate Viresh Kumar
2022-10-17 12:59 ` Kent Gibson
2022-10-18 11:22 ` Viresh Kumar
2022-10-18 11:42 ` Miguel Ojeda
2022-10-18 11:49 ` Kent Gibson
2022-10-19 6:46 ` Viresh Kumar
2022-10-19 7:21 ` Kent Gibson
2022-10-19 11:22 ` Viresh Kumar
2022-10-19 12:01 ` Kent Gibson
2022-10-20 5:34 ` Viresh Kumar
2022-10-27 12:18 ` Bartosz Golaszewski
2022-10-14 10:47 ` [PATCH V7 2/8] libgpiod-sys: Add pre generated rust bindings Viresh Kumar
2022-10-14 10:47 ` [PATCH V7 3/8] libgpiod: Add rust wrapper crate Viresh Kumar
2022-10-17 12:59 ` Kent Gibson
2022-10-21 11:22 ` Viresh Kumar
2022-10-21 12:39 ` Kent Gibson
2022-10-27 12:09 ` Bartosz Golaszewski
2022-10-31 12:33 ` Kent Gibson
2022-10-31 12:45 ` Bartosz Golaszewski
2022-11-02 4:00 ` Viresh Kumar
2022-11-02 12:47 ` Bartosz Golaszewski
2022-11-02 13:08 ` Kent Gibson
2022-11-02 16:34 ` Bartosz Golaszewski
2022-11-03 0:38 ` Kent Gibson
2022-11-03 8:35 ` Bartosz Golaszewski
2022-11-03 12:29 ` Kent Gibson
2022-11-04 15:51 ` Bartosz Golaszewski
2022-10-18 3:27 ` Kent Gibson
2022-10-21 11:25 ` Viresh Kumar
2022-10-14 10:47 ` [PATCH V7 4/8] libgpiod: Add rust examples Viresh Kumar
2022-10-17 13:00 ` Kent Gibson [this message]
2022-10-14 10:47 ` [PATCH V7 5/8] libgpiod: Add gpiosim rust crate Viresh Kumar
2022-10-17 13:00 ` Kent Gibson
2022-10-21 9:56 ` Viresh Kumar
2022-10-14 10:47 ` [PATCH V7 6/8] gpiosim: Add pre generated rust bindings Viresh Kumar
2022-10-14 10:47 ` [PATCH V7 7/8] libgpiod: Add rust tests Viresh Kumar
2022-10-17 13:00 ` Kent Gibson
2022-10-17 14:30 ` Kent Gibson
2022-10-20 10:37 ` Viresh Kumar
2022-10-20 11:02 ` Kent Gibson
2022-10-20 12:40 ` Bartosz Golaszewski
2022-10-20 15:16 ` Miguel Ojeda
2022-10-14 10:47 ` [PATCH V7 8/8] libgpiod: Integrate building of rust bindings with make Viresh Kumar
2022-10-14 17:03 ` [PATCH V7 0/8] libgpiod: Add Rust bindings Miguel Ojeda
2022-10-20 13:29 ` Björn Roy Baron
2022-10-21 9:39 ` Viresh Kumar
2022-10-21 14:34 ` Björn Roy Baron
2022-10-25 6:42 ` Viresh Kumar
2022-10-29 17:46 ` Björn Roy Baron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y01R0A0wrVR2Osyv@sol \
--to=warthog618@gmail.com \
--cc=alex.bennee@linaro.org \
--cc=brgl@bgdev.pl \
--cc=g.m0n3y.2503@gmail.com \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=stratos-dev@op-lists.linaro.org \
--cc=vincent.guittot@linaro.org \
--cc=viresh.kumar@linaro.org \
--cc=wedsonaf@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.