From c07aa9f0c068200d80f6635c221dd7480e53766a Mon Sep 17 00:00:00 2001 From: Agathe Porte Date: Sun, 23 Feb 2020 10:30:59 +0100 Subject: custom chars support --- examples/custom_char.rs | 23 +++++++++++++++++++++++ src/custom_char.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 12 ++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 examples/custom_char.rs create mode 100644 src/custom_char.rs diff --git a/examples/custom_char.rs b/examples/custom_char.rs new file mode 100644 index 0000000..059f173 --- /dev/null +++ b/examples/custom_char.rs @@ -0,0 +1,23 @@ +use charlcd::custom_char; +use charlcd::Screen; +use std::io::Write; + +fn main() -> std::io::Result<()> { + let mut screen = Screen::default()?; + + // Alarm symbol + screen.custom_char(0, [0x04, 0x0E, 0x0E, 0x0E, 0x0E, 0x1F, 0x04, 0x00])?; + + screen.custom_char(1, custom_char::RIGHT_ARROW)?; + screen.custom_char(2, custom_char::LEFT_ARROW)?; + screen.custom_char(3, custom_char::UP_ARROW)?; + screen.custom_char(4, custom_char::DOWN_ARROW)?; + + screen.clear()?; + screen.write(b"\x00 Alarm 10:00\n")?; + screen.write(b"\x01\x02 X Arrows\n")?; + screen.write(b"\x03\x04 Y Arrow")?; + screen.flush()?; + + Ok(()) +} diff --git a/src/custom_char.rs b/src/custom_char.rs new file mode 100644 index 0000000..47f99f5 --- /dev/null +++ b/src/custom_char.rs @@ -0,0 +1,50 @@ +/// X axis mirror of a custom LCD character +pub const fn mirror_x(src: [u8; 8]) -> [u8; 8] { + // const fn do not support for loops currently, hence manual unroll + [ + src[0].reverse_bits() >> 3, + src[1].reverse_bits() >> 3, + src[2].reverse_bits() >> 3, + src[3].reverse_bits() >> 3, + src[4].reverse_bits() >> 3, + src[5].reverse_bits() >> 3, + src[6].reverse_bits() >> 3, + src[7].reverse_bits() >> 3, + ] +} + +/// Y axis mirror of a custom LCD character +pub const fn mirror_y(src: [u8; 8]) -> [u8; 8] { + // const fn do not support for loops currently, hence manual unroll + [ + src[7], src[6], src[5], src[4], src[3], src[2], src[1], src[0], + ] +} + +#[cfg_attr(rustfmt, rustfmt_skip)] +pub const RIGHT_ARROW: [u8; 8] = [ + 0b00000, + 0b01000, + 0b01100, + 0b01110, + 0b01100, + 0b01000, + 0b00000, + 0b00000, +]; + +pub const LEFT_ARROW: [u8; 8] = mirror_x(RIGHT_ARROW); + +#[cfg_attr(rustfmt, rustfmt_skip)] +pub const UP_ARROW: [u8; 8] = [ + 0b00000, + 0b00000, + 0b00100, + 0b01110, + 0b11111, + 0b00000, + 0b00000, + 0b00000, +]; + +pub const DOWN_ARROW: [u8; 8] = mirror_y(UP_ARROW); diff --git a/src/lib.rs b/src/lib.rs index b573b1a..6b1d6e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod codes; +pub mod custom_char; mod of_node; use std::fs::{File, OpenOptions}; @@ -288,6 +289,17 @@ where write_simple_code!(self, SpecialCode::LargeFont) } + /// Custom character create + pub fn custom_char(&mut self, code: u8, value: [u8; 8]) -> std::io::Result<()> { + let mut res = 0u64; + let mut i = 0; + for b in value.iter().rev() { + res |= (*b as u64) << i; + i += 8; + } + write_simple_code!(self, SpecialCode::Generator(code, res)) + } + pub fn gotoxy(&mut self, x: u32, y: u32) -> std::io::Result<()> { write_simple_code!(self, SpecialCode::GotoXY(Some(x), Some(y))) } -- cgit v1.2.3