2022-01-27 00:08:02 +01:00
|
|
|
use super::{Duration, Instant};
|
2021-07-12 03:10:01 +02:00
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
/// Blocks for at least `duration`.
|
|
|
|
pub fn block_for(duration: Duration) {
|
|
|
|
let expires_at = Instant::now() + duration;
|
|
|
|
while Instant::now() < expires_at {}
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
|
2021-07-12 03:31:56 +02:00
|
|
|
/// Type implementing async delays and blocking `embedded-hal` delays.
|
|
|
|
///
|
|
|
|
/// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least
|
|
|
|
/// the amount provided, but accuracy can be affected by many factors, including interrupt usage.
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently
|
|
|
|
/// active driver.
|
2021-07-12 03:10:01 +02:00
|
|
|
pub struct Delay;
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
#[cfg(feature = "unstable-traits")]
|
|
|
|
mod eh1 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl embedded_hal_1::delay::blocking::DelayUs for Delay {
|
|
|
|
type Error = core::convert::Infallible;
|
|
|
|
|
|
|
|
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
|
|
|
|
Ok(block_for(Duration::from_micros(us as u64)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
|
|
|
|
Ok(block_for(Duration::from_millis(ms as u64)))
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
2022-02-12 00:24:04 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 03:54:39 +01:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(all(feature = "unstable-traits", feature = "nightly"))] {
|
2022-08-17 23:40:16 +02:00
|
|
|
use crate::Timer;
|
2022-02-16 03:54:39 +01:00
|
|
|
use core::future::Future;
|
2022-07-29 21:58:35 +02:00
|
|
|
use futures_util::FutureExt;
|
2021-07-12 03:10:01 +02:00
|
|
|
|
2022-02-16 03:54:39 +01:00
|
|
|
impl embedded_hal_async::delay::DelayUs for Delay {
|
|
|
|
type Error = core::convert::Infallible;
|
2022-01-27 00:08:02 +01:00
|
|
|
|
2022-02-16 03:54:39 +01:00
|
|
|
type DelayUsFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
2022-01-27 00:08:02 +01:00
|
|
|
|
2022-02-16 03:54:39 +01:00
|
|
|
fn delay_us(&mut self, micros: u32) -> Self::DelayUsFuture<'_> {
|
|
|
|
Timer::after(Duration::from_micros(micros as _)).map(Ok)
|
|
|
|
}
|
2022-01-27 00:08:02 +01:00
|
|
|
|
2022-02-16 03:54:39 +01:00
|
|
|
type DelayMsFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
2022-01-27 00:08:02 +01:00
|
|
|
|
2022-02-16 03:54:39 +01:00
|
|
|
fn delay_ms(&mut self, millis: u32) -> Self::DelayMsFuture<'_> {
|
|
|
|
Timer::after(Duration::from_millis(millis as _)).map(Ok)
|
|
|
|
}
|
2022-01-27 00:08:02 +01:00
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
mod eh02 {
|
|
|
|
use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use super::*;
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
impl DelayMs<u8> for Delay {
|
|
|
|
fn delay_ms(&mut self, ms: u8) {
|
|
|
|
block_for(Duration::from_millis(ms as u64))
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
impl DelayMs<u16> for Delay {
|
|
|
|
fn delay_ms(&mut self, ms: u16) {
|
|
|
|
block_for(Duration::from_millis(ms as u64))
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
impl DelayMs<u32> for Delay {
|
|
|
|
fn delay_ms(&mut self, ms: u32) {
|
|
|
|
block_for(Duration::from_millis(ms as u64))
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
impl DelayUs<u8> for Delay {
|
|
|
|
fn delay_us(&mut self, us: u8) {
|
|
|
|
block_for(Duration::from_micros(us as u64))
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
impl DelayUs<u16> for Delay {
|
|
|
|
fn delay_us(&mut self, us: u16) {
|
|
|
|
block_for(Duration::from_micros(us as u64))
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
impl DelayUs<u32> for Delay {
|
|
|
|
fn delay_us(&mut self, us: u32) {
|
|
|
|
block_for(Duration::from_micros(us as u64))
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 03:10:01 +02:00
|
|
|
}
|