2021-01-04 20:45:23 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
2021-01-03 10:31:51 +00:00
|
|
|
use crate::op::{HEIGHT, WIDTH};
|
|
|
|
|
use blitstr::{ClipRect, Cursor, GlyphStyle};
|
|
|
|
|
use core::cmp::{max, min};
|
2020-11-22 16:40:40 +00:00
|
|
|
use core::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign};
|
2021-01-03 10:31:51 +00:00
|
|
|
use xous::{Message, ScalarMessage};
|
2020-11-22 16:40:40 +00:00
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
|
pub enum PixelColor {
|
|
|
|
|
Dark,
|
|
|
|
|
Light,
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl From<usize> for PixelColor {
|
|
|
|
|
fn from(pc: usize) -> Self {
|
2021-01-03 10:31:51 +00:00
|
|
|
if pc != 0 {
|
|
|
|
|
PixelColor::Dark
|
|
|
|
|
} else {
|
|
|
|
|
PixelColor::Light
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl From<bool> for PixelColor {
|
|
|
|
|
fn from(pc: bool) -> Self {
|
2021-01-03 10:31:51 +00:00
|
|
|
if pc {
|
|
|
|
|
PixelColor::Dark
|
|
|
|
|
} else {
|
|
|
|
|
PixelColor::Light
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Into<usize> for PixelColor {
|
|
|
|
|
fn into(self) -> usize {
|
2021-01-03 10:31:51 +00:00
|
|
|
if self == PixelColor::Dark {
|
|
|
|
|
1
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Into<bool> for PixelColor {
|
|
|
|
|
fn into(self) -> bool {
|
2021-01-03 10:31:51 +00:00
|
|
|
if self == PixelColor::Dark {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Style properties for an object
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-12-02 17:48:10 +00:00
|
|
|
pub struct DrawStyle {
|
2020-11-22 16:40:40 +00:00
|
|
|
/// Fill colour of the object
|
|
|
|
|
pub fill_color: Option<PixelColor>,
|
|
|
|
|
|
|
|
|
|
/// Stroke (border/line) color of the object
|
|
|
|
|
pub stroke_color: Option<PixelColor>,
|
|
|
|
|
|
|
|
|
|
/// Stroke width
|
|
|
|
|
pub stroke_width: i16,
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl DrawStyle {
|
|
|
|
|
pub fn new(fill: PixelColor, stroke: PixelColor, width: i16) -> Self {
|
2021-01-03 10:31:51 +00:00
|
|
|
Self {
|
|
|
|
|
fill_color: Some(fill),
|
|
|
|
|
stroke_color: Some(stroke),
|
|
|
|
|
stroke_width: width,
|
|
|
|
|
}
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
/// 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),
|
2020-12-02 17:48:10 +00:00
|
|
|
..DrawStyle::default()
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Default for DrawStyle {
|
2020-11-22 16:40:40 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
2020-12-02 17:48:10 +00:00
|
|
|
fill_color: Some(PixelColor::Dark),
|
|
|
|
|
stroke_color: Some(PixelColor::Dark),
|
2020-11-22 16:40:40 +00:00
|
|
|
stroke_width: 1,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl From<usize> 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 {
|
2021-01-03 10:31:51 +00:00
|
|
|
fill_color: if s & 0b0010 != 0 { Some(fc) } else { None },
|
|
|
|
|
stroke_color: if s & 0b1000 != 0 { Some(sc) } else { None },
|
2020-12-02 17:48:10 +00:00
|
|
|
stroke_width: (s >> 16) as i16,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Into<usize> 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
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2020-08-29 17:17:56 +00:00
|
|
|
pub struct Point {
|
2020-11-22 16:40:40 +00:00
|
|
|
pub x: i16,
|
|
|
|
|
pub y: i16,
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Point {
|
2020-11-22 16:40:40 +00:00
|
|
|
pub fn new(x: i16, y: i16) -> Point {
|
2020-08-29 17:17:56 +00:00
|
|
|
Point { x, y }
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
|
|
|
|
|
/// Creates a point with X and Y equal to zero.
|
|
|
|
|
pub const fn zero() -> Self {
|
|
|
|
|
Point { x: 0, y: 0 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Add for Point {
|
|
|
|
|
type Output = Point;
|
|
|
|
|
|
|
|
|
|
fn add(self, other: Point) -> Point {
|
|
|
|
|
Point::new(self.x + other.x, self.y + other.y)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AddAssign for Point {
|
|
|
|
|
fn add_assign(&mut self, other: Point) {
|
|
|
|
|
self.x += other.x;
|
|
|
|
|
self.y += other.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Sub for Point {
|
|
|
|
|
type Output = Point;
|
|
|
|
|
|
|
|
|
|
fn sub(self, other: Point) -> Point {
|
|
|
|
|
Point::new(self.x - other.x, self.y - other.y)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SubAssign for Point {
|
|
|
|
|
fn sub_assign(&mut self, other: Point) {
|
|
|
|
|
self.x -= other.x;
|
|
|
|
|
self.y -= other.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Index<usize> for Point {
|
|
|
|
|
type Output = i16;
|
|
|
|
|
|
|
|
|
|
fn index(&self, idx: usize) -> &i16 {
|
|
|
|
|
match idx {
|
|
|
|
|
0 => &self.x,
|
|
|
|
|
1 => &self.y,
|
|
|
|
|
_ => panic!("index out of bounds: the len is 2 but the index is {}", idx),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Neg for Point {
|
|
|
|
|
type Output = Point;
|
|
|
|
|
|
|
|
|
|
fn neg(self) -> Self::Output {
|
|
|
|
|
Point::new(-self.x, -self.y)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<(i16, i16)> for Point {
|
|
|
|
|
fn from(other: (i16, i16)) -> Self {
|
|
|
|
|
Point::new(other.0, other.1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<[i16; 2]> for Point {
|
|
|
|
|
fn from(other: [i16; 2]) -> Self {
|
|
|
|
|
Point::new(other[0], other[1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<&[i16; 2]> for Point {
|
|
|
|
|
fn from(other: &[i16; 2]) -> Self {
|
|
|
|
|
Point::new(other[0], other[1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Point> for (i16, i16) {
|
|
|
|
|
fn from(other: Point) -> (i16, i16) {
|
|
|
|
|
(other.x, other.y)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<&Point> for (i16, i16) {
|
|
|
|
|
fn from(other: &Point) -> (i16, i16) {
|
|
|
|
|
(other.x, other.y)
|
|
|
|
|
}
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Into<usize> for Point {
|
|
|
|
|
fn into(self) -> usize {
|
|
|
|
|
(self.x as usize) << 16 | (self.y as usize)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl From<usize> for Point {
|
|
|
|
|
fn from(p: usize) -> Point {
|
|
|
|
|
Point {
|
|
|
|
|
x: (p >> 16 & 0xffff) as _,
|
|
|
|
|
y: (p & 0xffff) as _,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
/// A single pixel
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
pub struct Pixel(pub Point, pub PixelColor);
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct Rectangle {
|
|
|
|
|
/// Top left point of the rect
|
|
|
|
|
pub tl: Point,
|
|
|
|
|
|
|
|
|
|
/// Bottom right point of the rect
|
|
|
|
|
pub br: Point,
|
|
|
|
|
|
|
|
|
|
/// Drawing style
|
|
|
|
|
pub style: DrawStyle,
|
2020-10-30 02:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
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)),
|
2021-01-03 10:31:51 +00:00
|
|
|
style: DrawStyle::default(),
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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)),
|
2021-01-03 10:31:51 +00:00
|
|
|
style: DrawStyle::default(),
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-03 10:31:51 +00:00
|
|
|
pub fn new_coords_with_style(
|
|
|
|
|
x0: i16,
|
|
|
|
|
y0: i16,
|
|
|
|
|
x1: i16,
|
|
|
|
|
y1: i16,
|
|
|
|
|
style: DrawStyle,
|
|
|
|
|
) -> Rectangle {
|
2020-12-02 17:48:10 +00:00
|
|
|
Rectangle {
|
|
|
|
|
tl: Point::new(min(x0, x1), min(y0, y1)),
|
|
|
|
|
br: Point::new(max(x0, x1), max(y0, y1)),
|
2021-01-03 10:31:51 +00:00
|
|
|
style: style,
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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)),
|
2021-01-03 10:31:51 +00:00
|
|
|
style: style,
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-03 10:31:51 +00:00
|
|
|
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
|
|
|
|
|
}
|
2020-12-02 17:48:10 +00:00
|
|
|
|
|
|
|
|
/// Make a rectangle of the full screen size
|
|
|
|
|
pub fn full_screen() -> Rectangle {
|
|
|
|
|
Rectangle {
|
|
|
|
|
tl: Point::new(0, 0),
|
|
|
|
|
br: Point::new(WIDTH, HEIGHT),
|
2021-01-03 10:31:51 +00:00
|
|
|
style: DrawStyle::default(),
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// 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),
|
2021-01-03 10:31:51 +00:00
|
|
|
style: DrawStyle::default(),
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Into<ClipRect> for Rectangle {
|
|
|
|
|
fn into(self) -> ClipRect {
|
|
|
|
|
ClipRect::new(self.x0(), self.y0(), self.x1(), self.y1())
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct Line {
|
|
|
|
|
pub start: Point,
|
|
|
|
|
pub end: Point,
|
|
|
|
|
|
|
|
|
|
/// Drawing style
|
|
|
|
|
pub style: DrawStyle,
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Line {
|
|
|
|
|
pub fn new(start: Point, end: Point) -> Line {
|
2021-01-03 10:31:51 +00:00
|
|
|
Line {
|
|
|
|
|
start: start,
|
|
|
|
|
end: end,
|
|
|
|
|
style: DrawStyle::default(),
|
|
|
|
|
}
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
pub fn new_with_style(start: Point, end: Point, style: DrawStyle) -> Line {
|
2021-01-03 10:31:51 +00:00
|
|
|
Line {
|
|
|
|
|
start: start,
|
|
|
|
|
end: end,
|
|
|
|
|
style: style,
|
|
|
|
|
}
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2020-12-02 17:48:10 +00:00
|
|
|
pub struct Circle {
|
|
|
|
|
pub center: Point,
|
|
|
|
|
pub radius: i16,
|
2020-11-22 16:40:40 +00:00
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
/// Drawing style
|
|
|
|
|
pub style: DrawStyle,
|
|
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
impl Circle {
|
|
|
|
|
pub fn new(c: Point, r: i16) -> Circle {
|
2021-01-03 10:31:51 +00:00
|
|
|
Circle {
|
|
|
|
|
center: c,
|
|
|
|
|
radius: r,
|
|
|
|
|
style: DrawStyle::default(),
|
|
|
|
|
}
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
|
|
|
|
pub fn new_with_style(c: Point, r: i16, style: DrawStyle) -> Circle {
|
2021-01-03 10:31:51 +00:00
|
|
|
Circle {
|
|
|
|
|
center: c,
|
|
|
|
|
radius: r,
|
|
|
|
|
style,
|
|
|
|
|
}
|
2020-12-02 17:48:10 +00:00
|
|
|
}
|
2020-11-22 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-29 17:17:56 +00:00
|
|
|
#[derive(Debug)]
|
2020-10-30 02:22:03 +00:00
|
|
|
pub enum Opcode<'a> {
|
2020-08-29 17:17:56 +00:00
|
|
|
/// Flush the buffer to the screen
|
|
|
|
|
Flush,
|
|
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
/// Clear the buffer to "light" colored pixels
|
|
|
|
|
Clear,
|
2020-08-29 17:17:56 +00:00
|
|
|
|
|
|
|
|
/// Draw a line at the specified area
|
2020-12-02 17:48:10 +00:00
|
|
|
Line(Line),
|
2020-08-29 17:17:56 +00:00
|
|
|
|
|
|
|
|
/// Draw a rectangle or square at the specified coordinates
|
2020-12-02 17:48:10 +00:00
|
|
|
Rectangle(Rectangle),
|
2020-08-29 17:17:56 +00:00
|
|
|
|
|
|
|
|
/// Draw a circle with a specified radius
|
2020-12-02 17:48:10 +00:00
|
|
|
Circle(Circle),
|
2020-10-30 02:22:03 +00:00
|
|
|
|
2020-11-23 07:42:58 +00:00
|
|
|
/// Set the current string glyph set for strings
|
2020-12-02 17:48:10 +00:00
|
|
|
SetGlyphStyle(GlyphStyle),
|
2020-11-23 07:42:58 +00:00
|
|
|
|
2020-11-27 18:05:55 +00:00
|
|
|
/// Set the cursor point for the current string clipping region
|
|
|
|
|
SetCursor(Cursor),
|
|
|
|
|
|
|
|
|
|
/// Retrieve the current cursor porint for the current string clipping region
|
|
|
|
|
GetCursor,
|
|
|
|
|
|
2020-11-26 19:33:44 +00:00
|
|
|
/// Set the clipping region for the string.
|
2020-12-02 17:48:10 +00:00
|
|
|
SetStringClipping(ClipRect),
|
2020-11-26 19:33:44 +00:00
|
|
|
|
2021-01-12 20:39:04 +00:00
|
|
|
/// Overwrite the string inside the clipping region.
|
2020-10-30 02:22:03 +00:00
|
|
|
String(&'a str),
|
2020-11-23 06:55:56 +00:00
|
|
|
|
2021-01-12 20:39:04 +00:00
|
|
|
/// Xor the string inside the clipping region.
|
|
|
|
|
StringXor(&'a str),
|
|
|
|
|
|
2020-11-23 06:55:56 +00:00
|
|
|
/// Retrieve the X and Y dimensions of the screen
|
|
|
|
|
ScreenSize,
|
2020-11-23 07:42:58 +00:00
|
|
|
|
2020-12-02 17:48:10 +00:00
|
|
|
/// Retrieve the current Glyph style
|
|
|
|
|
QueryGlyphStyle,
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-30 02:22:03 +00:00
|
|
|
impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> {
|
2020-08-29 17:17:56 +00:00
|
|
|
type Error = &'static str;
|
|
|
|
|
fn try_from(message: &'a Message) -> Result<Self, Self::Error> {
|
|
|
|
|
match message {
|
|
|
|
|
Message::Scalar(m) => match m.id {
|
|
|
|
|
1 => Ok(Opcode::Flush),
|
2020-12-02 17:48:10 +00:00
|
|
|
2 => Ok(Opcode::Clear),
|
|
|
|
|
3 => Ok(Opcode::Line(Line::new_with_style(
|
2021-01-03 10:31:51 +00:00
|
|
|
Point::from(m.arg1),
|
|
|
|
|
Point::from(m.arg2),
|
|
|
|
|
DrawStyle::from(m.arg3),
|
|
|
|
|
))),
|
2020-12-02 17:48:10 +00:00
|
|
|
4 => Ok(Opcode::Rectangle(Rectangle::new_with_style(
|
2021-01-03 10:31:51 +00:00
|
|
|
Point::from(m.arg1),
|
|
|
|
|
Point::from(m.arg2),
|
|
|
|
|
DrawStyle::from(m.arg3),
|
|
|
|
|
))),
|
2020-12-02 17:48:10 +00:00
|
|
|
5 => Ok(Opcode::Circle(Circle::new_with_style(
|
2021-01-03 10:31:51 +00:00
|
|
|
Point::from(m.arg1),
|
|
|
|
|
m.arg2 as _,
|
|
|
|
|
DrawStyle::from(m.arg3),
|
|
|
|
|
))),
|
2020-12-02 17:48:10 +00:00
|
|
|
9 => Ok(Opcode::SetGlyphStyle(GlyphStyle::from(m.arg1))),
|
2021-01-03 10:31:51 +00:00
|
|
|
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 _,
|
|
|
|
|
))),
|
2020-10-30 02:22:03 +00:00
|
|
|
_ => Err("unrecognized opcode"),
|
|
|
|
|
},
|
2020-11-23 06:55:56 +00:00
|
|
|
Message::BlockingScalar(m) => match m.id {
|
|
|
|
|
8 => Ok(Opcode::ScreenSize),
|
2020-12-02 17:48:10 +00:00
|
|
|
10 => Ok(Opcode::QueryGlyphStyle),
|
2020-11-27 18:05:55 +00:00
|
|
|
13 => Ok(Opcode::GetCursor),
|
2020-11-23 06:55:56 +00:00
|
|
|
_ => Err("unrecognized opcode"),
|
|
|
|
|
},
|
2020-10-30 02:22:03 +00:00
|
|
|
Message::Borrow(m) => match m.id {
|
|
|
|
|
1 => {
|
|
|
|
|
let s = unsafe {
|
|
|
|
|
core::slice::from_raw_parts(
|
|
|
|
|
m.buf.as_ptr(),
|
|
|
|
|
m.valid.map(|x| x.get()).unwrap_or_else(|| m.buf.len()),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
Ok(Opcode::String(core::str::from_utf8(s).unwrap()))
|
2021-01-12 20:39:04 +00:00
|
|
|
},
|
|
|
|
|
2 => {
|
|
|
|
|
let s = unsafe {
|
|
|
|
|
core::slice::from_raw_parts(
|
|
|
|
|
m.buf.as_ptr(),
|
|
|
|
|
m.valid.map(|x| x.get()).unwrap_or_else(|| m.buf.len()),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
Ok(Opcode::StringXor(core::str::from_utf8(s).unwrap()))
|
|
|
|
|
},
|
2020-08-29 17:17:56 +00:00
|
|
|
_ => Err("unrecognized opcode"),
|
|
|
|
|
},
|
|
|
|
|
_ => Err("unhandled message type"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 02:22:03 +00:00
|
|
|
impl<'a> Into<Message> for Opcode<'a> {
|
2020-08-29 17:17:56 +00:00
|
|
|
fn into(self) -> Message {
|
|
|
|
|
match self {
|
|
|
|
|
Opcode::Flush => Message::Scalar(ScalarMessage {
|
|
|
|
|
id: 1,
|
2021-01-03 10:31:51 +00:00
|
|
|
arg1: 0,
|
|
|
|
|
arg2: 0,
|
|
|
|
|
arg3: 0,
|
|
|
|
|
arg4: 0,
|
2020-08-29 17:17:56 +00:00
|
|
|
}),
|
2020-12-02 17:48:10 +00:00
|
|
|
Opcode::Clear => Message::Scalar(ScalarMessage {
|
2020-08-29 17:17:56 +00:00
|
|
|
id: 2,
|
2021-01-03 10:31:51 +00:00
|
|
|
arg1: 0,
|
|
|
|
|
arg2: 0,
|
|
|
|
|
arg3: 0,
|
|
|
|
|
arg4: 0,
|
2020-08-29 17:17:56 +00:00
|
|
|
}),
|
2020-12-02 17:48:10 +00:00
|
|
|
Opcode::Line(line) => Message::Scalar(ScalarMessage {
|
2020-08-29 17:17:56 +00:00
|
|
|
id: 3,
|
2020-12-02 17:48:10 +00:00
|
|
|
arg1: line.start.into(),
|
|
|
|
|
arg2: line.end.into(),
|
|
|
|
|
arg3: line.style.into(),
|
2020-08-29 17:17:56 +00:00
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
2020-12-02 17:48:10 +00:00
|
|
|
Opcode::Rectangle(r) => Message::Scalar(ScalarMessage {
|
2020-08-29 17:17:56 +00:00
|
|
|
id: 4,
|
2020-12-02 17:48:10 +00:00
|
|
|
arg1: r.tl.into(),
|
|
|
|
|
arg2: r.br.into(),
|
|
|
|
|
arg3: r.style.into(),
|
2020-08-29 17:17:56 +00:00
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
2020-12-02 17:48:10 +00:00
|
|
|
Opcode::Circle(c) => Message::Scalar(ScalarMessage {
|
2020-08-29 17:17:56 +00:00
|
|
|
id: 5,
|
2020-12-02 17:48:10 +00:00
|
|
|
arg1: c.center.into(),
|
|
|
|
|
arg2: c.radius as usize,
|
|
|
|
|
arg3: c.style.into(),
|
2020-08-29 17:17:56 +00:00
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
2021-01-03 10:31:51 +00:00
|
|
|
Opcode::ScreenSize => Message::BlockingScalar(ScalarMessage {
|
|
|
|
|
id: 8,
|
|
|
|
|
arg1: 0,
|
|
|
|
|
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,
|
|
|
|
|
}),
|
2020-11-26 19:33:44 +00:00
|
|
|
Opcode::SetStringClipping(r) => Message::Scalar(ScalarMessage {
|
|
|
|
|
id: 11,
|
2021-01-03 10:31:51 +00:00
|
|
|
arg1: r.min.x as _,
|
|
|
|
|
arg2: r.min.y as _,
|
|
|
|
|
arg3: r.max.x as _,
|
|
|
|
|
arg4: r.max.y as _,
|
2020-11-26 19:33:44 +00:00
|
|
|
}),
|
2020-10-30 02:22:03 +00:00
|
|
|
Opcode::String(string) => {
|
|
|
|
|
let region = xous::carton::Carton::from_bytes(string.as_bytes());
|
|
|
|
|
Message::Borrow(region.into_message(1))
|
2021-01-03 10:31:51 +00:00
|
|
|
}
|
2021-01-12 20:39:04 +00:00
|
|
|
Opcode::StringXor(string) => {
|
|
|
|
|
let region = xous::carton::Carton::from_bytes(string.as_bytes());
|
|
|
|
|
Message::Borrow(region.into_message(2))
|
|
|
|
|
}
|
2021-01-03 10:31:51 +00:00
|
|
|
Opcode::SetCursor(c) => Message::Scalar(ScalarMessage {
|
|
|
|
|
id: 12,
|
|
|
|
|
arg1: c.pt.x,
|
|
|
|
|
arg2: c.pt.y,
|
|
|
|
|
arg3: c.line_height,
|
|
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
|
|
|
|
Opcode::GetCursor => Message::BlockingScalar(ScalarMessage {
|
|
|
|
|
id: 13,
|
|
|
|
|
arg1: 0,
|
|
|
|
|
arg2: 0,
|
|
|
|
|
arg3: 0,
|
|
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
2020-08-29 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|