Re: [libgpiod][PATCH 4/4] bindings: rust: examples: add dedicated examples

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>

Reviewed-by: Erik Schilling <erik.schilling@xxxxxxxxxx>

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@xxxxxxxxx>
> +//
> +// 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@xxxxxxxxx>
> +//
> +// 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@xxxxxxxxx>
> +//
> +// 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




[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux