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};
const WIDTH: usize = 336;
@ -6,40 +7,67 @@ const MAX_FPS: u64 = 15;
const LIGHT_COLOUR: u32 = 0xB5B5AD;
const DARK_COLOUR: u32 = 0x1B1B19;
pub fn run() {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
let mut window = Window::new(
"Betrusted",
WIDTH,
HEIGHT,
WindowOptions {
scale_mode: minifb::ScaleMode::AspectRatioStretch,
resize: true,
..WindowOptions::default()
},
)
.unwrap_or_else(|e| {
panic!("{}", e);
});
// Limit the maximum refresh rate
window.limit_update_rate(Some(std::time::Duration::from_micros(
1000 * 1000 / MAX_FPS,
)));
let mut lfsr = 0xace1u32;
while window.is_open() && !window.is_key_down(Key::Escape) {
for i in buffer.iter_mut() {
lfsr ^= lfsr >> 7;
lfsr ^= lfsr << 9;
lfsr ^= lfsr >> 13;
*i = if lfsr & 1 == 0 { LIGHT_COLOUR } else { DARK_COLOUR };
}
// We unwrap here as we want this code to exit if it fails.
// Real applications may want to handle this in a different way
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
}
std::process::exit(0);
pub struct XousDisplay {
buffer: Vec<u32>, //[u32; WIDTH * HEIGHT],
window: Window,
}
impl XousDisplay {
pub fn new() -> XousDisplay {
let mut window = Window::new(
"Betrusted",
WIDTH,
HEIGHT,
WindowOptions {
scale_mode: minifb::ScaleMode::AspectRatioStretch,
resize: true,
..WindowOptions::default()
},
)
.unwrap_or_else(|e| {
panic!("{}", e);
});
// Limit the maximum refresh rate
window.limit_update_rate(Some(std::time::Duration::from_micros(
1000 * 1000 / MAX_FPS,
)));
let buffer = vec![0u32; WIDTH * HEIGHT];
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
XousDisplay { buffer, window }
}
pub fn redraw(&mut self) {
self.window
.update_with_buffer(&self.buffer, WIDTH, HEIGHT)
.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)]
mod backend;
use backend::XousDisplay;
use embedded_graphics::{
fonts::{Font6x8, Text},
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::{PrimitiveStyle, TextStyle},
};
#[xous::xous_main]
fn xmain() -> ! {
backend::run();
loop {};
// Create a new monochrome simulator display with 128x64 pixels.
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();
}
}