Skip to content

Commit 3ed5ad8

Browse files
authored
Merge pull request #109 from reitermarkus/async-delay
Add async `DelayNs` implementation for `tokio`.
2 parents e50b26d + f7b1505 commit 3ed5ad8

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

.github/workflows/clippy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ jobs:
1313
- uses: actions/checkout@v4
1414
- uses: dtolnay/rust-toolchain@master
1515
with:
16-
toolchain: 1.73.0
16+
toolchain: 1.75.0
1717
components: clippy
1818
- run: cargo clippy --all-features -- --deny=warnings

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added async `DelayNs` implementation for `tokio`.
13+
1014
## [v0.4.0] - 2024-01-10
1115

1216
### Changed

Cargo.toml

+3-1
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

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,39 @@
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+
fn delay_us(&mut self, n: u32) {
19+
thread::sleep(Duration::from_micros(n.into()));
20+
}
21+
22+
fn delay_ms(&mut self, n: u32) {
23+
thread::sleep(Duration::from_millis(n.into()));
24+
}
25+
}
26+
27+
#[cfg(feature = "async-tokio")]
28+
impl embedded_hal_async::delay::DelayNs for Delay {
29+
async fn delay_ns(&mut self, n: u32) {
30+
tokio::time::sleep(Duration::from_nanos(n.into())).await;
31+
}
32+
33+
async fn delay_us(&mut self, n: u32) {
34+
tokio::time::sleep(Duration::from_micros(n.into())).await;
35+
}
36+
37+
async fn delay_ms(&mut self, n: u32) {
38+
tokio::time::sleep(Duration::from_millis(n.into())).await;
1639
}
1740
}

0 commit comments

Comments
 (0)