updates from ch9

This commit is contained in:
Yifan Wu 2023-01-13 08:55:42 +08:00
parent 73fcb72cbb
commit 3183b81f87
23 changed files with 350 additions and 409 deletions

View file

@ -1,77 +1,19 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
extern crate alloc;
use user_lib::{framebuffer, framebuffer_flush};
use user_lib::{VIRTGPU_XRES, VIRTGPU_YRES, Display};
use embedded_graphics::pixelcolor::Rgb888;
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
use embedded_graphics::primitives::Primitive;
use embedded_graphics::primitives::{PrimitiveStyle, Rectangle};
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};
pub const VIRTGPU_XRES: usize = 1280;
pub const VIRTGPU_YRES: usize = 800;
pub const VIRTGPU_LEN: usize = VIRTGPU_XRES * VIRTGPU_YRES * 4;
use embedded_graphics::prelude::{DrawTarget, Drawable, Point, RgbColor, Size};
use embedded_graphics::primitives::{Primitive, PrimitiveStyle, Rectangle};
const INIT_X: i32 = 640;
const INIT_Y: i32 = 400;
const RECT_SIZE: u32 = 40;
pub struct Display {
pub size: Size,
pub point: Point,
//pub fb: Arc<&'static mut [u8]>,
pub fb: &'static mut [u8],
}
impl Display {
pub fn new(size: Size, point: Point) -> Self {
let fb_ptr = framebuffer() as *mut u8;
println!(
"Hello world from user mode program! 0x{:X} , len {}",
fb_ptr as usize, VIRTGPU_LEN
);
let fb =
unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) };
Self { size, point, fb }
}
}
impl OriginDimensions for Display {
fn size(&self) -> Size {
self.size
}
}
impl DrawTarget for Display {
type Color = Rgb888;
type Error = core::convert::Infallible;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
pixels.into_iter().for_each(|px| {
let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x)
as usize
* 4;
if idx + 2 >= self.fb.len() {
return;
}
self.fb[idx] = px.1.b();
self.fb[idx + 1] = px.1.g();
self.fb[idx + 2] = px.1.r();
});
framebuffer_flush();
Ok(())
}
}
pub struct DrawingBoard {
disp: Display,
latest_pos: Point,
@ -80,7 +22,7 @@ pub struct DrawingBoard {
impl DrawingBoard {
pub fn new() -> Self {
Self {
disp: Display::new(Size::new(1280, 800), Point::new(0, 0)),
disp: Display::new(Size::new(VIRTGPU_XRES, VIRTGPU_YRES)),
latest_pos: Point::new(INIT_X, INIT_Y),
}
}
@ -106,7 +48,6 @@ impl DrawingBoard {
#[no_mangle]
pub fn main() -> i32 {
// let fb_ptr = framebuffer() as *mut u8;
let mut board = DrawingBoard::new();
let _ = board.disp.clear(Rgb888::BLACK).unwrap();
for i in 0..20 {

View file

@ -1,27 +1,23 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{framebuffer, framebuffer_flush};
pub const VIRTGPU_XRES: usize = 1280;
pub const VIRTGPU_YRES: usize = 800;
use user_lib::{VIRTGPU_XRES, VIRTGPU_YRES, Display};
use embedded_graphics::prelude::Size;
#[no_mangle]
pub fn main() -> i32 {
let fb_ptr =framebuffer() as *mut u8;
println!("Hello world from user mode program! 0x{:X} , len {}", fb_ptr as usize, VIRTGPU_XRES*VIRTGPU_YRES*4);
let fb= unsafe {core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_XRES*VIRTGPU_YRES*4 as usize)};
for y in 0..800 {
for x in 0..1280 {
let idx = (y * 1280 + x) * 4;
fb[idx] = x as u8;
fb[idx + 1] = y as u8;
fb[idx + 2] = (x + y) as u8;
let mut disp = Display::new(Size::new(VIRTGPU_XRES, VIRTGPU_YRES));
disp.paint_on_framebuffer(|fb| {
for y in 0..VIRTGPU_YRES as usize {
for x in 0..VIRTGPU_XRES as usize {
let idx = (y * VIRTGPU_XRES as usize + x) * 4;
fb[idx] = x as u8;
fb[idx + 1] = y as u8;
fb[idx + 2] = (x + y) as u8;
}
}
}
framebuffer_flush();
});
0
}

View file

@ -1,12 +1,11 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
extern crate alloc;
use user_lib::console::getchar;
use user_lib::{framebuffer, framebuffer_flush, key_pressed, sleep};
use user_lib::{Display, key_pressed, sleep, VIRTGPU_XRES, VIRTGPU_YRES};
use embedded_graphics::pixelcolor::*;
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
@ -16,58 +15,6 @@ use embedded_graphics::Pixel;
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};
use oorandom; //random generator
pub const VIRTGPU_XRES: usize = 1280;
pub const VIRTGPU_YRES: usize = 800;
pub const VIRTGPU_LEN: usize = VIRTGPU_XRES * VIRTGPU_YRES * 4;
pub struct Display {
pub size: Size,
pub point: Point,
pub fb: &'static mut [u8],
}
impl Display {
pub fn new(size: Size, point: Point) -> Self {
let fb_ptr = framebuffer() as *mut u8;
println!(
"Hello world from user mode program! 0x{:X} , len {}",
fb_ptr as usize, VIRTGPU_LEN
);
let fb =
unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) };
Self { size, point, fb }
}
}
impl OriginDimensions for Display {
fn size(&self) -> Size {
self.size
}
}
impl DrawTarget for Display {
type Color = Rgb888;
type Error = core::convert::Infallible;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
pixels.into_iter().for_each(|px| {
let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x)
as usize
* 4;
if idx + 2 >= self.fb.len() {
return;
}
self.fb[idx] = px.1.b();
self.fb[idx + 1] = px.1.g();
self.fb[idx + 2] = px.1.r();
});
framebuffer_flush();
Ok(())
}
}
struct Snake<T: PixelColor, const MAX_SIZE: usize> {
parts: [Pixel<T>; MAX_SIZE],
len: usize,
@ -380,7 +327,7 @@ const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8;
#[no_mangle]
pub fn main() -> i32 {
let mut disp = Display::new(Size::new(1280, 800), Point::new(0, 0));
let mut disp = Display::new(Size::new(VIRTGPU_XRES, VIRTGPU_YRES));
let mut game = SnakeGame::<20, Rgb888>::new(1280, 800, 20, 20, Rgb888::RED, Rgb888::YELLOW, 50);
let _ = disp.clear(Rgb888::BLACK).unwrap();
loop {

View file

@ -1,7 +1,7 @@
#![no_std]
#![no_main]
use user_lib::{event_get};
use user_lib::{event_get, DecodeType, Key, KeyType};
#[macro_use]
extern crate user_lib;
@ -9,13 +9,17 @@ extern crate user_lib;
#[no_mangle]
pub fn main() -> i32 {
println!("Input device event test");
let mut event=0;
for _ in 0..3 {
while event==0 {
event = event_get();
}
println!("event: {:?}", event);
loop {
if let Some(event) = event_get() {
if let Some(decoder_type) = event.decode() {
println!("{:?}", decoder_type);
if let DecodeType::Key(key, keytype) = decoder_type {
if key == Key::Enter && keytype == KeyType::Press {
break;
}
}
}
}
}
0
}