From 9ae7186262181a294fffa32b76a57dc7adec4317 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Sun, 25 Sep 2022 23:01:18 +0200 Subject: [PATCH] added luminance support --- airportdisplay/src/commands.rs | 28 ++++++++++++++++++++-------- airportdisplay/src/geometry.rs | 7 +++++++ airportdisplay/src/lib.rs | 1 + airportdisplay/src/luminance.rs | 4 ++++ airportdisplay/src/protocol.rs | 19 +++++++++++++++++-- 5 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 airportdisplay/src/luminance.rs diff --git a/airportdisplay/src/commands.rs b/airportdisplay/src/commands.rs index 2f93b25..c34a8dc 100644 --- a/airportdisplay/src/commands.rs +++ b/airportdisplay/src/commands.rs @@ -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 for Command { - fn from(raw: text::Raw) -> Self { - Command::Text(text::Text::Raw(raw)) +impl From 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 for Command { - fn from(buffer: text::Buffer) -> Self { - Command::Text(text::Text::Buffer(buffer)) +impl From for Command { + fn from(buffer: TextBuffer) -> Self { + Command::Text(Text::Buffer(buffer)) + } +} + +/// Shortcut to convert a luminance window +/// to a command. +impl From for Command { + fn from(luminance: Luminance) -> Self { + Command::Luminance(luminance) } } diff --git a/airportdisplay/src/geometry.rs b/airportdisplay/src/geometry.rs index 870c98c..f224732 100644 --- a/airportdisplay/src/geometry.rs +++ b/airportdisplay/src/geometry.rs @@ -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 { diff --git a/airportdisplay/src/lib.rs b/airportdisplay/src/lib.rs index 9660a6b..f61d1ab 100644 --- a/airportdisplay/src/lib.rs +++ b/airportdisplay/src/lib.rs @@ -1,6 +1,7 @@ mod commands; mod display; pub mod geometry; +pub mod luminance; mod protocol; pub mod text; diff --git a/airportdisplay/src/luminance.rs b/airportdisplay/src/luminance.rs new file mode 100644 index 0000000..4c9024b --- /dev/null +++ b/airportdisplay/src/luminance.rs @@ -0,0 +1,4 @@ +use super::geometry::Window; + +/// Set luminance of [0..5??] at window +pub struct Luminance(pub Window, pub u16); diff --git a/airportdisplay/src/protocol.rs b/airportdisplay/src/protocol.rs index 6254c0d..7c7a056 100644 --- a/airportdisplay/src/protocol.rs +++ b/airportdisplay/src/protocol.rs @@ -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; /// Data is a list of commands to be sent to the display. pub type Data = Vec; +fn encode_u16(v: u16) -> Frame { + vec![(v >> 8) as u8, v as u8] +} + /// Encode position data as big endian impl From 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 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 for Data { } } +/// Encode luminance +impl From 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 for Data { fn from(text::Buffer(Origin(x, y), text): text::Buffer) -> Data { @@ -98,6 +112,7 @@ impl From for Data { Command::Reboot => vec![vec![0x00, 0x0b]], Command::Fadeout => vec![vec![0x00, 0x0d]], Command::Text(text) => text.into(), + Command::Luminance(lum) => lum.into(), } } }