examples: graphics-server: use try_ calls for messaging

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-30 17:40:29 +08:00
parent d057cba0fb
commit 8d438e4796
3 changed files with 40 additions and 34 deletions

View file

@ -106,31 +106,33 @@ impl DrawTarget<BinaryColor> for XousDisplay {
/// Draw a `Pixel` that has a color defined as `BinaryColor`.
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
let Pixel(coord, color) = pixel;
let framebuffer = self.fb.as_mut_ptr() as *mut u32;
let offset = (coord.x / 32 + coord.y * FB_WIDTH_WORDS as i32) as usize;
match color {
BinaryColor::Off => {
unsafe {
framebuffer.add(offset).write_volatile(
framebuffer.add(offset).read_volatile() | (1 << (coord.x % 32)),
)
};
if coord.x < FB_WIDTH_PIXELS as _ && coord.y < FB_LINES as _ {
let framebuffer = self.fb.as_mut_ptr() as *mut u32;
let offset = (coord.x / 32 + coord.y * FB_WIDTH_WORDS as i32) as usize;
match color {
BinaryColor::Off => {
unsafe {
framebuffer.add(offset).write_volatile(
framebuffer.add(offset).read_volatile() | (1 << (coord.x % 32)),
)
};
}
BinaryColor::On => {
unsafe {
framebuffer.add(offset).write_volatile(
framebuffer.add(offset).read_volatile() & !(1 << (coord.x % 32)),
)
};
}
}
BinaryColor::On => {
unsafe {
framebuffer.add(offset).write_volatile(
framebuffer.add(offset).read_volatile() & !(1 << (coord.x % 32)),
)
};
}
}
// set the dirty bit on the line
unsafe {
framebuffer
.add(offset)
.write_volatile(framebuffer.add(offset).read_volatile() | 0x1_0000)
};
// set the dirty bit on the line
unsafe {
framebuffer
.add(offset)
.write_volatile(framebuffer.add(offset).read_volatile() | 0x1_0000)
};
}
Ok(())
}

View file

@ -59,11 +59,15 @@ impl DrawTarget<BinaryColor> for XousDisplay {
/// Draw a `Pixel` that has a color defined as `Gray8`.
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
let Pixel(point, color) = pixel;
self.buffer[(point.y * (WIDTH as i32) + point.x) as usize] = if color == BinaryColor::On {
LIGHT_COLOUR
} else {
DARK_COLOUR
};
let w = WIDTH as _;
let h = HEIGHT as _;
if point.x < w && point.y < h && point.x >= 0 && point.y >= 0 {
self.buffer[(point.y * w + point.x) as usize] = if color == BinaryColor::On {
LIGHT_COLOUR
} else {
DARK_COLOUR
};
}
Ok(())
}

View file

@ -3,24 +3,24 @@
pub mod api;
pub use api::{Point, Color};
use xous::{send_message, CID};
use xous::{try_send_message, CID};
pub fn draw_line(cid: CID, start: Point, end: Point) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Line(start, end).into())
try_send_message(cid, api::Opcode::Line(start, end).into())
}
pub fn draw_circle(cid: CID, center: Point, radius: u32) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Circle(center, radius).into())
try_send_message(cid, api::Opcode::Circle(center, radius).into())
}
pub fn draw_rectangle(cid: CID, start: Point, end: Point) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Rectangle(start, end).into())
try_send_message(cid, api::Opcode::Rectangle(start, end).into())
}
pub fn set_style(cid: CID, width: u32, stroke: Color, fill: Color) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Style(width, stroke, fill).into())
try_send_message(cid, api::Opcode::Style(width, stroke, fill).into())
}
pub fn flush(cid: CID) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Flush.into())
try_send_message(cid, api::Opcode::Flush.into())
}