diff --git a/airportdisplay/src/display.rs b/airportdisplay/src/display.rs index 6228ff2..59792b2 100644 --- a/airportdisplay/src/display.rs +++ b/airportdisplay/src/display.rs @@ -13,7 +13,7 @@ impl Display { /// Open a new UDP socket and create a display instance pub fn open(addr: String) -> Result { let socket = UdpSocket::bind("0.0.0.0:0")?; - Ok(Self{ + Ok(Self { addr: addr, socket: socket, }) @@ -23,10 +23,9 @@ impl Display { pub fn send(&self, cmd: Command) -> Result<()> { let data: Data = cmd.into(); for frame in data { - self.socket.send_to( - frame.as_slice(), self.addr.clone().as_str())?; + self.socket + .send_to(frame.as_slice(), self.addr.clone().as_str())?; } Ok(()) } } - diff --git a/airportdisplay/src/geometry.rs b/airportdisplay/src/geometry.rs index e05c7fd..02f6132 100644 --- a/airportdisplay/src/geometry.rs +++ b/airportdisplay/src/geometry.rs @@ -1,4 +1,3 @@ - /// An origin marks the top left position of the /// data sent to the display. #[derive(Default)] @@ -7,7 +6,7 @@ pub struct Origin(pub u16, pub u16); /// Size defines the width and height of a window pub struct Size(pub u16, pub u16); -/// A window +/// A window pub struct Window(pub Origin, pub Size); impl Window { @@ -15,4 +14,3 @@ impl Window { Window(Origin(x, y), Size(w, h)) } } - diff --git a/airportdisplay/src/lib.rs b/airportdisplay/src/lib.rs index 1cbc069..dba68b2 100644 --- a/airportdisplay/src/lib.rs +++ b/airportdisplay/src/lib.rs @@ -1,12 +1,12 @@ -mod display; -mod protocol; mod commands; +mod display; mod geometry; +mod protocol; mod text; pub const TEXT_COLUMNS: usize = 56; pub const TEXT_ROWS: usize = 20; -pub use commands::{Command}; -pub use protocol::Data; +pub use commands::Command; pub use display::Display; +pub use protocol::Data; diff --git a/airportdisplay/src/protocol.rs b/airportdisplay/src/protocol.rs index 720ce28..dc1d15e 100644 --- a/airportdisplay/src/protocol.rs +++ b/airportdisplay/src/protocol.rs @@ -1,12 +1,11 @@ use std::convert::From; -use codepage_437::{CP437_WINGDINGS, ToCp437}; +use codepage_437::{ToCp437, CP437_WINGDINGS}; use super::{ - commands::{Command}, - text, - geometry::{Window, Origin, Size}, - TEXT_COLUMNS, TEXT_ROWS, + commands::Command, + geometry::{Origin, Size, Window}, + text, TEXT_COLUMNS, TEXT_ROWS, }; const CMD_RAW_TEXT: &'static [u8] = &[0x00, 0x03]; @@ -21,20 +20,14 @@ pub type Data = Vec; /// 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, - ] + vec![(x >> 8) as u8, x as u8, (y >> 8) as u8, y as u8] } } /// 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, - ] + vec![(w >> 8) as u8, w as u8, (h >> 8) as u8, h as u8] } } @@ -62,23 +55,31 @@ impl From for Data { lines.truncate(TEXT_ROWS); let mut data = vec![]; - for (i, line) in lines.iter().enumerate() { + for (i, line) in lines.iter().enumerate() { + // Convert utf8 to codepage 437 if let Ok(bytes) = line.to_cp437(&CP437_WINGDINGS) { + let mut bytes: Frame = bytes.into(); + bytes.truncate(TEXT_COLUMNS); + let len = bytes.len() as u16; let pos = Origin(x, y + i as u16); let size = Size(len, 1); - data.push([ - Frame::from(CMD_RAW_TEXT), - pos.into(), - size.into(), - bytes.into(), - ].concat()); + data.push( + [ + Frame::from(CMD_RAW_TEXT), + pos.into(), + size.into(), + bytes.into(), + ] + .concat(), + ); } } data } } +/// Encode text command impl From for Data { fn from(text: text::Text) -> Data { match text { diff --git a/airportdisplay/src/text.rs b/airportdisplay/src/text.rs index 1291576..cafb468 100644 --- a/airportdisplay/src/text.rs +++ b/airportdisplay/src/text.rs @@ -1,9 +1,7 @@ - use std::convert::From; use super::geometry::Origin; - /// TextRaw holds bytes and a window pub struct Raw(pub Origin, pub Vec); @@ -14,14 +12,13 @@ impl From> for Raw { } } - /// TextBuffer holds a multiline block of utf8 text /// data and a origin. pub struct Buffer(pub Origin, pub String); impl Buffer { pub fn at(x: u16, y: u16, text: String) -> Self { - Self(Origin(x, y), text) + Self(Origin(x, y), text) } }