diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-08-17 23:40:16 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-08-18 01:22:30 +0200 |
| commit | 5daa173ce4b153a532b4daa9e94c7a248231f25b (patch) | |
| tree | 2ef0b4d6f9b1c02dac2589e7b57982c20cbc0e66 /embassy-executor/src/time/delay.rs | |
| parent | 1c5b54a4823d596db730eb476c3ab78110557214 (diff) | |
Split embassy-time from embassy-executor.
Diffstat (limited to 'embassy-executor/src/time/delay.rs')
| -rw-r--r-- | embassy-executor/src/time/delay.rs | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/embassy-executor/src/time/delay.rs b/embassy-executor/src/time/delay.rs deleted file mode 100644 index d76ed32eb..000000000 --- a/embassy-executor/src/time/delay.rs +++ /dev/null | |||
| @@ -1,98 +0,0 @@ | |||
| 1 | use super::{Duration, Instant}; | ||
| 2 | |||
| 3 | /// Blocks for at least `duration`. | ||
| 4 | pub fn block_for(duration: Duration) { | ||
| 5 | let expires_at = Instant::now() + duration; | ||
| 6 | while Instant::now() < expires_at {} | ||
| 7 | } | ||
| 8 | |||
| 9 | /// Type implementing async delays and blocking `embedded-hal` delays. | ||
| 10 | /// | ||
| 11 | /// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least | ||
| 12 | /// the amount provided, but accuracy can be affected by many factors, including interrupt usage. | ||
| 13 | /// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently | ||
| 14 | /// active driver. | ||
| 15 | pub struct Delay; | ||
| 16 | |||
| 17 | #[cfg(feature = "unstable-traits")] | ||
| 18 | mod eh1 { | ||
| 19 | use super::*; | ||
| 20 | |||
| 21 | impl embedded_hal_1::delay::blocking::DelayUs for Delay { | ||
| 22 | type Error = core::convert::Infallible; | ||
| 23 | |||
| 24 | fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { | ||
| 25 | Ok(block_for(Duration::from_micros(us as u64))) | ||
| 26 | } | ||
| 27 | |||
| 28 | fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { | ||
| 29 | Ok(block_for(Duration::from_millis(ms as u64))) | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | cfg_if::cfg_if! { | ||
| 35 | if #[cfg(all(feature = "unstable-traits", feature = "nightly"))] { | ||
| 36 | use crate::time::Timer; | ||
| 37 | use core::future::Future; | ||
| 38 | use futures_util::FutureExt; | ||
| 39 | |||
| 40 | impl embedded_hal_async::delay::DelayUs for Delay { | ||
| 41 | type Error = core::convert::Infallible; | ||
| 42 | |||
| 43 | type DelayUsFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 44 | |||
| 45 | fn delay_us(&mut self, micros: u32) -> Self::DelayUsFuture<'_> { | ||
| 46 | Timer::after(Duration::from_micros(micros as _)).map(Ok) | ||
| 47 | } | ||
| 48 | |||
| 49 | type DelayMsFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 50 | |||
| 51 | fn delay_ms(&mut self, millis: u32) -> Self::DelayMsFuture<'_> { | ||
| 52 | Timer::after(Duration::from_millis(millis as _)).map(Ok) | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | mod eh02 { | ||
| 59 | use embedded_hal_02::blocking::delay::{DelayMs, DelayUs}; | ||
| 60 | |||
| 61 | use super::*; | ||
| 62 | |||
| 63 | impl DelayMs<u8> for Delay { | ||
| 64 | fn delay_ms(&mut self, ms: u8) { | ||
| 65 | block_for(Duration::from_millis(ms as u64)) | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | impl DelayMs<u16> for Delay { | ||
| 70 | fn delay_ms(&mut self, ms: u16) { | ||
| 71 | block_for(Duration::from_millis(ms as u64)) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | impl DelayMs<u32> for Delay { | ||
| 76 | fn delay_ms(&mut self, ms: u32) { | ||
| 77 | block_for(Duration::from_millis(ms as u64)) | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | impl DelayUs<u8> for Delay { | ||
| 82 | fn delay_us(&mut self, us: u8) { | ||
| 83 | block_for(Duration::from_micros(us as u64)) | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | impl DelayUs<u16> for Delay { | ||
| 88 | fn delay_us(&mut self, us: u16) { | ||
| 89 | block_for(Duration::from_micros(us as u64)) | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | impl DelayUs<u32> for Delay { | ||
| 94 | fn delay_us(&mut self, us: u32) { | ||
| 95 | block_for(Duration::from_micros(us as u64)) | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
