Skip to content

Commit

Permalink
Merge pull request #109 from reitermarkus/async-delay
Browse files Browse the repository at this point in the history
Add async `DelayNs` implementation for `tokio`.
  • Loading branch information
eldruin authored Jan 25, 2024
2 parents e50b26d + f7b1505 commit 3ed5ad8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -24,13 +24,15 @@ 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 }
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"
Expand Down
29 changes: 26 additions & 3 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 3ed5ad8

Please sign in to comment.