tm1637_embedded_hal/
formatters.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
//! Format numbers into byte arrays.
//!
//! Functions for converting numbers ([`i8`]s, [`i16`]s, [`i32`]s or [`f32`]s) into arrays of bytes
//! that can be sent to a TM1637 display.
//!
//! There are versions of these functions that are meant for 4-digit displays
//! and for 6-digit displays. The 6-digit versions take into account that the
//! order of the bytes does not directly correlate with the order of the physical
//! digits.
//!
//! All numbers are aligned to the right.
//!
//! # Example
//!
//! ```rust
//! use tm1637_embedded_hal::{formatters::i16_to_4digits, mock::Noop, TM1637Builder};
//!
//! let mut tm = TM1637Builder::new(Noop, Noop, Noop).build_blocking::<4>();
//!
//! tm.init().ok();
//!
//! tm.display_slice(0, &i16_to_4digits(1234));
//! ```

use crate::mappings::{DigitBits, UpsideDownDigitBits};

/// Formats a [`i16`] clamped between `-999` and `9999`, for a `4-digit display`.
///
/// # Example
///
/// A counter that goes from `-100` to `100`:
///
/// ```rust
/// use tm1637_embedded_hal::{formatters::i16_to_4digits, mock::Noop, TM1637Builder};
/// use embedded_hal::delay::DelayNs;
///
/// let mut delay = Noop;
/// let mut tm = TM1637Builder::new(Noop, Noop, Noop).build_blocking::<4>();
///
/// tm.init().ok();
///
/// for i in -100..100 {
///     let segs = i16_to_4digits(i);
///     tm.display_slice(0, &segs).ok();
///
///     delay.delay_ms(100);
/// }
/// ```
pub fn i16_to_4digits(n: i16) -> [u8; 4] {
    let mut bytes: [u8; 4] = [0; 4];
    let mut m: i16 = n.clamp(-999, 9999).abs();

    for position in (0..4).rev() {
        bytes[position as usize] = DigitBits::from_digit((m % 10) as u8) as u8;

        m /= 10;

        if m == 0 {
            if n < 0 {
                bytes[position as usize - 1] = 0b01000000; // minus sign
            }
            break;
        };
    }

    bytes
}

/// Formats a [`i32`] clamped between `-99999` and `999999`, for a `6-digit display`.
pub fn i32_to_6digits(n: i32) -> [u8; 6] {
    let mut b: [u8; 6] = [0; 6];
    let mut m: i32 = n.clamp(-99999, 999999).abs();

    for position in (0..6).rev() {
        b[position as usize] = DigitBits::from_digit((m % 10) as u8) as u8;

        m /= 10;

        if m == 0 {
            if !n.is_positive() {
                b[position as usize - 1] = 0b01000000; // minus sign
            }
            break;
        };
    }

    // Swizzle bytes around to fit the order of 6-digit displays
    [b[2], b[1], b[0], b[5], b[4], b[3]]
}

/// Formats a [`i8`] clamped between `-9` and `99`, appending the degrees symbol `(°)`
/// and an `uppercase C`, for a `4-digit display`.
pub fn celsius_to_4digits(n: i8) -> [u8; 4] {
    let mut m: i8 = n.clamp(-9, 99);

    // 3rd and 4th bytes are the degrees symbol (°) and uppercase C
    let mut b: [u8; 4] = [0, 0, 0x63, 0x39];

    for position in (0..2).rev() {
        b[position as usize] = DigitBits::from_digit((m.abs() % 10) as u8) as u8;

        m /= 10;

        if m == 0 {
            if !n.is_positive() {
                b[position as usize - 1] = 0b01000000; // minus sign
            }
            break;
        };
    }
    b
}

/// Formats a [`i16`] clamped between `-99` and `999`, appending the degrees symbol `(°)`,
/// for a `4-digit display`.
pub fn degrees_to_4digits(n: i16) -> [u8; 4] {
    let mut m: i16 = n.clamp(-99, 999);

    // 4th byte is the degrees symbol (°)
    let mut b: [u8; 4] = [0, 0, 0, 0x63];

    for position in (0..3).rev() {
        b[position as usize] = DigitBits::from_digit((m.abs() % 10) as u8) as u8;

        m /= 10;

        if m == 0 {
            if !n.is_positive() {
                b[position as usize - 1] = 0b01000000; // minus sign
            }
            break;
        };
    }
    b
}

/// Formats two [`u8`]s between `0` and `99`, with an optional colon between them.
///
/// This will only work for `4-digit displays` where there's a physical colon,
/// and that colon acts as the decimal dot between the 2nd and 3rd digit.
///
/// # Example
///
/// Let's create a clock displaying `12:34` with a blinking colon:
///
/// ```rust
/// use tm1637_embedded_hal::{formatters::clock_to_4digits, mock::Noop, TM1637Builder};
/// use embedded_hal::delay::DelayNs;
///
/// let mut delay = Noop;
///
/// let mut tm = TM1637Builder::new(Noop, Noop, Noop).build_blocking::<4>();
///
/// tm.init().ok();
///
/// for hour in 12..24 {
///     for minute in 34..60 {
///         for second in 0..120 {
///             let blink = second % 2 == 0;
///             let segs = clock_to_4digits(hour, minute, blink);
///
///             tm.display_slice(0, &segs).ok();
///
///             delay.delay_ms(500);
///         }
///     }
/// }
/// ```
pub fn clock_to_4digits(hour: u8, minute: u8, colon: bool) -> [u8; 4] {
    let mut b: [u8; 4] = [0, 0, 0, 0];

    if hour >= 10 {
        b[0] = DigitBits::from_digit(hour / 10) as u8;
    }
    b[1] = DigitBits::from_digit(hour % 10) as u8;

    if colon {
        b[1] |= 0b1000_0000
    }
    b[2] = DigitBits::from_digit(minute / 10) as u8;
    b[3] = DigitBits::from_digit(minute % 10) as u8;

    b
}

/// Formats a [`i16`] clamped between `-999` and `9999`, for an `upside-down 4-digit display`.
pub fn i16_to_upsidedown_4digits(n: i16) -> [u8; 4] {
    let mut bytes: [u8; 4] = [0; 4];
    let mut m: i16 = n.clamp(-999, 9999).abs();

    for position in 0..4 {
        bytes[position as usize] = UpsideDownDigitBits::from_digit((m % 10) as u8) as u8;

        m /= 10;

        if m == 0 {
            if !n.is_positive() {
                bytes[position as usize + 1] = 0b01000000; // minus sign
            }
            break;
        };
    }

    bytes
}

/// Formats a [`f32`] with the given amount of decimal digits, for a `6-digit display`.
pub fn f32_to_6digits(n: f32, decimals: u8) -> [u8; 6] {
    use core::ops::Mul;

    let mut b: [u8; 6] = [0; 6];
    let decimal_position = 5 - decimals;

    let mut m: i32 = ((n.mul(10i32.pow(decimals as u32) as f32)
        + if n.is_sign_positive() {
            0.5_f32
        } else {
            -0.5_f32
        }) as i32)
        .clamp(-99999, 999999)
        .abs();

    for position in (0..6).rev() {
        b[position as usize] = DigitBits::from_digit((m % 10) as u8) as u8;

        m /= 10;

        if position == decimal_position {
            // Add a dot here
            b[position as usize] |= 0b1000_0000;
        }
        if m == 0 && position <= decimal_position {
            // Add the minus sign only when the digit with the decimal point
            // has been done; do not break earlier.
            if !n.is_sign_positive() {
                b[position as usize - 1] = 0b01000000; // minus sign
            }
            break;
        };
    }

    // Swizzle bytes around to fit the order of 6-digit displays
    [b[2], b[1], b[0], b[5], b[4], b[3]]
}