From c0842517f65f0e39412b7593603e91e6c5f7f8e4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 3 Dec 2020 01:48:10 +0800 Subject: [PATCH] major refactor of the graphics-server API - remove redundant Rect/Rectangle implementations - refactor Lines and Circles - add DrawStyle to the message passing regimen - depracate modal Styles; now every object has a DrawStyle shipped with it - remove unused/redundant API calls --- Cargo.lock | 3 +- services/graphics-server/Cargo.toml | 4 +- services/graphics-server/src/api.rs | 673 ++++++++------------------- services/graphics-server/src/lib.rs | 45 +- services/graphics-server/src/main.rs | 60 +-- services/graphics-server/src/op.rs | 292 ++++++------ services/shell/Cargo.toml | 4 +- services/shell/src/main.rs | 154 +++--- 8 files changed, 449 insertions(+), 786 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 06f806982..8695ff3d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,8 +67,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "blitstr" -version = "0.2.0" -source = "git+https://github.com/betrusted-io/blitstr.git?rev=7684e03f98cf72d78e7ed148a34aca97a7ee5e25#7684e03f98cf72d78e7ed148a34aca97a7ee5e25" +version = "0.3.0" [[package]] name = "bstr" diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index 8cd23e4da..cb49dc533 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -9,8 +9,8 @@ description = "Manage the video output" xous = { path = "../../xous-rs" } log-server = { path = "../log-server" } log = "0.4" -blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "7684e03f98cf72d78e7ed148a34aca97a7ee5e25" } -# blitstr = { path = "../../../blitstr" } +# blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "7684e03f98cf72d78e7ed148a34aca97a7ee5e25" } +blitstr = { path = "../../../blitstr" } [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/graphics-server/src/api.rs b/services/graphics-server/src/api.rs index 85fa92805..7d5937e1a 100644 --- a/services/graphics-server/src/api.rs +++ b/services/graphics-server/src/api.rs @@ -1,324 +1,112 @@ -use crate::op::PixelColor; use xous::{Message, ScalarMessage}; use core::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign}; -use blitstr::fonts::GlyphSet; -use blitstr::fonts::*; -use blitstr::Cursor; +use blitstr::{GlyphStyle, ClipRect, Cursor}; +use core::cmp::{min, max}; +use crate::op::{WIDTH, HEIGHT}; -/// 2D size. -/// -/// `Size` is used to define the width and height of an object. -/// -/// [Nalgebra] support can be enabled with the `nalgebra_support` feature. This implements -/// `From>` and `From<&Vector2>` where `N` is `Scalar + Into`. This allows use -/// of Nalgebra's [`Vector2`] with embedded-graphics where `u16`, `u16` or `u8` is used for value -/// storage. -/// -/// # Examples -/// -/// ## Create a `Size` from two integers -/// -/// -/// ```rust -/// use embedded_graphics::geometry::Size; -/// -/// // Create a size using the `new` constructor method -/// let s = Size::new(10, 20); -/// ``` -/// -/// ## Create a `Size` from a Nalgebra `Vector2` -/// -/// _Be sure to enable the `nalgebra_support` feature to get [Nalgebra] integration._ -/// -/// Any `Vector2` can be used where `N: Into + nalgebra::Scalar`. This includes the primitive types `u16`, `u16` and `u8`. -/// -/// ```rust -/// # #[cfg(feature = "nalgebra_support")] { -/// use nalgebra::Vector2; -/// use embedded_graphics::geometry::Size; -/// -/// assert_eq!(Size::from(Vector2::new(10u16, 20)), Size::new(10u16, 20)); -/// assert_eq!(Size::from(Vector2::new(10u16, 20)), Size::new(10u16, 20)); -/// assert_eq!(Size::from(Vector2::new(10u8, 20)), Size::new(10u16, 20)); -/// # } -/// ``` -/// -/// `.into()` can also be used, but may require more type annotations: -/// -/// ```rust -/// # #[cfg(feature = "nalgebra_support")] { -/// use nalgebra::Vector2; -/// use embedded_graphics::geometry::Size; -/// -/// let c: Size = Vector2::new(10u16, 20).into(); -/// -/// assert_eq!(c, Size::new(10u16, 20)); -/// # } -/// ``` -/// -/// [`Drawable`]: ../drawable/trait.Drawable.html -/// [`Point`]: struct.Point.html -/// [`Vector2`]: https://docs.rs/nalgebra/0.18.0/nalgebra/base/type.Vector2.html -/// [`Vector2`]: https://docs.rs/nalgebra/0.18.0/nalgebra/base/type.Vector2.html -/// [Nalgebra]: https://docs.rs/nalgebra -#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] -pub struct Size { - /// The width. - pub width: u16, - - /// The height. - pub height: u16, +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum PixelColor { + Dark, + Light, } -impl Size { - /// Creates a size from a width and a height. - pub const fn new(width: u16, height: u16) -> Self { - Size { width, height } - } - - /// Creates a size with width and height equal to zero. - pub const fn zero() -> Self { - Size { - width: 0, - height: 0, - } - } - - /// Creates a size from two corner points of a bounding box. - pub(crate) fn from_bounding_box(corner_1: Point, corner_2: Point) -> Self { - let width = (corner_1.x - corner_2.x).abs() as u16; - let height = (corner_1.y - corner_2.y).abs() as u16; - - Self { width, height } +impl From for PixelColor { + fn from(pc: usize) -> Self { + if pc != 0 { PixelColor::Dark } + else { PixelColor::Light } } } -impl Add for Size { - type Output = Size; - - fn add(self, other: Size) -> Size { - Size::new(self.width + other.width, self.height + other.height) +impl From for PixelColor { + fn from(pc: bool) -> Self { + if pc { PixelColor::Dark } + else { PixelColor::Light } } } -impl AddAssign for Size { - fn add_assign(&mut self, other: Size) { - self.width += other.width; - self.height += other.height; +impl Into for PixelColor { + fn into(self) -> usize { + if self == PixelColor::Dark { 1 } + else { 0 } } } -impl Sub for Size { - type Output = Size; - - fn sub(self, other: Size) -> Size { - Size::new(self.width - other.width, self.height - other.height) +impl Into for PixelColor { + fn into(self) -> bool { + if self == PixelColor::Dark { true } + else { false } } } -impl SubAssign for Size { - fn sub_assign(&mut self, other: Size) { - self.width -= other.width; - self.height -= other.height; - } -} - -impl Index for Size { - type Output = u16; - - fn index(&self, idx: usize) -> &u16 { - match idx { - 0 => &self.width, - 1 => &self.height, - _ => panic!("index out of bounds: the len is 2 but the index is {}", idx), - } - } -} - -impl From<(u16, u16)> for Size { - fn from(other: (u16, u16)) -> Self { - Size::new(other.0, other.1) - } -} - -impl From<[u16; 2]> for Size { - fn from(other: [u16; 2]) -> Self { - Size::new(other[0], other[1]) - } -} - -impl From<&[u16; 2]> for Size { - fn from(other: &[u16; 2]) -> Self { - Size::new(other[0], other[1]) - } -} - -impl From for (u16, u16) { - fn from(other: Size) -> (u16, u16) { - (other.width, other.height) - } -} - -impl From<&Size> for (u16, u16) { - fn from(other: &Size) -> (u16, u16) { - (other.width, other.height) - } -} - -#[cfg(feature = "nalgebra_support")] -use nalgebra::{base::Scalar, Vector2}; - -#[cfg(feature = "nalgebra_support")] -impl From> for Size -where - N: Into + Scalar, -{ - fn from(other: Vector2) -> Self { - Self::new(other[0].into(), other[1].into()) - } -} - -#[cfg(feature = "nalgebra_support")] -impl From<&Vector2> for Size -where - N: Into + Scalar, -{ - fn from(other: &Vector2) -> Self { - Self::new(other[0].into(), other[1].into()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn sizes_can_be_added() { - let left = Size::new(10, 20); - let right = Size::new(30, 40); - - assert_eq!(left + right, Size::new(40, 60)); - } - - #[test] - fn sizes_can_be_subtracted() { - let left = Size::new(30, 40); - let right = Size::new(10, 20); - - assert_eq!(left - right, Size::new(20, 20)); - } - - #[test] - fn from_tuple() { - assert_eq!(Size::from((20, 30)), Size::new(20, 30)); - } - - #[test] - fn from_array() { - assert_eq!(Size::from([20, 30]), Size::new(20, 30)); - } - - #[test] - fn from_array_ref() { - assert_eq!(Size::from(&[20, 30]), Size::new(20, 30)); - } - - #[test] - fn index() { - let size = Size::new(1, 2); - - assert_eq!(size.width, size[0]); - assert_eq!(size.height, size[1]); - } - - #[test] - #[should_panic] - fn index_out_of_bounds() { - let size = Size::new(1, 2); - let _ = size[2]; - } - - #[test] - #[cfg(feature = "nalgebra_support")] - fn nalgebra_support() { - let left = nalgebra::Vector2::new(30u16, 40); - let right = nalgebra::Vector2::new(10, 20); - - assert_eq!(Size::from(left - right), Size::new(20, 20)); - } -} - - - /// Style properties for an object #[derive(Debug, Copy, Clone)] -pub struct Style { +pub struct DrawStyle { /// Fill colour of the object - /// - /// For fonts, this is the background colour of the text pub fill_color: Option, /// Stroke (border/line) color of the object - /// - /// For fonts, this is the foreground colour of the text pub stroke_color: Option, /// Stroke width - /// - /// Set the stroke width for an object. Has no effect on fonts. pub stroke_width: i16, } -impl Style -{ +impl DrawStyle { + pub fn new(fill: PixelColor, stroke: PixelColor, width: i16) -> Self { + Self { fill_color: Some(fill), stroke_color: Some(stroke), stroke_width: width } + } + /// Create a new style with a given stroke value and defaults for everything else pub fn stroke_color(stroke_color: PixelColor) -> Self { Self { stroke_color: Some(stroke_color), - ..Style::default() + ..DrawStyle::default() } } - - /// Returns the stroke width as an `i16`. - /// - /// If the stroke width is too large to fit into an `i16` the maximum value - /// for an `i16` is returned instead. - pub(crate) fn stroke_width_i16(&self) -> i16 { - self.stroke_width - } } -impl Default for Style -{ +impl Default for DrawStyle { fn default() -> Self { Self { - fill_color: None, - stroke_color: None, + fill_color: Some(PixelColor::Dark), + stroke_color: Some(PixelColor::Dark), stroke_width: 1, } } } -/// Add a style to an object -pub trait WithStyle -{ - /// Add a complete style to the object - fn style(self, style: Style) -> Self; +impl From for DrawStyle { + fn from(s: usize) -> Self { + // usize split into these words: + // 31 ... 16 15 ... 4 3..2 1..0 + // width rsvd stroke fill + // where the MSB of stroke/fill encodes Some/None + let fc: PixelColor = (s & 0b0001).into(); + let sc: PixelColor = (s & 0b0100).into(); + DrawStyle { + fill_color: if s & 0b0010 != 0 {Some(fc)} else {None}, + stroke_color: if s & 0b1000 != 0 {Some(sc)} else {None}, + stroke_width: (s >> 16) as i16, + } + } +} - /// Set the stroke colour for the object - /// - /// This can be a noop - fn stroke_color(self, color: Option) -> Self; - - /// Set the stroke width for the object - /// - /// A stroke with a width of zero will not be rendered - fn stroke_width(self, width: u16) -> Self; - - /// Set the fill property of the object's style - /// - /// This can be a noop - fn fill_color(self, color: Option) -> Self; +impl Into for DrawStyle { + fn into(self) -> usize { + let sc: usize; + if self.stroke_color.is_some() { + sc = 0b10 | self.stroke_color.unwrap() as usize; + } else { + sc = 0; + } + let fc: usize; + if self.fill_color.is_some() { + fc = 0b10 | self.fill_color.unwrap() as usize; + } else { + fc = 0; + } + (self.stroke_width as usize) << 16 | sc << 2 | fc + } } #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -347,26 +135,6 @@ impl Add for Point { } } -impl Add for Point { - type Output = Point; - - /// Offsets a point by adding a size. - /// - /// # Panics - /// - /// This function will panic if `width` or `height` are too large to be represented as an `i16` - /// and debug assertions are enabled. - fn add(self, other: Size) -> Point { - let width = other.width as i16; - let height = other.height as i16; - - debug_assert!(width >= 0, "width is too large"); - debug_assert!(height >= 0, "height is too large"); - - Point::new(self.x + width, self.y + height) - } -} - impl AddAssign for Point { fn add_assign(&mut self, other: Point) { self.x += other.x; @@ -374,25 +142,6 @@ impl AddAssign for Point { } } -impl AddAssign for Point { - /// Offsets a point by adding a size. - /// - /// # Panics - /// - /// This function will panic if `width` or `height` are too large to be represented as an `i16` - /// and debug assertions are enabled. - fn add_assign(&mut self, other: Size) { - let width = other.width as i16; - let height = other.height as i16; - - debug_assert!(width >= 0, "width is too large"); - debug_assert!(height >= 0, "height is too large"); - - self.x += width; - self.y += height; - } -} - impl Sub for Point { type Output = Point; @@ -401,26 +150,6 @@ impl Sub for Point { } } -impl Sub for Point { - type Output = Point; - - /// Offsets a point by subtracting a size. - /// - /// # Panics - /// - /// This function will panic if `width` or `height` are too large to be represented as an `i16` - /// and debug assertions are enabled. - fn sub(self, other: Size) -> Point { - let width = other.width as i16; - let height = other.height as i16; - - debug_assert!(width >= 0, "width is too large"); - debug_assert!(height >= 0, "height is too large"); - - Point::new(self.x - width, self.y - height) - } -} - impl SubAssign for Point { fn sub_assign(&mut self, other: Point) { self.x -= other.x; @@ -428,25 +157,6 @@ impl SubAssign for Point { } } -impl SubAssign for Point { - /// Offsets a point by subtracting a size. - /// - /// # Panics - /// - /// This function will panic if `width` or `height` are too large to be represented as an `i16` - /// and debug assertions are enabled. - fn sub_assign(&mut self, other: Size) { - let width = other.width as i16; - let height = other.height as i16; - - debug_assert!(width >= 0, "width is too large"); - debug_assert!(height >= 0, "height is too large"); - - self.x -= width; - self.y -= height; - } -} - impl Index for Point { type Output = i16; @@ -503,30 +213,6 @@ impl Into for Point { } } -/// A single pixel -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct Pixel(pub Point, pub PixelColor); - -#[derive(Copy, Clone, Debug)] -pub struct Rect { - pub x0: i16, - pub y0: i16, - pub x1: i16, - pub y1: i16, -} - -impl Rect { - pub fn new(x0: i16, y0: i16, x1: i16, y1: i16) -> Self { - Self { x0, y0, x1, y1 } - } -} - -// impl Into for Point { -// fn into(self) -> embedded_graphics::geometry::Point { -// embedded_graphics::geometry::Point::new(self.x as _, self.y as _) -// } -// } - impl From for Point { fn from(p: usize) -> Point { Point { @@ -536,27 +222,118 @@ impl From for Point { } } -#[derive(Copy, Clone, Debug)] -pub struct Color { - pub color: u16, -} - -impl From for Color { - fn from(c: usize) -> Color { - Color { color: c as _ } - } -} +/// A single pixel +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct Pixel(pub Point, pub PixelColor); #[derive(Debug, Clone, Copy)] pub struct Rectangle { /// Top left point of the rect - pub top_left: Point, + pub tl: Point, /// Bottom right point of the rect - pub bottom_right: Point, + pub br: Point, - /// Object style - pub style: PixelColor, + /// Drawing style + pub style: DrawStyle, +} + +impl Rectangle { + pub fn new(p1: Point, p2: Point) -> Rectangle { + // always check point ordering + Rectangle { + tl: Point::new(min(p1.x, p2.x), min(p1.y, p2.y)), + br: Point::new(max(p1.x, p2.x), max(p1.y, p2.y)), + style: DrawStyle::default() + } + } + pub fn new_coords(x0: i16, y0: i16, x1: i16, y1: i16) -> Rectangle { + Rectangle { + tl: Point::new(min(x0, x1), min(y0, y1)), + br: Point::new(max(x0, x1), max(y0, y1)), + style: DrawStyle::default() + } + } + pub fn new_coords_with_style(x0: i16, y0: i16, x1: i16, y1: i16, style: DrawStyle) -> Rectangle { + Rectangle { + tl: Point::new(min(x0, x1), min(y0, y1)), + br: Point::new(max(x0, x1), max(y0, y1)), + style: style + } + } + pub fn new_with_style(p1: Point, p2: Point, style: DrawStyle) -> Rectangle { + // always check point ordering + Rectangle { + tl: Point::new(min(p1.x, p2.x), min(p1.y, p2.y)), + br: Point::new(max(p1.x, p2.x), max(p1.y, p2.y)), + style: style + } + } + pub fn x0(&self) -> usize { self.tl.x as usize } + pub fn x1(&self) -> usize { self.br.x as usize } + pub fn y0(&self) -> usize { self.tl.y as usize } + pub fn y1(&self) -> usize { self.br.y as usize } + + /// Make a rectangle of the full screen size + pub fn full_screen() -> Rectangle { + Rectangle { + tl: Point::new(0, 0), + br: Point::new(WIDTH, HEIGHT), + style: DrawStyle::default() + } + } + /// Make a rectangle of the screen size minus padding + pub fn padded_screen() -> Rectangle { + let pad = 6; + Rectangle { + tl: Point::new(pad, pad), + br: Point::new(WIDTH - pad, HEIGHT - pad), + style: DrawStyle::default() + } + } +} + +impl Into for Rectangle { + fn into(self) -> ClipRect { + ClipRect::new(self.x0(), self.y0(), self.x1(), self.y1()) + } +} + +#[derive(Debug, Clone, Copy)] +pub struct Line { + pub start: Point, + pub end: Point, + + /// Drawing style + pub style: DrawStyle, +} + +impl Line { + pub fn new(start: Point, end: Point) -> Line { + Line { start: start, end: end, style: DrawStyle::default() } + } + pub fn new_with_style(start: Point, end: Point, style: DrawStyle) -> Line { + Line { start: start, end: end, style: style, } + } +} + + +#[derive(Debug, Clone, Copy)] +pub struct Circle { + pub center: Point, + pub radius: i16, + + /// Drawing style + pub style: DrawStyle, +} + +impl Circle { + pub fn new(c: Point, r: i16) -> Circle { + Circle { center: c, radius: r, style: DrawStyle::default() } + } + pub fn new_with_style(c: Point, r: i16, style: DrawStyle) -> Circle { + Circle { center: c, radius: r, style } + } } #[derive(Debug)] @@ -564,30 +341,20 @@ pub enum Opcode<'a> { /// Flush the buffer to the screen Flush, - /// Clear the buffer to the specified color - Clear(Color), + /// Clear the buffer to "light" colored pixels + Clear, /// Draw a line at the specified area - Line(Point /* start */, Point /* end */), + Line(Line), /// Draw a rectangle or square at the specified coordinates - Rectangle(Point /* upper-left */, Point /* lower-right */), + Rectangle(Rectangle), /// Draw a circle with a specified radius - Circle(Point, u16 /* radius */), - - /// Change the style of the current pen - Style( - u16, /* stroke width */ - Color, /* stroke color */ - Color, /* fill color */ - ), - - /// Clear the specified region - ClearRegion(blitstr::Rect), + Circle(Circle), /// Set the current string glyph set for strings - SetGlyph(GlyphSet), + SetGlyphStyle(GlyphStyle), /// Set the cursor point for the current string clipping region SetCursor(Cursor), @@ -596,7 +363,7 @@ pub enum Opcode<'a> { GetCursor, /// Set the clipping region for the string. - SetStringClipping(blitstr::Rect), + SetStringClipping(ClipRect), /// Render the string inside the clipping region. String(&'a str), @@ -604,8 +371,8 @@ pub enum Opcode<'a> { /// Retrieve the X and Y dimensions of the screen ScreenSize, - /// Retrieve the current Glyph characteristics - QueryGlyph, + /// Retrieve the current Glyph style + QueryGlyphStyle, } impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> { @@ -614,29 +381,21 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> { match message { Message::Scalar(m) => match m.id { 1 => Ok(Opcode::Flush), - 2 => Ok(Opcode::Clear(Color::from(m.arg1))), - 3 => Ok(Opcode::Line(Point::from(m.arg1), Point::from(m.arg2))), - 4 => Ok(Opcode::Rectangle(Point::from(m.arg1), Point::from(m.arg2))), - 5 => Ok(Opcode::Circle(Point::from(m.arg1), m.arg2 as _)), - 6 => Ok(Opcode::Style( - m.arg1 as _, - Color::from(m.arg2), - Color::from(m.arg3), - )), - 7 => Ok(Opcode::ClearRegion(blitstr::Rect::new( - m.arg1 as _, - m.arg2 as _, - m.arg3 as _, - m.arg4 as _, - ))), - 9 => Ok(Opcode::SetGlyph(arg_to_glyph(m.arg1))), - 11 => Ok(Opcode::SetStringClipping(blitstr::Rect::new(m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _))), + 2 => Ok(Opcode::Clear), + 3 => Ok(Opcode::Line(Line::new_with_style( + Point::from(m.arg1), Point::from(m.arg2), DrawStyle::from(m.arg3)))), + 4 => Ok(Opcode::Rectangle(Rectangle::new_with_style( + Point::from(m.arg1), Point::from(m.arg2), DrawStyle::from(m.arg3)))), + 5 => Ok(Opcode::Circle(Circle::new_with_style( + Point::from(m.arg1), m.arg2 as _, DrawStyle::from(m.arg3)))), + 9 => Ok(Opcode::SetGlyphStyle(GlyphStyle::from(m.arg1))), + 11 => Ok(Opcode::SetStringClipping(ClipRect::new(m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _))), 12 => Ok(Opcode::SetCursor(Cursor::new(m.arg1 as _, m.arg2 as _, m.arg3 as _))), _ => Err("unrecognized opcode"), }, Message::BlockingScalar(m) => match m.id { 8 => Ok(Opcode::ScreenSize), - 10 => Ok(Opcode::QueryGlyph), + 10 => Ok(Opcode::QueryGlyphStyle), 13 => Ok(Opcode::GetCursor), _ => Err("unrecognized opcode"), }, @@ -662,58 +421,36 @@ impl<'a> Into for Opcode<'a> { match self { Opcode::Flush => Message::Scalar(ScalarMessage { id: 1, - arg1: 0, - arg2: 0, - arg3: 0, - arg4: 0, + arg1: 0, arg2: 0, arg3: 0, arg4: 0, }), - Opcode::Clear(color) => Message::Scalar(ScalarMessage { + Opcode::Clear => Message::Scalar(ScalarMessage { id: 2, - arg1: color.color as _, - arg2: 0, - arg3: 0, - arg4: 0, + arg1: 0, arg2: 0, arg3: 0, arg4: 0, }), - Opcode::Line(start, end) => Message::Scalar(ScalarMessage { + Opcode::Line(line) => Message::Scalar(ScalarMessage { id: 3, - arg1: start.into(), - arg2: end.into(), - arg3: 0, + arg1: line.start.into(), + arg2: line.end.into(), + arg3: line.style.into(), arg4: 0, }), - Opcode::Rectangle(start, end) => Message::Scalar(ScalarMessage { + Opcode::Rectangle(r) => Message::Scalar(ScalarMessage { id: 4, - arg1: start.into(), - arg2: end.into(), - arg3: 0, + arg1: r.tl.into(), + arg2: r.br.into(), + arg3: r.style.into(), arg4: 0, }), - Opcode::Circle(center, radius) => Message::Scalar(ScalarMessage { + Opcode::Circle(c) => Message::Scalar(ScalarMessage { id: 5, - arg1: center.into(), - arg2: radius as usize, - arg3: 0, + arg1: c.center.into(), + arg2: c.radius as usize, + arg3: c.style.into(), arg4: 0, }), - Opcode::Style(stroke_width, stroke_color, fill_color) => { - Message::Scalar(ScalarMessage { - id: 6, - arg1: stroke_width as _, - arg2: stroke_color.color as _, - arg3: fill_color.color as _, - arg4: 0, - }) - } - Opcode::ClearRegion(rect) => Message::Scalar(ScalarMessage { - id: 7, - arg1: rect.min.x as _, - arg2: rect.min.y as _, - arg3: rect.max.x as _, - arg4: rect.max.y as _, - }), Opcode::ScreenSize => Message::BlockingScalar(ScalarMessage {id: 8, arg1: 0, arg2: 0, arg3: 0, arg4: 0}), - Opcode::QueryGlyph => Message::BlockingScalar(ScalarMessage {id: 10, arg1: 0, arg2: 0, arg3: 0, arg4: 0}), - Opcode::SetGlyph(glyph) => Message::Scalar(ScalarMessage { id:9, arg1: glyph_to_arg(glyph), arg2: 0, arg3: 0, arg4: 0 }), + Opcode::QueryGlyphStyle => Message::BlockingScalar(ScalarMessage {id: 10, arg1: 0, arg2: 0, arg3: 0, arg4: 0}), + Opcode::SetGlyphStyle(glyph) => Message::Scalar(ScalarMessage { id:9, arg1: glyph as usize, arg2: 0, arg3: 0, arg4: 0 }), Opcode::SetStringClipping(r) => Message::Scalar(ScalarMessage { id: 11, arg1: r.min.x as _, arg2: r.min.y as _, arg3: r.max.x as _, arg4: r.max.y as _, diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index 98a5ce4dd..95e3d3e32 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -2,43 +2,30 @@ // pub mod size; pub mod api; -pub use api::{Point, Color, Rect}; -pub use blitstr::fonts::GlyphSet; -pub use blitstr::Cursor; +pub use api::{Point, PixelColor, Rectangle, DrawStyle, Line, Circle}; +pub use blitstr::{Cursor, GlyphStyle, ClipRect}; use xous::String; pub mod op; use xous::{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()).map(|_| ()) +pub fn draw_line(cid: CID, line: Line) -> Result<(), xous::Error> { + send_message(cid, api::Opcode::Line(line).into()).map(|_| ()) } -pub fn draw_circle(cid: CID, center: Point, radius: u16) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Circle(center, radius).into()).map(|_| ()) +pub fn draw_circle(cid: CID, circ: Circle) -> Result<(), xous::Error> { + send_message(cid, api::Opcode::Circle(circ).into()).map(|_| ()) } -pub fn draw_rectangle(cid: CID, start: Point, end: Point) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Rectangle(start, end).into()).map(|_| ()) -} - -pub fn set_style(cid: CID, width: u16, stroke: Color, fill: Color) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Style(width, stroke, fill).into()).map(|_| ()) +pub fn draw_rectangle(cid: CID, rect: Rectangle) -> Result<(), xous::Error> { + send_message(cid, api::Opcode::Rectangle(rect).into()).map(|_| ()) } pub fn flush(cid: CID) -> Result<(), xous::Error> { send_message(cid, api::Opcode::Flush.into()).map(|_| ()) } -pub fn clear_region(cid: CID, x0: usize, y0: usize, x1: usize, y1: usize) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::ClearRegion(blitstr::Rect::new(x0 as _, y0 as _, x1 as _, y1 as _)).into()).map(|_| ()) -} - -pub fn clear_rectangle(cid: CID, r: blitstr::Rect) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::ClearRegion(r).into()).map(|_| ()) -} - -pub fn set_string_clipping(cid: CID, r: blitstr::Rect) -> Result<(), xous::Error> { +pub fn set_string_clipping(cid: CID, r: ClipRect) -> Result<(), xous::Error> { send_message(cid, api::Opcode::SetStringClipping(r).into()).map(|_| ()) } @@ -46,11 +33,11 @@ pub fn set_cursor(cid: CID, c: Cursor) -> Result<(), xous::Error> { send_message(cid, api::Opcode::SetCursor(c).into()).map(|_| ()) } -pub fn get_cursor(cid: CID, c: Cursor) -> Result { +pub fn get_cursor(cid: CID) -> Result { let response = send_message(cid, api::Opcode::GetCursor.into())?; if let xous::Result::Scalar2(pt_as_usize, h) = response { let p: Point = pt_as_usize.into(); - Ok(Cursor::new(p.x, p.y, h as _)) + Ok(Cursor::new(p.x as usize, p.y as usize, h as _)) } else { panic!("unexpected return value: {:#?}", response); } @@ -60,8 +47,8 @@ pub fn draw_string(cid: CID, s: &String) -> Result<(), xous::Error> { s.lend(cid, 1).map(|_| ()) } -pub fn set_glyph(cid: CID, glyph: GlyphSet) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::SetGlyph(glyph).into()).map( |_| ()) +pub fn set_glyph_style(cid: CID, glyph: GlyphStyle) -> Result<(), xous::Error> { + send_message(cid, api::Opcode::SetGlyphStyle(glyph).into()).map( |_| ()) } pub fn screen_size(cid: CID) -> Result { @@ -73,10 +60,10 @@ pub fn screen_size(cid: CID) -> Result { } } -pub fn query_glyph(cid: CID) -> Result<(GlyphSet, usize), xous::Error> { - let response = send_message(cid, api::Opcode::QueryGlyph.into())?; +pub fn query_glyph(cid: CID) -> Result<(GlyphStyle, usize), xous::Error> { + let response = send_message(cid, api::Opcode::QueryGlyphStyle.into())?; if let xous::Result::Scalar2(glyph, h) = response { - Ok((blitstr::fonts::arg_to_glyph(glyph), h)) + Ok((GlyphStyle::from(glyph), h)) } else { panic!("unexpected return value: {:#?}", response); } diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index 63774816d..f9df4d911 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -16,6 +16,7 @@ use core::convert::TryFrom; mod logo; use blitstr; +use api::{PixelColor, Rectangle, DrawStyle}; fn draw_boot_logo(display: &mut XousDisplay) { display.blit_screen(logo::LOGO_MAP); @@ -30,9 +31,8 @@ fn xmain() -> ! { draw_boot_logo(&mut display); - let mut current_color = api::Color::from(0usize); - let mut current_glyph = blitstr::fonts::GlyphSet::Regular; - let mut current_string_clip = blitstr::Rect::full_screen(); + let mut current_glyph = blitstr::GlyphStyle::Regular; + let mut current_string_clip = blitstr::ClipRect::full_screen(); let mut current_cursor = blitstr::Cursor::from_top_left_of(current_string_clip); display.redraw(); @@ -50,50 +50,24 @@ fn xmain() -> ! { display.update(); display.redraw(); }, - Opcode::Clear(_color) => { - blitstr::clear_region(display.native_buffer(), blitstr::Rect::full_screen()); + Opcode::Clear => { + let mut r = Rectangle::full_screen(); + r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 0); + op::rectangle(display.native_buffer(), r) } - Opcode::Line(start, end) => { - info!("GFX: Drawing line from {:?} to {:?}", start, end); - op::line(display.native_buffer(), start.x as _, start.y as _, end.x as _, end.y as _, if current_color.color == 0 { op::PixelColor::Off } else {op::PixelColor::On }); + Opcode::Line(l) => { + op::line(display.native_buffer(), l); } - Opcode::Rectangle(start, end) => { - todo!(); - // Line::new(start.into(), end.into()) - // .into_styled(current_style) - // .draw(&mut display) - // .ok(); + Opcode::Rectangle(r) => { + op::rectangle(display.native_buffer(), r); } - Opcode::Circle(mid, radius) => { - info!("GFX: Drawing cicrle at {:?} radius {:?}", mid, radius); - op::circle(display.native_buffer(), mid.x as _, mid.y as _, radius as _, 0, op::PixelColor::On); - } - Opcode::Style(stroke_width, stroke_color, fill_color) => { - current_color = stroke_color; - // todo!(); - // current_style.stroke_width = stroke_width; - // current_style.stroke_color = Some(if stroke_color.color == 0 { - // BinaryColor::Off - // } else { - // BinaryColor::On - // }); - // current_style.fill_color = Some(if fill_color.color == 0 { - // BinaryColor::Off - // } else { - // BinaryColor::On - // }); - } - Opcode::ClearRegion(rect) => { - blitstr::clear_region(display.native_buffer(), blitstr::Rect::new( - rect.min.x as _, - rect.min.y as _, - rect.max.x as _, - rect.max.y as _,)); + Opcode::Circle(c) => { + op::circle(display.native_buffer(), c); } Opcode::String(s) => { blitstr::paint_str(display.native_buffer(), current_string_clip.into(), &mut current_cursor, current_glyph.into(), s); } - Opcode::SetGlyph(glyph) => { + Opcode::SetGlyphStyle(glyph) => { current_glyph = glyph; } Opcode::SetCursor(c) => { @@ -119,11 +93,11 @@ fn xmain() -> ! { ) .expect("GFX: couldn't return ScreenSize request"); } - Opcode::QueryGlyph => { + Opcode::QueryGlyphStyle => { xous::return_scalar2( msg.sender, - blitstr::fonts::glyph_to_arg(current_glyph), - blitstr::fonts::glyph_to_height(current_glyph), + current_glyph.into(), + blitstr::glyph_to_height_hint(current_glyph), ) .expect("GFX: could not return QueryGlyph request"); } diff --git a/services/graphics-server/src/op.rs b/services/graphics-server/src/op.rs index c1a8387b7..0cb824949 100644 --- a/services/graphics-server/src/op.rs +++ b/services/graphics-server/src/op.rs @@ -1,7 +1,4 @@ -use blitstr::fonts; -use blitstr::fonts::{Font, GlyphHeader}; -use crate::api::{Point, Style, Pixel, Rect}; -use blitstr::fonts::GlyphSet; +use crate::api::{Point, DrawStyle, Pixel, Rectangle, Circle, PixelColor, Line}; /// LCD Frame buffer bounds pub const LCD_WORDS_PER_LINE: usize = 11; @@ -9,14 +6,8 @@ pub const LCD_PX_PER_LINE: usize = 336; pub const LCD_LINES: usize = 536; pub const LCD_FRAME_BUF_SIZE: usize = LCD_WORDS_PER_LINE * LCD_LINES; -const WIDTH: usize = 336; -const HEIGHT: usize = 536; - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum PixelColor { - On, - Off, -} +pub const WIDTH: i16 = 336; +pub const HEIGHT: i16 = 536; /// For passing frame buffer references pub type LcdFB = [u32; LCD_FRAME_BUF_SIZE]; @@ -24,66 +15,18 @@ pub type LcdFB = [u32; LCD_FRAME_BUF_SIZE]; /// For storing a full-row wide blit pattern pub type BlitRow = [u32; LCD_WORDS_PER_LINE]; -/// For specifying a vertical region contiguous rows in the frame buffer -/// Range is yr.0..yr.1 (yr.0 included, yr.1 excluded) -#[derive(Copy, Clone)] -pub struct YRegion(pub usize, pub usize); - -/// For specifying a region of pixels in the frame buffer -/// Ranges are x0..x1 and y0..y1 (x0 & y0 are included, x1 & y1 are excluded) -#[derive(Copy, Clone)] -pub struct ClipRegion { - pub x0: usize, - pub x1: usize, - pub y0: usize, - pub y1: usize, -} - -impl ClipRegion { - pub fn screen() -> ClipRegion { - ClipRegion { - x0: 0, - x1: WIDTH - 1, - y0: 0, - y1: HEIGHT - 1, - } - } -} - -impl From for ClipRegion { - fn from(r: Rect) -> Self { - let mut cr = r.clone(); - if cr.x0 < 0 { cr.x0 = 0 }; - if cr.x0 >= WIDTH as _ { cr.x0 = WIDTH as i16 - 1 }; - if cr.x1 < 0 { cr.x1 = 0 }; - if cr.x1 >= WIDTH as _ { cr.x1 = WIDTH as i16 - 1 }; - if cr.y0 < 0 { cr.y0 = 0 }; - if cr.y0 >= HEIGHT as _ { cr.y0 = HEIGHT as i16 - 1 }; - if cr.y1 < 0 { cr.y1 = 0 }; - if cr.y1 >= HEIGHT as _ { cr.y1 = HEIGHT as i16 - 1 }; - ClipRegion {x0: cr.x0 as usize, y0: cr.y0 as usize, x1: cr.x1 as usize, y1: cr.y1 as usize} - } -} - -use core::cmp::{min, max}; -impl Into for ClipRegion { - fn into(self) -> blitstr::Rect { - blitstr::Rect::new( min(self.x0, self. x1), min(self.y0, self.y1), max(self.x0, self.x1), max(self.y0, self.y1) ) - } -} - /// Invert a screen region bounded by (cr.x0,cr.y0)..(cr.x0,cr.y1) -pub fn invert_region(fb: &mut LcdFB, cr: ClipRegion) { - if cr.y1 > LCD_LINES || cr.y0 >= cr.y1 || cr.x1 > LCD_PX_PER_LINE || cr.x0 >= cr.x1 { +pub fn invert_region(fb: &mut LcdFB, cr: Rectangle) { + if cr.y1() > LCD_LINES || cr.y0() >= cr.y1() || cr.x1() > LCD_PX_PER_LINE || cr.x0() >= cr.x1() { return; } // Calculate word alignment for destination buffer - let dest_low_word = cr.x0 >> 5; - let dest_high_word = cr.x1 >> 5; - let px_in_dest_low_word = 32 - (cr.x0 & 0x1f); - let px_in_dest_high_word = cr.x1 & 0x1f; + let dest_low_word = cr.x0() >> 5; + let dest_high_word = cr.x1() >> 5; + let px_in_dest_low_word = 32 - (cr.x0() & 0x1f); + let px_in_dest_high_word = cr.x1() & 0x1f; // Blit it - for y in cr.y0..cr.y1 { + for y in cr.y0()..cr.y1() { let base = y * LCD_WORDS_PER_LINE; fb[base + dest_low_word] ^= 0xffffffff << (32 - px_in_dest_low_word); for w in dest_low_word + 1..dest_high_word { @@ -95,28 +38,12 @@ pub fn invert_region(fb: &mut LcdFB, cr: ClipRegion) { } } -/// Outline a full width screen region with pad and border box -pub fn outline_region(fb: &mut LcdFB, yr: YRegion) { - if yr.1 > LCD_LINES || yr.0 + 6 >= yr.1 { - return; - } - line_fill_clear(fb, yr.0); - line_fill_clear(fb, yr.0 + 1); - line_fill_padded_solid(fb, yr.0 + 2); - for y in yr.0 + 3..yr.1 - 3 { - line_fill_padded_border(fb, y); - } - line_fill_padded_solid(fb, yr.1 - 3); - line_fill_clear(fb, yr.1 - 2); - line_fill_clear(fb, yr.1 - 1); -} - /// Clear a line of the screen -pub fn line_fill_clear(fb: &mut LcdFB, y: usize) { - if y >= LCD_LINES { - return; - } - let base = y * LCD_WORDS_PER_LINE; +pub fn line_fill_clear(fb: &mut LcdFB, y: i16) { + let mut clip_y = y as usize; + if clip_y >= LCD_LINES { clip_y = LCD_LINES - 1; } + + let base = clip_y * LCD_WORDS_PER_LINE; for i in 0..=9 { fb[base + i] = 0xffff_ffff; } @@ -125,10 +52,10 @@ pub fn line_fill_clear(fb: &mut LcdFB, y: usize) { /// Fill a line of the screen with full-width pattern pub fn line_fill_pattern(fb: &mut LcdFB, y: usize, pattern: &BlitRow) { - if y >= LCD_LINES { - return; - } - let base = y * LCD_WORDS_PER_LINE; + let mut clip_y = y; + if clip_y >= LCD_LINES { clip_y = LCD_LINES - 1; } + + let base = clip_y * LCD_WORDS_PER_LINE; for (i, v) in pattern.iter().enumerate() { fb[base + i] = *v; } @@ -136,10 +63,10 @@ pub fn line_fill_pattern(fb: &mut LcdFB, y: usize, pattern: &BlitRow) { /// Fill a line of the screen with black, padded with clear to left and right fn line_fill_padded_solid(fb: &mut LcdFB, y: usize) { - if y >= LCD_LINES { - return; - } - let base = y * LCD_WORDS_PER_LINE; + let mut clip_y = y; + if clip_y >= LCD_LINES { clip_y = LCD_LINES - 1; } + + let base = clip_y * LCD_WORDS_PER_LINE; fb[base] = 0x0000_0003; for i in 1..=9 { fb[base + i] = 0x0000_0000; @@ -149,10 +76,10 @@ fn line_fill_padded_solid(fb: &mut LcdFB, y: usize) { /// Fill a line of the screen with clear, bordered by black, padded with clear fn line_fill_padded_border(fb: &mut LcdFB, y: usize) { - if y >= LCD_LINES { - return; - } - let base = y * LCD_WORDS_PER_LINE; + let mut clip_y = y; + if clip_y >= LCD_LINES { clip_y = LCD_LINES - 1; } + + let base = clip_y * LCD_WORDS_PER_LINE; fb[base] = 0xffff_fffb; for i in 1..=9 { fb[base + i] = 0xffff_ffff; @@ -160,43 +87,33 @@ fn line_fill_padded_border(fb: &mut LcdFB, y: usize) { fb[base + 10] = 0x0000_dfff; } -fn put_pixel(fb: &mut LcdFB, x: usize, y: usize, color: PixelColor) { - if (x >= LCD_PX_PER_LINE) || (y >= LCD_LINES) { - return; - } - if color == PixelColor::Off { - fb[(x + y * LCD_WORDS_PER_LINE * 32) / 32] |= 1 << (x % 32) +fn put_pixel(fb: &mut LcdFB, x: i16, y: i16, color: PixelColor) { + let mut clip_y: usize = y as usize; + if clip_y >= LCD_LINES { clip_y = LCD_LINES - 1; } + + let clip_x: usize = x as usize; + if clip_x >= LCD_PX_PER_LINE { clip_y = LCD_PX_PER_LINE - 1; } + + if color == PixelColor::Dark { + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] |= 1 << (clip_x % 32) } else { - fb[(x + y * LCD_WORDS_PER_LINE * 32) / 32] &= !(1 << (x % 32)) + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] &= !(1 << (clip_x % 32)) } // set the dirty bit on the line that contains the pixel - fb[y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; + fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; } -// plotLine(int x0, int y0, int x1, int y1) -// dx = abs(x1-x0); -// sx = x0= dy) /* e_xy+e_x > 0 */ -// err += dy; -// x0 += sx; -// end if -// if (e2 <= dx) /* e_xy+e_y < 0 */ -// err += dx; -// y0 += sy; -// end if -// end while -pub fn line(fb: &mut LcdFB, x0: usize, y0: usize, x1: usize, y1: usize, color: PixelColor) { - let mut x0 = x0 as i32; - let mut y0 = y0 as i32; - let x1 = x1 as i32; - let y1 = y1 as i32; +pub fn line(fb: &mut LcdFB, l: Line) { + let color: PixelColor; + if l.style.stroke_color.is_some() { + color = l.style.stroke_color.unwrap(); + } else { + return + } + let mut x0 = l.start.x; + let mut y0 = l.start.y; + let x1 = l.end.x; + let y1 = l.end.y; let dx = (x1 - x0).abs(); let sx = if x0 < x1 { 1 } else { -1 }; @@ -231,7 +148,7 @@ pub fn line(fb: &mut LcdFB, x0: usize, y0: usize, x1: usize, y1: usize, color: P pub struct CircleIterator { center: Point, radius: u16, - style: Style, + style: DrawStyle, p: Point, } @@ -246,7 +163,7 @@ impl Iterator for CircleIterator return None; } - let radius = self.radius as i16 - self.style.stroke_width_i16() + 1; + let radius = self.radius as i16 - self.style.stroke_width + 1; let outer_radius = self.radius as i16; let radius_sq = radius * radius; @@ -292,41 +209,96 @@ impl Iterator for CircleIterator } } -pub fn circle(fb: &mut LcdFB, x: usize, y: usize, r: usize, stroke_width: usize, color: PixelColor) { +pub fn circle(fb: &mut LcdFB, circle: Circle) { let c = CircleIterator { - center: Point{x: x as i16, y: y as i16}, - radius: r as u16, - style: Style{ fill_color: Some(color), stroke_color: Some(color), stroke_width: stroke_width as i16}, - p: Point::new(-(r as i16), -(r as i16)), + center: circle.center, + radius: circle.radius as _, + style: circle.style, + p: Point::new(-(circle.radius as i16), -(circle.radius as i16)), }; for pixel in c { - put_pixel(fb, pixel.0.x as usize, pixel.0.y as usize, pixel.1); + put_pixel(fb, pixel.0.x, pixel.0.y, pixel.1); } } -#[cfg(test)] -mod tests { - use super::fonts; - #[test] - fn bold_font_at_sign() { - let offset = fonts::bold::get_glyph_pattern_offset('@'); - assert_eq!(offset, 197); - assert_eq!(fonts::bold::DATA[offset], 0x00121008); - } +/// Pixel iterator for each pixel in the rect border +#[derive(Debug, Clone, Copy)] +pub struct RectangleIterator { + top_left: Point, + bottom_right: Point, + style: DrawStyle, + p: Point, +} - #[test] - fn regular_font_at_sign() { - let offset = fonts::regular::get_glyph_pattern_offset('@'); - assert_eq!(offset, 182); - assert_eq!(fonts::regular::DATA[offset], 0x00101008); - } +impl Iterator for RectangleIterator { + type Item = Pixel; - #[test] - fn small_font_at_sign() { - let offset = fonts::small::get_glyph_pattern_offset('@'); - assert_eq!(offset, 143); - assert_eq!(fonts::small::DATA[offset], 0x000e1006); + fn next(&mut self) -> Option { + // Don't render anything if the rectangle has no border or fill color. + if self.style.stroke_color.is_none() && self.style.fill_color.is_none() { + return None; + } + + loop { + let mut out = None; + + // Finished, i.e. we're below the rect + if self.p.y > self.bottom_right.y { + break None; + } + + let border_width = self.style.stroke_width; + let tl = self.top_left; + let br = self.bottom_right; + + // Border + if ( + // Top border + (self.p.y >= tl.y && self.p.y < tl.y + border_width) + // Bottom border + || (self.p.y <= br.y && self.p.y > br.y - border_width) + // Left border + || (self.p.x >= tl.x && self.p.x < tl.x + border_width) + // Right border + || (self.p.x <= br.x && self.p.x > br.x - border_width) + ) && self.style.stroke_color.is_some() + { + out = Some(Pixel( + self.p, + self.style.stroke_color.expect("Expected stroke"), + )); + } + // Fill + else if let Some(fill) = self.style.fill_color { + out = Some(Pixel(self.p, fill)); + } + + self.p.x += 1; + + // Reached end of row? Jump down one line + if self.p.x > self.bottom_right.x { + self.p.x = self.top_left.x; + self.p.y += 1; + } + + if out.is_some() { + break out; + } + } } } + +pub fn rectangle(fb: &mut LcdFB, rect: Rectangle) { + let r = RectangleIterator { + top_left: rect.tl, + bottom_right: rect.br, + style: rect.style, + p: rect.tl, + }; + + for pixel in r { + put_pixel(fb, pixel.0.x, pixel.0.y, pixel.1); + } +} \ No newline at end of file diff --git a/services/shell/Cargo.toml b/services/shell/Cargo.toml index 4ae0bc9c0..13bf0269e 100644 --- a/services/shell/Cargo.toml +++ b/services/shell/Cargo.toml @@ -13,8 +13,8 @@ ticktimer-server = { path = "../ticktimer-server" } log-server = { path = "../log-server" } log = "0.4" com = { path = "../com"} -blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "7684e03f98cf72d78e7ed148a34aca97a7ee5e25" } -# blitstr = { path = "../../../blitstr" } +#blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "7684e03f98cf72d78e7ed148a34aca97a7ee5e25" } +blitstr = { path = "../../../blitstr" } # com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "9a91a81f482ea8eba2f2de5d6c20b9bd399f39a9" } diff --git a/services/shell/src/main.rs b/services/shell/src/main.rs index ef72886a1..ffcd04efd 100644 --- a/services/shell/src/main.rs +++ b/services/shell/src/main.rs @@ -1,40 +1,15 @@ #![cfg_attr(baremetal, no_main)] #![cfg_attr(baremetal, no_std)] -use com::api::BattStats; use com::*; use core::fmt::Write; -use blitstr::fonts::GlyphSet; -use graphics_server::Point; -use blitstr::Rect; +use graphics_server::{Point, Rectangle, PixelColor, DrawStyle, Line, Circle}; +use blitstr::{Cursor, GlyphStyle}; use log::{error, info}; use xous::String; use core::convert::TryFrom; -#[derive(Debug, Clone, Copy)] -pub struct Rectangle { - /// Top left point of the rect - pub top_left: Point, - /// Bottom right point of the rect - pub bottom_right: Point, -} - -impl Rectangle { - pub fn top_left(&self) -> Point { - self.top_left - } - pub fn bottom_right(&self) -> Point { - self.bottom_right - } - pub fn new(top_left: Point, bottom_right: Point) -> Self { - Rectangle { - top_left, - bottom_right, - } - } -} - fn move_lfsr(mut lfsr: u32) -> u32 { lfsr ^= lfsr >> 7; lfsr ^= lfsr << 9; @@ -57,8 +32,8 @@ impl Bounce { radius: radius, bounds: bounds, loc: Point::new( - (bounds.bottom_right.x - bounds.top_left.x) / 2, - (bounds.bottom_right.y - bounds.top_left.y) / 2, + (bounds.br.x - bounds.tl.x) / 2, + (bounds.br.y - bounds.tl.y) / 2, ), lfsr: 0xace1u32, } @@ -90,26 +65,26 @@ impl Bounce { y = self.loc.y + self.vector.y; let r: i16 = self.radius as i16; - if (x >= (self.bounds.bottom_right().x - r)) - || (x <= (self.bounds.top_left().x + r)) - || (y >= (self.bounds.bottom_right().y - r)) - || (y <= (self.bounds.top_left().y + r)) + if (x >= (self.bounds.br.x - r)) + || (x <= (self.bounds.tl.x + r)) + || (y >= (self.bounds.br.y - r)) + || (y <= (self.bounds.tl.y + r)) { - if x >= (self.bounds.bottom_right().x - r - 1) { + if x >= (self.bounds.br.x - r - 1) { self.vector.x = -self.next_rand(); - x = self.bounds.bottom_right().x - r; + x = self.bounds.br.x - r; } - if x <= self.bounds.top_left().x + r + 1 { + if x <= self.bounds.tl.x + r + 1 { self.vector.x = self.next_rand(); - x = self.bounds.top_left().x + r; + x = self.bounds.tl.x + r; } - if y >= (self.bounds.bottom_right().y - r - 1) { + if y >= (self.bounds.br.y - r - 1) { self.vector.y = -self.next_rand(); - y = self.bounds.bottom_right().y - r; + y = self.bounds.br.y - r; } - if y <= (self.bounds.top_left().y + r + 1) { + if y <= (self.bounds.tl.y + r + 1) { self.vector.y = self.next_rand(); - y = self.bounds.top_left().y + r; + y = self.bounds.tl.y + r; } } @@ -134,7 +109,7 @@ fn com_thread(_arg: Option) { xous::create_server(b"shell ").expect("Couldn't create Shell server"); info!("SHELL|com_thread: starting COM response handler thread"); loop { - let mut envelope = + let envelope = xous::syscall::receive_message(shell_server).expect("couldn't get address"); info!("SHELL|com_thread: got message {:?}", envelope); if let Ok(opcode) = com::api::Opcode::try_from(&envelope.body) { @@ -189,8 +164,6 @@ fn shell_main() -> ! { let screensize = graphics_server::screen_size(graphics_conn).expect("Couldn't get screen size"); - let dark = graphics_server::Color::from(!0); - let light = graphics_server::Color::from(0); let mut bouncyball = Bounce::new( 14, Rectangle::new( @@ -200,8 +173,6 @@ fn shell_main() -> ! { ); bouncyball.update(); - let mut batt_stats: BattStats = BattStats::default(); - #[cfg(baremetal)] { // use this to select which UART to monitor in the main loop @@ -217,84 +188,107 @@ fn shell_main() -> ! { gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // 0 = kernel, 1 = log, 2-3 are various servers } - graphics_server::set_style(graphics_conn, 1, dark, dark) - .expect("unable to draw to screen: {:?}"); + let style_dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); + let style_light = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); let mut last_time: u64 = 0; ticktimer_server::reset(ticktimer_conn).unwrap(); let mut string_buffer = String::new(4096); - graphics_server::set_glyph(graphics_conn, GlyphSet::Small).expect("unable to set glyph"); + graphics_server::set_glyph_style(graphics_conn, GlyphStyle::Small).expect("unable to set glyph"); let (_, font_h) = graphics_server::query_glyph(graphics_conn).expect("unable to query glyph"); - let status_clipregion = blitstr::Rect::new(4, 0, screensize.x as _, font_h as usize); - let mut status_cursor = blitstr::Cursor::from_top_left_of(status_clipregion); - let work_clipregion = blitstr::Rect::new(4, font_h + 2 as usize, screensize.x as _, font_h as usize * 11); - let mut work_cursor = blitstr::Cursor::from_top_left_of(work_clipregion); + let status_clipregion = Rectangle::new_coords_with_style(4, 0, screensize.x, font_h as _, style_light); + let mut status_cursor; + + graphics_server::set_glyph_style(graphics_conn, GlyphStyle::Regular).expect("unable to set glyph"); + let (_, font_h) = graphics_server::query_glyph(graphics_conn).expect("unable to query glyph"); + let mut work_clipregion = Rectangle::new_coords_with_style(4, font_h as i16 + 2, screensize.x, font_h as i16 * 8 + 18, style_light); + let mut work_cursor; + graphics_server::draw_rectangle(graphics_conn, work_clipregion) + .expect("unable to clear region"); + + let mut firsttime = true; loop { // status bar - graphics_server::set_glyph(graphics_conn, GlyphSet::Small).expect("unable to set glyph"); + graphics_server::set_glyph_style(graphics_conn, GlyphStyle::Small).expect("unable to set glyph"); - graphics_server::clear_rectangle(graphics_conn, status_clipregion) + graphics_server::draw_rectangle(graphics_conn, status_clipregion) .expect("unable to clear region"); - graphics_server::set_string_clipping(graphics_conn, status_clipregion) + graphics_server::set_string_clipping(graphics_conn, status_clipregion.into()) .expect("unable to set string clip region"); string_buffer.clear(); write!(&mut string_buffer, "{}mV", BATT_STATS_VOLTAGE.load(Ordering::Relaxed)).expect("Can't write"); - status_cursor = blitstr::Cursor::from_top_left_of(status_clipregion); - graphics_server::set_cursor(graphics_conn, status_cursor); + status_cursor = Cursor::from_top_left_of(status_clipregion.into()); + graphics_server::set_cursor(graphics_conn, status_cursor).expect("can't set cursor"); graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); status_cursor.pt.x = 95; string_buffer.clear(); write!(&mut string_buffer, "{}mA", BATT_STATS_CURRENT.load(Ordering::Relaxed)).expect("Can't write"); - graphics_server::set_cursor(graphics_conn, status_cursor); + graphics_server::set_cursor(graphics_conn, status_cursor).expect("can't set cursor"); graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); status_cursor.pt.x = 190; string_buffer.clear(); write!(&mut string_buffer, "{}mA", BATT_STATS_REMAINING.load(Ordering::Relaxed)).expect("Can't write"); - graphics_server::set_cursor(graphics_conn, status_cursor); + graphics_server::set_cursor(graphics_conn, status_cursor).expect("can't set cursor"); graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); status_cursor.pt.x = 280; string_buffer.clear(); write!(&mut string_buffer, "{}%", BATT_STATS_SOC.load(Ordering::Relaxed)).expect("Can't write"); - graphics_server::set_cursor(graphics_conn, status_cursor); + graphics_server::set_cursor(graphics_conn, status_cursor).expect("can't set cursor"); graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); - graphics_server::set_style(graphics_conn, 1, dark, dark); - graphics_server::draw_line(graphics_conn, Point::new(0, font_h as i16 + 1), Point::new(screensize.x as _, font_h as i16 + 1)); + graphics_server::draw_line(graphics_conn, Line::new_with_style( + Point::new(0, font_h as i16), + Point::new(screensize.x as _, font_h as i16), + style_dark)).expect("can't draw line"); // work area string_buffer.clear(); write!(&mut string_buffer, "Uptime: {:.2}s\n\n", last_time as f32 / 1000f32 ).expect("Can't write"); - work_cursor = blitstr::Cursor::from_top_left_of(work_clipregion); - graphics_server::clear_rectangle(graphics_conn, work_clipregion) + work_cursor = Cursor::from_top_left_of(work_clipregion.into()); + work_clipregion.br = Point::new(screensize.x, font_h as i16 * 3); + graphics_server::draw_rectangle(graphics_conn, work_clipregion) .expect("unable to clear region"); - graphics_server::set_string_clipping(graphics_conn, work_clipregion) + work_clipregion.br = Point::new(screensize.x, font_h as i16 * 8); + graphics_server::set_string_clipping(graphics_conn, work_clipregion.into()) .expect("unable to set string clip region"); - graphics_server::set_cursor(graphics_conn, work_cursor); - graphics_server::set_glyph(graphics_conn, GlyphSet::Regular).expect("unable to set glyph"); + graphics_server::set_cursor(graphics_conn, work_cursor).expect("can't set cursor"); + graphics_server::set_glyph_style(graphics_conn, GlyphStyle::Regular).expect("unable to set glyph"); graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); - string_buffer.clear(); - write!(&mut string_buffer, "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich.\n 😸 🎩 🔑\n cat hat key\n").expect("Can't write"); - graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); + if firsttime { + string_buffer.clear(); + write!(&mut string_buffer, "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich.\n 😸 🎩 🔑\n cat hat key\n").expect("Can't write"); + graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string"); + firsttime = false; + } // ticktimer_server::sleep_ms(ticktimer_conn, 500).expect("couldn't sleep"); // draw the ball - graphics_server::clear_region( + graphics_server::draw_rectangle( graphics_conn, - bouncyball.ball_center().x as usize - bouncyball.radius() as usize - 1, - bouncyball.ball_center().y as usize - bouncyball.radius() as usize - 1, - bouncyball.ball_center().x as usize + bouncyball.radius() as usize + 1, - bouncyball.ball_center().y as usize + bouncyball.radius() as usize + 1, + Rectangle::new_with_style( + Point::new( + bouncyball.ball_center().x - bouncyball.radius() as i16 - 1, + bouncyball.ball_center().y - bouncyball.radius() as i16 - 1), + Point::new( + bouncyball.ball_center().x + bouncyball.radius() as i16 + 1, + bouncyball.ball_center().y + bouncyball.radius() as i16 + 1), + style_light + ) ) - .expect("unable to clear region"); + .expect("unable to clear ball region"); bouncyball.update(); + + // draw the top line that contains the ball graphics_server::draw_line(graphics_conn, - Point::new(0, bouncyball.bounds().top_left().y as i16 - 1), - Point::new(screensize.x as i16, bouncyball.bounds().top_left().y as i16 - 1)); - graphics_server::draw_circle(graphics_conn, bouncyball.loc, bouncyball.radius as u16) + Line::new_with_style(Point::new(0, bouncyball.bounds.tl.y - 1), + Point::new(screensize.x, bouncyball.bounds.tl.y - 1), style_dark)).expect("can't draw border"); + // draw the ball + graphics_server::draw_circle(graphics_conn, + Circle::new_with_style(bouncyball.loc, bouncyball.radius as i16, style_dark)) .expect("unable to draw to screen"); // Periodic tasks