tm1637_embedded_hal/options/circles/
default_options.rs

1use crate::{
2    maybe_flipped::MaybeFlipped,
3    options::{repeat::RepeatDisplayOptions, scroll::Scroller, DisplayOptions},
4    TM1637,
5};
6
7use super::{bits::RotatingCircleBits, RotatingDirection};
8
9/// Default rotating circle options.
10///
11/// The animation consists of a single circle that rotates clockwise or counter-clockwise on a given position.
12#[derive(Debug)]
13#[cfg_attr(feature = "defmt", derive(defmt::Format))]
14pub struct RotatingCircleOptions<'d, const N: usize, T, CLK, DIO, DELAY, M> {
15    device: &'d mut TM1637<N, T, CLK, DIO, DELAY>,
16    position: usize,
17    delay_ms: u32,
18    direction: RotatingDirection,
19    _flip: M,
20}
21
22impl<'d, const N: usize, T, CLK, DIO, DELAY, M>
23    RotatingCircleOptions<'d, N, T, CLK, DIO, DELAY, M>
24{
25    /// Create a new [`RotatingCircleOptions`] instance.
26    pub const fn new(
27        device: &'d mut TM1637<N, T, CLK, DIO, DELAY>,
28        position: usize,
29        delay_ms: u32,
30        direction: RotatingDirection,
31        flip: M,
32    ) -> Self {
33        Self {
34            device,
35            position,
36            delay_ms,
37            direction,
38            _flip: flip,
39        }
40    }
41
42    /// Create a new [`RotatingCircleOptions`] instance with default settings.
43    pub const fn new_with_defaults(device: &'d mut TM1637<N, T, CLK, DIO, DELAY>, flip: M) -> Self {
44        Self::new(device, 0, 500, RotatingDirection::Clockwise, flip)
45    }
46
47    /// Set the starting position of the rotating circle.
48    pub const fn position(mut self, position: usize) -> Self {
49        self.position = position;
50        self
51    }
52
53    /// Set the delay in milliseconds between each animation step.
54    pub const fn delay_ms(mut self, delay_ms: u32) -> Self {
55        self.delay_ms = delay_ms;
56        self
57    }
58
59    /// Set the rotating direction.
60    pub const fn direction(mut self, direction: RotatingDirection) -> Self {
61        self.direction = direction;
62        self
63    }
64
65    /// Flip the display.
66    pub fn flip(self) -> RotatingCircleOptions<'d, N, T, CLK, DIO, DELAY, impl MaybeFlipped<N>>
67    where
68        M: MaybeFlipped<N>,
69    {
70        RotatingCircleOptions {
71            device: self.device,
72            position: self.position,
73            delay_ms: self.delay_ms,
74            direction: self.direction,
75            _flip: M::flip(),
76        }
77    }
78
79    /// Finish setting the rotating circle animation.
80    pub fn finish(
81        self,
82    ) -> Scroller<
83        'd,
84        N,
85        T,
86        CLK,
87        DIO,
88        DELAY,
89        impl Iterator<Item = impl DoubleEndedIterator<Item = u8> + ExactSizeIterator>,
90        M,
91    > {
92        let bytes = match self.direction {
93            RotatingDirection::Clockwise => RotatingCircleBits::all_u8_reversed(),
94            RotatingDirection::CounterClockwise => RotatingCircleBits::all_u8(),
95        };
96
97        RepeatDisplayOptions::new(
98            DisplayOptions {
99                device: self.device,
100                position: self.position,
101                iter: bytes.into_iter(),
102                _flip: self._flip,
103            },
104            self.delay_ms,
105        )
106        .finish()
107    }
108}