1use super::DelayMs;
2use core::{
3 pin::Pin,
4 task::{Context, Poll},
5};
6use embedded_hal::timer::{Cancel, CountDown};
7use fugit::MillisDurationU32;
8
9pub struct Timer<T> {
11 counter: T,
12}
13
14impl<T> Timer<T> {
15 pub const fn new(counter: T) -> Self {
17 Self { counter }
18 }
19}
20
21impl<T> DelayMs for Timer<T>
22where
23 T: CountDown + Cancel + Unpin,
24 T::Time: From<MillisDurationU32>,
25{
26 type Delay = u32;
27 type Error = T::Error;
28
29 fn start(&mut self, ms: Self::Delay) -> Result<(), Self::Error> {
30 self.counter.start(MillisDurationU32::millis(ms));
31 Ok(())
32 }
33
34 fn cancel(&mut self) -> Result<(), Self::Error> {
35 self.counter.cancel()
36 }
37
38 fn poll_delay_ms(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
39 match self.counter.wait() {
40 Ok(()) => Poll::Ready(Ok(())),
41 Err(nb::Error::Other(_void)) => unreachable!(),
42 Err(nb::Error::WouldBlock) => Poll::Pending,
43 }
44 }
45}