Skip to content

Commit

Permalink
Replace gpio_cdev with gpiocdev.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 27, 2024
1 parent 6203207 commit e2c7ef4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 162 deletions.
7 changes: 3 additions & 4 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", "tokio/rt", "gpiocdev/async_tokio"]
gpio_cdev = ["dep:gpiocdev"]
async-tokio = ["dep:embedded-hal-async", "tokio/time", "tokio/rt", "gpiocdev?/async_tokio"]
i2c = ["i2cdev"]
spi = ["spidev"]

Expand All @@ -25,8 +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.0" }
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 Down
23 changes: 13 additions & 10 deletions examples/gpio-wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ use std::time::Duration;

use embedded_hal::digital::{InputPin, OutputPin, PinState};
use embedded_hal_async::digital::Wait;
use gpio_cdev::{Chip, LineRequestFlags};
use gpiocdev::Request;
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 chip = Chip::new("/dev/gpiochip0")?;
let input = chip.get_line(INPUT_LINE)?;
let output = chip.get_line(OUTPUT_LINE)?;

let mut input_pin =
CdevPin::new(input.request(LineRequestFlags::INPUT, 0, "")?)?.into_input_pin()?;
let mut output_pin = CdevPin::new(output.request(LineRequestFlags::OUTPUT, 0, "")?)?
.into_output_pin(PinState::Low)?;
let input = Request::builder()
.on_chip(CHIP)
.with_line(INPUT_LINE)
.request()?;
let output = Request::builder()
.on_chip(CHIP)
.with_line(OUTPUT_LINE)
.request()?;

let mut input_pin = CdevPin::new(input)?.into_input_pin()?;
let mut output_pin = CdevPin::new(output)?.into_output_pin(PinState::Low)?;

timeout(Duration::from_secs(10), async move {
let set_output = tokio::spawn(async move {
Expand All @@ -30,7 +34,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
});

println!("Waiting for input to go high.");

input_pin.wait_for_high().await?;

assert!(input_pin.is_high()?);
Expand Down
Loading

0 comments on commit e2c7ef4

Please sign in to comment.