tm1637_embedded_hal/
device.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Device definition and implementation.

use duplicate::duplicate_item;

/// Identity trait.
///
/// Used to trick the compiler while using [`duplicate_item`] to implement `async` and `blocking` versions of the same module.
/// Using this trait, we can write normal rust code that can also be formatted by `rustfmt`.
#[cfg(any(feature = "async", feature = "blocking"))]
trait Identity: Sized {
    fn identity(self) -> Self {
        self
    }
}

#[cfg(any(feature = "async", feature = "blocking"))]
impl<T: Sized> Identity for T {}

#[duplicate_item(
    feature_        module        async     await               delay_trait;
    ["async"]       [asynch]      [async]   [await.identity()]  [embedded_hal_async::delay::DelayNs];
    ["blocking"]    [blocking]    []        [identity()]        [embedded_hal::delay::DelayNs];
)]
pub mod module {
    //! Device definition and implementation.

    #[cfg(feature=feature_)]
    mod inner {
        use super::super::Identity;
        use crate::brightness::{Brightness, DisplayState};
        use embedded_hal::digital::OutputPin;

        /// `TM1637` 7-segment display builder.
        #[derive(Clone)]
        #[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
        #[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
        pub struct TM1637Builder<CLK, DIO, DELAY> {
            /// The inner [`TM1637`] instance.
            inner: TM1637<CLK, DIO, DELAY>,
        }

        impl<CLK, DIO, DELAY> TM1637Builder<CLK, DIO, DELAY> {
            /// Create a new [`TM1637Builder`] instance.
            pub fn new(clk: CLK, dio: DIO, delay: DELAY) -> Self {
                Self {
                    inner: TM1637 {
                        clk,
                        dio,
                        delay,
                        brightness: Brightness::L0,
                        delay_us: 10,
                        num_positions: 4,
                    },
                }
            }

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

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

            /// Set the number of positions on the display.
            pub fn num_positions(mut self, num_positions: u8) -> Self {
                self.inner.num_positions = num_positions;
                self
            }

            /// Build the [`TM1637`] instance.
            pub fn build(self) -> TM1637<CLK, DIO, DELAY> {
                self.inner
            }
        }

        /// `TM1637` 7-segment display driver.
        #[derive(Clone)]
        #[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
        #[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
        pub struct TM1637<CLK, DIO, DELAY> {
            /// Clock.
            clk: CLK,
            /// Data input/output.
            dio: DIO,
            /// Delay provider.
            delay: DELAY,
            /// Brightness level.
            brightness: Brightness,
            /// The delay in microseconds.
            ///
            /// Experiment with this value to find the best value for your display.
            delay_us: u32,
            /// The number of positions on the display.
            num_positions: u8,
        }

        impl<CLK, DIO, DELAY, ERR> TM1637<CLK, DIO, DELAY>
        where
            CLK: OutputPin<Error = ERR>,
            DIO: OutputPin<Error = ERR>,
            DELAY: delay_trait,
        {
            /// Create a new [`TM1637`] instance.
            pub fn new(
                clk: CLK,
                dio: DIO,
                delay: DELAY,
                brightness: Brightness,
                delay_us: u32,
                num_positions: u8,
            ) -> Self {
                Self {
                    clk,
                    dio,
                    delay,
                    brightness,
                    delay_us,
                    num_positions,
                }
            }

            /// Create a new [`TM1637Builder`] instance.
            pub fn builder(clk: CLK, dio: DIO, delay: DELAY) -> TM1637Builder<CLK, DIO, DELAY> {
                TM1637Builder::new(clk, dio, delay)
            }

            /// Send a byte to the display and wait for the ACK.
            async fn write_byte(&mut self, byte: u8) -> Result<(), ERR> {
                let mut rest = byte;

                for _ in 0..8 {
                    self.bit_delay().await;
                    tri!(self.clk.set_low());
                    self.bit_delay().await;

                    match rest & 0x01 {
                        1 => tri!(self.dio.set_high()),
                        _ => tri!(self.dio.set_low()),
                    }

                    self.bit_delay().await;
                    tri!(self.clk.set_high());
                    self.bit_delay().await;

                    rest >>= 1;
                }

                tri!(self.clk.set_low());
                tri!(self.dio.set_high());
                self.bit_delay().await;

                tri!(self.clk.set_high());
                self.bit_delay().await;

                tri!(self.clk.set_low());
                self.bit_delay().await;

                Ok(())
            }

            /// Write the `cmd` to the display.
            async fn write_cmd_raw(&mut self, cmd: u8) -> Result<(), ERR> {
                tri!(self.start().await);
                tri!(self.write_byte(cmd).await);
                tri!(self.stop().await);

                Ok(())
            }

            /// Start the communication with the display.
            async fn start(&mut self) -> Result<(), ERR> {
                tri!(self.dio.set_low());
                self.bit_delay().await;
                tri!(self.clk.set_low());
                self.bit_delay().await;

                Ok(())
            }

            /// Stop the communication with the display.
            async fn stop(&mut self) -> Result<(), ERR> {
                tri!(self.dio.set_low());
                self.bit_delay().await;
                tri!(self.clk.set_high());
                self.bit_delay().await;
                tri!(self.dio.set_high());
                self.bit_delay().await;

                Ok(())
            }

            /// Delay for [`TM1637::delay_us`] microseconds using [`TM1637::delay`] provider.
            async fn bit_delay(&mut self) {
                self.delay.delay_us(self.delay_us).await;
            }

            /// Initialize the display.
            ///
            /// Clear the display and set the brightness level.
            pub async fn init(&mut self) -> Result<(), ERR> {
                tri!(self.clear().await);
                self.write_cmd_raw(self.brightness as u8).await
            }

            /// Turn the display on.
            pub async fn on(&mut self) -> Result<(), ERR> {
                self.write_cmd_raw(self.brightness as u8).await
            }

            /// Turn the display off.
            pub async fn off(&mut self) -> Result<(), ERR> {
                self.write_cmd_raw(DisplayState::Off as u8).await
            }

            /// Clear the display.
            pub async fn clear(&mut self) -> Result<(), ERR> {
                self.write_segments_raw_iter(
                    0,
                    core::iter::repeat(0).take(self.num_positions as usize),
                )
                .await
            }

            /// Write the given bytes to the display starting from the given position.
            ///
            /// See [`TM1637::write_segments_raw_iter`].
            pub async fn write_segments_raw(
                &mut self,
                position: u8,
                bytes: &[u8],
            ) -> Result<(), ERR> {
                self.write_segments_raw_iter(position, bytes.iter().copied())
                    .await
            }

            /// Write the given bytes to the display starting from the given position.
            ///
            /// # Notes:
            /// - Positions greater than [`TM1637::num_positions`] will be ignored.
            /// - Bytes with index greater than [`TM1637::num_positions`] will be ignored.
            ///
            /// Brightness level will not be written to the device on each call. Make sure to call [`TM1637::write_brightness`] or [`TM1637::init`] to set the brightness level.
            pub async fn write_segments_raw_iter<ITER: Iterator<Item = u8>>(
                &mut self,
                position: u8,
                bytes: ITER,
            ) -> Result<(), ERR> {
                #[cfg(not(feature = "disable-checks"))]
                if position >= self.num_positions {
                    return Ok(());
                }

                // COMM1
                tri!(self.write_cmd_raw(0x40).await);

                // COMM2
                tri!(self.start().await);
                tri!(self.write_byte(0xc0 | (position & 0x03)).await);

                #[cfg(not(feature = "disable-checks"))]
                let bytes = bytes.take(self.num_positions as usize - position as usize);

                for byte in bytes {
                    tri!(self.write_byte(byte).await);
                }

                tri!(self.stop().await);

                Ok(())
            }

            /// Set [`TM1637::brightness`] and write the brightness level to the display.
            pub async fn write_brightness(&mut self, brightness: Brightness) -> Result<(), ERR> {
                self.brightness = brightness;
                self.write_cmd_raw(brightness as u8).await
            }

            /// Move all segments across the display starting and ending at `position`.
            ///
            /// If the length of the bytes is less than or equal to [`TM1637::num_positions`] - `position`, the bytes will only be written to the display.
            ///
            /// `N` is the size of the internal window used to move the segments. Please make sure that `N` is equal to [`TM1637::num_positions`].
            /// [`TM1637::num_positions`] will be removed in the future in favor of a constant generic parameter representing the number of positions.
            pub async fn move_segments_raw<const N: usize>(
                &mut self,
                position: u8,
                bytes: &[u8],
                delay_ms: u32,
            ) -> Result<(), ERR> {
                let num_positions = self.num_positions as usize;

                if bytes.len() <= num_positions - position as usize {
                    return self.write_segments_raw(position, bytes).await;
                }

                for i in 0..=bytes.len() {
                    let mut window = [0u8; N];
                    for j in 0..num_positions {
                        window[j] = bytes[(i + j) % bytes.len()];
                    }

                    tri!(self.write_segments_raw(position, &window).await);

                    self.delay.delay_ms(delay_ms).await;
                }

                Ok(())
            }

            /// Convert an `ASCII` string to a byte iterator using [`from_ascii_byte`](crate::mappings::from_ascii_byte) and write the segments to the display using [`TM1637::write_segments_raw_iter`].
            ///
            /// Only available when the `mappings` feature of this library is activated.
            ///
            /// # Example
            ///
            /// Write the string `"Err"` to the display:
            ///
            /// ```rust, ignore
            /// let mut tm = TM1637::builder(clk_pin, dio_pin, delay)
            ///    .brightness(Brightness::L3)
            ///    .build();
            ///
            /// tm.init().ok();
            ///
            /// tm.write_ascii_str(0, "Err").ok();
            /// ```
            ///
            /// On a `4-digit display`, this will look like this:
            ///
            /// ```text
            /// +---+ +---+ +---+ +---+
            /// | E | | r | | r | |   |
            /// +---+ +---+ +---+ +---+
            /// ```
            #[cfg(feature = "mappings")]
            pub async fn write_ascii_str(
                &mut self,
                position: u8,
                ascii_str: &str,
            ) -> Result<(), ERR> {
                let iter = ascii_str
                    .as_bytes()
                    .iter()
                    .take(self.num_positions as usize - position as usize)
                    .copied()
                    .map(crate::mappings::from_ascii_byte);

                self.write_segments_raw_iter(position, iter).await
            }

            /// Convert an `ASCII` string to a byte array using [`from_ascii_byte`](crate::mappings::from_ascii_byte) and move the segments across the display using [`TM1637::move_segments_raw`].
            ///
            /// - `N` is the size of the internal window used to move the segments. See [`TM1637::move_segments_raw`] for more information.
            /// - `M` is the maximum number of bytes that can be converted from the `ASCII` string.
            ///
            /// Only available when the `mappings` feature of this library is activated.
            ///
            /// # Example
            ///
            /// Move the string `"HELLO "` across a `4-digit display`:
            ///
            /// ```rust, ignore
            /// let mut tm = TM1637::builder(clk_pin, dio_pin, delay)
            ///    .brightness(Brightness::L3)
            ///    .build();
            ///
            /// tm.init().ok();
            ///
            /// // 4 is the actual number of positions on the display.
            /// // 6 is the maximum number of bytes that can be converted from the ASCII string.
            /// // In case of "HELLO ", all six bytes will be converted.
            /// tm.move_ascii_str::<4, 6>(0, "HELLO ", 500).ok();
            /// ```
            ///
            /// On a `4-digit display`, this will look like this:
            ///
            /// ```text
            /// +---+ +---+ +---+ +---+
            /// | H | | E | | L | | L |
            /// +---+ +---+ +---+ +---+
            ///
            /// +---+ +---+ +---+ +---+
            /// | E | | L | | L | | O |
            /// +---+ +---+ +---+ +---+
            ///
            /// +---+ +---+ +---+ +---+
            /// | L | | L | | O | |   |
            /// +---+ +---+ +---+ +---+
            ///
            /// +---+ +---+ +---+ +---+
            /// | L | | O | |   | | H |
            /// +---+ +---+ +---+ +---+
            ///
            /// +---+ +---+ +---+ +---+
            /// | O | |   | | H | | E |
            /// +---+ +---+ +---+ +---+
            ///
            /// +---+ +---+ +---+ +---+
            /// |   | | H | | E | | L |
            /// +---+ +---+ +---+ +---+
            ///
            /// +---+ +---+ +---+ +---+
            /// | H | | E | | L | | L |
            /// +---+ +---+ +---+ +---+
            /// ```
            #[cfg(feature = "mappings")]
            pub async fn move_ascii_str<const N: usize, const M: usize>(
                &mut self,
                position: u8,
                ascii_str: &str,
                delay_ms: u32,
            ) -> Result<(), ERR> {
                let mut bytes = [0u8; M];
                ascii_str
                    .as_bytes()
                    .iter()
                    .take(M)
                    .enumerate()
                    .for_each(|(i, &byte)| bytes[i] = crate::mappings::from_ascii_byte(byte));

                self.move_segments_raw::<N>(position, &bytes, delay_ms)
                    .await
            }
        }
    }

    #[cfg(feature=feature_)]
    pub use inner::*;
}