Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Wait for CdevPin with tokio runtime. #110

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ edition = "2018"

[features]
gpio_sysfs = ["sysfs_gpio"]
gpio_cdev = ["gpio-cdev"]
async-tokio = ["gpio-cdev/async-tokio", "dep:embedded-hal-async", "tokio/time"]
gpio_cdev = ["dep:gpiocdev"]
async-tokio = ["dep:embedded-hal-async", "tokio/time", "tokio/rt", "gpiocdev?/async_tokio"]
i2c = ["i2cdev"]
spi = ["spidev"]

Expand All @@ -25,7 +25,7 @@ default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]
embedded-hal = "1"
embedded-hal-nb = "1"
embedded-hal-async = { version = "1", optional = true }
gpio-cdev = { version = "0.6.0", optional = true }
gpiocdev = { version = "0.6.1", optional = true }
sysfs_gpio = { version = "0.6.1", optional = true }
i2cdev = { version = "0.6.0", optional = true }
nb = "1"
Expand All @@ -36,8 +36,17 @@ tokio = { version = "1", default-features = false, optional = true }

[dev-dependencies]
openpty = "0.2.0"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[dependencies.cast]
# we don't need the `Error` implementation
default-features = false
version = "0.3"

[[example]]
name = "gpio-wait"
required-features = ["async-tokio"]


[patch.crates-io]
gpiocdev = { git = "https://github.com/warthog618/gpiocdev-rs" }
39 changes: 39 additions & 0 deletions examples/gpio-wait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::error::Error;
use std::time::Duration;

use embedded_hal::digital::{InputPin, OutputPin, PinState};
use embedded_hal_async::digital::Wait;
use linux_embedded_hal::CdevPin;
use tokio::time::{sleep, timeout};

// This example assumes that input/output pins are shorted.
const CHIP: &str = "/dev/gpiochip0";
const INPUT_LINE: u32 = 4;
const OUTPUT_LINE: u32 = 17;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut input_pin = CdevPin::new_input(CHIP, INPUT_LINE)?;
let mut output_pin = CdevPin::new_output(CHIP, OUTPUT_LINE, PinState::Low)?;

timeout(Duration::from_secs(10), async move {
let set_output = tokio::spawn(async move {
sleep(Duration::from_secs(5)).await;
println!("Setting output high.");
output_pin.set_high()
});

println!("Waiting for input to go high.");
input_pin.wait_for_high().await?;

assert!(input_pin.is_high()?);
println!("Input is now high.");

set_output.await??;

Ok::<_, Box<dyn Error>>(())
})
.await??;

Ok(())
}
Loading