Skip to content

Commit

Permalink
Merge pull request #20 from JadKHaddad/feat/more-mappings
Browse files Browse the repository at this point in the history
feat(mappings): add more test cases
  • Loading branch information
JadKHaddad authored Sep 2, 2024
2 parents 2f8c291 + fc957d5 commit 7e392fa
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions tm1637/src/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,16 +458,10 @@ pub const fn flip(byte: u8) -> u8 {
/// - 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
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.
Expand All @@ -493,6 +487,15 @@ mod tests {
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;
Expand All @@ -505,6 +508,19 @@ mod tests {
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;
Expand All @@ -526,4 +542,22 @@ mod tests {

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_mirror_is_original() {
let five = DigitBits::Five as u8;

let mirrored_mirrored_five = mirror(mirror(five));

assert_eq!(five, mirrored_mirrored_five);
}
}

0 comments on commit 7e392fa

Please sign in to comment.