tm1637_embedded_hal/
mappings.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Mappings for 7-segment display characters.
//!
//! This module is only available when the `mappings` feature of this
//! library is activated.

//       A
//      ---
//  F  |   |  B
//      -G-
//  E  |   |  C
//      ---
//       D

/// Maps the segment from the device to its bit.
#[repr(u8)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
#[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
pub enum SegmentBits {
    /// A segment
    SegA = 0b00000001,
    /// B segment
    SegB = 0b00000010,
    /// C segment
    SegC = 0b00000100,
    /// D segment
    SegD = 0b00001000,
    /// E segment
    SegE = 0b00010000,
    /// F segment
    SegF = 0b00100000,
    /// G segment
    SegG = 0b01000000,
    /// Double point
    ///
    /// ## Usage
    /// `Or` this bit with the bit responsible for displaying the double point. Often second position.
    SegPoint = 0b10000000,
}

impl SegmentBits {
    /// Returns all segments.
    pub const fn all() -> [SegmentBits; 8] {
        [
            SegmentBits::SegA,
            SegmentBits::SegB,
            SegmentBits::SegC,
            SegmentBits::SegD,
            SegmentBits::SegE,
            SegmentBits::SegF,
            SegmentBits::SegG,
            SegmentBits::SegPoint,
        ]
    }

    /// Returns all segments as u8.
    pub const fn all_u8() -> [u8; 8] {
        [
            SegmentBits::SegA as u8,
            SegmentBits::SegB as u8,
            SegmentBits::SegC as u8,
            SegmentBits::SegD as u8,
            SegmentBits::SegE as u8,
            SegmentBits::SegF as u8,
            SegmentBits::SegG as u8,
            SegmentBits::SegPoint as u8,
        ]
    }
}

/// Maps a digit to its closest possible representation on a 7-segment display.
#[repr(u8)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
#[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
pub enum DigitBits {
    /// 0
    Zero = 0b00111111,
    /// 1
    One = 0b00000110,
    /// 2
    Two = 0b01011011,
    /// 3
    Three = 0b01001111,
    /// 4
    Four = 0b01100110,
    /// 5
    Five = 0b01101101,
    /// 6
    Six = 0b01111101,
    /// 7
    Seven = 0b00000111,
    /// 8
    Eight = 0b01111111,
    /// 9
    Nine = 0b01101111,
}

impl DigitBits {
    /// Returns all digits.
    pub const fn all() -> [DigitBits; 10] {
        [
            DigitBits::Zero,
            DigitBits::One,
            DigitBits::Two,
            DigitBits::Three,
            DigitBits::Four,
            DigitBits::Five,
            DigitBits::Six,
            DigitBits::Seven,
            DigitBits::Eight,
            DigitBits::Nine,
        ]
    }

    /// Returns all digits as [`u8`].
    pub const fn all_u8() -> [u8; 10] {
        [
            DigitBits::Zero as u8,
            DigitBits::One as u8,
            DigitBits::Two as u8,
            DigitBits::Three as u8,
            DigitBits::Four as u8,
            DigitBits::Five as u8,
            DigitBits::Six as u8,
            DigitBits::Seven as u8,
            DigitBits::Eight as u8,
            DigitBits::Nine as u8,
        ]
    }

    /// Creates a new [`DigitBits`] from a [`u8`] digit.
    pub fn from_digit(digit: u8) -> Self {
        match digit {
            0 => DigitBits::Zero,
            1 => DigitBits::One,
            2 => DigitBits::Two,
            3 => DigitBits::Three,
            4 => DigitBits::Four,
            5 => DigitBits::Five,
            6 => DigitBits::Six,
            7 => DigitBits::Seven,
            8 => DigitBits::Eight,
            9 => DigitBits::Nine,
            _ => DigitBits::Zero,
        }
    }
}

/// Maps a upside-down digit to its closest possible representation on a 7-segment display.
#[repr(u8)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
#[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
pub enum UpsideDownDigitBits {
    /// Upside-down 0
    Zero = 0b00111111,
    /// Upside-down 1
    One = 0b00110000,
    /// Upside-down 2
    Two = 0b01011011,
    /// Upside-down 3
    Three = 0b01111001,
    /// Upside-down 4
    Four = 0b01110100,
    /// Upside-down 5
    Five = 0b01101101,
    /// Upside-down 6
    Six = 0b01101111,
    /// Upside-down 7
    Seven = 0b00111000,
    /// Upside-down 8
    Eight = 0b01111111,
    /// Upside-down 9
    Nine = 0b01111101,
}
impl UpsideDownDigitBits {
    /// Returns all digits.
    pub const fn all() -> [UpsideDownDigitBits; 10] {
        [
            UpsideDownDigitBits::Zero,
            UpsideDownDigitBits::One,
            UpsideDownDigitBits::Two,
            UpsideDownDigitBits::Three,
            UpsideDownDigitBits::Four,
            UpsideDownDigitBits::Five,
            UpsideDownDigitBits::Six,
            UpsideDownDigitBits::Seven,
            UpsideDownDigitBits::Eight,
            UpsideDownDigitBits::Nine,
        ]
    }

    /// Returns all digits as [`u8`].
    pub const fn all_u8() -> [u8; 10] {
        [
            UpsideDownDigitBits::Zero as u8,
            UpsideDownDigitBits::One as u8,
            UpsideDownDigitBits::Two as u8,
            UpsideDownDigitBits::Three as u8,
            UpsideDownDigitBits::Four as u8,
            UpsideDownDigitBits::Five as u8,
            UpsideDownDigitBits::Six as u8,
            UpsideDownDigitBits::Seven as u8,
            UpsideDownDigitBits::Eight as u8,
            UpsideDownDigitBits::Nine as u8,
        ]
    }

    /// Creates a new [`DigitBits`] from a [`u8`] digit.
    pub fn from_digit(digit: u8) -> Self {
        match digit {
            0 => UpsideDownDigitBits::Zero,
            1 => UpsideDownDigitBits::One,
            2 => UpsideDownDigitBits::Two,
            3 => UpsideDownDigitBits::Three,
            4 => UpsideDownDigitBits::Four,
            5 => UpsideDownDigitBits::Five,
            6 => UpsideDownDigitBits::Six,
            7 => UpsideDownDigitBits::Seven,
            8 => UpsideDownDigitBits::Eight,
            9 => UpsideDownDigitBits::Nine,
            _ => UpsideDownDigitBits::Zero,
        }
    }
}

/// Maps a character to its closest possible representation on a 7-segment display.
#[repr(u8)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
#[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
pub enum UpCharBits {
    /// Uppercase A
    UpA = 0x77,
    /// Uppercase B
    UpB = 0x7F,
    /// Uppercase C
    UpC = 0x39,
    /// Uppercase E
    UpE = 0x79,
    // can be also done like this (OR'ing segment bits) :
    /// Uppercase F
    UpF = SegmentBits::SegA as u8
        | SegmentBits::SegF as u8
        | SegmentBits::SegE as u8
        | SegmentBits::SegG as u8,
    /// Uppercase G
    UpG = 0x3D,
    /// Uppercase H
    UpH = 0x76,
    /// Uppercase I
    UpI = 0x30,
    /// Uppercase J
    UpJ = 0x1E,
    /// Uppercase L
    UpL = 0x38,
    /// Uppercase O
    UpO = 0x3F,
    /// Uppercase P
    UpP = 0x73,
    /// Uppercase S
    UpS = 0x6D,
    /// Uppercase U
    UpU = 0x3E,
    /// Uppercase Z
    UpZ = 0x5B,
}

impl UpCharBits {
    /// Returns all uppercase characters.
    pub const fn all() -> [UpCharBits; 15] {
        [
            UpCharBits::UpA,
            UpCharBits::UpB,
            UpCharBits::UpC,
            UpCharBits::UpE,
            UpCharBits::UpF,
            UpCharBits::UpG,
            UpCharBits::UpH,
            UpCharBits::UpI,
            UpCharBits::UpJ,
            UpCharBits::UpL,
            UpCharBits::UpO,
            UpCharBits::UpP,
            UpCharBits::UpS,
            UpCharBits::UpU,
            UpCharBits::UpZ,
        ]
    }

    /// Returns all uppercase characters as [`u8`].
    pub const fn all_u8() -> [u8; 15] {
        [
            UpCharBits::UpA as u8,
            UpCharBits::UpB as u8,
            UpCharBits::UpC as u8,
            UpCharBits::UpE as u8,
            UpCharBits::UpF as u8,
            UpCharBits::UpG as u8,
            UpCharBits::UpH as u8,
            UpCharBits::UpI as u8,
            UpCharBits::UpJ as u8,
            UpCharBits::UpL as u8,
            UpCharBits::UpO as u8,
            UpCharBits::UpP as u8,
            UpCharBits::UpS as u8,
            UpCharBits::UpU as u8,
            UpCharBits::UpZ as u8,
        ]
    }
}

/// Maps a character to its closest possible representation on a 7-segment display.
#[repr(u8)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
#[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
pub enum LoCharBits {
    /// Lowercase A
    LoA = 0x5F,
    /// Lowercase B
    LoB = 0x7C,
    /// Lowercase C
    LoC = 0x58,
    /// Lowercase D
    LoD = 0x5E,
    /// Lowercase e
    LoE = 0x7B,
    /// Lowercase G
    LoG = 0x6F,
    /// Lowercase H
    LoH = 0x74,
    /// Lowercase I
    LoI = 0x10,
    /// Lowercase N
    LoN = 0x54,
    /// Lowercase O
    LoO = 0x5C,
    /// Lowercase Q
    LoQ = 0x67,
    /// Lowercase R
    LoR = 0x50,
    /// Lowercase T
    LoT = 0x78,
    /// Lowercase U
    LoU = 0x1C,
    /// Lowercase Y
    LoY = 0x6E,
}

impl LoCharBits {
    /// Returns all lowercase characters.
    pub const fn all() -> [LoCharBits; 15] {
        [
            LoCharBits::LoA,
            LoCharBits::LoB,
            LoCharBits::LoC,
            LoCharBits::LoD,
            LoCharBits::LoE,
            LoCharBits::LoG,
            LoCharBits::LoH,
            LoCharBits::LoI,
            LoCharBits::LoN,
            LoCharBits::LoO,
            LoCharBits::LoQ,
            LoCharBits::LoR,
            LoCharBits::LoT,
            LoCharBits::LoU,
            LoCharBits::LoY,
        ]
    }

    /// Returns all lowercase characters as [`u8`].
    pub const fn all_u8() -> [u8; 15] {
        [
            LoCharBits::LoA as u8,
            LoCharBits::LoB as u8,
            LoCharBits::LoC as u8,
            LoCharBits::LoD as u8,
            LoCharBits::LoE as u8,
            LoCharBits::LoG as u8,
            LoCharBits::LoH as u8,
            LoCharBits::LoI as u8,
            LoCharBits::LoN as u8,
            LoCharBits::LoO as u8,
            LoCharBits::LoQ as u8,
            LoCharBits::LoR as u8,
            LoCharBits::LoT as u8,
            LoCharBits::LoU as u8,
            LoCharBits::LoY as u8,
        ]
    }
}

/// Maps a character to its closest possible representation on a 7-segment display.
#[repr(u8)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "impl-defmt-format", derive(defmt::Format))]
#[cfg_attr(feature = "impl-debug", derive(core::fmt::Debug))]
pub enum SpecialCharBits {
    /// Space symbol
    Space = 0,
    /// Minus or dash symbol
    Minus = SegmentBits::SegG as u8,
    /// Underscore (_)
    Underscore = SegmentBits::SegD as u8,
    /// Equal sign (=)
    Equals = SegmentBits::SegG as u8 | SegmentBits::SegD as u8,
    /// Question mark (?)
    QuestionMark = SegmentBits::SegA as u8
        | SegmentBits::SegB as u8
        | SegmentBits::SegG as u8
        | SegmentBits::SegE as u8,
}

impl SpecialCharBits {
    /// Returns all special characters.
    pub const fn all() -> [SpecialCharBits; 5] {
        [
            SpecialCharBits::Space,
            SpecialCharBits::Minus,
            SpecialCharBits::Underscore,
            SpecialCharBits::Equals,
            SpecialCharBits::QuestionMark,
        ]
    }

    /// Returns all special characters as [`u8`].
    pub const fn all_u8() -> [u8; 5] {
        [
            SpecialCharBits::Space as u8,
            SpecialCharBits::Minus as u8,
            SpecialCharBits::Underscore as u8,
            SpecialCharBits::Equals as u8,
            SpecialCharBits::QuestionMark as u8,
        ]
    }
}

/// Flips the segments of a byte upside down.
///
/// Swaps the segments:
/// - A and D
/// - B and C
/// - E and F
pub const fn flip(byte: u8) -> u8 {
    let a_d_swapped = ((byte & 0b00001000) >> 3) | ((byte & 0b00000001) << 3);
    let b_c_swapped = ((byte & 0b00000100) >> 1) | ((byte & 0b00000010) << 1);
    let e_f_swapped = ((byte & 0b00100000) >> 1) | ((byte & 0b00010000) << 1);

    (byte & 0b11000000) | a_d_swapped | b_c_swapped | e_f_swapped
}

/// Mirrors the segments of a byte.
///
/// Swaps the segments:
/// - B and F
/// - C and E
pub const fn mirror(byte: u8) -> u8 {
    let b_f_swapped = ((byte & 0b00100000) >> 4) | ((byte & 0b00000010) << 4);
    let c_e_swapped = ((byte & 0b00010000) >> 2) | ((byte & 0b00000100) << 2);

    (byte & 0b11001001) | b_f_swapped | c_e_swapped
}

/// Flips and mirrors the segments of a byte.
///
/// See [`flip`] and [`mirror`] for more information.
pub const fn flip_mirror(byte: u8) -> u8 {
    mirror(flip(byte))
}

/// Converts an `ASCII` byte to a 7-segment display byte.
///
/// Unknown characters are converted to `0` (all segments off).
///
/// # Note
///
/// Rust strings are `UTF-8` encoded, so what you see as a single character may be multiple bytes.
///
/// # Example
///
/// Display `Err` text on a 4-digit display:
///
/// ```rust,ignore
/// let err = "Err".as_bytes().iter().copied().map(from_ascii_byte);
/// tm.write_segments_raw_iter(0, err).ok();
/// ```
pub const fn from_ascii_byte(byte: u8) -> u8 {
    match byte {
        b'0' => DigitBits::Zero as u8,
        b'1' => DigitBits::One as u8,
        b'2' => DigitBits::Two as u8,
        b'3' => DigitBits::Three as u8,
        b'4' => DigitBits::Four as u8,
        b'5' => DigitBits::Five as u8,
        b'6' => DigitBits::Six as u8,
        b'7' => DigitBits::Seven as u8,
        b'8' => DigitBits::Eight as u8,
        b'9' => DigitBits::Nine as u8,

        b'A' => UpCharBits::UpA as u8,
        b'B' => UpCharBits::UpB as u8,
        b'C' => UpCharBits::UpC as u8,
        b'E' => UpCharBits::UpE as u8,
        b'F' => UpCharBits::UpF as u8,
        b'G' => UpCharBits::UpG as u8,
        b'H' => UpCharBits::UpH as u8,
        b'I' => UpCharBits::UpI as u8,
        b'J' => UpCharBits::UpJ as u8,
        b'L' => UpCharBits::UpL as u8,
        b'O' => UpCharBits::UpO as u8,
        b'P' => UpCharBits::UpP as u8,
        b'S' => UpCharBits::UpS as u8,
        b'U' => UpCharBits::UpU as u8,
        b'Z' => UpCharBits::UpZ as u8,

        b'a' => LoCharBits::LoA as u8,
        b'b' => LoCharBits::LoB as u8,
        b'c' => LoCharBits::LoC as u8,
        b'd' => LoCharBits::LoD as u8,
        b'e' => LoCharBits::LoE as u8,
        b'g' => LoCharBits::LoG as u8,
        b'h' => LoCharBits::LoH as u8,
        b'i' => LoCharBits::LoI as u8,
        b'n' => LoCharBits::LoN as u8,
        b'o' => LoCharBits::LoO as u8,
        b'q' => LoCharBits::LoQ as u8,
        b'r' => LoCharBits::LoR as u8,
        b't' => LoCharBits::LoT as u8,
        b'u' => LoCharBits::LoU as u8,
        b'y' => LoCharBits::LoY as u8,

        b' ' => SpecialCharBits::Space as u8,
        b'-' => SpecialCharBits::Minus as u8,
        b'_' => SpecialCharBits::Underscore as u8,
        b'=' => SpecialCharBits::Equals as u8,
        b'?' => SpecialCharBits::QuestionMark as u8,

        _ => 0,
    }
}

/// Converts a `char` to a 7-segment display byte. See [`from_ascii_byte`] for more information.
pub const fn from_char(c: char) -> u8 {
    from_ascii_byte(c as u8)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn flipped_four() {
        let four = DigitBits::Four as u8;
        let flipped_four = flip(four);
        let should_flipped_four = SegmentBits::SegB as u8
            | SegmentBits::SegC as u8
            | SegmentBits::SegE as u8
            | SegmentBits::SegG as u8;

        assert_eq!(flipped_four, should_flipped_four);
    }

    #[test]
    fn flipped_e() {
        let e = UpCharBits::UpE as u8;
        let flipped_e = flip(e);
        let should_flipped_e = UpCharBits::UpE as u8;

        assert_eq!(flipped_e, should_flipped_e);
    }

    #[test]
    fn mirrored_four() {
        let four = DigitBits::Four as u8;
        let mirrored_four = mirror(four);
        let should_mirrored_four = SegmentBits::SegB as u8
            | SegmentBits::SegE as u8
            | SegmentBits::SegF as u8
            | SegmentBits::SegG as u8;

        assert_eq!(mirrored_four, should_mirrored_four);
    }

    #[test]
    fn mirrored_e() {
        let e = UpCharBits::UpE as u8;
        let mirrored_e = mirror(e);
        let should_mirrored_e = SegmentBits::SegA as u8
            | SegmentBits::SegB as u8
            | SegmentBits::SegC as u8
            | SegmentBits::SegD as u8
            | SegmentBits::SegG as u8;

        assert_eq!(mirrored_e, should_mirrored_e);
    }

    #[test]
    fn flipped_mirrored_four() {
        let four = DigitBits::Four as u8;
        let flipped_mirrored_four = flip_mirror(four);
        let should_flipped_mirrored_four = SegmentBits::SegC as u8
            | SegmentBits::SegE as u8
            | SegmentBits::SegF as u8
            | SegmentBits::SegG as u8;

        assert_eq!(flipped_mirrored_four, should_flipped_mirrored_four);
    }

    #[test]
    fn mirrored_flipped_is_flipped_mirrored() {
        let four = DigitBits::Four as u8;

        let mirrored_flipped_four = mirror(flip(four));
        let flipped_mirrored_four = flip(mirror(four));

        assert_eq!(mirrored_flipped_four, flipped_mirrored_four);
    }

    #[test]
    fn flipped_flipped_is_original() {
        let seven = DigitBits::Seven as u8;

        let flipped_flipped_seven = flip(flip(seven));

        assert_eq!(seven, flipped_flipped_seven);
    }

    #[test]
    fn mirrored_mirrored_is_original() {
        let five = DigitBits::Five as u8;

        let mirrored_mirrored_five = mirror(mirror(five));

        assert_eq!(five, mirrored_mirrored_five);
    }
}