Skip to content

Commit 78527ac

Browse files
committed
Add async Delay implementation for tokio.
1 parent e50b26d commit 78527ac

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ edition = "2018"
1515
[features]
1616
gpio_sysfs = ["sysfs_gpio"]
1717
gpio_cdev = ["gpio-cdev"]
18-
async-tokio = ["gpio-cdev/async-tokio"]
18+
async-tokio = ["gpio-cdev/async-tokio", "dep:embedded-hal-async", "tokio/time"]
1919
i2c = ["i2cdev"]
2020
spi = ["spidev"]
2121

@@ -24,13 +24,15 @@ default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]
2424
[dependencies]
2525
embedded-hal = "1"
2626
embedded-hal-nb = "1"
27+
embedded-hal-async = { version = "1", optional = true }
2728
gpio-cdev = { version = "0.6.0", optional = true }
2829
sysfs_gpio = { version = "0.6.1", optional = true }
2930
i2cdev = { version = "0.6.0", optional = true }
3031
nb = "1"
3132
serialport = { version = "4.2.0", default-features = false }
3233
spidev = { version = "0.6.0", optional = true }
3334
nix = "0.27.1"
35+
tokio = { version = "1", default-features = false, optional = true }
3436

3537
[dev-dependencies]
3638
openpty = "0.2.0"

src/delay.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5-
use cast::u64;
65
use embedded_hal::delay::DelayNs;
76
use std::thread;
87
use std::time::Duration;
98

10-
/// Empty struct that provides delay functionality on top of `thread::sleep`
9+
/// Empty struct that provides delay functionality on top of `thread::sleep`,
10+
/// and `tokio::time::sleep` if the `async-tokio` feature is enabled.
1111
pub struct Delay;
1212

1313
impl DelayNs for Delay {
1414
fn delay_ns(&mut self, n: u32) {
15-
thread::sleep(Duration::from_nanos(u64(n)));
15+
thread::sleep(Duration::from_nanos(n.into()));
16+
}
17+
}
18+
19+
#[cfg(feature = "async-tokio")]
20+
impl embedded_hal_async::delay::DelayNs for Delay {
21+
async fn delay_ns(&mut self, n: u32) {
22+
tokio::time::sleep(Duration::from_nanos(n.into())).await;
1623
}
1724
}

0 commit comments

Comments
 (0)