Skip to content

Commit

Permalink
add softwire example
Browse files Browse the repository at this point in the history
  • Loading branch information
warthog618 committed Sep 7, 2024
1 parent 5378ccd commit fe9174d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/examples/softwire.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2024 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Basic piece of wire example mirroring an input to an output.

use anyhow::Context;
use gpiocdev::line::{EdgeDetection, EdgeKind, Value};
use gpiocdev::Request;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = Request::builder()
.on_chip("/dev/gpiochip0")
.with_consumer("softwire")
.with_line(22)
.with_edge_detection(EdgeDetection::BothEdges)
.request()
.context("Failed to request input")?;

// copy value to output
let value = input.value(22)?;
let output = Request::builder()
.on_chip("/dev/gpiochip0")
.with_consumer("softwire")
.with_line(23)
.as_output(value)
.request()
.context("Failed to request output")?;

// track value
for event in input.edge_events() {
let value = match event?.kind {
EdgeKind::Rising => Value::Active,
EdgeKind::Falling => Value::Inactive,
};
output.set_lone_value(value)?;
}
Ok(())
}

0 comments on commit fe9174d

Please sign in to comment.