From: Viresh Kumar <viresh.kumar@linaro.org>
To: Linus Walleij <linus.walleij@linaro.org>,
Bartosz Golaszewski <brgl@bgdev.pl>
Cc: "Viresh Kumar" <viresh.kumar@linaro.org>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
linux-gpio@vger.kernel.org, "Kent Gibson" <warthog618@gmail.com>,
"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
Subject: [PATCH V2 3/4] rust: Add examples
Date: Thu, 2 Dec 2021 16:52:48 +0530 [thread overview]
Message-ID: <394d3d478bf844766155b4ffdf2cbf8daa1d4551.1638443930.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1638443930.git.viresh.kumar@linaro.org>
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/examples/gpiodetect.rs | 38 ++++++++++++
bindings/rust/examples/gpiofind.rs | 43 +++++++++++++
bindings/rust/examples/gpioget.rs | 45 ++++++++++++++
bindings/rust/examples/gpioinfo.rs | 90 ++++++++++++++++++++++++++++
bindings/rust/examples/gpiomon.rs | 73 ++++++++++++++++++++++
bindings/rust/examples/gpioset.rs | 55 +++++++++++++++++
6 files changed, 344 insertions(+)
create mode 100644 bindings/rust/examples/gpiodetect.rs
create mode 100644 bindings/rust/examples/gpiofind.rs
create mode 100644 bindings/rust/examples/gpioget.rs
create mode 100644 bindings/rust/examples/gpioinfo.rs
create mode 100644 bindings/rust/examples/gpiomon.rs
create mode 100644 bindings/rust/examples/gpioset.rs
diff --git a/bindings/rust/examples/gpiodetect.rs b/bindings/rust/examples/gpiodetect.rs
new file mode 100644
index 000000000000..c4b2ef31469d
--- /dev/null
+++ b/bindings/rust/examples/gpiodetect.rs
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2021 Linaro Ltd. All Rights Reserved.
+// Viresh Kumar <viresh.kumar@linaro.org>
+//
+// Simplified Rust implementation of gpiodetect tool.
+
+use std::env;
+use std::fs;
+use std::path::Path;
+
+use libgpiod::chip::GpiodChip;
+use libgpiod::gpiod_is_gpiochip_device;
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() > 1 {
+ println!("Usage: {}", args[0]);
+ return;
+ }
+
+ for entry in fs::read_dir(Path::new("/dev")).unwrap() {
+ let pathbuf = entry.unwrap().path();
+ let path = pathbuf.to_str().unwrap();
+
+ if gpiod_is_gpiochip_device(path) {
+ let chip = GpiodChip::open(path).unwrap();
+ let ngpio = chip.get_num_lines();
+
+ println!(
+ "{} [{}] ({})",
+ chip.get_name().unwrap(),
+ chip.get_label().unwrap(),
+ ngpio
+ );
+ }
+ }
+}
diff --git a/bindings/rust/examples/gpiofind.rs b/bindings/rust/examples/gpiofind.rs
new file mode 100644
index 000000000000..5050f4a39b6b
--- /dev/null
+++ b/bindings/rust/examples/gpiofind.rs
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2021 Linaro Ltd. All Rights Reserved.
+// Viresh Kumar <viresh.kumar@linaro.org>
+//
+// Simplified Rust implementation of gpiofind tool.
+
+use std::env;
+use std::fs;
+use std::path::Path;
+
+use libgpiod::chip::GpiodChip;
+use libgpiod::gpiod_is_gpiochip_device;
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() != 2 {
+ println!("Usage: {} <line-name>", args[0]);
+ return;
+ }
+
+ for entry in fs::read_dir(Path::new("/dev")).unwrap() {
+ let pathbuf = entry.unwrap().path();
+ let path = pathbuf.to_str().unwrap();
+
+ if gpiod_is_gpiochip_device(path) {
+ let chip = GpiodChip::open(path).unwrap();
+
+ let offset = chip.find_line(&args[1]);
+ if offset.is_ok() {
+ println!(
+ "Line {} found: Chip: {}, offset: {}",
+ args[1],
+ chip.get_name().unwrap(),
+ offset.unwrap()
+ );
+ return;
+ }
+ }
+ }
+
+ println!("Failed to find line: {}", args[1]);
+}
diff --git a/bindings/rust/examples/gpioget.rs b/bindings/rust/examples/gpioget.rs
new file mode 100644
index 000000000000..47a51b7c56b9
--- /dev/null
+++ b/bindings/rust/examples/gpioget.rs
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2021 Linaro Ltd. All Rights Reserved.
+// Viresh Kumar <viresh.kumar@linaro.org>
+//
+// Simplified Rust implementation of gpioget tool.
+
+use std::env;
+
+use libgpiod::chip::GpiodChip;
+use libgpiod::line_config::GpiodLineConfig;
+use libgpiod::request_config::GpiodRequestConfig;
+use libgpiod::Direction;
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() < 3 {
+ println!("Usage: {} <chip> <line_offset0> ...", args[0]);
+ return;
+ }
+
+ let mut config = GpiodLineConfig::new().unwrap();
+ let mut offsets = Vec::<u32>::new();
+
+ for arg in &args[2..] {
+ let offset = arg.parse::<u32>().unwrap();
+
+ offsets.push(offset);
+ config.set_direction_offset(Direction::Input, offset);
+ }
+
+ let path = format!("/dev/gpiochip{}", args[1]);
+ let chip = GpiodChip::open(&path).unwrap();
+
+ let rconfig = GpiodRequestConfig::new().unwrap();
+ rconfig.set_consumer(&args[0]);
+ rconfig.set_offsets(&offsets);
+
+ let request = chip.request_lines(&rconfig, &config).unwrap();
+
+ let mut values: Vec<i32> = vec![0; offsets.len()];
+ request.get_values(&mut values).unwrap();
+
+ println!("{:?}", values);
+}
diff --git a/bindings/rust/examples/gpioinfo.rs b/bindings/rust/examples/gpioinfo.rs
new file mode 100644
index 000000000000..1b84df70e811
--- /dev/null
+++ b/bindings/rust/examples/gpioinfo.rs
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2021 Linaro Ltd. All Rights Reserved.
+// Viresh Kumar <viresh.kumar@linaro.org>
+//
+// Simplified Rust implementation of gpioinfo tool.
+
+use std::env;
+use std::fs;
+use std::path::Path;
+
+use libgpiod::chip::GpiodChip;
+use libgpiod::{gpiod_is_gpiochip_device, Direction};
+
+fn line_info(chip: &GpiodChip, offset: u32) {
+ let info = chip.line_info(offset).unwrap();
+ let off = info.get_offset();
+
+ let name = match info.get_name() {
+ Ok(name) => name,
+ _ => "unused",
+ };
+
+ let consumer = match info.get_consumer() {
+ Ok(name) => name,
+ _ => "unnamed",
+ };
+
+ let low = if info.is_active_low() {
+ "active-low"
+ } else {
+ "active-high"
+ };
+
+ let dir = match info.get_direction().unwrap() {
+ Direction::AsIs => "None",
+ Direction::Input => "Input",
+ Direction::Output => "Output",
+ };
+
+ println!(
+ "\tline {:>3}\
+ \t{:>10}\
+ \t{:>10}\
+ \t{:>6}\
+ \t{:>14}",
+ off, name, consumer, dir, low
+ );
+}
+
+fn chip_info(path: &str) {
+ if gpiod_is_gpiochip_device(path) {
+ let chip = GpiodChip::open(path).unwrap();
+ let ngpio = chip.get_num_lines();
+
+ println!("GPIO Chip name: {}", chip.get_name().unwrap());
+ println!("\tlabel: {}", chip.get_label().unwrap());
+ println!("\tpath: {}", chip.get_path().unwrap());
+ println!("\tngpio: {}\n", ngpio);
+
+ println!("\tLine information:");
+
+ for offset in 0..ngpio {
+ line_info(&chip, offset);
+ }
+ println!("\n");
+ }
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() > 2 {
+ println!("Usage: {}", args[0]);
+ return;
+ }
+
+ if args.len() == 1 {
+ for entry in fs::read_dir(Path::new("/dev")).unwrap() {
+ let pathbuf = entry.unwrap().path();
+ let path = pathbuf.to_str().unwrap();
+
+ chip_info(path);
+ }
+ } else {
+ let index = args[1].parse::<u32>().unwrap();
+ let path = format!("/dev/gpiochip{}", index);
+
+ chip_info(&path);
+ }
+}
diff --git a/bindings/rust/examples/gpiomon.rs b/bindings/rust/examples/gpiomon.rs
new file mode 100644
index 000000000000..7e44ec0da357
--- /dev/null
+++ b/bindings/rust/examples/gpiomon.rs
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2021 Linaro Ltd. All Rights Reserved.
+// Viresh Kumar <viresh.kumar@linaro.org>
+//
+// Simplified Rust implementation of the gpiomon tool.
+
+use std::env;
+use std::time::Duration;
+
+use libgpiod::chip::GpiodChip;
+use libgpiod::event_buffer::GpiodEdgeEventBuffer;
+use libgpiod::line_config::GpiodLineConfig;
+use libgpiod::request_config::GpiodRequestConfig;
+use libgpiod::{Edge, EdgeEvent, Error};
+
+fn usage(name: &str) {
+ println!("Usage: {} <chip> <offset0> ...", name);
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() < 3 {
+ usage(&args[0]);
+ return;
+ }
+
+ let mut config = GpiodLineConfig::new().unwrap();
+ let mut offsets = Vec::<u32>::new();
+
+ for arg in &args[2..] {
+ let offset = arg.parse::<u32>().unwrap();
+
+ offsets.push(offset);
+ }
+
+ config.set_edge_detection(Edge::Both);
+
+ let path = format!("/dev/gpiochip{}", args[1]);
+ let chip = GpiodChip::open(&path).unwrap();
+
+ let rconfig = GpiodRequestConfig::new().unwrap();
+ rconfig.set_consumer(&args[0]);
+ rconfig.set_offsets(&offsets);
+
+ let buffer = GpiodEdgeEventBuffer::new(64).unwrap();
+ let request = chip.request_lines(&rconfig, &config).unwrap();
+
+ loop {
+ match request.edge_event_wait(Duration::new(1, 0)) {
+ Err(Error::OperationTimedOut) => continue,
+ Err(x) => {
+ println!("{:?}", x);
+ return;
+ }
+ Ok(()) => (),
+ }
+
+ let count = request.edge_event_read(&buffer, 64).unwrap();
+ for i in 0..count {
+ let event = buffer.get_event(i as u64).unwrap();
+ println!(
+ "line: {} type: {}, time: {:?}",
+ event.get_line_offset(),
+ match event.get_event_type().unwrap() {
+ EdgeEvent::Rising => "Rising",
+ EdgeEvent::Falling => "Falling",
+ },
+ event.get_timestamp()
+ );
+ }
+ }
+}
diff --git a/bindings/rust/examples/gpioset.rs b/bindings/rust/examples/gpioset.rs
new file mode 100644
index 000000000000..956f2d754717
--- /dev/null
+++ b/bindings/rust/examples/gpioset.rs
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2021 Linaro Ltd. All Rights Reserved.
+// Viresh Kumar <viresh.kumar@linaro.org>
+//
+// Simplified Rust implementation of the gpioset tool.
+
+use std::env;
+
+use libgpiod::chip::GpiodChip;
+use libgpiod::line_config::GpiodLineConfig;
+use libgpiod::request_config::GpiodRequestConfig;
+use libgpiod::Direction;
+
+fn usage(name: &str) {
+ println!("Usage: {} <chip> <line_offset0>=<value0> ...", name);
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() < 3 {
+ usage(&args[0]);
+ return;
+ }
+
+ let mut config = GpiodLineConfig::new().unwrap();
+ let mut offsets = Vec::<u32>::new();
+ let mut values = Vec::<i32>::new();
+
+ for arg in &args[2..] {
+ let pair: Vec<&str> = arg.split('=').collect();
+ if pair.len() != 2 {
+ usage(&args[0]);
+ return;
+ }
+
+ let offset = pair[0].parse::<u32>().unwrap();
+ let value = pair[1].parse::<u32>().unwrap();
+
+ offsets.push(offset);
+ values.push(value as i32);
+ }
+
+ config.set_direction(Direction::Output);
+ config.set_output_values(&offsets, &values);
+
+ let path = format!("/dev/gpiochip{}", args[1]);
+ let chip = GpiodChip::open(&path).unwrap();
+
+ let rconfig = GpiodRequestConfig::new().unwrap();
+ rconfig.set_consumer(&args[0]);
+ rconfig.set_offsets(&offsets);
+
+ chip.request_lines(&rconfig, &config).unwrap();
+}
--
2.31.1.272.g89b43f80a514
next prev parent reply other threads:[~2021-12-02 11:23 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-02 11:22 [PATCH V2 0/4] libgpiod: Add Rust bindings Viresh Kumar
2021-12-02 11:22 ` [PATCH V2 1/4] libgpiod: Generate rust FFI bindings Viresh Kumar
2021-12-02 11:22 ` [PATCH V2 2/4] libgpiod: Add rust wrappers Viresh Kumar
2021-12-16 13:59 ` Bartosz Golaszewski
2021-12-17 0:12 ` Kent Gibson
2021-12-17 5:11 ` Viresh Kumar
2022-01-06 15:47 ` Kent Gibson
2022-01-07 7:37 ` Viresh Kumar
2021-12-17 5:01 ` Viresh Kumar
2021-12-17 5:52 ` Wedson Almeida Filho
2021-12-17 6:29 ` Viresh Kumar
2021-12-17 7:48 ` Viresh Kumar
2021-12-17 9:12 ` Bartosz Golaszewski
2021-12-17 9:32 ` Viresh Kumar
2021-12-17 9:43 ` Bartosz Golaszewski
2021-12-17 9:54 ` Viresh Kumar
2021-12-17 12:02 ` Bartosz Golaszewski
2021-12-20 5:28 ` Viresh Kumar
2021-12-17 9:44 ` Miguel Ojeda
2021-12-17 10:04 ` Viresh Kumar
2021-12-17 9:21 ` Miguel Ojeda
2021-12-17 9:43 ` Viresh Kumar
2021-12-02 11:22 ` Viresh Kumar [this message]
2021-12-02 11:22 ` [PATCH V2 4/4] rust: Integrate building of rust bindings with make Viresh Kumar
2022-04-11 3:33 ` [PATCH V2 0/4] libgpiod: Add Rust bindings Viresh Kumar
2022-04-11 8:57 ` Bartosz Golaszewski
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=394d3d478bf844766155b4ffdf2cbf8daa1d4551.1638443930.git.viresh.kumar@linaro.org \
--to=viresh.kumar@linaro.org \
--cc=alex.bennee@linaro.org \
--cc=brgl@bgdev.pl \
--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=warthog618@gmail.com \
--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 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).