From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Erik Wierich <erik@riscstar.com>,
Viresh Kumar <viresh.kumar@linaro.org>,
Linus Walleij <linus.walleij@linaro.org>
Cc: linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [PATCH libgpiod 3/3] bindings: rust: add examples to the README.md in the libgpiod crate
Date: Fri, 26 Sep 2025 16:35:44 +0200 [thread overview]
Message-ID: <20250926-rust-release-tweaks-v1-3-beae932eb691@linaro.org> (raw)
In-Reply-To: <20250926-rust-release-tweaks-v1-0-beae932eb691@linaro.org>
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Add some code examples to the README.md with the aim of showing how to
use the bindings on the crates.io landing page for the libgpiod crate.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
bindings/rust/libgpiod/README.md | 78 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/bindings/rust/libgpiod/README.md b/bindings/rust/libgpiod/README.md
index 1ef3743a99c967dca1be15f16bd3f299612bed16..0d764f472abf113d096a0e73b5d21f5790db9137 100644
--- a/bindings/rust/libgpiod/README.md
+++ b/bindings/rust/libgpiod/README.md
@@ -31,6 +31,84 @@ versions:
- `v2_1`: Minimum version of `2.1.x`
- `vnext`: The upcoming, still unreleased version of the C lib
+## Examples
+
+Get GPIO chip information:
+
+```rust
+let chip = Chip::open(&Path::new("/dev/gpiochip0"))?;
+let info = chip.info()?;
+
+println!("{} [{}] ({} lines)", info.name()?, info.label()?, info.num_lines());
+```
+
+Print GPIO line name:
+
+```rust
+let chip = Chip::open(&Path::new("/dev/gpiochip0"))?;
+let info = chip.line_info(0)?;
+let name = info.name().unwrap_or("unnamed");
+
+println!("{name}");
+```
+
+Toggle GPIO line output value:
+
+```rust
+let mut settings = line::Settings::new()?;
+settings
+ .set_direction(line::Direction::Output)?
+ .set_output_value(Value::Active)?;
+
+let mut line_cfg = line::Config::new()?;
+line_cfg.add_line_settings(&[line_offset], settings)?;
+
+let mut req_cfg = request::Config::new()?;
+req_cfg.set_consumer("toggle-line-value")?;
+
+let chip = Chip::open(&Path::new("/dev/gpiochip0"))?;
+
+/* Request with value 1 */
+let mut req = chip.request_lines(Some(&req_cfg), &line_cfg)?;
+
+/* Toggle to value 0 */
+req.set_value(line_offset, Value::InActive)?;
+```
+
+Read GPIO line event:
+
+```rust
+let mut lsettings = line::Settings::new()?;
+lsettings
+ .set_direction(line::Direction::Input)?
+ .set_edge_detection(Some(line::Edge::Both))?;
+
+let mut line_cfg = line::Config::new()?;
+line_cfg.add_line_settings(&[line_offset], settings)?;
+
+let mut req_cfg = request::Config::new()?;
+req_cfg.set_consumer("toggle-line-value")?;
+
+let chip = Chip::open(&Path::new("/dev/gpiochip0"))?;
+let req = chip.request_lines(Some(&req_cfg), &line_cfg)?;
+
+let mut buffer = request::Buffer::new(1)?;
+
+loop {
+ let events = req.read_edge_events(&mut buffer)?;
+ for event in events {
+ println!(
+ "line: {} type: {}",
+ event.line_offset(),
+ match event.event_type()? {
+ EdgeKind::Rising => "Rising",
+ EdgeKind::Falling => "Falling",
+ }
+ );
+ }
+}
+```
+
## License
This project is licensed under either of
--
2.48.1
next prev parent reply other threads:[~2025-09-26 14:36 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-26 14:35 [PATCH libgpiod 0/3] bindings: rust: pre-release tweaks Bartosz Golaszewski
2025-09-26 14:35 ` [PATCH libgpiod 1/3] bindings: rust: complete the unification of exports in examples Bartosz Golaszewski
2025-09-29 6:21 ` Erik Schilling
2025-09-29 7:29 ` Bartosz Golaszewski
2025-09-26 14:35 ` [PATCH libgpiod 2/3] bindings: rust: update formatting to --edition 2024 Bartosz Golaszewski
2025-09-26 14:35 ` Bartosz Golaszewski [this message]
2025-09-29 6:21 ` [PATCH libgpiod 0/3] bindings: rust: pre-release tweaks Erik Schilling
2025-09-29 7:34 ` Viresh Kumar
2025-09-30 7:54 ` 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=20250926-rust-release-tweaks-v1-3-beae932eb691@linaro.org \
--to=brgl@bgdev.pl \
--cc=bartosz.golaszewski@linaro.org \
--cc=erik@riscstar.com \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=viresh.kumar@linaro.org \
/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).