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