From: "Erik Schilling" <erik.schilling@linaro.org>
To: "Kent Gibson" <warthog618@gmail.com>,
<linux-gpio@vger.kernel.org>, <brgl@bgdev.pl>
Subject: Re: [libgpiod][PATCH 4/4] bindings: rust: examples: add dedicated examples
Date: Wed, 14 Jun 2023 09:52:20 +0200 [thread overview]
Message-ID: <CTC7KTWRXMS4.1G8UBSCYLSMIT@fedora> (raw)
In-Reply-To: <20230614035426.15097-5-warthog618@gmail.com>
On Wed Jun 14, 2023 at 5:54 AM CEST, Kent Gibson wrote:
> Add rust equivalents of the core examples.
>
> Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
Some nit-picks below, but those are a matter of taste and the change
looks ok either way.
> ---
> .../rust/libgpiod/examples/get_line_value.rs | 28 +++++++++++
> .../libgpiod/examples/toggle_line_value.rs | 43 ++++++++++++++++
> .../libgpiod/examples/watch_line_value.rs | 50 +++++++++++++++++++
> 3 files changed, 121 insertions(+)
> create mode 100644 bindings/rust/libgpiod/examples/get_line_value.rs
> create mode 100644 bindings/rust/libgpiod/examples/toggle_line_value.rs
> create mode 100644 bindings/rust/libgpiod/examples/watch_line_value.rs
>
> diff --git a/bindings/rust/libgpiod/examples/get_line_value.rs b/bindings/rust/libgpiod/examples/get_line_value.rs
> new file mode 100644
> index 0000000..732fb71
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/get_line_value.rs
> @@ -0,0 +1,28 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
> +//
> +// Minimal example of reading a single line.
> +
> +use libgpiod::line;
I think one could also just import the other used modules. Or, since
this is an example anyway, just `use libgpiod::*`.
> +
> +fn main() -> libgpiod::Result<()> {
> + // example configuration - customize to suit your situation
> + let chip_path = "/dev/gpiochip0";
> + let line_offset = 5;
> +
> + let mut lsettings = line::Settings::new()?;
I think `line_settings` would still be an okish length (same below) :).
> + lsettings.set_direction(line::Direction::Input)?;
> +
> + let mut lconfig = line::Config::new()?;
> + lconfig.add_line_settings(&[line_offset], lsettings)?;
> +
> + let mut rconfig = libgpiod::request::Config::new()?;
> + rconfig.set_consumer("get-line-value")?;
> +
> + let chip = libgpiod::chip::Chip::open(&chip_path)?;
> + let request = chip.request_lines(Some(&rconfig), &lconfig)?;
> +
> + let value = request.value(line_offset)?;
> + println!("{:?}", value);
Could also be:
+ println!("{value:?}");
(same below)
> + Ok(())
> +}
> diff --git a/bindings/rust/libgpiod/examples/toggle_line_value.rs b/bindings/rust/libgpiod/examples/toggle_line_value.rs
> new file mode 100644
> index 0000000..cd7038e
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/toggle_line_value.rs
> @@ -0,0 +1,43 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
> +//
> +// Minimal example of toggling a single line.
> +
> +use libgpiod::line;
> +use std::time::Duration;
> +
> +fn toggle_value(value: line::Value) -> line::Value {
> + match value {
> + line::Value::Active => line::Value::InActive,
> + line::Value::InActive => line::Value::Active,
> + }
> +}
> +
> +fn main() -> libgpiod::Result<()> {
> + // example configuration - customize to suit your situation
> + let chip_path = "/dev/gpiochip0";
> + let line_offset = 5;
> +
> + let mut value = line::Value::Active;
> +
> + let mut settings = line::Settings::new()?;
> + settings
> + .set_direction(line::Direction::Output)?
> + .set_output_value(value)?;
> +
> + let mut lconfig = line::Config::new()?;
> + lconfig.add_line_settings(&[line_offset], settings)?;
> +
> + let mut rconfig = libgpiod::request::Config::new()?;
> + rconfig.set_consumer("toggle-line-value")?;
> +
> + let chip = libgpiod::chip::Chip::open(&chip_path)?;
> + let mut req = chip.request_lines(Some(&rconfig), &lconfig)?;
> +
> + loop {
> + println!("{:?}", value);
> + std::thread::sleep(Duration::from_secs(1));
> + value = toggle_value(value);
> + req.set_value(line_offset, value)?;
> + }
> +}
> diff --git a/bindings/rust/libgpiod/examples/watch_line_value.rs b/bindings/rust/libgpiod/examples/watch_line_value.rs
> new file mode 100644
> index 0000000..5a95b6a
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/watch_line_value.rs
> @@ -0,0 +1,50 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
> +//
> +// Minimal example of watching for edges on a single line.
> +
> +use libgpiod::line;
> +use std::time::Duration;
> +
> +fn main() -> libgpiod::Result<()> {
> + // example configuration - customize to suit your situation
> + let chip_path = "/dev/gpiochip0";
> + let line_offset = 5;
> +
> + let mut lsettings = line::Settings::new()?;
> + // assume a button connecting the pin to ground,
> + // so pull it up and provide some debounce.
> + lsettings
> + .set_edge_detection(Some(line::Edge::Both))?
> + .set_bias(Some(line::Bias::PullUp))?
> + .set_debounce_period(Duration::from_millis(10));
> +
> + let mut lconfig = line::Config::new()?;
> + lconfig.add_line_settings(&[line_offset], lsettings)?;
> +
> + let mut rconfig = libgpiod::request::Config::new()?;
> + rconfig.set_consumer("watch-line-value")?;
> +
> + let chip = libgpiod::chip::Chip::open(&chip_path)?;
> + let request = chip.request_lines(Some(&rconfig), &lconfig)?;
> +
> + // a larger buffer is an optimisation for reading bursts of events from the
> + // kernel, but that is not necessary in this case, so 1 is fine.
> + let mut buffer = libgpiod::request::Buffer::new(1)?;
> + loop {
> + // blocks until at least one event is available
> + let events = request.read_edge_events(&mut buffer)?;
> + for event in events {
> + let event = event?;
> + println!(
> + "line: {}, type: {}, event #{}",
> + event.line_offset(),
> + match event.event_type()? {
> + line::EdgeKind::Rising => "Rising ",
> + line::EdgeKind::Falling => "Falling",
> + },
> + event.line_seqno()
> + );
println!("{: <8}") could also be used to pad things (would allow
removing the trailing space).
> + }
> + }
> +}
> --
> 2.40.1
- Erik
next prev parent reply other threads:[~2023-06-14 7:53 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-14 3:54 [libgpiod][PATCH 0/4] dedicated examples Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 1/4] core: examples: add " Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 2/4] bindings: cxx: " Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 3/4] bindings: python: " Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 4/4] bindings: rust: " Kent Gibson
2023-06-14 7:52 ` Erik Schilling [this message]
2023-06-14 8:18 ` Kent Gibson
2023-06-14 8:29 ` Erik Schilling
2023-06-14 13:03 ` [libgpiod][PATCH 0/4] " Bartosz Golaszewski
2023-06-14 13:21 ` Kent Gibson
2023-06-14 13:26 ` Bartosz Golaszewski
2023-06-14 13:57 ` Kent Gibson
2023-06-14 15:11 ` Bartosz Golaszewski
2023-06-14 16:00 ` Kent Gibson
2023-06-15 15:16 ` Bartosz Golaszewski
2023-06-15 15:39 ` Kent Gibson
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=CTC7KTWRXMS4.1G8UBSCYLSMIT@fedora \
--to=erik.schilling@linaro.org \
--cc=brgl@bgdev.pl \
--cc=linux-gpio@vger.kernel.org \
--cc=warthog618@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).