From 697a781ff2aff78af357d460522c32f6016a7b91 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 22 Oct 2018 15:43:50 +0200 Subject: [PATCH] Remove more or less duplicate test cases for 2in9 buffer --- Cargo.toml | 8 +++--- src/color.rs | 5 +++- src/epd1in54/graphics.rs | 2 +- src/epd1in54/mod.rs | 6 ++--- src/epd2in9/graphics.rs | 41 ++++++++++++++++++++++++++++ src/epd2in9/mod.rs | 3 +++ src/graphics.rs | 58 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++- 8 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 src/epd2in9/graphics.rs diff --git a/Cargo.toml b/Cargo.toml index 34de391..ffe9c4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,16 +20,16 @@ travis-ci = { repository = "Caemor/eink-waveshare-rs" } [features] default = ["epd1in54", "epd2in9", "epd4in2", "graphics"] -graphics = [] +graphics = ["embedded-graphics"] epd1in54 = [] epd2in9 = [] epd4in2 = [] # Activates the fast LUT for EPD4in2 epd4in2_fast_update = [] -[dependencies] - -embedded-graphics = "0.4.3" +[dependencies.embedded-graphics] +optional = true +version = "0.4.3" # embedded-graphics = {git = "https://github.com/caemor/embedded-graphics", branch = "master"} # embedded-graphics = {git = "https://github.com/jamwaffles/embedded-graphics", branch = "master"} diff --git a/src/color.rs b/src/color.rs index f825949..07a9f47 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,5 +1,5 @@ -use embedded_graphics::prelude::*; + /// Only for the B/W Displays atm #[derive(Clone, Copy, PartialEq, Debug)] @@ -85,6 +85,9 @@ impl Color { } } +#[cfg(feature = "graphics")] +use embedded_graphics::prelude::*; +#[cfg(feature = "graphics")] impl PixelColor for Color {} impl From for Color { diff --git a/src/epd1in54/graphics.rs b/src/epd1in54/graphics.rs index 010b974..2604645 100644 --- a/src/epd1in54/graphics.rs +++ b/src/epd1in54/graphics.rs @@ -12,7 +12,7 @@ impl Default for Buffer1in54BlackWhite { WIDTH as usize * HEIGHT as usize / 8 ] } - } + } } diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index ce59f51..2bb8eea 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -19,10 +19,10 @@ //! epd4in2.sleep(); //! ``` -const WIDTH: u32 = 200; -const HEIGHT: u32 = 200; +pub const WIDTH: u32 = 200; +pub const HEIGHT: u32 = 200; //const DPI: u16 = 184; -const DEFAULT_BACKGROUND_COLOR: Color = Color::White; +pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White; use hal::{ blocking::{delay::*, spi::Write}, diff --git a/src/epd2in9/graphics.rs b/src/epd2in9/graphics.rs new file mode 100644 index 0000000..45e17d9 --- /dev/null +++ b/src/epd2in9/graphics.rs @@ -0,0 +1,41 @@ +use epd2in9::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; + +pub struct Buffer2in9 { + pub buffer: [u8; WIDTH as usize * HEIGHT as usize / 8], +} + +impl Default for Buffer2in9 { + fn default() -> Self { + Buffer2in9 { + buffer: [ + DEFAULT_BACKGROUND_COLOR.get_byte_value(); + WIDTH as usize * HEIGHT as usize / 8 + ] + } + } +} + + +#[cfg(test)] +mod tests { + use super::*; + use graphics::Display; + + // test buffer length + #[test] + fn graphics_size() { + let mut buffer = Buffer2in9::default(); + let display = Display::new(WIDTH, HEIGHT, &mut buffer.buffer); + assert_eq!(display.buffer().len(), 4736); + } + + // test default background color on all bytes + #[test] + fn graphics_default() { + let mut buffer = Buffer2in9::default(); + let display = Display::new(WIDTH, HEIGHT, &mut buffer.buffer); + for &byte in display.buffer() { + assert_eq!(byte, DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } +} \ No newline at end of file diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 163b655..9da3e14 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -39,6 +39,9 @@ use traits::*; use interface::DisplayInterface; +mod graphics; +pub use epd2in9::graphics::Buffer2in9; + /// EPD2in9 driver /// pub struct EPD2in9 { diff --git a/src/graphics.rs b/src/graphics.rs index 1342dba..2cbd3ff 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -131,6 +131,9 @@ fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) mod tests { use super::{DisplayRotation, outside_display, rotation, Display}; use color::Color; + use embedded_graphics::coord::Coord; + use embedded_graphics::primitives::Line; + use embedded_graphics::prelude::*; #[test] fn buffer_clear() { @@ -176,4 +179,59 @@ mod tests { } } } + + + + #[test] + fn graphics_rotation_0() { + use epd2in9::{DEFAULT_BACKGROUND_COLOR}; + let width = 128; + let height = 296; + + let mut buffer = [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 128 / 8 * 296]; + let mut display = Display::new(width, height, &mut buffer); + + display.draw( + Line::new(Coord::new(0, 0), Coord::new(7, 0)) + .with_stroke(Some(Color::Black)) + .into_iter(), + ); + + let buffer = display.buffer(); + + assert_eq!(buffer[0], Color::Black.get_byte_value()); + + for &byte in buffer.iter().skip(1) { + assert_eq!(byte, DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_rotation_90() { + use epd2in9::{DEFAULT_BACKGROUND_COLOR}; + let width = 128; + let height = 296; + + let mut buffer = [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 128 / 8 * 296]; + let mut display = Display::new(width, height, &mut buffer); + + display.set_rotation(DisplayRotation::Rotate90); + + display.draw( + Line::new(Coord::new(0, 120), Coord::new(0, 295)) + .with_stroke(Some(Color::Black)) + .into_iter(), + ); + + let buffer = display.buffer(); + + extern crate std; + std::println!("{:?}", buffer); + + assert_eq!(buffer[0], Color::Black.get_byte_value()); + + for &byte in buffer.iter().skip(1) { + assert_eq!(byte, DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 736ffa0..6268ff6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,6 +47,9 @@ extern crate embedded_hal as hal; use hal::spi::{Mode, Phase, Polarity}; +#[cfg(feature = "graphics")] +extern crate embedded_graphics; + #[cfg(feature = "graphics")] pub mod graphics; @@ -74,7 +77,7 @@ pub mod epd2in9; #[cfg(any(feature = "epd1in54", feature = "epd2in9"))] pub(crate) mod type_a; -extern crate embedded_graphics; + /// SPI mode - /// For more infos see [Requirements: SPI](index.html#spi)