|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +#[macro_use] |
| 5 | +extern crate user_lib; |
| 6 | +extern crate alloc; |
| 7 | + |
| 8 | +use user_lib::console::getchar; |
| 9 | +use user_lib::{framebuffer, framebuffer_flush}; |
| 10 | + |
| 11 | +use embedded_graphics::pixelcolor::Rgb888; |
| 12 | +use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size}; |
| 13 | +use embedded_graphics::primitives::Primitive; |
| 14 | +use embedded_graphics::primitives::{PrimitiveStyle, Rectangle}; |
| 15 | +use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions}; |
| 16 | + |
| 17 | +pub const VIRTGPU_XRES: usize = 1280; |
| 18 | +pub const VIRTGPU_YRES: usize = 800; |
| 19 | +pub const VIRTGPU_LEN: usize = VIRTGPU_XRES * VIRTGPU_YRES * 4; |
| 20 | + |
| 21 | +const INIT_X: i32 = 640; |
| 22 | +const INIT_Y: i32 = 400; |
| 23 | +const RECT_SIZE: u32 = 40; |
| 24 | + |
| 25 | +pub struct Display { |
| 26 | + pub size: Size, |
| 27 | + pub point: Point, |
| 28 | + //pub fb: Arc<&'static mut [u8]>, |
| 29 | + pub fb: &'static mut [u8], |
| 30 | +} |
| 31 | + |
| 32 | +impl Display { |
| 33 | + pub fn new(size: Size, point: Point) -> Self { |
| 34 | + let fb_ptr = framebuffer() as *mut u8; |
| 35 | + println!( |
| 36 | + "Hello world from user mode program! 0x{:X} , len {}", |
| 37 | + fb_ptr as usize, VIRTGPU_LEN |
| 38 | + ); |
| 39 | + let fb = |
| 40 | + unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) }; |
| 41 | + Self { size, point, fb } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl OriginDimensions for Display { |
| 46 | + fn size(&self) -> Size { |
| 47 | + self.size |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl DrawTarget for Display { |
| 52 | + type Color = Rgb888; |
| 53 | + |
| 54 | + type Error = core::convert::Infallible; |
| 55 | + |
| 56 | + fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error> |
| 57 | + where |
| 58 | + I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>, |
| 59 | + { |
| 60 | + pixels.into_iter().for_each(|px| { |
| 61 | + let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x) |
| 62 | + as usize |
| 63 | + * 4; |
| 64 | + if idx + 2 >= self.fb.len() { |
| 65 | + return; |
| 66 | + } |
| 67 | + self.fb[idx] = px.1.b(); |
| 68 | + self.fb[idx + 1] = px.1.g(); |
| 69 | + self.fb[idx + 2] = px.1.r(); |
| 70 | + }); |
| 71 | + framebuffer_flush(); |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub struct DrawingBoard { |
| 77 | + disp: Display, |
| 78 | + latest_pos: Point, |
| 79 | +} |
| 80 | + |
| 81 | +impl DrawingBoard { |
| 82 | + pub fn new() -> Self { |
| 83 | + Self { |
| 84 | + disp: Display::new(Size::new(1280, 800), Point::new(0, 0)), |
| 85 | + latest_pos: Point::new(INIT_X, INIT_Y), |
| 86 | + } |
| 87 | + } |
| 88 | + fn paint(&mut self) { |
| 89 | + Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE)) |
| 90 | + .into_styled(PrimitiveStyle::with_stroke(Rgb888::WHITE, 1)) |
| 91 | + .draw(&mut self.disp) |
| 92 | + .ok(); |
| 93 | + } |
| 94 | + fn unpaint(&mut self) { |
| 95 | + Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE)) |
| 96 | + .into_styled(PrimitiveStyle::with_stroke(Rgb888::BLACK, 1)) |
| 97 | + .draw(&mut self.disp) |
| 98 | + .ok(); |
| 99 | + } |
| 100 | + pub fn move_rect(&mut self, dx: i32, dy: i32) { |
| 101 | + self.unpaint(); |
| 102 | + self.latest_pos.x += dx; |
| 103 | + self.latest_pos.y += dy; |
| 104 | + self.paint(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +const LF: u8 = 0x0au8; |
| 109 | +const CR: u8 = 0x0du8; |
| 110 | +#[no_mangle] |
| 111 | +pub fn main() -> i32 { |
| 112 | + // let fb_ptr = framebuffer() as *mut u8; |
| 113 | + let mut board = DrawingBoard::new(); |
| 114 | + for i in 0..20 { |
| 115 | + let c=getchar(); |
| 116 | + if c == LF || c == CR { |
| 117 | + break; |
| 118 | + } |
| 119 | + board.latest_pos.x += i; |
| 120 | + board.latest_pos.y += i; |
| 121 | + board.paint(); |
| 122 | + } |
| 123 | + 0 |
| 124 | +} |
0 commit comments