rust: Update for embedded-graphics 0.6.0-alpha.2

This commit is contained in:
Wladimir J. van der Laan 2019-09-16 14:51:19 +00:00
parent ce5c41f0da
commit ff064f596f

View File

@ -6,11 +6,11 @@
use embedded_graphics::fonts::Font6x8;
use embedded_graphics::image::ImageBmp;
use embedded_graphics::pixelcolor::raw::{RawData, RawU16};
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{Circle, Rectangle};
use embedded_graphics::Drawing;
use embedded_graphics::{icoord, text_6x8};
use embedded_graphics::{text_6x8, Drawing};
use k210_hal::prelude::*;
use k210_hal::stdout::Stdout;
use k210_hal::Peripherals;
@ -72,9 +72,11 @@ impl Drawing<Rgb565> for Display {
let data =
unsafe { core::slice::from_raw_parts_mut(self.data.as_ptr() as *mut u16, DISP_PIXELS) };
for Pixel(coord, color) in item {
if coord[0] < (DISP_WIDTH as u32) && coord[1] < (DISP_HEIGHT as u32) {
let index = (coord[0] ^ 1) + (coord[1] * (DISP_WIDTH as u32));
data[index as usize] = u16::from(color);
let x = coord[0] as usize;
let y = coord[1] as usize;
if x < (DISP_WIDTH as usize) && y < (DISP_HEIGHT as usize) {
let index = (x ^ 1) + (y * (DISP_WIDTH as usize));
data[index] = RawU16::from(color).into_inner();
}
}
}
@ -113,21 +115,21 @@ fn main() -> ! {
let t = Font6x8::render_str("Hello Rust!")
.fill(Some(Rgb565::GREEN))
.translate(Coord::new(20, 16));
.translate(Point::new(20, 16));
let image: ImageBmp<Rgb565> = ImageBmp::new(include_bytes!("./rust-pride.bmp"))
.unwrap()
.translate(Coord::new(100, 20));
.translate(Point::new(100, 20));
let mut coord = icoord!(20, 20);
let mut dir = icoord!(3, 3);
let mut coord = Point::new(20, 20);
let mut dir = Point::new(3, 3);
loop {
// clear screen
// shouldn't really be necessary to clear the entire screen every frame
// then again it redraws everything anyway
display.draw(
Rectangle::new(
icoord!(0, 0),
icoord!(i32::from(DISP_WIDTH), i32::from(DISP_HEIGHT)),
Point::new(0, 0),
Point::new(i32::from(DISP_WIDTH), i32::from(DISP_HEIGHT)),
)
.fill(Some(Rgb565::BLACK)),
);
@ -140,7 +142,7 @@ fn main() -> ! {
display.draw(
text_6x8!("Hello world! - no background", stroke = Some(Rgb565::WHITE))
.translate(icoord!(15, 115)),
.translate(Point::new(15, 115)),
);
display.draw(
@ -149,7 +151,7 @@ fn main() -> ! {
stroke = Some(Rgb565::YELLOW),
fill = Some(Rgb565::BLUE)
)
.translate(icoord!(15, 130)),
.translate(Point::new(15, 130)),
);
display.draw(
@ -158,23 +160,23 @@ fn main() -> ! {
stroke = Some(Rgb565::BLUE),
fill = Some(Rgb565::YELLOW)
)
.translate(icoord!(15, 145)),
.translate(Point::new(15, 145)),
);
display.flush(&lcd);
coord += dir;
if coord.0 > i32::from(DISP_WIDTH) {
dir.0 = -(dir.0.abs() + 1);
if coord.x > i32::from(DISP_WIDTH) {
dir.x = -(dir.x.abs() + 1);
}
if coord.1 > i32::from(DISP_HEIGHT) {
dir.1 = -(dir.1.abs() + 1);
if coord.y > i32::from(DISP_HEIGHT) {
dir.y = -(dir.y.abs() + 1);
}
if coord.0 < 0 {
dir.0 = dir.0.abs() + 1;
if coord.x < 0 {
dir.x = dir.x.abs() + 1;
}
if coord.1 < 0 {
dir.1 = dir.1.abs() + 1;
if coord.y < 0 {
dir.y = dir.y.abs() + 1;
}
}
}