Skip to content

Commit 6e8fc39

Browse files
committed
add embedded-graphics crate, but compiling error
1 parent 737d07d commit 6e8fc39

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

user/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ buddy_system_allocator = "0.6"
1111
bitflags = "1.2.1"
1212
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
1313
embedded-graphics = "0.7.1"
14+
# lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
1415

1516
[profile.release]
1617
debug = true

user/src/bin/embed_graph.rs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use user_lib::{framebuffer, framebuffer_flush};
5+
6+
#[macro_use]
7+
extern crate user_lib;
8+
9+
extern crate alloc;
10+
11+
use alloc::sync::Arc;
12+
use embedded_graphics::pixelcolor::Rgb888;
13+
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
14+
use embedded_graphics::primitives::Primitive;
15+
use embedded_graphics::primitives::{PrimitiveStyle, Rectangle};
16+
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};
17+
18+
pub const VIRTGPU_XRES: usize = 1280;
19+
pub const VIRTGPU_YRES: usize = 800;
20+
pub const VIRTGPU_LEN: usize = VIRTGPU_XRES * VIRTGPU_YRES * 4;
21+
22+
const INIT_X: i32 = 640;
23+
const INIT_Y: i32 = 400;
24+
const RECT_SIZE: u32 = 40;
25+
26+
// lazy_static::lazy_static! {
27+
// pub static ref FB: Arc<Display> = Arc::new(Display::new(Size::new(VIRTGPU_XRES as u32, VIRTGPU_YRES as u32), Point::new(INIT_X, INIT_Y)));
28+
// }
29+
30+
#[derive(Clone)]
31+
pub struct Display {
32+
pub size: Size,
33+
pub point: Point,
34+
pub fb: Arc<&'static mut [u8]>,
35+
}
36+
37+
impl Display {
38+
pub fn new(size: Size, point: Point) -> Self {
39+
let fb_ptr = framebuffer() as *mut u8;
40+
println!(
41+
"Hello world from user mode program! 0x{:X} , len {}",
42+
fb_ptr as usize, VIRTGPU_LEN
43+
);
44+
let fb =
45+
unsafe { Arc::new(core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize)) };
46+
47+
Self { size, point, fb }
48+
}
49+
// pub fn reset(&self) {
50+
// let fb = self.drv.get_framebuffer();
51+
// fb.fill(0u8);
52+
// }
53+
}
54+
55+
impl OriginDimensions for Display {
56+
fn size(&self) -> Size {
57+
self.size
58+
}
59+
}
60+
61+
impl DrawTarget for Display {
62+
type Color = Rgb888;
63+
64+
type Error = core::convert::Infallible;
65+
66+
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
67+
where
68+
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
69+
{
70+
let fb = self.fb.clone();
71+
//let mut arc_data_mut = Arc::make_mut(fb);
72+
pixels.into_iter().for_each(|px| {
73+
let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x)
74+
as usize
75+
* 4;
76+
if idx + 2 >= fb.len() {
77+
return;
78+
}
79+
fb[idx] = px.1.b();
80+
fb[idx + 1] = px.1.g();
81+
fb[idx + 2] = px.1.r();
82+
});
83+
framebuffer_flush();
84+
Ok(())
85+
}
86+
}
87+
88+
pub struct DrawingBoard {
89+
disp: Display,
90+
latest_pos: Point,
91+
}
92+
93+
impl DrawingBoard {
94+
pub fn new() -> Self {
95+
Self {
96+
disp: Display::new(Size::new(1280, 800), Point::new(0, 0)),
97+
latest_pos: Point::new(INIT_X, INIT_Y),
98+
}
99+
}
100+
fn paint(&mut self) {
101+
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
102+
.into_styled(PrimitiveStyle::with_stroke(Rgb888::WHITE, 1))
103+
.draw(&mut self.disp)
104+
.ok();
105+
}
106+
fn unpaint(&mut self) {
107+
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
108+
.into_styled(PrimitiveStyle::with_stroke(Rgb888::BLACK, 1))
109+
.draw(&mut self.disp)
110+
.ok();
111+
}
112+
pub fn move_rect(&mut self, dx: i32, dy: i32) {
113+
self.unpaint();
114+
self.latest_pos.x += dx;
115+
self.latest_pos.y += dy;
116+
self.paint();
117+
}
118+
119+
// pub fn reset(&mut self) {
120+
// self.latest_pos = Point::new(INIT_X, INIT_Y);
121+
// self.disp.reset();
122+
// }
123+
}
124+
125+
// lazy_static! {
126+
// pub static ref DRAWING_BOARD: UPIntrFreeCell<DrawingBoard> =
127+
// unsafe { UPIntrFreeCell::new(DrawingBoard::new()) };
128+
// }
129+
130+
// pub fn init_paint() {
131+
// DRAWING_BOARD.exclusive_session(|ripple| {
132+
// ripple.paint();
133+
// });
134+
// }
135+
136+
// pub fn move_rect(dx: i32, dy: i32) {
137+
// DRAWING_BOARD.exclusive_session(|ripple| {
138+
// ripple.move_rect(dx, dy);
139+
// });
140+
// }
141+
142+
// pub fn reset() {
143+
// DRAWING_BOARD.exclusive_session(|ripple| {
144+
// ripple.reset();
145+
// });
146+
// }
147+
148+
#[no_mangle]
149+
pub fn main() -> i32 {
150+
// let fb_ptr = framebuffer() as *mut u8;
151+
let mut board=DrawingBoard::new();
152+
board.paint();
153+
for i in 0..100 {
154+
board.latest_pos.x += i;
155+
board.latest_pos.y += i;
156+
board.paint();
157+
}
158+
// println!(
159+
// "Hello world from user mode program! 0x{:X} , len {}",
160+
// fb_ptr as usize, VIRTGPU_LEN
161+
// );
162+
// let fb = unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) };
163+
// for y in 0..800 {
164+
// for x in 0..1280 {
165+
// let idx = (y * 1280 + x) * 4;
166+
// fb[idx] = x as u8;
167+
// fb[idx + 1] = y as u8;
168+
// fb[idx + 2] = (x + y) as u8;
169+
// }
170+
// }
171+
// framebuffer_flush();
172+
0
173+
}

0 commit comments

Comments
 (0)