diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index f5fc395..af11e80 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -13,6 +13,6 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: - toolchain: 1.73.0 + toolchain: 1.75.0 components: clippy - run: cargo clippy --all-features -- --deny=warnings diff --git a/CHANGELOG.md b/CHANGELOG.md index a8d0219..06cdae9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + +- Added async `DelayNs` implementation for `tokio`. + ## [v0.4.0] - 2024-01-10 ### Changed diff --git a/Cargo.toml b/Cargo.toml index ea35bed..590aa91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" [features] gpio_sysfs = ["sysfs_gpio"] gpio_cdev = ["gpio-cdev"] -async-tokio = ["gpio-cdev/async-tokio"] +async-tokio = ["gpio-cdev/async-tokio", "dep:embedded-hal-async", "tokio/time"] i2c = ["i2cdev"] spi = ["spidev"] @@ -24,6 +24,7 @@ default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ] [dependencies] embedded-hal = "1" embedded-hal-nb = "1" +embedded-hal-async = { version = "1", optional = true } gpio-cdev = { version = "0.6.0", optional = true } sysfs_gpio = { version = "0.6.1", optional = true } i2cdev = { version = "0.6.0", optional = true } @@ -31,6 +32,7 @@ nb = "1" serialport = { version = "4.2.0", default-features = false } spidev = { version = "0.6.0", optional = true } nix = "0.27.1" +tokio = { version = "1", default-features = false, optional = true } [dev-dependencies] openpty = "0.2.0" diff --git a/src/delay.rs b/src/delay.rs index ffd435a..b9eaea9 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -2,16 +2,39 @@ //! //! [`embedded-hal`]: https://docs.rs/embedded-hal -use cast::u64; use embedded_hal::delay::DelayNs; use std::thread; use std::time::Duration; -/// Empty struct that provides delay functionality on top of `thread::sleep` +/// Empty struct that provides delay functionality on top of `thread::sleep`, +/// and `tokio::time::sleep` if the `async-tokio` feature is enabled. pub struct Delay; impl DelayNs for Delay { fn delay_ns(&mut self, n: u32) { - thread::sleep(Duration::from_nanos(u64(n))); + thread::sleep(Duration::from_nanos(n.into())); + } + + fn delay_us(&mut self, n: u32) { + thread::sleep(Duration::from_micros(n.into())); + } + + fn delay_ms(&mut self, n: u32) { + thread::sleep(Duration::from_millis(n.into())); + } +} + +#[cfg(feature = "async-tokio")] +impl embedded_hal_async::delay::DelayNs for Delay { + async fn delay_ns(&mut self, n: u32) { + tokio::time::sleep(Duration::from_nanos(n.into())).await; + } + + async fn delay_us(&mut self, n: u32) { + tokio::time::sleep(Duration::from_micros(n.into())).await; + } + + async fn delay_ms(&mut self, n: u32) { + tokio::time::sleep(Duration::from_millis(n.into())).await; } }