diff --git a/src/drawing/font.rs b/src/drawing/font.rs index 79f8654..c4097f5 100644 --- a/src/drawing/font.rs +++ b/src/drawing/font.rs @@ -1,109 +1,213 @@ +//width must be multiple of 8 +// +//chars are build in the bitmap like this example of a width 16, height 2 font: +//12 +//34 +// first char is the first ascii letter you want +#[allow(dead_code)] +pub struct Font<'a> { + width: u8, + height: u8, + first_char: u8, + last_char: u8, + bitmap: &'a [u8], + widthmap: &'a [u8] +} + +impl<'a> Font<'a> { + /// Panics if either Bitmap or Widthmap of the Font are to small for the amount and size of chars + pub fn new(width: u8, height: u8, first_char: u8, last_char: u8, bitmap: &'a [u8], widthmap: &'a [u8]) -> Font<'a> { + //Assertion so it shouldn't be able to panic later + let length_of_char = width as usize / 8 * height as usize; + let amount_of_chars = last_char as usize - first_char as usize + 1; + assert!(bitmap.len() >= amount_of_chars * length_of_char); + assert!(widthmap.len() >= amount_of_chars); + + Font {width, height, first_char, last_char, bitmap, widthmap } + + } + + fn get_length_of_char(&self) -> usize { + self.width as usize / 8 * self.height as usize + } + + fn get_char_pos(&self, input: char) -> usize { + (input as usize - self.first_char as usize) + } + + /// Can panic, when end_pos > bitmap.len, should be caught in Font::new already + pub(crate) fn get_char(&'a self, input: char) -> &'a [u8] { + let start_pos = self.get_char_pos(input) * self.get_length_of_char(); + let end_pos = start_pos + self.get_length_of_char(); + + &self.bitmap[start_pos .. end_pos] + } + + /// Can panic, when get_char_pos > widthmap.len(), should be caught in Font::new already + pub(crate) fn get_char_width(&self, input: char) -> u8 { + self.widthmap[self.get_char_pos(input)] + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fonts_test() { + let bitmap = [ + 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, // '!' + 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, // '"' + 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00, // '#' + 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00]; // '$' + + let widthmap = [8,8,8,8]; + + let font = Font::new(8, 8, '!' as u8, '$' as u8, &bitmap, &widthmap); + + let hashtag = [0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00]; + + assert_eq!(font.get_char('#'), hashtag); + + assert_eq!(font.get_char('$')[7], 0x00); + + assert_eq!(font.get_char_width('#'), widthmap[2]); + assert_eq!(font.get_char_width('$'), widthmap[3]); + } + + + + + #[test] + fn bitmap_8x8_test() { + let and = [0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00, 0x00]; + let zero = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; + let first_value = [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00]; + let last_value = [0x00, 0x41, 0x36, 0x08, 0x00, 0x00, 0x00, 0x00]; + + assert_eq!(bitmap_8x8('&'), and); + + assert_eq!(bitmap_8x8('ß'), zero); + assert_eq!(bitmap_8x8('°'), zero); + + assert_eq!(bitmap_8x8('!'), first_value); + assert_eq!(bitmap_8x8('}'), last_value); + + assert_eq!(bitmap_8x8('0')[1], 0x3E); + } +} + + + - pub(crate) fn to_bitmap(input: char) -> [u8; 8] { - // Populate the array with the data from the character array at the right index - match input { - '!' => [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00], - '"' => [0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00], - '#' => [0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00], - '$' => [0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00, 0x00, 0x00], - '%' => [0x23, 0x13, 0x08, 0x64, 0x62, 0x00, 0x00, 0x00], - '&' => [0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00, 0x00], - '\'' => [0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], - '(' => [0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x00, 0x00], - ')' => [0x00, 0x41, 0x22, 0x1C, 0x00, 0x00, 0x00, 0x00], - '*' => [0x08, 0x2A, 0x1C, 0x2A, 0x08, 0x00, 0x00, 0x00], - '+' => [0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x00, 0x00], - ',' => [0x00, 0x50, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00], - '-' => [0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00], - '.' => [0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00], - '/' => [0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00, 0x00], - '0' => [0x1C, 0x3E, 0x61, 0x41, 0x43, 0x3E, 0x1C, 0x00], - '1' => [0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00, 0x00], - '2' => [0x62, 0x73, 0x79, 0x59, 0x5D, 0x4F, 0x46, 0x00], - '3' => [0x20, 0x61, 0x49, 0x4D, 0x4F, 0x7B, 0x31, 0x00], - '4' => [0x18, 0x1C, 0x16, 0x13, 0x7F, 0x7F, 0x10, 0x00], - '5' => [0x27, 0x67, 0x45, 0x45, 0x45, 0x7D, 0x38, 0x00], - '6' => [0x3C, 0x7E, 0x4B, 0x49, 0x49, 0x79, 0x30, 0x00], - '7' => [0x03, 0x03, 0x71, 0x79, 0x0D, 0x07, 0x03, 0x00], - '8' => [0x36, 0x7F, 0x49, 0x49, 0x49, 0x7F, 0x36, 0x00], - '9' => [0x06, 0x4F, 0x49, 0x49, 0x69, 0x3F, 0x1E, 0x00], - ':' => [0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00], - ';' => [0x00, 0x56, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00], - '<' => [0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00], - '=' => [0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x00, 0x00], - '>' => [0x41, 0x22, 0x14, 0x08, 0x00, 0x00, 0x00, 0x00], - '?' => [0x02, 0x01, 0x51, 0x09, 0x06, 0x00, 0x00, 0x00], - '@' => [0x32, 0x49, 0x79, 0x41, 0x3E, 0x00, 0x00, 0x00], - 'A' => [0x7E, 0x11, 0x11, 0x11, 0x7E, 0x00, 0x00, 0x00], - 'B' => [0x7F, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00, 0x00], - 'C' => [0x3E, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00, 0x00], - 'D' => [0x7F, 0x7F, 0x41, 0x41, 0x63, 0x3E, 0x1C, 0x00], - 'E' => [0x7F, 0x49, 0x49, 0x49, 0x41, 0x00, 0x00, 0x00], - 'F' => [0x7F, 0x09, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00], - 'G' => [0x3E, 0x41, 0x41, 0x51, 0x32, 0x00, 0x00, 0x00], - 'H' => [0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00, 0x00, 0x00], - 'I' => [0x00, 0x41, 0x7F, 0x41, 0x00, 0x00, 0x00, 0x00], - 'J' => [0x20, 0x40, 0x41, 0x3F, 0x01, 0x00, 0x00, 0x00], - 'K' => [0x7F, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00], - 'L' => [0x7F, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00], - 'M' => [0x7F, 0x02, 0x04, 0x02, 0x7F, 0x00, 0x00, 0x00], - 'N' => [0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00, 0x00, 0x00], - 'O' => [0x3E, 0x7F, 0x41, 0x41, 0x41, 0x7F, 0x3E, 0x00], - 'P' => [0x7F, 0x09, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00], - 'Q' => [0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00, 0x00, 0x00], - 'R' => [0x7F, 0x7F, 0x11, 0x31, 0x79, 0x6F, 0x4E, 0x00], - 'S' => [0x46, 0x49, 0x49, 0x49, 0x31, 0x00, 0x00, 0x00], - 'T' => [0x01, 0x01, 0x7F, 0x01, 0x01, 0x00, 0x00, 0x00], - 'U' => [0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00], - 'V' => [0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00, 0x00, 0x00], - 'W' => [0x7F, 0x7F, 0x38, 0x1C, 0x38, 0x7F, 0x7F, 0x00], - 'X' => [0x63, 0x14, 0x08, 0x14, 0x63, 0x00, 0x00, 0x00], - 'Y' => [0x03, 0x04, 0x78, 0x04, 0x03, 0x00, 0x00, 0x00], - 'Z' => [0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x00, 0x00], - '[' => [0x00, 0x00, 0x7F, 0x41, 0x41, 0x00, 0x00, 0x00], - '\\' => [0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x00, 0x00], - ']' => [0x41, 0x41, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00], - '^' => [0x04, 0x02, 0x01, 0x02, 0x04, 0x00, 0x00, 0x00], - '_' => [0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00], - '`' => [0x00, 0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00], - 'a' => [0x20, 0x54, 0x54, 0x54, 0x78, 0x00, 0x00, 0x00], - 'b' => [0x7F, 0x48, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00], - 'c' => [0x38, 0x44, 0x44, 0x44, 0x20, 0x00, 0x00, 0x00], - 'd' => [0x38, 0x44, 0x44, 0x48, 0x7F, 0x00, 0x00, 0x00], - 'e' => [0x38, 0x54, 0x54, 0x54, 0x18, 0x00, 0x00, 0x00], - 'f' => [0x08, 0x7E, 0x09, 0x01, 0x02, 0x00, 0x00, 0x00], - 'g' => [0x08, 0x14, 0x54, 0x54, 0x3C, 0x00, 0x00, 0x00], - 'h' => [0x7F, 0x08, 0x04, 0x04, 0x78, 0x00, 0x00, 0x00], - 'i' => [0x00, 0x44, 0x7D, 0x40, 0x00, 0x00, 0x00, 0x00], - 'j' => [0x20, 0x40, 0x44, 0x3D, 0x00, 0x00, 0x00, 0x00], - 'k' => [0x00, 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00, 0x00], - 'l' => [0x00, 0x41, 0x7F, 0x40, 0x00, 0x00, 0x00, 0x00], - 'm' => [0x7C, 0x04, 0x18, 0x04, 0x78, 0x00, 0x00, 0x00], - 'n' => [0x7C, 0x08, 0x04, 0x04, 0x78, 0x00, 0x00, 0x00], - 'o' => [0x38, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00], - 'p' => [0x7C, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00, 0x00], - 'q' => [0x08, 0x14, 0x14, 0x18, 0x7C, 0x00, 0x00, 0x00], - 'r' => [0x7C, 0x08, 0x04, 0x04, 0x08, 0x00, 0x00, 0x00], - 's' => [0x48, 0x54, 0x54, 0x54, 0x20, 0x00, 0x00, 0x00], - 't' => [0x04, 0x3F, 0x44, 0x40, 0x20, 0x00, 0x00, 0x00], - 'u' => [0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00, 0x00, 0x00], - 'v' => [0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00, 0x00, 0x00], - 'w' => [0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00, 0x00, 0x00], - 'x' => [0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00], - 'y' => [0x0C, 0x50, 0x50, 0x50, 0x3C, 0x00, 0x00, 0x00], - 'z' => [0x44, 0x64, 0x54, 0x4C, 0x44, 0x00, 0x00, 0x00], - '{' => [0x00, 0x08, 0x36, 0x41, 0x00, 0x00, 0x00, 0x00], - '|' => [0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00], - '}' => [0x00, 0x41, 0x36, 0x08, 0x00, 0x00, 0x00, 0x00], - _ => [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], - } +//bad font as the order is not the one we want to use +//goes from bottom left -> up -> right +pub(crate) fn bitmap_8x8(input: char) -> [u8; 8] { + // Populate the array with the data from the character array at the right index + match input { + '!' => [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00], + '"' => [0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00], + '#' => [0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00], + '$' => [0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00, 0x00, 0x00], + '%' => [0x23, 0x13, 0x08, 0x64, 0x62, 0x00, 0x00, 0x00], + '&' => [0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00, 0x00], + '\'' => [0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], + '(' => [0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x00, 0x00], + ')' => [0x00, 0x41, 0x22, 0x1C, 0x00, 0x00, 0x00, 0x00], + '*' => [0x08, 0x2A, 0x1C, 0x2A, 0x08, 0x00, 0x00, 0x00], + '+' => [0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x00, 0x00], + ',' => [0x00, 0x50, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00], + '-' => [0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00], + '.' => [0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00], + '/' => [0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00, 0x00], + '0' => [0x1C, 0x3E, 0x61, 0x41, 0x43, 0x3E, 0x1C, 0x00], + '1' => [0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00, 0x00], + '2' => [0x62, 0x73, 0x79, 0x59, 0x5D, 0x4F, 0x46, 0x00], + '3' => [0x20, 0x61, 0x49, 0x4D, 0x4F, 0x7B, 0x31, 0x00], + '4' => [0x18, 0x1C, 0x16, 0x13, 0x7F, 0x7F, 0x10, 0x00], + '5' => [0x27, 0x67, 0x45, 0x45, 0x45, 0x7D, 0x38, 0x00], + '6' => [0x3C, 0x7E, 0x4B, 0x49, 0x49, 0x79, 0x30, 0x00], + '7' => [0x03, 0x03, 0x71, 0x79, 0x0D, 0x07, 0x03, 0x00], + '8' => [0x36, 0x7F, 0x49, 0x49, 0x49, 0x7F, 0x36, 0x00], + '9' => [0x06, 0x4F, 0x49, 0x49, 0x69, 0x3F, 0x1E, 0x00], + ':' => [0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00], + ';' => [0x00, 0x56, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00], + '<' => [0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00], + '=' => [0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x00, 0x00], + '>' => [0x41, 0x22, 0x14, 0x08, 0x00, 0x00, 0x00, 0x00], + '?' => [0x02, 0x01, 0x51, 0x09, 0x06, 0x00, 0x00, 0x00], + '@' => [0x32, 0x49, 0x79, 0x41, 0x3E, 0x00, 0x00, 0x00], + 'A' => [0x7E, 0x11, 0x11, 0x11, 0x7E, 0x00, 0x00, 0x00], + 'B' => [0x7F, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00, 0x00], + 'C' => [0x3E, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00, 0x00], + 'D' => [0x7F, 0x7F, 0x41, 0x41, 0x63, 0x3E, 0x1C, 0x00], + 'E' => [0x7F, 0x49, 0x49, 0x49, 0x41, 0x00, 0x00, 0x00], + 'F' => [0x7F, 0x09, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00], + 'G' => [0x3E, 0x41, 0x41, 0x51, 0x32, 0x00, 0x00, 0x00], + 'H' => [0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00, 0x00, 0x00], + 'I' => [0x00, 0x41, 0x7F, 0x41, 0x00, 0x00, 0x00, 0x00], + 'J' => [0x20, 0x40, 0x41, 0x3F, 0x01, 0x00, 0x00, 0x00], + 'K' => [0x7F, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00], + 'L' => [0x7F, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00], + 'M' => [0x7F, 0x02, 0x04, 0x02, 0x7F, 0x00, 0x00, 0x00], + 'N' => [0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00, 0x00, 0x00], + 'O' => [0x3E, 0x7F, 0x41, 0x41, 0x41, 0x7F, 0x3E, 0x00], + 'P' => [0x7F, 0x09, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00], + 'Q' => [0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00, 0x00, 0x00], + 'R' => [0x7F, 0x7F, 0x11, 0x31, 0x79, 0x6F, 0x4E, 0x00], + 'S' => [0x46, 0x49, 0x49, 0x49, 0x31, 0x00, 0x00, 0x00], + 'T' => [0x01, 0x01, 0x7F, 0x01, 0x01, 0x00, 0x00, 0x00], + 'U' => [0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00], + 'V' => [0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00, 0x00, 0x00], + 'W' => [0x7F, 0x7F, 0x38, 0x1C, 0x38, 0x7F, 0x7F, 0x00], + 'X' => [0x63, 0x14, 0x08, 0x14, 0x63, 0x00, 0x00, 0x00], + 'Y' => [0x03, 0x04, 0x78, 0x04, 0x03, 0x00, 0x00, 0x00], + 'Z' => [0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x00, 0x00], + '[' => [0x00, 0x00, 0x7F, 0x41, 0x41, 0x00, 0x00, 0x00], + '\\' => [0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x00, 0x00], + ']' => [0x41, 0x41, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00], + '^' => [0x04, 0x02, 0x01, 0x02, 0x04, 0x00, 0x00, 0x00], + '_' => [0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00], + '`' => [0x00, 0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00], + 'a' => [0x20, 0x54, 0x54, 0x54, 0x78, 0x00, 0x00, 0x00], + 'b' => [0x7F, 0x48, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00], + 'c' => [0x38, 0x44, 0x44, 0x44, 0x20, 0x00, 0x00, 0x00], + 'd' => [0x38, 0x44, 0x44, 0x48, 0x7F, 0x00, 0x00, 0x00], + 'e' => [0x38, 0x54, 0x54, 0x54, 0x18, 0x00, 0x00, 0x00], + 'f' => [0x08, 0x7E, 0x09, 0x01, 0x02, 0x00, 0x00, 0x00], + 'g' => [0x08, 0x14, 0x54, 0x54, 0x3C, 0x00, 0x00, 0x00], + 'h' => [0x7F, 0x08, 0x04, 0x04, 0x78, 0x00, 0x00, 0x00], + 'i' => [0x00, 0x44, 0x7D, 0x40, 0x00, 0x00, 0x00, 0x00], + 'j' => [0x20, 0x40, 0x44, 0x3D, 0x00, 0x00, 0x00, 0x00], + 'k' => [0x00, 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00, 0x00], + 'l' => [0x00, 0x41, 0x7F, 0x40, 0x00, 0x00, 0x00, 0x00], + 'm' => [0x7C, 0x04, 0x18, 0x04, 0x78, 0x00, 0x00, 0x00], + 'n' => [0x7C, 0x08, 0x04, 0x04, 0x78, 0x00, 0x00, 0x00], + 'o' => [0x38, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00], + 'p' => [0x7C, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00, 0x00], + 'q' => [0x08, 0x14, 0x14, 0x18, 0x7C, 0x00, 0x00, 0x00], + 'r' => [0x7C, 0x08, 0x04, 0x04, 0x08, 0x00, 0x00, 0x00], + 's' => [0x48, 0x54, 0x54, 0x54, 0x20, 0x00, 0x00, 0x00], + 't' => [0x04, 0x3F, 0x44, 0x40, 0x20, 0x00, 0x00, 0x00], + 'u' => [0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00, 0x00, 0x00], + 'v' => [0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00, 0x00, 0x00], + 'w' => [0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00, 0x00, 0x00], + 'x' => [0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00], + 'y' => [0x0C, 0x50, 0x50, 0x50, 0x3C, 0x00, 0x00, 0x00], + 'z' => [0x44, 0x64, 0x54, 0x4C, 0x44, 0x00, 0x00, 0x00], + '{' => [0x00, 0x08, 0x36, 0x41, 0x00, 0x00, 0x00, 0x00], + '|' => [0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00], + '}' => [0x00, 0x41, 0x36, 0x08, 0x00, 0x00, 0x00, 0x00], + _ => [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], + } } /* pub(crate) const VCR_OSD_MONO_Bitmap = [ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, +af afa 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0x30, 0x0C, 0x30, 0x0C, 0x00, 0x0F, 0x00, 0x00, 0xF0, 0x0F, - 0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF, 0xF0, + aaa 0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0x0F, 0x0F, 0xF0, 0xF0, 0x0F, 0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0x0F, 0x0F, 0xF0, @@ -641,4 +745,4 @@ pub(crate) const LUT_VCOM0_QUICK: [u8; 44] = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -];*/ \ No newline at end of file +];*/ diff --git a/src/drawing/mod.rs b/src/drawing/mod.rs index 8ca5431..69c73fc 100644 --- a/src/drawing/mod.rs +++ b/src/drawing/mod.rs @@ -1,5 +1,6 @@ mod font; +use self::font::Font; #[derive(Clone, Copy)] @@ -15,11 +16,11 @@ pub enum Displayorientation { } //WARNING: Adapt for bigger sized displays! -pub struct DisplayDescription { - width: u16, - height: u16, - buffer_size: u16 -} +// pub struct DisplayDescription { +// width: u16, +// height: u16, +// buffer_size: u16 +// } // impl Display_Description { // pub fn new(width: u16, height: u16, buffer_size: u16) -> Display_Description { @@ -43,12 +44,7 @@ impl Display { } } -pub struct Graphics { - width: u16, - height: u16, - rotation: Displayorientation, - //buffer: Box//[u8; 15000], -} + pub enum Color { Black, @@ -56,24 +52,71 @@ pub enum Color { } impl Color { - fn get_bit_value(&self) -> u8 { + pub(crate) fn _get_bit_value(&self) -> u8 { match self { Color::White => 1u8, Color::Black => 0u8, } } - fn get_full_byte(&self) -> u8 { + pub(crate) fn get_full_byte(&self) -> u8 { match self { Color::White => 0xff, Color::Black => 0x00, } - } -} + } + //position counted from the left (highest value) from 0 to 7 + //remember: 1 is white, 0 is black + pub(crate) fn get_color(input: u8, pos: u8) -> Color { + match ((input >> (7 - pos)) & 1u8) > 0u8 { + true => Color::White, + false => Color::Black + } + } + fn inverse_color(color: &Color) -> Color { + match color { + Color::White => Color::Black, + Color::Black => Color::White, + } + } + fn normal_color(color: &Color) -> Color { + match color { + Color::White => Color::White, + Color::Black => Color::Black, + } + } + //position counted from the left (highest value) from 0 to 7 + //remember: 1 is white, 0 is black + pub(crate) fn _is_drawable_pixel(input: u8, pos: u8) -> bool { + ((input >> (7 - pos)) & 1u8) > 0u8 + } + + + pub(crate) fn convert_color(input: u8, pos: u8, foreground_color: &Color) -> Color { + //match color: + // - white for "nothing to draw"/background drawing + // - black for pixel to draw + // + //foreground color is the color you want to have in the foreground + match Color::get_color(input, pos) { + Color::White => Color::normal_color(foreground_color), + Color::Black => Color::inverse_color(foreground_color) + } + } +} + + +#[allow(dead_code)] +pub struct Graphics { + width: u16, + height: u16, + rotation: Displayorientation, + //buffer: Box//[u8; 15000], +} impl Graphics { /// width needs to be a multiple of 8! @@ -142,9 +185,11 @@ impl Graphics { /// font map doesn't work as expected, so everything with chars and string is broken atm ///TODO: implement! - pub fn draw_char(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, color: &Color) { + /// TODO: use Fonts + pub fn draw_char(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) { let mut counter = 0; - for &elem in font::to_bitmap(input).iter() { + let _a = font.get_char(input); + for &elem in font::bitmap_8x8(input).iter() { self.draw_byte(buffer, x0, y0 + counter * self.width, elem, color); counter += 1; } @@ -152,10 +197,31 @@ impl Graphics { ///TODO: implement! /// no autobreak line yet - pub fn draw_string(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) { + pub fn draw_string(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, font: &Font, color: &Color) { let mut counter = 0; for input_char in input.chars() { - self.draw_char(buffer, x0 + counter, y0, input_char, color); + self.draw_char(buffer, x0 + counter, y0, input_char, font, color); + counter += font.get_char_width(input_char) as u16; + } + } + + ///TODO: test! + pub fn draw_char_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, color: &Color) { + let mut counter = 0; + for &elem in font::bitmap_8x8(input).iter() { + for i in 0..8u8 { + self.draw_pixel(buffer, x0 + counter, y0 + 7 - i as u16, &Color::convert_color(elem, i, color)) + } + counter += 1; + } + } + + ///TODO: test! + /// no autobreak line yet + pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) { + let mut counter = 0; + for input_char in input.chars() { + self.draw_char_8x8(buffer, x0 + counter*8, y0, input_char, color); counter += 1; } } @@ -308,7 +374,7 @@ impl Graphics { // } ///TODO: implement! - pub fn draw_filled_circle(&self, buffer: &mut[u8]) { + pub fn draw_filled_circle(&self, _buffer: &mut[u8]) { unimplemented!(); } @@ -452,11 +518,6 @@ mod graphics { graphics.draw_pixel(&mut buffer, 9, 0, &Color::Black); assert_eq!(buffer[0], Color::White.get_full_byte()); assert_eq!(buffer[1], !0x40); - - for &elem in buffer.iter() { - - //assert_eq!(elem, 0x00u8); - } } #[test] @@ -466,30 +527,69 @@ mod graphics { graphics.draw_byte(&mut buffer, 0, 0, 0xff, &Color::Black); assert_eq!(buffer[0], Color::Black.get_full_byte()); - assert_eq!(buffer[1], Color::White.get_full_byte()); + + for i in 1..buffer.len() { + assert_eq!(buffer[i], Color::White.get_full_byte()); + } + + graphics.draw_byte(&mut buffer, 0, 0, 0x5A, &Color::Black) ; + assert_eq!(buffer[0], !0x5A); + } + + #[test] + fn test_char_with_8x8_font() { + + // Test ! + let mut buffer = [Color::White.get_full_byte(); 8]; + let graphics = Graphics::new(8, 8); + graphics.draw_char_8x8(&mut buffer, 0, 0, '!', &Color::Black); + + for i in 0..5 { + assert_eq!(buffer[i], !0x20); + } + assert_eq!(buffer[5], Color::White.get_full_byte()); + assert_eq!(buffer[6], !0x20); + assert_eq!(buffer[7], Color::White.get_full_byte()); + // Test H + let mut buffer = [Color::White.get_full_byte(); 8]; + let graphics = Graphics::new(8, 8); + graphics.draw_char_8x8(&mut buffer, 0, 0, 'H', &Color::Black); + + for i in 0..3 { + assert_eq!(buffer[i], !0x88); + } + assert_eq!(buffer[3], !0xF8); + for i in 4..7 { + assert_eq!(buffer[i], !0x88); + } + assert_eq!(buffer[7], Color::White.get_full_byte()); + } + + #[test] + fn test_string_with_8x8_font() { + + // Test !H let mut buffer = [Color::White.get_full_byte(); 16]; let graphics = Graphics::new(16, 8); - graphics.draw_pixel(&mut buffer, 9, 0, &Color::Black); - assert_eq!(buffer[0], Color::White.get_full_byte()); - assert_eq!(buffer[1], !0x40); - - for &elem in buffer.iter() { - - //assert_eq!(elem, 0x00u8); + graphics.draw_string_8x8(&mut buffer, 0, 0, "!H", &Color::Black); + + for i in 0..5 { + assert_eq!(buffer[i*2], !0x20); } - } + assert_eq!(buffer[5*2], Color::White.get_full_byte()); + assert_eq!(buffer[6*2], !0x20); + assert_eq!(buffer[7*2], Color::White.get_full_byte()); - // #[test] - // #[should_panic] - // fn test_any_panic() { - // divide_non_zero_result(1, 0); - // } - // #[test] - // #[should_panic(expected = "Divide result is zero")] - // fn test_specific_panic() { - // divide_non_zero_result(1, 10); - // } + for i in 0..3 { + assert_eq!(buffer[i*2 + 1], !0x88); + } + assert_eq!(buffer[3*2 + 1], !0xF8); + for i in 4..7 { + assert_eq!(buffer[i*2 + 1], !0x88); + } + assert_eq!(buffer[7*2 + 1], Color::White.get_full_byte()); + } } \ No newline at end of file diff --git a/src/epd4in2/command.rs b/src/epd4in2/command.rs index c82d930..c81bd79 100644 --- a/src/epd4in2/command.rs +++ b/src/epd4in2/command.rs @@ -151,4 +151,19 @@ impl Command { pub fn addr(self) -> u8 { self as u8 } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn command_addr() { + assert_eq!(Command::POWER_SAVING.addr(), 0xE3); + + assert_eq!(Command::PANEL_SETTING.addr(), 0x00); + + assert_eq!(Command::DISPLAY_REFRESH.addr(), 0x12); + } } \ No newline at end of file diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 33ccb7f..aaf52dd 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -61,6 +61,8 @@ use hal::{ mod lut; use self::lut::*; +use drawing::Color; + pub mod command; pub use command::Command as Command; @@ -338,8 +340,8 @@ where /// /// TODO: should that option be removed? E.g. the struct contains an additional default background value /// which is settable? - pub fn clear_frame(&mut self, reset_color: Option) -> Result<(), E> { - let reset_color: u8 = reset_color.unwrap_or(0xff); + pub fn clear_frame(&mut self, reset_color: Option) -> Result<(), E> { + let reset_color: Color = reset_color.unwrap_or(Color::White); self.send_resolution()?; @@ -348,7 +350,7 @@ where self.send_command(Command::DATA_START_TRANSMISSION_1)?; self.delay_ms(2); for _ in 0..size { - self.send_data(reset_color)?; + self.send_data(reset_color.get_full_byte())?; } self.delay_ms(2); @@ -356,7 +358,7 @@ where self.send_command(Command::DATA_START_TRANSMISSION_2)?; self.delay_ms(2); for _ in 0..size { - self.send_data(reset_color)?; + self.send_data(reset_color.get_full_byte())?; } Ok(()) }