initial graphics

This commit is contained in:
Annika Hannig 2022-09-26 15:22:30 +02:00
parent 9ae7186262
commit 644c0d8e73
4 changed files with 46 additions and 9 deletions

View file

@ -1,6 +1,7 @@
use std::convert::From; use std::convert::From;
use super::{ use super::{
graphics::Graphics,
luminance::Luminance, luminance::Luminance,
text::{Buffer as TextBuffer, Raw as TextRaw, Text}, text::{Buffer as TextBuffer, Raw as TextRaw, Text},
}; };
@ -13,6 +14,7 @@ pub enum Command {
Fadeout, Fadeout,
Text(Text), Text(Text),
Luminance(Luminance), Luminance(Luminance),
Graphics(Graphics),
} }
/// Directly converty a raw text into a command which /// Directly converty a raw text into a command which
@ -38,3 +40,10 @@ impl From<Luminance> for Command {
Command::Luminance(luminance) Command::Luminance(luminance)
} }
} }
/// Shortcut for graphics
impl From<Graphics> for Command {
fn from(gfx: Graphics) -> Self {
Command::Graphics(gfx)
}
}

View file

@ -0,0 +1,9 @@
pub enum Graphics {
/// Raw is a series
Raw,
}
/// Raw: Offset + Raw pixel content.
/// Pixels content: series of byte-sized 8 pixel
/// horizontal blocks. highest bit is the top left pixel
pub struct Raw(pub u16, pub Vec<u8>);

View file

@ -1,6 +1,7 @@
mod commands; mod commands;
mod display; mod display;
pub mod geometry; pub mod geometry;
pub mod graphics;
pub mod luminance; pub mod luminance;
mod protocol; mod protocol;
pub mod text; pub mod text;

View file

@ -5,12 +5,14 @@ use codepage_437::{ToCp437, CP437_WINGDINGS};
use super::{ use super::{
commands::Command, commands::Command,
geometry::{Origin, Size, Window, COLUMNS, ROWS}, geometry::{Origin, Size, Window, COLUMNS, ROWS},
graphics::Raw as GraphicsRaw,
luminance::Luminance, luminance::Luminance,
text, text::{Buffer as TextBuffer, Raw as TextRaw, Text},
}; };
const CMD_RAW_TEXT: &'static [u8] = &[0x00, 0x03]; const CMD_RAW_TEXT: &'static [u8] = &[0x00, 0x03];
const CMD_RAW_LUM: &'static [u8] = &[0x00, 0x05]; const CMD_RAW_LUM: &'static [u8] = &[0x00, 0x05];
const CMD_RAW_GFX: &'static [u8] = &[0x00, 0x12];
/// A frame holds a single encoded display command, /// A frame holds a single encoded display command,
/// like set text at pos x, y. /// like set text at pos x, y.
@ -44,9 +46,25 @@ impl From<Window> for Frame {
} }
} }
/// Encode raw graphics
impl From<GraphicsRaw> for Data {
fn from(raw: GraphicsRaw) -> Self {
let GraphicsRaw(offset, data) = raw;
vec![[
CMD_RAW_GFX.into(),
encode_u16(offset),
encode_u16(data.len() as u16),
encode_u16(0),
encode_u16(0),
data.into(),
]
.concat()]
}
}
/// Encode raw text byte command /// Encode raw text byte command
impl From<text::Raw> for Data { impl From<TextRaw> for Data {
fn from(text::Raw(origin, bytes): text::Raw) -> Data { fn from(TextRaw(origin, bytes): TextRaw) -> Data {
let mut bytes = bytes.clone(); let mut bytes = bytes.clone();
bytes.truncate(COLUMNS); bytes.truncate(COLUMNS);
let size = Size(bytes.len() as u16, 1); let size = Size(bytes.len() as u16, 1);
@ -63,8 +81,8 @@ impl From<Luminance> for Data {
} }
/// Encode a text buffer as a series of commands (data). /// Encode a text buffer as a series of commands (data).
impl From<text::Buffer> for Data { impl From<TextBuffer> for Data {
fn from(text::Buffer(Origin(x, y), text): text::Buffer) -> Data { fn from(TextBuffer(Origin(x, y), text): TextBuffer) -> Data {
let mut lines: Vec<&str> = text.split("\n").collect(); let mut lines: Vec<&str> = text.split("\n").collect();
lines.truncate(ROWS); lines.truncate(ROWS);
@ -94,11 +112,11 @@ impl From<text::Buffer> for Data {
} }
/// Encode text command /// Encode text command
impl From<text::Text> for Data { impl From<Text> for Data {
fn from(text: text::Text) -> Data { fn from(text: Text) -> Data {
match text { match text {
text::Text::Raw(raw) => raw.into(), Text::Raw(raw) => raw.into(),
text::Text::Buffer(buffer) => buffer.into(), Text::Buffer(buffer) => buffer.into(),
} }
} }
} }