tm1637_embedded_hal/options/
clock.rs

1use crate::formatters::clock_to_4digits;
2
3use super::DisplayOptions;
4
5/// High-level API for setting a clock.
6///
7/// # Example
8///
9/// Display the time `14:28` 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()
17///     .clock()
18///     .hour(14)
19///     .minute(28)
20///     .finish()
21///     // Set the colon between the hours and minutes.
22///     .dot(1)
23///     .display()
24///     .ok();
25/// ```
26///
27/// The display will show:
28///
29/// ```text
30/// +---+ +---+ +---+ +---+
31/// | 1 | | 4 |:| 2 | | 8 |
32/// +---+ +---+ +---+ +---+
33/// ```
34#[derive(Debug)]
35#[cfg_attr(feature = "defmt", derive(defmt::Format))]
36pub struct ClockDisplayOptions<'d, const N: usize, T, CLK, DIO, DELAY, I, M> {
37    options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>,
38    hour: u8,
39    minute: u8,
40}
41
42impl<'d, const N: usize, T, CLK, DIO, DELAY, I, M>
43    ClockDisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>
44{
45    /// Create a new [`ClockDisplayOptions`] instance.
46    pub const fn new(options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>) -> Self {
47        Self {
48            options,
49            hour: 0,
50            minute: 0,
51        }
52    }
53
54    /// Set the hour.
55    pub const fn hour(mut self, hour: u8) -> Self {
56        self.hour = hour;
57        self
58    }
59
60    /// Set the minute.
61    pub const fn minute(mut self, minute: u8) -> Self {
62        self.minute = minute;
63        self
64    }
65
66    /// Finish setting the clock.
67    pub fn finish(
68        self,
69    ) -> DisplayOptions<
70        'd,
71        N,
72        T,
73        CLK,
74        DIO,
75        DELAY,
76        impl DoubleEndedIterator<Item = u8> + ExactSizeIterator,
77        M,
78    >
79    where
80        I: DoubleEndedIterator<Item = u8> + ExactSizeIterator,
81    {
82        self.options
83            .iter(clock_to_4digits(self.hour, self.minute, false).into_iter())
84    }
85}