Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mappings): add three functions flip, mirror and flip_mirror #19

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tm1637/src/formatters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Format numbers into byte arrays
//! 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.
Expand Down
90 changes: 90 additions & 0 deletions tm1637/src/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,93 @@ impl SpecialCharBits {
]
}
}

/// 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 seg_a = byte & 0b00000001;
let seg_b = (byte & 0b00000010) << 4; // B -> F
let seg_c = (byte & 0b00000100) << 2; // C -> E
let seg_d = byte & 0b00001000;
let seg_e = (byte & 0b00010000) >> 2; // E -> C
let seg_f = (byte & 0b00100000) >> 4; // F -> B
let seg_g = byte & 0b01000000;
let seg_point = byte & 0b10000000;

seg_a | seg_b | seg_c | seg_d | seg_e | seg_f | seg_g | seg_point
}

/// 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))
}

#[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 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 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);
}
}
Loading