aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Gibson <warthog618@gmail.com>2023-06-14 11:54:26 +0800
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>2023-06-14 15:28:07 +0200
commit53226d51aaf762b8e3f67aa3f23520fe04a25a96 (patch)
treedd8ee769f33fa455014943b20e0bc021ef2c5fa5
parent953dbd6af794e528e05a22ba4bed4b02e49a46fd (diff)
downloadlibgpiod-53226d51aaf762b8e3f67aa3f23520fe04a25a96.tar.gz
bindings: rust: examples: add dedicated examples
Add rust equivalents of the core examples. Signed-off-by: Kent Gibson <warthog618@gmail.com> Reviewed-by: Erik Schilling <erik.schilling@linaro.org> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
-rw-r--r--bindings/rust/libgpiod/examples/get_line_value.rs28
-rw-r--r--bindings/rust/libgpiod/examples/toggle_line_value.rs43
-rw-r--r--bindings/rust/libgpiod/examples/watch_line_value.rs50
3 files changed, 121 insertions, 0 deletions
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 00000000..732fb716
--- /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;
+
+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()?;
+ 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);
+ 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 00000000..cd7038e0
--- /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 00000000..5a95b6ad
--- /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()
+ );
+ }
+ }
+}