diff --git a/pico-st7789/src/font/bits_5_8.rs b/pico-st7789/src/font/bits_5_8.rs new file mode 100644 index 0000000..301e3dd --- /dev/null +++ b/pico-st7789/src/font/bits_5_8.rs @@ -0,0 +1,180 @@ +use alloc::collections::btree_map::BTreeMap; + +use crate::canvas::{Canvas, RGB}; + +use super::{Font, Glyph}; + +pub struct BitmapGlyph([u8; 8]); + +pub struct BitmapFont(BTreeMap); + +impl BitmapFont { + pub fn new() -> Self { + let mut font = BTreeMap::new(); + font.insert(' ', BitmapGlyph([ + 0b10101, + 0b01010, + 0b10101, + 0b01010, + 0b10101, + 0b01010, + 0b10101, + 0b01010, + ])); + font.insert( + '/', + BitmapGlyph([ + 0b00001, + 0b00010, + 0b00010, + 0b00100, + 0b00100, + 0b01000, + 0b01000, + 0b10000, + ]), + ); + font.insert( + '0', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10001, + 0b10101, + 0b10101, + 0b10001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + '1', + BitmapGlyph([ + 0b00001, 0b00011, 0b00101, 0b00001, 0b00001, 0b00001, 0b00001, 0b00001, + ]), + ); + font.insert( + '2', + BitmapGlyph([ + 0b01110, 0b10001, 0b00001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111, + ]), + ); + font.insert( + '3', + BitmapGlyph([ + 0b01110, 0b10001, 0b00001, 0b01110, 0b00001, 0b00001, 0b10001, 0b01110, + ]), + ); + font.insert( + '4', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b10001, + 0b11111, + 0b00001, + 0b00001, + 0b00001, + 0b00001, + ]), + ); + font.insert( + '5', + BitmapGlyph([ + 0b11111, + 0b10000, + 0b10000, + 0b11111, + 0b00001, + 0b00001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + '6', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10000, + 0b10000, + 0b11110, + 0b10001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + '7', + BitmapGlyph([ + 0b11111, + 0b00001, + 0b00001, + 0b00010, + 0b00110, + 0b00100, + 0b01000, + 0b01000, + ]), + ); + font.insert( + '8', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10001, + 0b01110, + 0b01110, + 0b10001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + '9', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10001, + 0b10001, + 0b01111, + 0b00001, + 0b00001, + 0b01110, + ]), + ); + Self(font) + } +} + +impl Font for BitmapFont { + fn glyph(&self, c: char) -> &BitmapGlyph { + self.0.get(&c).unwrap_or(self.0.get(&' ').unwrap()) + } +} + +impl Glyph for BitmapGlyph { + fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) { + for row in 0..8 { + if self.0[row] & (1 << 4) > 0 { + canvas.set_pixel(x, y + row, color); + } + if self.0[row] & (1 << 3) > 0 { + canvas.set_pixel(x + 1, y + row, color); + } + if self.0[row] & (1 << 2) > 0 { + canvas.set_pixel(x + 2, y + row, color); + } + if self.0[row] & (1 << 1) > 0 { + canvas.set_pixel(x + 3, y + row, color); + } + if self.0[row] & 1 > 0 { + canvas.set_pixel(x + 4, y + row, color); + } + } + } + + fn extents(&self) -> (usize, usize) { + (5, 8) + } +} diff --git a/pico-st7789/src/font/mod.rs b/pico-st7789/src/font/mod.rs index 3886765..f20065b 100644 --- a/pico-st7789/src/font/mod.rs +++ b/pico-st7789/src/font/mod.rs @@ -1,5 +1,8 @@ use crate::canvas::{Canvas, RGB}; +mod bits_5_8; +pub use bits_5_8::BitmapFont; + mod seven_segment; pub use seven_segment::SevenSegmentFont; diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index 79da94f..5537613 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -17,7 +17,7 @@ mod canvas; use canvas::{Canvas, RGB, print}; mod font; -use font::{Font, Glyph, SevenSegmentFont, SixteenSegmentFont}; +use font::{BitmapFont, Font, Glyph, SevenSegmentFont, SixteenSegmentFont}; mod st7789; use st7789::{ST7789Display, SETUP_PROGRAM}; @@ -59,12 +59,13 @@ impl Canvas for FrameBuf { unsafe fn main() -> ! { { use core::mem::MaybeUninit; - const HEAP_SIZE: usize = 1024; + const HEAP_SIZE: usize = 64 * 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } } let font_sixteen = SixteenSegmentFont::new(); + let font_bitmap = BitmapFont::new(); // rp_pico::pac::Peripherals is a reference to physical hardware defined on the Pico. let mut peripherals = pac::Peripherals::take().unwrap(); @@ -157,6 +158,16 @@ unsafe fn main() -> ! { print(&mut canvas, &font_sixteen, 1, 81, "`abcdefghijklmno", &RGB{ r: 63, g: 63, b: 63 }); print(&mut canvas, &font_sixteen, 1, 101, "pqrstuvwxyz{|}", &RGB{ r: 63, g: 63, b: 63 }); + + print(&mut canvas, &font_bitmap, 1, 121, " !\"#$%&'<>*+,-./", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 141, "0123456789| = ?", &RGB{ r: 63, g: 63, b: 63 }); + /* + print(&mut canvas, &font_bitmap, 1, 161, "@ABCDEFGHIJKLMNO", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 181, "PQRSTUVWXYZ[\\]^_", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 201, "`abcdefghijklmno", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 221, "pqrstuvwxyz{|}", &RGB{ r: 63, g: 63, b: 63 }); + */ + // canvas.square(10, 70, 160, 310, &white); {