Update embedded_graphics and profont dependencies to latest versions.

This commit is contained in:
Constantine Verutin 2020-07-19 01:42:24 +01:00 committed by Constantine Verutin
parent b1ec1762b1
commit 599839c084
4 changed files with 102 additions and 88 deletions

View file

@ -26,7 +26,7 @@ version = "0.2.3"
[dependencies.embedded-graphics] [dependencies.embedded-graphics]
optional = true optional = true
version = "0.5.2" version = "0.6.2"
[dependencies.linux-embedded-hal] [dependencies.linux-embedded-hal]
optional = true optional = true
@ -34,7 +34,7 @@ version = "0.3.0"
[dependencies.profont] [dependencies.profont]
optional = true optional = true
version = "0.3.0" version = "0.4.0"
[features] [features]
default = ["graphics"] default = ["graphics"]

View file

@ -8,10 +8,9 @@ extern crate ssd1675;
use ssd1675::{Builder, Color, Dimensions, Display, GraphicDisplay, Rotation}; use ssd1675::{Builder, Color, Dimensions, Display, GraphicDisplay, Rotation};
// Graphics // Graphics
#[macro_use]
extern crate embedded_graphics; extern crate embedded_graphics;
use embedded_graphics::coord::Coord;
use embedded_graphics::prelude::*; use embedded_graphics::prelude::*;
use embedded_graphics::Drawing;
// Font // Font
extern crate profont; extern crate profont;
@ -117,49 +116,69 @@ fn main() -> Result<(), std::io::Error> {
display.clear(Color::White); display.clear(Color::White);
println!("Clear"); println!("Clear");
display.draw( egtext!(
ProFont24Point::render_str("Raspberry Pi") text = "Raspberry Pi",
.stroke(Some(Color::Red)) top_left = (1, -4),
.fill(Some(Color::White)) style = text_style!(
.translate(Coord::new(1, -4)) font = ProFont24Point,
.into_iter(), background_color = Color::White,
); text_color = Color::Red,
)
)
.draw(&mut display)
.expect("error drawing text");
if let Ok(cpu_temp) = read_cpu_temp() { if let Ok(cpu_temp) = read_cpu_temp() {
display.draw( egtext!(
ProFont14Point::render_str("CPU Temp:") text = "CPU Temp:",
.stroke(Some(Color::Black)) top_left = (1, 30),
.fill(Some(Color::White)) style = text_style!(
.translate(Coord::new(1, 30)) font = ProFont14Point,
.into_iter(), background_color = Color::White,
); text_color = Color::Black,
display.draw( )
ProFont12Point::render_str(&format!("{:.1}°C", cpu_temp)) )
.stroke(Some(Color::Black)) .draw(&mut display)
.fill(Some(Color::White)) .expect("error drawing text");
.translate(Coord::new(95, 34)) egtext!(
.into_iter(), 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() { if let Some(uptime) = read_uptime() {
display.draw( egtext!(
ProFont9Point::render_str(uptime.trim()) text = uptime.trim(),
.stroke(Some(Color::Black)) top_left = (1, 93),
.fill(Some(Color::White)) style = text_style!(
.translate(Coord::new(1, 93)) font = ProFont9Point,
.into_iter(), background_color = Color::White,
); text_color = Color::Black,
)
)
.draw(&mut display)
.expect("error drawing text");
} }
if let Some(uname) = read_uname() { if let Some(uname) = read_uname() {
display.draw( egtext!(
ProFont9Point::render_str(uname.trim()) text = uname.trim(),
.stroke(Some(Color::Black)) top_left = (1, 84),
.fill(Some(Color::White)) style = text_style!(
.translate(Coord::new(1, 84)) font = ProFont9Point,
.into_iter(), background_color = Color::White,
); text_color = Color::Black,
)
)
.draw(&mut display)
.expect("error drawing text");
} }
display.update(&mut delay).expect("error updating display"); display.update(&mut delay).expect("error updating display");

View file

@ -9,9 +9,13 @@ pub enum Color {
#[cfg(feature = "graphics")] #[cfg(feature = "graphics")]
extern crate embedded_graphics; extern crate embedded_graphics;
#[cfg(feature = "graphics")] #[cfg(feature = "graphics")]
use self::embedded_graphics::pixelcolor::raw::RawU8;
#[cfg(feature = "graphics")]
use self::embedded_graphics::prelude::*; use self::embedded_graphics::prelude::*;
#[cfg(feature = "graphics")] #[cfg(feature = "graphics")]
impl PixelColor for Color {} impl PixelColor for Color {
type Raw = RawU8;
}
impl From<u8> for Color { impl From<u8> for Color {
fn from(value: u8) -> Self { fn from(value: u8) -> Self {

View file

@ -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")] #[cfg(feature = "graphics")]
extern crate embedded_graphics; extern crate embedded_graphics;
#[cfg(feature = "graphics")] #[cfg(feature = "graphics")]
use self::embedded_graphics::{drawable::Pixel, prelude::UnsignedCoord, Drawing}; use self::embedded_graphics::prelude::*;
#[cfg(feature = "graphics")] #[cfg(feature = "graphics")]
impl<'a, I> Drawing<Color> for GraphicDisplay<'a, I> impl<'a, I> DrawTarget<Color> for GraphicDisplay<'a, I>
where where
I: DisplayInterface, I: DisplayInterface,
{ {
fn draw<T>(&mut self, item_pixels: T) type Error = core::convert::Infallible;
where
T: IntoIterator<Item = Pixel<Color>>, fn draw_pixel(
{ &mut self,
for Pixel(UnsignedCoord(x, y), colour) in item_pixels { Pixel(Point { x, y }, color): Pixel<Color>,
if outside_display( ) -> Result<(), Self::Error> {
x, let sz = self.size();
y, let x = x as u32;
self.cols() as u32, let y = y as u32;
self.rows() as u32, if x < sz.width && y < sz.height {
self.rotation(), self.set_pixel(x, y, color)
) { }
continue; Ok(())
} }
self.set_pixel(x, y, colour); 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)] #[cfg(test)]
mod tests { mod tests {
use self::embedded_graphics::coord::Coord; use self::embedded_graphics::{egrectangle, primitive_style};
use self::embedded_graphics::prelude::*;
use self::embedded_graphics::primitives::Rectangle;
use self::embedded_graphics::Drawing;
use super::*; use super::*;
use {Builder, Color, Dimensions, Display, DisplayInterface, GraphicDisplay, Rotation}; use {Builder, Color, Dimensions, Display, DisplayInterface, GraphicDisplay, Rotation};
@ -278,11 +265,13 @@ mod tests {
let mut display = let mut display =
GraphicDisplay::new(build_mock_display(), &mut black_buffer, &mut red_buffer); GraphicDisplay::new(build_mock_display(), &mut black_buffer, &mut red_buffer);
display.draw( egrectangle!(
Rectangle::new(Coord::new(0, 0), Coord::new(2, 2)) top_left = (0, 0),
.stroke(Some(Color::White)) bottom_right = (2, 2),
.into_iter(), style = primitive_style!(stroke_color = Color::White, stroke_width = 1)
); )
.draw(&mut display)
.unwrap()
} }
#[rustfmt::skip] #[rustfmt::skip]
@ -305,11 +294,13 @@ mod tests {
let mut display = let mut display =
GraphicDisplay::new(build_mock_display(), &mut black_buffer, &mut red_buffer); GraphicDisplay::new(build_mock_display(), &mut black_buffer, &mut red_buffer);
display.draw( egrectangle!(
Rectangle::new(Coord::new(0, 0), Coord::new(2, 2)) top_left = (0, 0),
.stroke(Some(Color::Red)) bottom_right = (2, 2),
.into_iter(), style = primitive_style!(stroke_color = Color::Red, stroke_width = 1)
); )
.draw(&mut display)
.unwrap();
} }
#[rustfmt::skip] #[rustfmt::skip]