tm1637_embedded_hal/options/
repeat.rs

1use super::{scroll::Scroller, DisplayOptions};
2
3/// High-level API for repeat animations.
4///
5/// Display all bytes of the given iterator on the same position.
6///
7/// # Example
8///
9/// Repeat the string "HELLO" on the display.
10///
11/// ```rust
12/// use tm1637_embedded_hal::{mock::Noop, TM1637Builder};
13///
14/// let mut tm = TM1637Builder::new(Noop, Noop, Noop).build_blocking::<4>();
15///
16/// tm.options().str("HELLO").repeat().finish().run();
17/// ```
18///
19/// The display will show:
20///
21/// ```text
22/// +---+ +---+ +---+ +---+
23/// | H | |   | |   | |   |
24/// +---+ +---+ +---+ +---+
25///
26/// +---+ +---+ +---+ +---+
27/// | E | |   | |   | |   |
28/// +---+ +---+ +---+ +---+
29///
30/// +---+ +---+ +---+ +---+
31/// | L | |   | |   | |   |
32/// +---+ +---+ +---+ +---+
33///
34/// +---+ +---+ +---+ +---+
35/// | L | |   | |   | |   |
36/// +---+ +---+ +---+ +---+
37///
38/// +---+ +---+ +---+ +---+
39/// | O | |   | |   | |   |
40/// +---+ +---+ +---+ +---+
41/// ```
42#[derive(Debug)]
43#[cfg_attr(feature = "defmt", derive(defmt::Format))]
44pub struct RepeatDisplayOptions<'d, const N: usize, T, CLK, DIO, DELAY, I, D> {
45    options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, D>,
46    delay_ms: u32,
47}
48
49impl<'d, const N: usize, T, CLK, DIO, DELAY, I, M>
50    RepeatDisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>
51{
52    /// Create a new [`RepeatDisplayOptions`] instance.
53    pub const fn new(
54        options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>,
55        delay_ms: u32,
56    ) -> Self {
57        Self { options, delay_ms }
58    }
59
60    /// Create a new [`RepeatDisplayOptions`] instance with default settings.
61    pub const fn new_with_defaults(
62        options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>,
63    ) -> Self {
64        Self::new(options, 500)
65    }
66
67    /// Set the delay in milliseconds between each animation step.
68    pub const fn delay_ms(mut self, delay_ms: u32) -> Self {
69        self.delay_ms = delay_ms;
70        self
71    }
72
73    /// Finish setting the repeat animation.
74    pub fn finish(
75        self,
76    ) -> Scroller<
77        'd,
78        N,
79        T,
80        CLK,
81        DIO,
82        DELAY,
83        impl Iterator<Item = impl DoubleEndedIterator<Item = u8> + ExactSizeIterator>,
84        M,
85    >
86    where
87        I: DoubleEndedIterator<Item = u8> + ExactSizeIterator,
88    {
89        let iter = self.options.iter.map(move |i| [i]).map(|i| i.into_iter());
90
91        Scroller::new(
92            self.options.device,
93            1,
94            self.options.position,
95            self.delay_ms,
96            iter,
97            self.options._flip,
98        )
99    }
100}