1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00

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>
This commit is contained in:
Kent Gibson
2023-06-14 11:54:26 +08:00
committed by Bartosz Golaszewski
parent 953dbd6af7
commit 53226d51aa
3 changed files with 121 additions and 0 deletions

View File

@@ -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(())
}

View File

@@ -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)?;
}
}

View File

@@ -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()
);
}
}
}