embassy_socket/
wait.rs

1use embassy_time::{Duration, Timer};
2use embedded_hal::digital::{Error, ErrorKind, ErrorType};
3use embedded_hal_async::digital::Wait;
4
5/// timeout wait<br />
6/// generally used when there are no interrupt pins
7pub struct TimeOutWait {
8    pub timeout: Duration,
9}
10
11/// timeout wait error, nothing error
12#[derive(Debug)]
13pub struct TimeoutWaitError;
14
15/// out order error
16impl Error for TimeoutWaitError {
17    fn kind(&self) -> ErrorKind {
18        ErrorKind::Other
19    }
20}
21
22/// custom error type
23impl ErrorType for TimeOutWait { type Error = TimeoutWaitError; }
24
25/// support default 100 millis
26impl Default for TimeOutWait {
27    #[inline]
28    fn default() -> Self {
29        Self::new(Duration::from_millis(100))
30    }
31}
32
33/// custom method
34impl TimeOutWait {
35    /// custom timeout
36    #[inline]
37    pub fn new(timeout: Duration) -> Self {
38        Self { timeout }
39    }
40}
41
42/// support wait
43impl Wait for TimeOutWait {
44    async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
45        Timer::after(self.timeout).await;
46        Ok(())
47    }
48
49    async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
50        Timer::after(self.timeout).await;
51        Ok(())
52    }
53
54    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
55        Timer::after(self.timeout).await;
56        Ok(())
57    }
58
59    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
60        Timer::after(self.timeout).await;
61        Ok(())
62    }
63
64    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
65        Timer::after(self.timeout).await;
66        Ok(())
67    }
68}