tm1637_embedded_hal/options/
clock.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::formatters::clock_to_4digits;

use super::DisplayOptions;

/// High-level API for setting a clock.
///
/// # Example
///
/// Display the time `14:28` on the display.
///
/// ```rust
/// use tm1637_embedded_hal::{mock::Noop, TM1637Builder};
///
/// let mut tm = TM1637Builder::new(Noop, Noop, Noop).build_blocking::<4>();
///
/// tm.options()
///     .clock()
///     .hour(14)
///     .minute(28)
///     .finish()
///     // Set the colon between the hours and minutes.
///     .dot(1)
///     .display()
///     .ok();
/// ```
///
/// The display will show:
///
/// ```text
/// +---+ +---+ +---+ +---+
/// | 1 | | 4 |:| 2 | | 8 |
/// +---+ +---+ +---+ +---+
/// ```
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ClockDisplayOptions<'d, const N: usize, T, CLK, DIO, DELAY, I, M> {
    options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>,
    hour: u8,
    minute: u8,
}

impl<'d, const N: usize, T, CLK, DIO, DELAY, I, M>
    ClockDisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>
{
    /// Create a new [`ClockDisplayOptions`] instance.
    pub fn new(options: DisplayOptions<'d, N, T, CLK, DIO, DELAY, I, M>) -> Self {
        Self {
            options,
            hour: 0,
            minute: 0,
        }
    }

    /// Set the hour.
    pub const fn hour(mut self, hour: u8) -> Self {
        self.hour = hour;
        self
    }

    /// Set the minute.
    pub const fn minute(mut self, minute: u8) -> Self {
        self.minute = minute;
        self
    }

    /// Finish setting the clock.
    pub fn finish(
        self,
    ) -> DisplayOptions<
        'd,
        N,
        T,
        CLK,
        DIO,
        DELAY,
        impl DoubleEndedIterator<Item = u8> + ExactSizeIterator,
        M,
    >
    where
        I: DoubleEndedIterator<Item = u8> + ExactSizeIterator,
    {
        self.options
            .iter(clock_to_4digits(self.hour, self.minute, false).into_iter())
    }
}