From d2a47eb05c22b4da1fb35bbea63d73bbf586bb00 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 Oct 2018 22:11:25 +0200 Subject: [PATCH] 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