diff --git a/Cargo.toml b/Cargo.toml index e0b8e13..b2fda10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ version = "0.2.3" [dependencies.embedded-graphics] optional = true -version = "0.5.2" +version = "0.6.2" [dependencies.linux-embedded-hal] optional = true @@ -34,7 +34,7 @@ version = "0.3.0" [dependencies.profont] optional = true -version = "0.3.0" +version = "0.4.0" [features] default = ["graphics"] diff --git a/examples/raspberry_pi_inky_phat.rs b/examples/raspberry_pi_inky_phat.rs index 7b4f199..498357d 100644 --- a/examples/raspberry_pi_inky_phat.rs +++ b/examples/raspberry_pi_inky_phat.rs @@ -8,10 +8,9 @@ extern crate ssd1675; use ssd1675::{Builder, Color, Dimensions, Display, GraphicDisplay, Rotation}; // Graphics +#[macro_use] extern crate embedded_graphics; -use embedded_graphics::coord::Coord; use embedded_graphics::prelude::*; -use embedded_graphics::Drawing; // Font extern crate profont; @@ -117,49 +116,69 @@ fn main() -> Result<(), std::io::Error> { display.clear(Color::White); println!("Clear"); - display.draw( - ProFont24Point::render_str("Raspberry Pi") - .stroke(Some(Color::Red)) - .fill(Some(Color::White)) - .translate(Coord::new(1, -4)) - .into_iter(), - ); + egtext!( + text = "Raspberry Pi", + top_left = (1, -4), + style = text_style!( + font = ProFont24Point, + background_color = Color::White, + text_color = Color::Red, + ) + ) + .draw(&mut display) + .expect("error drawing text"); if let Ok(cpu_temp) = read_cpu_temp() { - display.draw( - ProFont14Point::render_str("CPU Temp:") - .stroke(Some(Color::Black)) - .fill(Some(Color::White)) - .translate(Coord::new(1, 30)) - .into_iter(), - ); - display.draw( - ProFont12Point::render_str(&format!("{:.1}°C", cpu_temp)) - .stroke(Some(Color::Black)) - .fill(Some(Color::White)) - .translate(Coord::new(95, 34)) - .into_iter(), - ); + egtext!( + text = "CPU Temp:", + top_left = (1, 30), + style = text_style!( + font = ProFont14Point, + background_color = Color::White, + text_color = Color::Black, + ) + ) + .draw(&mut display) + .expect("error drawing text"); + egtext!( + text = &format!("{:.1}°C", cpu_temp), + top_left = (95, 34), + style = text_style!( + font = ProFont12Point, + background_color = Color::White, + text_color = Color::Black, + ) + ) + .draw(&mut display) + .expect("error drawing text"); } if let Some(uptime) = read_uptime() { - display.draw( - ProFont9Point::render_str(uptime.trim()) - .stroke(Some(Color::Black)) - .fill(Some(Color::White)) - .translate(Coord::new(1, 93)) - .into_iter(), - ); + egtext!( + text = uptime.trim(), + top_left = (1, 93), + style = text_style!( + font = ProFont9Point, + background_color = Color::White, + text_color = Color::Black, + ) + ) + .draw(&mut display) + .expect("error drawing text"); } if let Some(uname) = read_uname() { - display.draw( - ProFont9Point::render_str(uname.trim()) - .stroke(Some(Color::Black)) - .fill(Some(Color::White)) - .translate(Coord::new(1, 84)) - .into_iter(), - ); + egtext!( + text = uname.trim(), + top_left = (1, 84), + style = text_style!( + font = ProFont9Point, + background_color = Color::White, + text_color = Color::Black, + ) + ) + .draw(&mut display) + .expect("error drawing text"); } display.update(&mut delay).expect("error updating display"); diff --git a/src/color.rs b/src/color.rs index 83d13c2..c8edc2e 100644 --- a/src/color.rs +++ b/src/color.rs @@ -9,9 +9,13 @@ pub enum Color { #[cfg(feature = "graphics")] extern crate embedded_graphics; #[cfg(feature = "graphics")] +use self::embedded_graphics::pixelcolor::raw::RawU8; +#[cfg(feature = "graphics")] use self::embedded_graphics::prelude::*; #[cfg(feature = "graphics")] -impl PixelColor for Color {} +impl PixelColor for Color { + type Raw = RawU8; +} impl From for Color { fn from(value: u8) -> Self { diff --git a/src/graphics.rs b/src/graphics.rs index b6c2fcd..a263d30 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -124,59 +124,46 @@ fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: Rotation) -> (u32 } } -fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: Rotation) -> bool { - match rotation { - Rotation::Rotate0 | Rotation::Rotate180 => { - if x >= width || y >= height { - return true; - } - } - Rotation::Rotate90 | Rotation::Rotate270 => { - if y >= width || x >= height { - return true; - } - } - } - - false -} - #[cfg(feature = "graphics")] extern crate embedded_graphics; #[cfg(feature = "graphics")] -use self::embedded_graphics::{drawable::Pixel, prelude::UnsignedCoord, Drawing}; +use self::embedded_graphics::prelude::*; #[cfg(feature = "graphics")] -impl<'a, I> Drawing for GraphicDisplay<'a, I> +impl<'a, I> DrawTarget for GraphicDisplay<'a, I> where I: DisplayInterface, { - fn draw(&mut self, item_pixels: T) - where - T: IntoIterator>, - { - for Pixel(UnsignedCoord(x, y), colour) in item_pixels { - if outside_display( - x, - y, - self.cols() as u32, - self.rows() as u32, - self.rotation(), - ) { - continue; - } + type Error = core::convert::Infallible; - self.set_pixel(x, y, colour); + fn draw_pixel( + &mut self, + Pixel(Point { x, y }, color): Pixel, + ) -> Result<(), Self::Error> { + let sz = self.size(); + let x = x as u32; + let y = y as u32; + if x < sz.width && y < sz.height { + self.set_pixel(x, y, color) + } + Ok(()) + } + + fn size(&self) -> Size { + match self.rotation() { + Rotation::Rotate0 | Rotation::Rotate180 => { + Size::new(self.cols().into(), self.rows().into()) + } + Rotation::Rotate90 | Rotation::Rotate270 => { + Size::new(self.rows().into(), self.cols().into()) + } } } } #[cfg(test)] mod tests { - use self::embedded_graphics::coord::Coord; - use self::embedded_graphics::prelude::*; - use self::embedded_graphics::primitives::Rectangle; - use self::embedded_graphics::Drawing; + use self::embedded_graphics::{egrectangle, primitive_style}; use super::*; use {Builder, Color, Dimensions, Display, DisplayInterface, GraphicDisplay, Rotation}; @@ -278,11 +265,13 @@ mod tests { let mut display = GraphicDisplay::new(build_mock_display(), &mut black_buffer, &mut red_buffer); - display.draw( - Rectangle::new(Coord::new(0, 0), Coord::new(2, 2)) - .stroke(Some(Color::White)) - .into_iter(), - ); + egrectangle!( + top_left = (0, 0), + bottom_right = (2, 2), + style = primitive_style!(stroke_color = Color::White, stroke_width = 1) + ) + .draw(&mut display) + .unwrap() } #[rustfmt::skip] @@ -305,11 +294,13 @@ mod tests { let mut display = GraphicDisplay::new(build_mock_display(), &mut black_buffer, &mut red_buffer); - display.draw( - Rectangle::new(Coord::new(0, 0), Coord::new(2, 2)) - .stroke(Some(Color::Red)) - .into_iter(), - ); + egrectangle!( + top_left = (0, 0), + bottom_right = (2, 2), + style = primitive_style!(stroke_color = Color::Red, stroke_width = 1) + ) + .draw(&mut display) + .unwrap(); } #[rustfmt::skip]