tm1637_embedded_hal/demo/
asynch.rs

1//! Asynchronous demo module.
2//!
3//! This module is only available when the `demo` and `async` features of this
4//! library are activated.
5
6use embedded_hal::digital::OutputPin;
7use embedded_hal_async::delay::DelayNs;
8
9use crate::{asynch::TM1637, mappings::DigitBits};
10
11/// Asynchronous demo.
12pub struct Demo<CLK, DIO, DELAY, ERR>
13where
14    CLK: OutputPin<Error = ERR>,
15    DIO: OutputPin<Error = ERR>,
16    DELAY: DelayNs,
17{
18    device: TM1637<CLK, DIO, DELAY>,
19    delay: DELAY,
20}
21impl<CLK, DIO, DELAY, ERR> Demo<CLK, DIO, DELAY, ERR>
22where
23    ERR: core::fmt::Debug,
24    CLK: OutputPin<Error = ERR>,
25    DIO: OutputPin<Error = ERR>,
26    DELAY: DelayNs,
27{
28    /// Create a new demo instance.
29    pub fn new(device: TM1637<CLK, DIO, DELAY>, delay: DELAY) -> Self {
30        Self { device, delay }
31    }
32
33    /// Create a timer that counts down from 9 to 0 at the first position.
34    pub async fn timer(&mut self) -> Result<(), ERR> {
35        for i in (0..=9).rev() {
36            self.device
37                .write_segments_raw(0, &[DigitBits::from_digit(i) as u8])
38                .await?;
39            self.delay.delay_ms(1000).await;
40        }
41
42        self.device
43            .write_segments_raw(
44                0,
45                &[
46                    DigitBits::Zero,
47                    DigitBits::Zero,
48                    DigitBits::Zero,
49                    DigitBits::Zero,
50                ]
51                .map(|d| d as u8),
52            )
53            .await?;
54
55        for _ in 0..5 {
56            self.delay.delay_ms(300).await;
57            self.device.off().await?;
58            self.delay.delay_ms(300).await;
59            self.device.on().await?;
60        }
61
62        self.delay.delay_ms(300).await;
63
64        self.device.clear().await?;
65
66        Ok(())
67    }
68}