Skip to content

Add async DelayNs implementation for tokio. #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}