tm1637_embedded_hal/demo/
blocking.rs

1//! Blocking demo module.
2//!
3//! This module is only available when the `demo` and `blocking` features of this
4//! library are activated.
5
6use embedded_hal::{delay::DelayNs, digital::OutputPin};
7
8use crate::{
9    blocking::TM1637,
10    formatters::i16_to_4digits,
11    mappings::{DigitBits, LoCharBits, SegmentBits, SpecialCharBits, UpCharBits},
12};
13
14/// Blocking demo.
15pub struct Demo<CLK, DIO, DELAY, ERR>
16where
17    CLK: OutputPin<Error = ERR>,
18    DIO: OutputPin<Error = ERR>,
19    DELAY: DelayNs,
20{
21    device: TM1637<CLK, DIO, DELAY>,
22    delay: DELAY,
23    moving_delay_ms: u32,
24}
25
26impl<CLK, DIO, DELAY, ERR> Demo<CLK, DIO, DELAY, ERR>
27where
28    ERR: core::fmt::Debug,
29    CLK: OutputPin<Error = ERR>,
30    DIO: OutputPin<Error = ERR>,
31    DELAY: DelayNs,
32{
33    /// Create a new demo instance.
34    pub fn new(device: TM1637<CLK, DIO, DELAY>, delay: DELAY, moving_delay_ms: u32) -> Self {
35        Self {
36            device,
37            delay,
38            moving_delay_ms,
39        }
40    }
41
42    /// Move all segments across the display.
43    pub fn moving_segments(&mut self) -> Result<(), ERR> {
44        let mut all_seg_bits = [0; 13];
45        all_seg_bits[4..11].copy_from_slice(&SegmentBits::all_u8()[0..7]);
46        for _ in 0..11 {
47            all_seg_bits.rotate_left(1);
48            self.device.write_segments_raw(0, &all_seg_bits)?;
49            self.delay.delay_ms(self.moving_delay_ms);
50        }
51
52        Ok(())
53    }
54
55    /// Move all digits across the display.
56    pub fn moving_digits(&mut self) -> Result<(), ERR> {
57        let mut all_dig_bits = [0; 16];
58        all_dig_bits[4..14].copy_from_slice(&DigitBits::all_u8());
59        for _ in 0..14 {
60            all_dig_bits.rotate_left(1);
61            self.device.write_segments_raw(0, &all_dig_bits)?;
62            self.delay.delay_ms(self.moving_delay_ms);
63        }
64
65        Ok(())
66    }
67
68    /// Countdown from 100 to 0.
69    pub fn countdown(&mut self) -> Result<(), ERR> {
70        for i in (0..100).rev() {
71            self.device.write_segments_raw(0, &i16_to_4digits(i))?;
72            self.delay.delay_ms(self.moving_delay_ms / 10);
73        }
74
75        Ok(())
76    }
77
78    /// Move all uppercase characters across the display.
79    pub fn moving_up_chars(&mut self) -> Result<(), ERR> {
80        let mut all_up_char_bits = [0; 21];
81        all_up_char_bits[4..19].copy_from_slice(&UpCharBits::all_u8());
82        for _ in 0..19 {
83            all_up_char_bits.rotate_left(1);
84            self.device.write_segments_raw(0, &all_up_char_bits)?;
85            self.delay.delay_ms(self.moving_delay_ms);
86        }
87
88        Ok(())
89    }
90
91    /// Move all lowercase characters across the display.
92    pub fn moving_lo_chars(&mut self) -> Result<(), ERR> {
93        let mut all_lo_char_bits = [0; 21];
94        all_lo_char_bits[4..19].copy_from_slice(&LoCharBits::all_u8());
95        for _ in 0..19 {
96            all_lo_char_bits.rotate_left(1);
97            self.device.write_segments_raw(0, &all_lo_char_bits)?;
98            self.delay.delay_ms(self.moving_delay_ms);
99        }
100
101        Ok(())
102    }
103
104    /// Move all special characters across the display.
105    pub fn moving_special_chars(&mut self) -> Result<(), ERR> {
106        let mut all_sp_char_bits = [0; 11];
107        all_sp_char_bits[4..9].copy_from_slice(&SpecialCharBits::all_u8());
108        for _ in 0..9 {
109            all_sp_char_bits.rotate_left(1);
110            self.device.write_segments_raw(0, &all_sp_char_bits)?;
111            self.delay.delay_ms(self.moving_delay_ms);
112        }
113
114        Ok(())
115    }
116
117    /// Turn the display on and off.
118    pub fn on_off(&mut self, cycles: u32, on_off_delay_ms: u32) -> Result<(), ERR> {
119        for _ in 0..cycles {
120            self.device.off()?;
121            self.delay.delay_ms(on_off_delay_ms);
122            self.device.on()?;
123            self.delay.delay_ms(on_off_delay_ms);
124        }
125
126        Ok(())
127    }
128
129    /// Display the time and make the dots blink.
130    ///
131    /// Displays 19:06 with blinking dots.
132    pub fn time(&mut self, cycles: u32, blink_delay_ms: u32) -> Result<(), ERR> {
133        self.device.write_segments_raw(
134            0,
135            &[
136                DigitBits::One as u8,
137                DigitBits::Nine as u8 | SegmentBits::SegPoint as u8,
138                DigitBits::Zero as u8,
139                DigitBits::Six as u8,
140            ],
141        )?;
142
143        let mut show = true;
144        for _ in 0..cycles {
145            let byte = match show {
146                true => DigitBits::Nine as u8 | SegmentBits::SegPoint as u8,
147                false => DigitBits::Nine as u8,
148            };
149
150            self.device.write_segments_raw(1, &[byte])?;
151
152            self.delay.delay_ms(blink_delay_ms);
153
154            show = !show;
155        }
156
157        Ok(())
158    }
159
160    /// Create a rotating circle animation.
161    ///
162    /// Creates a rotating circle at address 0.
163    pub fn rotating_circle(&mut self, cycles: u32, rotating_delay_ms: u32) -> Result<(), ERR> {
164        // First of all we create the shapes we want to animate
165
166        //  ---
167        // |   |
168        // |
169        //  ---
170        // This shape consists of these segments: B, A, F, E and D.
171        // Let's create the shape
172        let shape_1 = SegmentBits::SegB as u8
173            | SegmentBits::SegA as u8
174            | SegmentBits::SegF as u8
175            | SegmentBits::SegE as u8
176            | SegmentBits::SegD as u8;
177
178        //  ---
179        // |
180        // |   |
181        //  ---
182        // This shape consists of these segments: A, F, E, D and C.
183        let shape_2 = SegmentBits::SegA as u8
184            | SegmentBits::SegF as u8
185            | SegmentBits::SegE as u8
186            | SegmentBits::SegD as u8
187            | SegmentBits::SegC as u8;
188
189        // and so on...
190
191        //
192        // |   |
193        // |   |
194        //  ---
195        let shape_3 = SegmentBits::SegF as u8
196            | SegmentBits::SegE as u8
197            | SegmentBits::SegD as u8
198            | SegmentBits::SegC as u8
199            | SegmentBits::SegB as u8;
200
201        //  ---
202        //     |
203        // |   |
204        //  ---
205        let shape_4 = SegmentBits::SegE as u8
206            | SegmentBits::SegD as u8
207            | SegmentBits::SegC as u8
208            | SegmentBits::SegB as u8
209            | SegmentBits::SegA as u8;
210
211        //  ---
212        // |   |
213        //     |
214        //  ---
215        let shape_5 = SegmentBits::SegD as u8
216            | SegmentBits::SegC as u8
217            | SegmentBits::SegB as u8
218            | SegmentBits::SegA as u8
219            | SegmentBits::SegF as u8;
220
221        //  ---
222        // |   |
223        // |   |
224        //
225        let shape_6 = SegmentBits::SegC as u8
226            | SegmentBits::SegB as u8
227            | SegmentBits::SegA as u8
228            | SegmentBits::SegF as u8
229            | SegmentBits::SegE as u8;
230
231        let mut shapes = [shape_1, shape_2, shape_3, shape_4, shape_5, shape_6];
232        for _ in 0..cycles {
233            shapes.rotate_left(1);
234            self.device.write_segments_raw(0, &shapes[0..1]).unwrap();
235            self.delay.delay_ms(rotating_delay_ms);
236        }
237
238        Ok(())
239    }
240}