tm1637_embedded_hal/options/circles/
default_options.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::{
    maybe_flipped::MaybeFlipped,
    options::{repeat::RepeatDisplayOptions, scroll::Scroller, DisplayOptions},
    TM1637,
};

use super::{bits::RotatingCircleBits, RotatingDirection};

/// Default rotating circle options.
///
/// The animation consists of a single circle that rotates clockwise or counter-clockwise on a given position.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RotatingCircleOptions<'d, const N: usize, T, CLK, DIO, DELAY, M> {
    device: &'d mut TM1637<N, T, CLK, DIO, DELAY>,
    position: usize,
    delay_ms: u32,
    direction: RotatingDirection,
    _flip: M,
}

impl<'d, const N: usize, T, CLK, DIO, DELAY, M>
    RotatingCircleOptions<'d, N, T, CLK, DIO, DELAY, M>
{
    /// Create a new [`RotatingCircleOptions`] instance.
    pub fn new(
        device: &'d mut TM1637<N, T, CLK, DIO, DELAY>,
        position: usize,
        delay_ms: u32,
        direction: RotatingDirection,
        flip: M,
    ) -> Self {
        Self {
            device,
            position,
            delay_ms,
            direction,
            _flip: flip,
        }
    }

    /// Create a new [`RotatingCircleOptions`] instance with default settings.
    pub fn new_with_defaults(device: &'d mut TM1637<N, T, CLK, DIO, DELAY>, flip: M) -> Self {
        Self::new(device, 0, 500, RotatingDirection::Clockwise, flip)
    }

    /// Set the starting position of the rotating circle.
    pub const fn position(mut self, position: usize) -> Self {
        self.position = position;
        self
    }

    /// Set the delay in milliseconds between each animation step.
    pub const fn delay_ms(mut self, delay_ms: u32) -> Self {
        self.delay_ms = delay_ms;
        self
    }

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

    /// Flip the display.
    pub fn flip(self) -> RotatingCircleOptions<'d, N, T, CLK, DIO, DELAY, impl MaybeFlipped<N>>
    where
        M: MaybeFlipped<N>,
    {
        RotatingCircleOptions {
            device: self.device,
            position: self.position,
            delay_ms: self.delay_ms,
            direction: self.direction,
            _flip: M::flip(),
        }
    }

    /// Finish setting the rotating circle animation.
    pub fn finish(
        self,
    ) -> Scroller<
        'd,
        N,
        T,
        CLK,
        DIO,
        DELAY,
        impl Iterator<Item = impl DoubleEndedIterator<Item = u8> + ExactSizeIterator>,
        M,
    > {
        let bytes = match self.direction {
            RotatingDirection::Clockwise => RotatingCircleBits::all_u8_reversed(),
            RotatingDirection::CounterClockwise => RotatingCircleBits::all_u8(),
        };

        RepeatDisplayOptions::new(
            DisplayOptions {
                device: self.device,
                position: self.position,
                iter: bytes.into_iter(),
                _flip: self._flip,
            },
            self.delay_ms,
        )
        .finish()
    }
}