graphics: add embedded-graphics

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-14 18:46:23 +08:00
parent 6abe2af34e
commit dc3dc20bd3
2 changed files with 126 additions and 38 deletions

View file

@ -1,3 +1,4 @@
use embedded_graphics::{drawable::Pixel, geometry::Size, pixelcolor::BinaryColor, DrawTarget};
use minifb::{Key, Window, WindowOptions}; use minifb::{Key, Window, WindowOptions};
const WIDTH: usize = 336; const WIDTH: usize = 336;
@ -6,40 +7,67 @@ const MAX_FPS: u64 = 15;
const LIGHT_COLOUR: u32 = 0xB5B5AD; const LIGHT_COLOUR: u32 = 0xB5B5AD;
const DARK_COLOUR: u32 = 0x1B1B19; const DARK_COLOUR: u32 = 0x1B1B19;
pub fn run() { pub struct XousDisplay {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT]; buffer: Vec<u32>, //[u32; WIDTH * HEIGHT],
window: Window,
let mut window = Window::new( }
"Betrusted",
WIDTH, impl XousDisplay {
HEIGHT, pub fn new() -> XousDisplay {
WindowOptions { let mut window = Window::new(
scale_mode: minifb::ScaleMode::AspectRatioStretch, "Betrusted",
resize: true, WIDTH,
..WindowOptions::default() HEIGHT,
}, WindowOptions {
) scale_mode: minifb::ScaleMode::AspectRatioStretch,
.unwrap_or_else(|e| { resize: true,
panic!("{}", e); ..WindowOptions::default()
}); },
)
// Limit the maximum refresh rate .unwrap_or_else(|e| {
window.limit_update_rate(Some(std::time::Duration::from_micros( panic!("{}", e);
1000 * 1000 / MAX_FPS, });
)));
// Limit the maximum refresh rate
let mut lfsr = 0xace1u32; window.limit_update_rate(Some(std::time::Duration::from_micros(
while window.is_open() && !window.is_key_down(Key::Escape) { 1000 * 1000 / MAX_FPS,
for i in buffer.iter_mut() { )));
lfsr ^= lfsr >> 7;
lfsr ^= lfsr << 9; let buffer = vec![0u32; WIDTH * HEIGHT];
lfsr ^= lfsr >> 13; window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
*i = if lfsr & 1 == 0 { LIGHT_COLOUR } else { DARK_COLOUR };
} XousDisplay { buffer, window }
}
// We unwrap here as we want this code to exit if it fails.
// Real applications may want to handle this in a different way pub fn redraw(&mut self) {
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap(); self.window
} .update_with_buffer(&self.buffer, WIDTH, HEIGHT)
std::process::exit(0); .unwrap();
}
pub fn update(&mut self) {
self.window.update();
if !self.window.is_open() || self.window.is_key_down(Key::Escape) {
std::process::exit(0);
}
}
}
impl DrawTarget<BinaryColor> for XousDisplay {
type Error = core::convert::Infallible;
/// Draw a `Pixel` that has a color defined as `Gray8`.
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
let Pixel(point, color) = pixel;
self.buffer[(point.y * (WIDTH as i32) + point.x) as usize] = if color == BinaryColor::On {
LIGHT_COLOUR
} else {
DARK_COLOUR
};
Ok(())
}
fn size(&self) -> Size {
Size::new(WIDTH as _, HEIGHT as _)
}
} }

View file

@ -2,9 +2,69 @@
#![cfg_attr(target_os = "none", no_main)] #![cfg_attr(target_os = "none", no_main)]
mod backend; mod backend;
use backend::XousDisplay;
use embedded_graphics::{
fonts::{Font6x8, Text},
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::{PrimitiveStyle, TextStyle},
};
#[xous::xous_main] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {
backend::run(); // Create a new monochrome simulator display with 128x64 pixels.
loop {}; let mut display = XousDisplay::new();
// Create styles used by the drawing operations.
let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1);
let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3);
let fill = PrimitiveStyle::with_fill(BinaryColor::On);
let text_style = TextStyle::new(Font6x8, BinaryColor::On);
let yoffset = 10;
// Draw a 3px wide outline around the display.
let bottom_right = Point::zero() + display.size() - Point::new(1, 1);
Rectangle::new(Point::zero(), bottom_right)
.into_styled(thick_stroke)
.draw(&mut display)
.unwrap();
// Draw a triangle.
Triangle::new(
Point::new(16, 16 + yoffset),
Point::new(16 + 16, 16 + yoffset),
Point::new(16 + 8, yoffset),
)
.into_styled(thin_stroke)
.draw(&mut display)
.unwrap();
// Draw a filled square
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
.into_styled(fill)
.draw(&mut display)
.unwrap();
// Draw a circle with a 3px wide stroke.
Circle::new(Point::new(96, yoffset + 8), 8)
.into_styled(thick_stroke)
.draw(&mut display)
.unwrap();
// Draw centered text.
let text = "embedded-graphics";
let width = text.len() as i32 * 6;
Text::new(text, Point::new(64 - width / 2, 40))
.into_styled(text_style)
.draw(&mut display)
.unwrap();
display.redraw();
loop {
display.update();
}
} }