From e49e144a348c055c2b335dc275b821d0d8637a54 Mon Sep 17 00:00:00 2001 From: Mitch Souders Date: Thu, 19 Nov 2020 19:22:41 -0800 Subject: [PATCH] Minor fixups --- src/color.rs | 10 +++++++++ src/epd5in65f/graphics.rs | 44 +++++++++++++++++++++++++++++++++++++++ src/graphics.rs | 6 ++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/color.rs b/src/color.rs index b9388c7..6767673 100644 --- a/src/color.rs +++ b/src/color.rs @@ -185,4 +185,14 @@ mod tests { assert_eq!(Color::from(Color::White.get_bit_value()), Color::White); assert_eq!(Color::from(1u8).get_bit_value(), 1u8); } + + #[test] + fn test_oct() { + let left = OctColor::Red; + let right = OctColor::Green; + assert_eq!( + OctColor::split_byte(OctColor::colors_byte(left, right)), + Ok((left, right)) + ); + } } diff --git a/src/epd5in65f/graphics.rs b/src/epd5in65f/graphics.rs index 2a52b15..d21f8e8 100644 --- a/src/epd5in65f/graphics.rs +++ b/src/epd5in65f/graphics.rs @@ -204,4 +204,48 @@ mod tests { ); } } + + #[test] + fn graphics_colors() { + let mut display = Display5in65f::default(); + + const COLORS: [OctColor; 8] = [ + OctColor::HiZ, + OctColor::White, + OctColor::Black, + OctColor::Red, + OctColor::Green, + OctColor::Orange, + OctColor::Blue, + OctColor::Yellow, + ]; + for c in &COLORS { + display.clear_buffer(*c); + for b in display.buffer() { + assert_eq!(OctColor::split_byte(*b), Ok((*c, *c))); + } + } + + for (w, c) in (0..WIDTH).zip(COLORS.iter().cycle()) { + let _ = Line::new( + Point::new(w as i32, 0), + Point::new(w as i32, HEIGHT as i32 - 1), + ) + .into_styled(PrimitiveStyle::with_stroke(*c, 1)) + .draw(&mut display); + } + + COLORS + .chunks(2) + .cycle() + .take(WIDTH as usize * 2) + .cycle() + .zip(display.buffer()) + .for_each(|(window, b)| match (window, b) { + (&[c1, c2], b) => { + assert_eq!(OctColor::split_byte(*b), Ok((c1, c2))); + } + _ => panic!("unexpected pattern"), + }) + } } diff --git a/src/graphics.rs b/src/graphics.rs index f9aac40..c7b752e 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -93,9 +93,9 @@ pub trait Display: DrawTarget { /// - Clearing pub trait OctDisplay: DrawTarget { /// Clears the buffer of the display with the chosen background color - fn clear_buffer(&mut self, background_color: Color) { + fn clear_buffer(&mut self, background_color: OctColor) { for elem in self.get_mut_buffer().iter_mut() { - *elem = background_color.get_byte_value(); + *elem = OctColor::colors_byte(background_color, background_color); } } @@ -274,7 +274,9 @@ fn find_rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotat fn find_oct_position(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, bool) { let (nx, ny) = find_rotation(x, y, width, height, rotation); ( + /* what byte address is this? */ nx / 2 + (width / 2) * ny, + /* is this the lower nibble (within byte)? */ (nx & 0x1) == 0, ) }