added luminance support

This commit is contained in:
Annika Hannig 2022-09-25 23:01:18 +02:00
parent 4a0e762142
commit 9ae7186262
5 changed files with 49 additions and 10 deletions

View file

@ -1,6 +1,9 @@
use std::convert::From;
use super::text;
use super::{
luminance::Luminance,
text::{Buffer as TextBuffer, Raw as TextRaw, Text},
};
/// Display Commands
pub enum Command {
@ -8,21 +11,30 @@ pub enum Command {
Clear,
Reboot,
Fadeout,
Text(text::Text),
Text(Text),
Luminance(Luminance),
}
/// Directly converty a raw text into a command which
/// can be sent to the display.
impl From<text::Raw> for Command {
fn from(raw: text::Raw) -> Self {
Command::Text(text::Text::Raw(raw))
impl From<TextRaw> for Command {
fn from(raw: TextRaw) -> Self {
Command::Text(Text::Raw(raw))
}
}
/// Shortcut to directly convert a text buffer into
/// a commmand which can be sent to the display.
impl From<text::Buffer> for Command {
fn from(buffer: text::Buffer) -> Self {
Command::Text(text::Text::Buffer(buffer))
impl From<TextBuffer> for Command {
fn from(buffer: TextBuffer) -> Self {
Command::Text(Text::Buffer(buffer))
}
}
/// Shortcut to convert a luminance window
/// to a command.
impl From<Luminance> for Command {
fn from(luminance: Luminance) -> Self {
Command::Luminance(luminance)
}
}

View file

@ -9,7 +9,14 @@ pub struct Origin(pub u16, pub u16);
/// Size defines the width and height of a window
pub struct Size(pub u16, pub u16);
impl Default for Size {
fn default() -> Self {
Self(1, 1)
}
}
/// A window
#[derive(Default)]
pub struct Window(pub Origin, pub Size);
impl Window {

View file

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

View file

@ -0,0 +1,4 @@
use super::geometry::Window;
/// Set luminance of [0..5??] at window
pub struct Luminance(pub Window, pub u16);

View file

@ -5,10 +5,12 @@ use codepage_437::{ToCp437, CP437_WINGDINGS};
use super::{
commands::Command,
geometry::{Origin, Size, Window, COLUMNS, ROWS},
luminance::Luminance,
text,
};
const CMD_RAW_TEXT: &'static [u8] = &[0x00, 0x03];
const CMD_RAW_LUM: &'static [u8] = &[0x00, 0x05];
/// A frame holds a single encoded display command,
/// like set text at pos x, y.
@ -17,17 +19,21 @@ pub type Frame = Vec<u8>;
/// Data is a list of commands to be sent to the display.
pub type Data = Vec<Frame>;
fn encode_u16(v: u16) -> Frame {
vec![(v >> 8) as u8, v as u8]
}
/// Encode position data as big endian
impl From<Origin> for Frame {
fn from(Origin(x, y): Origin) -> Self {
vec![(x >> 8) as u8, x as u8, (y >> 8) as u8, y as u8]
[encode_u16(x), encode_u16(y)].concat()
}
}
/// Encode size as big endian
impl From<Size> for Frame {
fn from(Size(w, h): Size) -> Self {
vec![(w >> 8) as u8, w as u8, (h >> 8) as u8, h as u8]
[encode_u16(w), encode_u16(h)].concat()
}
}
@ -48,6 +54,14 @@ impl From<text::Raw> for Data {
}
}
/// Encode luminance
impl From<Luminance> for Data {
fn from(luminance: Luminance) -> Data {
let Luminance(window, value) = luminance;
vec![[CMD_RAW_LUM.into(), Vec::from(window), encode_u16(value)].concat()]
}
}
/// Encode a text buffer as a series of commands (data).
impl From<text::Buffer> for Data {
fn from(text::Buffer(Origin(x, y), text): text::Buffer) -> Data {
@ -98,6 +112,7 @@ impl From<Command> for Data {
Command::Reboot => vec![vec![0x00, 0x0b]],
Command::Fadeout => vec![vec![0x00, 0x0d]],
Command::Text(text) => text.into(),
Command::Luminance(lum) => lum.into(),
}
}
}