tm1637_embedded_hal/
builder.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
86
87
88
89
use crate::{
    mode::Mode,
    tokens::{Async, Blocking},
    Brightness, TM1637,
};

/// `TM1637` 7-segment display builder.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TM1637Builder<CLK, DIO, DELAY> {
    clk: CLK,
    dio: DIO,
    delay: DELAY,
    brightness: Brightness,
    delay_us: u32,
}

impl<CLK, DIO, DELAY> TM1637Builder<CLK, DIO, DELAY> {
    /// Create a new [`TM1637Builder`] instance with default values.
    ///
    /// - `brightness`: [`Brightness::L0`]
    /// - `delay_us`: 100
    pub const fn new(clk: CLK, dio: DIO, delay: DELAY) -> Self {
        Self {
            clk,
            dio,
            delay,
            brightness: Brightness::L0,
            delay_us: 100,
        }
    }

    /// Set the brightness level.
    pub fn brightness(mut self, brightness: Brightness) -> Self {
        self.brightness = brightness;
        self
    }

    /// Set the delay in microseconds.
    pub fn delay_us(mut self, delay_us: u32) -> Self {
        self.delay_us = delay_us;
        self
    }

    /// Build a [`TM1637`] instance with the specified mode.
    ///
    /// ## Async
    ///
    /// ```rust
    /// use tm1637_embedded_hal::{mock::Noop, tokens::Async, TM1637Builder};
    ///
    /// let clk = Noop;
    /// let dio = Noop;
    /// let delay = Noop;
    ///
    /// let tm = TM1637Builder::new(clk, dio, delay).build::<4, Async>();
    /// ```
    ///
    /// ## Blocking
    ///
    /// ```rust
    /// use tm1637_embedded_hal::{mock::Noop, tokens::Blocking, TM1637Builder};
    ///
    /// let clk = Noop;
    /// let dio = Noop;
    /// let delay = Noop;
    ///
    /// let tm = TM1637Builder::new(clk, dio, delay).build::<4, Blocking>();
    /// ```
    pub fn build<const N: usize, T: Mode>(self) -> TM1637<N, T, CLK, DIO, DELAY> {
        TM1637::new(
            self.clk,
            self.dio,
            self.delay,
            self.brightness,
            self.delay_us,
        )
    }

    /// Build an async [`TM1637`] instance.
    pub fn build_async<const N: usize>(self) -> TM1637<N, Async, CLK, DIO, DELAY> {
        self.build()
    }

    /// Build a blocking [`TM1637`] instance.
    pub fn build_blocking<const N: usize>(self) -> TM1637<N, Blocking, CLK, DIO, DELAY> {
        self.build()
    }
}