81 lines
2.7 KiB
Rust
81 lines
2.7 KiB
Rust
use server_framework::{Decode, Encode};
|
|
use types::display::text::Text;
|
|
use types::display::{Color, Dimension, ScreenCoord};
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum DisplayRequest {
|
|
DrawRect { x: ScreenCoord, y: ScreenCoord, w: Dimension, h: Dimension, color: Color },
|
|
Clear { color: Color },
|
|
Flush,
|
|
GetDimensions,
|
|
DrawText(Text),
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum DisplayResponse {
|
|
Ok,
|
|
Dimensions { width: Dimension, height: Dimension },
|
|
Error,
|
|
}
|
|
|
|
impl Encode for DisplayRequest {
|
|
fn encode(&self) -> [usize; 4] {
|
|
match self {
|
|
DisplayRequest::DrawRect { x, y, w, h, color } => {
|
|
let xy = (x.raw() as usize) | ((y.raw() as usize) << 16);
|
|
let wh = (w.raw() as usize) | ((h.raw() as usize) << 16);
|
|
[0, xy, wh, color.raw() as usize]
|
|
}
|
|
DisplayRequest::Clear { color } => [1, color.raw() as usize, 0, 0],
|
|
DisplayRequest::Flush => [2, 0, 0, 0],
|
|
DisplayRequest::GetDimensions => [3, 0, 0, 0],
|
|
DisplayRequest::DrawText(t) => t.words(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Decode for DisplayRequest {
|
|
fn decode(words: [usize; 4]) -> Option<Self> {
|
|
match words[0] {
|
|
0 => {
|
|
let x = ScreenCoord::new((words[1] & 0xFFFF) as u16);
|
|
let y = ScreenCoord::new(((words[1] >> 16) & 0xFFFF) as u16);
|
|
let w = Dimension::new((words[2] & 0xFFFF) as u16);
|
|
let h = Dimension::new(((words[2] >> 16) & 0xFFFF) as u16);
|
|
let color = Color::from_raw(words[3] as u32);
|
|
Some(DisplayRequest::DrawRect { x, y, w, h, color })
|
|
}
|
|
1 => Some(DisplayRequest::Clear { color: Color::from_raw(words[1] as u32) }),
|
|
2 => Some(DisplayRequest::Flush),
|
|
3 => Some(DisplayRequest::GetDimensions),
|
|
_ => Text::from_words(words).map(DisplayRequest::DrawText),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Encode for DisplayResponse {
|
|
fn encode(&self) -> [usize; 4] {
|
|
match self {
|
|
DisplayResponse::Ok => [0, 0, 0, 0],
|
|
DisplayResponse::Dimensions { width, height } => {
|
|
[1, width.raw() as usize, height.raw() as usize, 0]
|
|
}
|
|
DisplayResponse::Error => [2, 0, 0, 0],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Decode for DisplayResponse {
|
|
fn decode(words: [usize; 4]) -> Option<Self> {
|
|
match words[0] {
|
|
0 => Some(DisplayResponse::Ok),
|
|
1 => Some(DisplayResponse::Dimensions {
|
|
width: Dimension::new(words[1] as u16),
|
|
height: Dimension::new(words[2] as u16),
|
|
}),
|
|
2 => Some(DisplayResponse::Error),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|