From 6b89ee1b8eaa783f25c265a4f5af0f7246315c01 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 12 Oct 2018 11:30:04 +0200 Subject: [PATCH 01/14] Set spi chunk limit on linux with cfg! Not tested yet --- src/interface.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/interface.rs b/src/interface.rs index 1b1a6b9..76cbde5 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -100,8 +100,17 @@ where { // activate spi with cs low self.cs.set_low(); + // transfer spi data - spi.write(data)?; + // Be careful!! Linux has a default limit of 4096 bytes per spi transfer + if cfg!(target_os = "linux") { + for data_chunk in data.chunks(4096) { + spi.write(data_chunk)?; + } + } else { + spi.write(data)?; + } + // deativate spi with cs high self.cs.set_high(); From 4916ac674adaa6daaaf88e670fb5faa73ba1ca06 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 12 Oct 2018 11:35:13 +0200 Subject: [PATCH 02/14] Add an reference to the linux specific max spi bytes size --- src/interface.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/interface.rs b/src/interface.rs index 76cbde5..4e07baf 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -103,6 +103,7 @@ where // transfer spi data // Be careful!! Linux has a default limit of 4096 bytes per spi transfer + // see https://raspberrypi.stackexchange.com/questions/65595/spi-transfer-fails-with-buffer-size-greater-than-4096 if cfg!(target_os = "linux") { for data_chunk in data.chunks(4096) { spi.write(data_chunk)?; From 2bab732b6fa89fb59cb79db06f8fd7658c0e5e3f Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 18:08:43 +0200 Subject: [PATCH 03/14] use new graphics in example --- examples/embedded_linux_epd4in2/src/main.rs | 48 +++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index 922cf67..8e61235 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -8,6 +8,7 @@ extern crate eink_waveshare_rs; use eink_waveshare_rs::{ EPD4in2, drawing_old::{Graphics}, + drawing::DisplayEink42BlackWhite, color::Color, WaveshareDisplay, }; @@ -136,6 +137,8 @@ fn run() -> Result<(), std::io::Error> { epd4in2.clear_frame(&mut spi).expect("clear frame error"); epd4in2.update_frame(&mut spi, graphics.get_buffer()).expect("update frame error"); epd4in2.display_frame(&mut spi)?; + + println!("Finished basic old graphics test"); delay.delay_ms(3000u16); @@ -158,6 +161,8 @@ fn run() -> Result<(), std::io::Error> { epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 160,240, 32, 32).expect("update partial frame error"); epd4in2.display_frame(&mut spi)?; + println!("Finished partial update test"); + delay.delay_ms(3000u16); @@ -170,7 +175,44 @@ fn run() -> Result<(), std::io::Error> { epd4in2.update_frame(&mut spi, graphics.get_buffer())?; epd4in2.display_frame(&mut spi)?; - delay.delay_ms(3000u16); + println!("Finished draw string test"); - epd4in2.sleep(&mut spi) -} + delay.delay_ms(3000u16); + println!("Now test new graphics:"); + + let mut i = 0; + loop { + println!("Loop {}", i); + i += 1; + let mut display = DisplayEink42BlackWhite::default(); + display.draw( + Circle::new(Coord::new(64, 64), 64) + .with_stroke(Some(1u8.into())) + .into_iter(), + ); + display.draw( + Line::new(Coord::new(64, 64), Coord::new(0, 64)) + .with_stroke(Some(1u8.into())) + .into_iter(), + ); + display.draw( + Line::new(Coord::new(64, 64), Coord::new(80, 80)) + .with_stroke(Some(1u8.into())) + .into_iter(), + ); + display.draw( + Font6x8::render_str("Hello World!") + .with_stroke(Some(1u8.into())) + .translate(Coord::new(5 + i, 50)) + .into_iter(), + ); + + epd4in2.update_frame(&mut spi, &display.get_buffer()).unwrap(); + epd4in2.display_frame(&mut spi); + if i > 296 { + epd4in2.sleep(&mut spi)?; + return; + } + delay.delay_ms(1_000u16); + } +} From fe97740f0414cf9ef133b80fd3c5dc3577eba3be Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 18:13:43 +0200 Subject: [PATCH 04/14] Fix example --- examples/embedded_linux_epd4in2/Cargo.toml | 2 ++ examples/embedded_linux_epd4in2/src/main.rs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/embedded_linux_epd4in2/Cargo.toml b/examples/embedded_linux_epd4in2/Cargo.toml index 5bb31a6..cf2e8f9 100644 --- a/examples/embedded_linux_epd4in2/Cargo.toml +++ b/examples/embedded_linux_epd4in2/Cargo.toml @@ -11,4 +11,6 @@ eink_waveshare_rs = { path = "../../", default-features = false, features = ["ep linux-embedded-hal = "0.2.0" +embedded-graphics = "0.4.1" + embedded-hal = { version = "0.2.1", features = ["unproven"] } diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index 8e61235..ebf93a7 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -8,11 +8,17 @@ extern crate eink_waveshare_rs; use eink_waveshare_rs::{ EPD4in2, drawing_old::{Graphics}, - drawing::DisplayEink42BlackWhite, + drawing::{DisplayEink42BlackWhite, Buffer}, color::Color, WaveshareDisplay, }; +extern crate embedded_graphics; +use embedded_graphics::coord::Coord; +use embedded_graphics::fonts::Font6x8; +use embedded_graphics::prelude::*; +use embedded_graphics::primitives::{Circle, Line}; + use lin_hal::spidev::{self, SpidevOptions}; use lin_hal::{Pin, Spidev}; use lin_hal::sysfs_gpio::Direction; @@ -63,7 +69,7 @@ impl<'a> InputPin for HackInputPin<'a> { * */ fn main() { - run().map_err(|e| println!("{}", e.to_string())); + run().map_err(|e| println!("{}", e.to_string())).unwrap(); } @@ -208,11 +214,13 @@ fn run() -> Result<(), std::io::Error> { ); epd4in2.update_frame(&mut spi, &display.get_buffer()).unwrap(); - epd4in2.display_frame(&mut spi); + epd4in2.display_frame(&mut spi).expect("display frame new graphics"); if i > 296 { - epd4in2.sleep(&mut spi)?; - return; + + break; } delay.delay_ms(1_000u16); } + println!("Finished tests - going to sleep"); + epd4in2.sleep(&mut spi) } From 533cb841236171abc69b6e64a88afcb1bfa5379e Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 18:24:14 +0200 Subject: [PATCH 05/14] Make lines visible --- examples/embedded_linux_epd4in2/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index ebf93a7..be268a7 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -193,29 +193,29 @@ fn run() -> Result<(), std::io::Error> { let mut display = DisplayEink42BlackWhite::default(); display.draw( Circle::new(Coord::new(64, 64), 64) - .with_stroke(Some(1u8.into())) + .with_stroke(Color::Black)) .into_iter(), ); display.draw( Line::new(Coord::new(64, 64), Coord::new(0, 64)) - .with_stroke(Some(1u8.into())) + .with_stroke(Color::Black)) .into_iter(), ); display.draw( Line::new(Coord::new(64, 64), Coord::new(80, 80)) - .with_stroke(Some(1u8.into())) + .with_stroke(Color::Black)) .into_iter(), ); display.draw( Font6x8::render_str("Hello World!") .with_stroke(Some(1u8.into())) - .translate(Coord::new(5 + i, 50)) + .translate(Coord::new(5 + i*5, 50)) .into_iter(), ); epd4in2.update_frame(&mut spi, &display.get_buffer()).unwrap(); epd4in2.display_frame(&mut spi).expect("display frame new graphics"); - if i > 296 { + if i > 60 { break; } From e92af2198a3651050950404984331998e713886d Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 18:26:29 +0200 Subject: [PATCH 06/14] Fix previous commit --- examples/embedded_linux_epd4in2/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index be268a7..2f30b63 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -193,29 +193,29 @@ fn run() -> Result<(), std::io::Error> { let mut display = DisplayEink42BlackWhite::default(); display.draw( Circle::new(Coord::new(64, 64), 64) - .with_stroke(Color::Black)) + .with_stroke(Color::Black) .into_iter(), ); display.draw( Line::new(Coord::new(64, 64), Coord::new(0, 64)) - .with_stroke(Color::Black)) + .with_stroke(Color::Black) .into_iter(), ); display.draw( Line::new(Coord::new(64, 64), Coord::new(80, 80)) - .with_stroke(Color::Black)) + .with_stroke(Color::Black) .into_iter(), ); display.draw( Font6x8::render_str("Hello World!") - .with_stroke(Some(1u8.into())) - .translate(Coord::new(5 + i*5, 50)) + .with_stroke(Color::Black) + .translate(Coord::new(5 + i*10, 50)) .into_iter(), ); epd4in2.update_frame(&mut spi, &display.get_buffer()).unwrap(); epd4in2.display_frame(&mut spi).expect("display frame new graphics"); - if i > 60 { + if i > 20 { break; } From ff21d6e611e9617bb6abbbf1e54981ef0aded171 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 18:33:06 +0200 Subject: [PATCH 07/14] add impl Drawing for DisplayEink42BlackWhite fix error by using option for style --- examples/embedded_linux_epd4in2/src/main.rs | 8 ++--- src/drawing.rs | 34 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index 2f30b63..3097018 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -193,22 +193,22 @@ fn run() -> Result<(), std::io::Error> { let mut display = DisplayEink42BlackWhite::default(); display.draw( Circle::new(Coord::new(64, 64), 64) - .with_stroke(Color::Black) + .with_stroke(Some(Color::Black)) .into_iter(), ); display.draw( Line::new(Coord::new(64, 64), Coord::new(0, 64)) - .with_stroke(Color::Black) + .with_stroke(Some(Color::Black)) .into_iter(), ); display.draw( Line::new(Coord::new(64, 64), Coord::new(80, 80)) - .with_stroke(Color::Black) + .with_stroke(Some(Color::Black)) .into_iter(), ); display.draw( Font6x8::render_str("Hello World!") - .with_stroke(Color::Black) + .with_stroke(Some(Color::Black)) .translate(Coord::new(5 + i*10, 50)) .into_iter(), ); diff --git a/src/drawing.rs b/src/drawing.rs index 97c3ef7..28b81e5 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -93,6 +93,40 @@ impl Drawing for DisplayEink42BlackWhite { } } +impl Drawing for DisplayEink42BlackWhite { + fn draw(&mut self, item_pixels: T) + where + T: Iterator> + { + use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; + for Pixel(UnsignedCoord(x,y), color) in item_pixels { + let (idx, bit) = match self.rotation { + DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => ( + (x as usize / 8 + (WIDTH as usize / 8) * y as usize), + 0x80 >> (x % 8), + ), + DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => ( + y as usize / 8 * WIDTH as usize + x as usize, + 0x80 >> (y % 8), + ), + }; + + if idx >= self.buffer.len() { + return; + } + + match color { + Color::Black => { + self.buffer[idx] &= !bit; + } + Color::White => { + self.buffer[idx] |= bit; + } + } + } + } +} + // impl Drawing for DisplayRibbonLeft { // fn draw(&mut self, item_pixels: T) // where From 8904066de615aae3e6cea8e3ee07ef302be2117f Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 21:43:27 +0200 Subject: [PATCH 08/14] Moved constants of type_a to its own file --- src/epd1in54/mod.rs | 5 ++++- src/epd2in9/mod.rs | 5 ++++- src/type_a/constants.rs | 14 ++++++++++++++ src/type_a/mod.rs | 16 +--------------- 4 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 src/type_a/constants.rs diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 99732d1..a4c625f 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -29,7 +29,10 @@ use hal::{ digital::*, }; -use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; +use type_a::{ + command::Command, + constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE} +}; use color::Color; diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 1110d11..4fd3b2a 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -28,7 +28,10 @@ use hal::{ digital::*, }; -use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; +use type_a::{ + command::Command, + constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE} +}; use color::Color; diff --git a/src/type_a/constants.rs b/src/type_a/constants.rs new file mode 100644 index 0000000..63de296 --- /dev/null +++ b/src/type_a/constants.rs @@ -0,0 +1,14 @@ +// Original Waveforms from Waveshare +pub(crate) const LUT_FULL_UPDATE: [u8; 30] =[ + 0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22, + 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88, + 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51, + 0x35, 0x51, 0x51, 0x19, 0x01, 0x00 +]; + +pub(crate) const LUT_PARTIAL_UPDATE: [u8; 30] =[ + 0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +]; \ No newline at end of file diff --git a/src/type_a/mod.rs b/src/type_a/mod.rs index 987a032..11b59c7 100644 --- a/src/type_a/mod.rs +++ b/src/type_a/mod.rs @@ -1,16 +1,2 @@ pub(crate) mod command; - -// Original Waveforms from Waveshare -pub(crate) const LUT_FULL_UPDATE: [u8; 30] =[ - 0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22, - 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88, - 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51, - 0x35, 0x51, 0x51, 0x19, 0x01, 0x00 -]; - -pub(crate) const LUT_PARTIAL_UPDATE: [u8; 30] =[ - 0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -]; +pub(crate) mod constants; \ No newline at end of file From d2a47eb05c22b4da1fb35bbea63d73bbf586bb00 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 22:11:25 +0200 Subject: [PATCH 09/14] Rename Buffer Trait to Display and add a Rotation Function to it --- examples/embedded_linux_epd4in2/src/main.rs | 57 +++++--- src/drawing.rs | 140 ++------------------ 2 files changed, 50 insertions(+), 147 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index 3097018..84090c1 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -8,7 +8,7 @@ extern crate eink_waveshare_rs; use eink_waveshare_rs::{ EPD4in2, drawing_old::{Graphics}, - drawing::{DisplayEink42BlackWhite, Buffer}, + drawing::{DisplayEink42BlackWhite, Display}, color::Color, WaveshareDisplay, }; @@ -184,34 +184,51 @@ fn run() -> Result<(), std::io::Error> { println!("Finished draw string test"); delay.delay_ms(3000u16); + println!("Now test new graphics:"); + let mut display = DisplayEink42BlackWhite::default(); + display.draw( + Circle::new(Coord::new(64, 64), 64) + .with_stroke(Some(Color::Black)) + .into_iter(), + ); + display.draw( + Line::new(Coord::new(64, 64), Coord::new(0, 64)) + .with_stroke(Some(Color::Black)) + .into_iter(), + ); + display.draw( + Line::new(Coord::new(64, 64), Coord::new(80, 80)) + .with_stroke(Some(Color::Black)) + .into_iter(), + ); + display.draw( + Font6x8::render_str("It's working!") + // Using Style here + .with_style(Style { + fill_color: Some(Color::Black), + stroke_color: Some(Color::White), + stroke_width: 0u8, // Has no effect on fonts + }) + .translate(Coord::new(175, 250)) + .into_iter(), + ); + + //display.clear(); + //display.set_rotation(Rotation) let mut i = 0; loop { - println!("Loop {}", i); i += 1; - let mut display = DisplayEink42BlackWhite::default(); - display.draw( - Circle::new(Coord::new(64, 64), 64) - .with_stroke(Some(Color::Black)) - .into_iter(), - ); - display.draw( - Line::new(Coord::new(64, 64), Coord::new(0, 64)) - .with_stroke(Some(Color::Black)) - .into_iter(), - ); - display.draw( - Line::new(Coord::new(64, 64), Coord::new(80, 80)) - .with_stroke(Some(Color::Black)) - .into_iter(), - ); + println!("Moving Hello World. Loop {} from 20", i); + display.draw( Font6x8::render_str("Hello World!") .with_stroke(Some(Color::Black)) + .with_fill(Some(Color::White)) .translate(Coord::new(5 + i*10, 50)) .into_iter(), - ); + ); epd4in2.update_frame(&mut spi, &display.get_buffer()).unwrap(); epd4in2.display_frame(&mut spi).expect("display frame new graphics"); @@ -221,6 +238,8 @@ fn run() -> Result<(), std::io::Error> { } delay.delay_ms(1_000u16); } + + println!("Finished tests - going to sleep"); epd4in2.sleep(&mut spi) } diff --git a/src/drawing.rs b/src/drawing.rs index 28b81e5..9bf4600 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -19,29 +19,18 @@ impl Default for DisplayRotation { } } -pub enum Display { - Eink42BlackWhite, -} -impl Display { - /// Gets the Dimensions of a dipslay in the following order: - /// - Width - /// - Height - /// - Neccessary Buffersize - pub fn get_dimensions(&self) -> (u16, u16, u16) { - match self { - Display::Eink42BlackWhite => (400, 300, 15000), - } - } -} - -pub trait Buffer { +pub trait Display { fn get_buffer(&self) -> &[u8]; + fn set_rotation(&mut self, rotation: DisplayRotation); + fn rotation(&self) -> DisplayRotation; } + pub struct DisplayEink42BlackWhite { buffer: [u8; 400 * 300 / 8], rotation: DisplayRotation, //TODO: check embedded_graphics for orientation } + impl Default for DisplayEink42BlackWhite { fn default() -> Self { use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; @@ -54,42 +43,16 @@ impl Default for DisplayEink42BlackWhite { } } } -impl Buffer for DisplayEink42BlackWhite { + +impl Display for DisplayEink42BlackWhite { fn get_buffer(&self) -> &[u8] { &self.buffer } -} -impl Drawing for DisplayEink42BlackWhite { - fn draw(&mut self, item_pixels: T) - where - T: Iterator> - { - use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; - for Pixel(UnsignedCoord(x,y), color) in item_pixels { - let (idx, bit) = match self.rotation { - DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => ( - (x as usize / 8 + (WIDTH as usize / 8) * y as usize), - 0x80 >> (x % 8), - ), - DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => ( - y as usize / 8 * WIDTH as usize + x as usize, - 0x80 >> (y % 8), - ), - }; - - if idx >= self.buffer.len() { - return; - } - - match Color::from(color) { - Color::Black => { - self.buffer[idx] &= !bit; - } - Color::White => { - self.buffer[idx] |= bit; - } - } - } + fn set_rotation(&mut self, rotation: DisplayRotation) { + self.rotation = rotation; + } + fn rotation(&self) -> DisplayRotation { + self.rotation } } @@ -127,83 +90,4 @@ impl Drawing for DisplayEink42BlackWhite { } } -// impl Drawing for DisplayRibbonLeft { -// fn draw(&mut self, item_pixels: T) -// where -// T: Iterator>, -// { -// for Pixel(UnsignedCoord(x, y), color) in item_pixels { -// if y > 127 || x > 295 { -// continue; -// } -// let cell = &mut self.0[y as usize / 8 + (295 - x as usize) * 128 / 8]; -// let bit = 7 - y % 8; -// if color != 0 { -// *cell &= !(1 << bit); -// } else { -// *cell |= 1 << bit; -// } -// } -// } -// } - - - - // /// Draw a single Pixel with `color` - // /// - // /// limited to i16::max images (buffer_size) at the moment - // pub fn draw_pixel(&mut self, x: u16, y: u16, color: &Color) { - // let (idx, bit) = match self.rotation { - // Displayorientation::Rotate0 | Displayorientation::Rotate180 => ( - // (x as usize / 8 + (self.width as usize / 8) * y as usize), - // 0x80 >> (x % 8), - // ), - // Displayorientation::Rotate90 | Displayorientation::Rotate270 => ( - // y as usize / 8 * self.width as usize + x as usize, - // 0x80 >> (y % 8), - // ), - // }; - - // if idx >= self.buffer.len() { - // return; - // } - - // match color { - // Color::Black => { - // self.buffer[idx] &= !bit; - // } - // Color::White => { - // self.buffer[idx] |= bit; - // } - // } - // } - - // /// Draw a single Pixel with `color` - // /// - // /// limited to i16::max images (buffer_size) at the moment - // #[allow(dead_code)] - // fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) { - // let idx = match self.rotation { - // Displayorientation::Rotate0 | Displayorientation::Rotate180 => { - // x as usize / 8 + (self.width as usize / 8) * y as usize - // }, - // Displayorientation::Rotate90 | Displayorientation::Rotate270 => { - // y as usize / 8 + (self.width as usize / 8) * x as usize - // }, - // }; - - // if idx >= self.buffer.len() { - // return; - // } - - // match color { - // Color::Black => { - // self.buffer[idx] = !filling; - // }, - // Color::White => { - // self.buffer[idx] = filling; - // } - // } - // } - //TODO: write tests \ No newline at end of file From 2fb19b2df9b366b8e609d8cbfa457c87c83e3c93 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 23:28:39 +0200 Subject: [PATCH 10/14] added many tests to drawing --- src/drawing.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 3 deletions(-) diff --git a/src/drawing.rs b/src/drawing.rs index 9bf4600..7747908 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -20,7 +20,7 @@ impl Default for DisplayRotation { } pub trait Display { - fn get_buffer(&self) -> &[u8]; + fn buffer(&self) -> &[u8]; fn set_rotation(&mut self, rotation: DisplayRotation); fn rotation(&self) -> DisplayRotation; } @@ -45,7 +45,7 @@ impl Default for DisplayEink42BlackWhite { } impl Display for DisplayEink42BlackWhite { - fn get_buffer(&self) -> &[u8] { + fn buffer(&self) -> &[u8] { &self.buffer } fn set_rotation(&mut self, rotation: DisplayRotation) { @@ -90,4 +90,122 @@ impl Drawing for DisplayEink42BlackWhite { } } -//TODO: write tests \ No newline at end of file +//TODO: write tests +#[cfg(test)] +mod tests { + use super::*; + use epd4in2; + use embedded_graphics::coord::Coord; + use embedded_graphics::fonts::Font6x8; + use embedded_graphics::prelude::*; + use embedded_graphics::primitives::{Circle, Line}; + + #[test] + fn from_u8() { + assert_eq!(Color::Black, Color::from(0u8)); + assert_eq!(Color::White, Color::from(1u8)); + } + + // test all values aside from 0 and 1 which all should panic + #[test] + fn from_u8_panic() { + for val in 2..=u8::max_value() { + let result = std::panic::catch_unwind(|| Color::from(val)); + assert!(result.is_err()); + } + } + + // test buffer length + #[test] + fn graphics_4in2_size() { + let display = DisplayEink42BlackWhite::default(); + assert_eq!(display.buffer().len(), 15000); + } + + // test default background color on all bytes + #[test] + fn graphics_4in2_default() { + let display = DisplayEink42BlackWhite::default(); + use epd4in2; + for &byte in display.buffer() { + assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_4in2_rotation_0() { + let mut display = DisplayEink42BlackWhite::default(); + 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, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_4in2_rotation_90() { + let mut display = DisplayEink42BlackWhite::default(); + display.set_rotation(DisplayRotation::Rotate90); + display.draw( + Line::new(Coord::new(0, 392), Coord::new(0, 399)) + .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, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_4in2_rotation_180() { + let mut display = DisplayEink42BlackWhite::default(); + display.set_rotation(DisplayRotation::Rotate180); + display.draw( + Line::new(Coord::new(392, 299), Coord::new(399, 299)) + .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, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + + } + + #[test] + fn graphics_4in2_rotation_270() { + let mut display = DisplayEink42BlackWhite::default(); + display.set_rotation(DisplayRotation::Rotate270); + display.draw( + Line::new(Coord::new(299, 0), Coord::new(299, 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, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + + } +} \ No newline at end of file From 4ca0321573f6d7e4d981d56d21b218d0f8b54a2a Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 23:28:51 +0200 Subject: [PATCH 11/14] added rotation examples --- examples/embedded_linux_epd4in2/src/main.rs | 52 +++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index 84090c1..a9cb102 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -8,7 +8,7 @@ extern crate eink_waveshare_rs; use eink_waveshare_rs::{ EPD4in2, drawing_old::{Graphics}, - drawing::{DisplayEink42BlackWhite, Display}, + drawing::{DisplayEink42BlackWhite, Display, DisplayRotation}, color::Color, WaveshareDisplay, }; @@ -186,6 +186,51 @@ fn run() -> Result<(), std::io::Error> { delay.delay_ms(3000u16); println!("Now test new graphics:"); + + println!("Now test new graphics with rotate90:"); + let mut display = DisplayEink42BlackWhite::default(); + display.set_rotation(DisplayRotation::Rotate90); + display.draw( + Font6x8::render_str("Rotate 90!") + .with_stroke(Some(Color::Black)) + .with_fill(Some(Color::White)) + .translate(Coord::new(5, 50)) + .into_iter(), + ); + epd4in2.update_frame(&mut spi, &display.buffer()).unwrap(); + epd4in2.display_frame(&mut spi).expect("display frame new graphics"); + delay.delay_ms(2000u16); + + println!("Now test new graphics with rotate180:"); + let mut display = DisplayEink42BlackWhite::default(); + display.set_rotation(DisplayRotation::Rotate180); + display.draw( + Font6x8::render_str("Rotate 180!") + .with_stroke(Some(Color::Black)) + .with_fill(Some(Color::White)) + .translate(Coord::new(5, 50)) + .into_iter(), + ); + epd4in2.update_frame(&mut spi, &display.buffer()).unwrap(); + epd4in2.display_frame(&mut spi).expect("display frame new graphics"); + delay.delay_ms(2000u16); + + println!("Now test new graphics with rotate270:"); + let mut display = DisplayEink42BlackWhite::default(); + display.set_rotation(DisplayRotation::Rotate270); + display.draw( + Font6x8::render_str("Rotate 270!") + .with_stroke(Some(Color::Black)) + .with_fill(Some(Color::White)) + .translate(Coord::new(5, 50)) + .into_iter(), + ); + epd4in2.update_frame(&mut spi, &display.buffer()).unwrap(); + epd4in2.display_frame(&mut spi).expect("display frame new graphics"); + delay.delay_ms(2000u16); + + + println!("Now test new graphics with default rotation and some special stuff:"); let mut display = DisplayEink42BlackWhite::default(); display.draw( Circle::new(Coord::new(64, 64), 64) @@ -214,8 +259,7 @@ fn run() -> Result<(), std::io::Error> { .into_iter(), ); - //display.clear(); - //display.set_rotation(Rotation) + let mut i = 0; loop { @@ -230,7 +274,7 @@ fn run() -> Result<(), std::io::Error> { .into_iter(), ); - epd4in2.update_frame(&mut spi, &display.get_buffer()).unwrap(); + epd4in2.update_frame(&mut spi, &display.buffer()).unwrap(); epd4in2.display_frame(&mut spi).expect("display frame new graphics"); if i > 20 { From dd70d02eb7ef1bf566c73ed34e6319733aa84708 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 23:36:17 +0200 Subject: [PATCH 12/14] use style instead of with_stroke --- examples/embedded_linux_epd4in2/src/main.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index a9cb102..ba2089c 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -268,8 +268,11 @@ fn run() -> Result<(), std::io::Error> { display.draw( Font6x8::render_str("Hello World!") - .with_stroke(Some(Color::Black)) - .with_fill(Some(Color::White)) + .with_style(Style { + fill_color: Some(Color::White), + stroke_color: Some(Color::Black), + stroke_width: 0u8, // Has no effect on fonts + }) .translate(Coord::new(5 + i*10, 50)) .into_iter(), ); From cc2d09f4aa9a543c94fb67f255af50f9cd19b81d Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 23:49:21 +0200 Subject: [PATCH 13/14] try fixed embedded_graphics version --- examples/embedded_linux_epd4in2/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/embedded_linux_epd4in2/Cargo.toml b/examples/embedded_linux_epd4in2/Cargo.toml index cf2e8f9..237f292 100644 --- a/examples/embedded_linux_epd4in2/Cargo.toml +++ b/examples/embedded_linux_epd4in2/Cargo.toml @@ -11,6 +11,7 @@ eink_waveshare_rs = { path = "../../", default-features = false, features = ["ep linux-embedded-hal = "0.2.0" -embedded-graphics = "0.4.1" +#embedded-graphics = "0.4.1" +embedded-graphics = {git = "https://github.com/Caemor/embedded-graphics", branch = "patch-1"} embedded-hal = { version = "0.2.1", features = ["unproven"] } From d37ffd5489580e67278886a9030190d432438e42 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 23:55:27 +0200 Subject: [PATCH 14/14] test patch --- Cargo.toml | 3 ++- examples/embedded_linux_epd4in2/Cargo.toml | 2 +- examples/embedded_linux_epd4in2/src/main.rs | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc9e5d3..cb92be1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,8 @@ epd4in2_fast_update = [] [dependencies] -embedded-graphics = "0.4.1" +#embedded-graphics = "0.4.1" +embedded-graphics = {git = "https://github.com/caemor/embedded-graphics", branch = "patch-1"} [dependencies.embedded-hal] diff --git a/examples/embedded_linux_epd4in2/Cargo.toml b/examples/embedded_linux_epd4in2/Cargo.toml index 237f292..3940b13 100644 --- a/examples/embedded_linux_epd4in2/Cargo.toml +++ b/examples/embedded_linux_epd4in2/Cargo.toml @@ -12,6 +12,6 @@ eink_waveshare_rs = { path = "../../", default-features = false, features = ["ep linux-embedded-hal = "0.2.0" #embedded-graphics = "0.4.1" -embedded-graphics = {git = "https://github.com/Caemor/embedded-graphics", branch = "patch-1"} +embedded-graphics = {git = "https://github.com/caemor/embedded-graphics", branch = "patch-1"} embedded-hal = { version = "0.2.1", features = ["unproven"] } diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index ba2089c..06d9312 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -18,6 +18,7 @@ use embedded_graphics::coord::Coord; use embedded_graphics::fonts::Font6x8; use embedded_graphics::prelude::*; use embedded_graphics::primitives::{Circle, Line}; +use embedded_graphics::Drawing; use lin_hal::spidev::{self, SpidevOptions}; use lin_hal::{Pin, Spidev};