From 36eef459710d391515f5db6244d8a886be9bf533 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 8 Mar 2025 01:49:43 -0500 Subject: [PATCH] Fill out an alphabet and animate the screen --- pico-st7789/src/canvas.rs | 8 + pico-st7789/src/font/bits_5_8.rs | 377 +++++++++++++++++++++++++++++-- pico-st7789/src/main.rs | 28 +-- 3 files changed, 374 insertions(+), 39 deletions(-) diff --git a/pico-st7789/src/canvas.rs b/pico-st7789/src/canvas.rs index a695b86..7a16821 100644 --- a/pico-st7789/src/canvas.rs +++ b/pico-st7789/src/canvas.rs @@ -19,6 +19,14 @@ pub struct RGB { pub trait Canvas { fn set_pixel(&mut self, x: usize, y: usize, color: &RGB); + fn fill(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) { + for x in x1..x2 + 1 { + for y in y1..y2 + 1 { + self.set_pixel(x, y, color); + } + } + } + fn square(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) { for x in x1..x2 + 1 { self.set_pixel(x, y1, color); diff --git a/pico-st7789/src/font/bits_5_8.rs b/pico-st7789/src/font/bits_5_8.rs index 301e3dd..ecada60 100644 --- a/pico-st7789/src/font/bits_5_8.rs +++ b/pico-st7789/src/font/bits_5_8.rs @@ -4,7 +4,7 @@ use crate::canvas::{Canvas, RGB}; use super::{Font, Glyph}; -pub struct BitmapGlyph([u8; 8]); +pub struct BitmapGlyph([u8; 7]); pub struct BitmapFont(BTreeMap); @@ -12,15 +12,26 @@ impl BitmapFont { pub fn new() -> Self { let mut font = BTreeMap::new(); font.insert(' ', BitmapGlyph([ - 0b10101, - 0b01010, - 0b10101, - 0b01010, - 0b10101, - 0b01010, - 0b10101, - 0b01010, + 0b00000, + 0b00000, + 0b00000, + 0b00000, + 0b00000, + 0b00000, + 0b00000, ])); + font.insert( + ':', + BitmapGlyph([ + 0b00000, + 0b00100, + 0b00100, + 0b00000, + 0b00100, + 0b00100, + 0b00000, + ]), + ); font.insert( '/', BitmapGlyph([ @@ -28,7 +39,6 @@ impl BitmapFont { 0b00010, 0b00010, 0b00100, - 0b00100, 0b01000, 0b01000, 0b10000, @@ -41,7 +51,6 @@ impl BitmapFont { 0b10001, 0b10001, 0b10101, - 0b10101, 0b10001, 0b10001, 0b01110, @@ -50,19 +59,37 @@ impl BitmapFont { font.insert( '1', BitmapGlyph([ - 0b00001, 0b00011, 0b00101, 0b00001, 0b00001, 0b00001, 0b00001, 0b00001, + 0b00001, + 0b00011, + 0b00101, + 0b00001, + 0b00001, + 0b00001, + 0b00001, ]), ); font.insert( '2', BitmapGlyph([ - 0b01110, 0b10001, 0b00001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111, + 0b01110, + 0b10001, + 0b00001, + 0b00010, + 0b00100, + 0b01000, + 0b11111, ]), ); font.insert( '3', BitmapGlyph([ - 0b01110, 0b10001, 0b00001, 0b01110, 0b00001, 0b00001, 0b10001, 0b01110, + 0b01110, + 0b10001, + 0b00001, + 0b01110, + 0b00001, + 0b10001, + 0b01110, ]), ); font.insert( @@ -75,7 +102,6 @@ impl BitmapFont { 0b00001, 0b00001, 0b00001, - 0b00001, ]), ); font.insert( @@ -86,7 +112,6 @@ impl BitmapFont { 0b10000, 0b11111, 0b00001, - 0b00001, 0b10001, 0b01110, ]), @@ -97,7 +122,6 @@ impl BitmapFont { 0b01110, 0b10001, 0b10000, - 0b10000, 0b11110, 0b10001, 0b10001, @@ -109,7 +133,6 @@ impl BitmapFont { BitmapGlyph([ 0b11111, 0b00001, - 0b00001, 0b00010, 0b00110, 0b00100, @@ -124,7 +147,6 @@ impl BitmapFont { 0b10001, 0b10001, 0b01110, - 0b01110, 0b10001, 0b10001, 0b01110, @@ -136,13 +158,324 @@ impl BitmapFont { 0b01110, 0b10001, 0b10001, - 0b10001, 0b01111, 0b00001, 0b00001, 0b01110, ]), ); + font.insert( + 'A', + BitmapGlyph([ + 0b00100, + 0b01010, + 0b01010, + 0b10001, + 0b11111, + 0b10001, + 0b10001, + ]), + ); + font.insert( + 'B', + BitmapGlyph([ + 0b11110, + 0b10001, + 0b10001, + 0b1111 , + 0b10001, + 0b10001, + 0b11110, + ]), + ); + font.insert( + 'C', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10000, + 0b10000, + 0b10000, + 0b10001, + 0b01110, + ]), + ); + font.insert( + 'D', + BitmapGlyph([ + 0b11110, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b11110, + ]), + ); + font.insert( + 'E', + BitmapGlyph([ + 0b11111, + 0b10000, + 0b10000, + 0b11111, + 0b10000, + 0b10000, + 0b11111, + ]), + ); + font.insert( + 'F', + BitmapGlyph([ + 0b11111, + 0b10000, + 0b10000, + 0b11110, + 0b10000, + 0b10000, + 0b10000, + ]), + ); + font.insert( + 'G', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10000, + 0b10011, + 0b10001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + 'H', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b10001, + 0b11111, + 0b10001, + 0b10001, + 0b10001, + ]), + ); + font.insert( + 'I', + BitmapGlyph([ + 0b11111, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b11111, + ]), + ); + font.insert( + 'J', + BitmapGlyph([ + 0b00111, + 0b00001, + 0b00001, + 0b00001, + 0b00001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + 'K', + BitmapGlyph([ + 0b10001, + 0b10010, + 0b10100, + 0b11000, + 0b10100, + 0b10010, + 0b10001, + ]), + ); + font.insert( + 'L', + BitmapGlyph([ + 0b10000, + 0b10000, + 0b10000, + 0b10000, + 0b10000, + 0b10000, + 0b11111, + ]), + ); + font.insert( + 'M', + BitmapGlyph([ + 0b10001, + 0b11011, + 0b10101, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + ]), + ); + font.insert( + 'N', + BitmapGlyph([ + 0b10001, + 0b11001, + 0b11001, + 0b10101, + 0b10011, + 0b10011, + 0b10001, + ]), + ); + font.insert( + 'O', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + 'P', + BitmapGlyph([ + 0b11110, + 0b10001, + 0b10001, + 0b11110, + 0b10000, + 0b10000, + 0b10000, + ]), + ); + font.insert( + 'Q', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10001, + 0b10001, + 0b10101, + 0b10011, + 0b01110, + ]), + ); + font.insert( + 'R', + BitmapGlyph([ + 0b11110, + 0b10001, + 0b10001, + 0b11110, + 0b10100, + 0b10010, + 0b10001, + ]), + ); + font.insert( + 'S', + BitmapGlyph([ + 0b01110, + 0b10001, + 0b10000, + 0b01110, + 0b00001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + 'T', + BitmapGlyph([ + 0b11111, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + ]), + ); + font.insert( + 'U', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b01110, + ]), + ); + font.insert( + 'V', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b01010, + 0b01010, + 0b00100, + ]), + ); + font.insert( + 'W', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b10001, + 0b10001, + 0b10101, + 0b10101, + 0b01010, + ]), + ); + font.insert( + 'X', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b01010, + 0b00100, + 0b01010, + 0b10001, + 0b10001, + ]), + ); + font.insert( + 'Y', + BitmapGlyph([ + 0b10001, + 0b10001, + 0b01010, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + ]), + ); + font.insert( + 'Z', + BitmapGlyph([ + 0b11111, + 0b00001, + 0b00010, + 0b00100, + 0b01000, + 0b10000, + 0b11111, + ]), + ); Self(font) } } @@ -155,7 +488,7 @@ impl Font for BitmapFont { impl Glyph for BitmapGlyph { fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) { - for row in 0..8 { + for row in 0..7 { if self.0[row] & (1 << 4) > 0 { canvas.set_pixel(x, y + row, color); } @@ -175,6 +508,6 @@ impl Glyph for BitmapGlyph { } fn extents(&self) -> (usize, usize) { - (5, 8) + (5, 7) } } diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index 5537613..ad3a90f 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -3,6 +3,7 @@ extern crate alloc; +use alloc::fmt::format; use embedded_alloc::LlffHeap as Heap; use embedded_hal::{delay::DelayNs, digital::OutputPin}; use fugit::RateExtU32; @@ -64,7 +65,6 @@ unsafe fn main() -> ! { 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. @@ -149,24 +149,17 @@ unsafe fn main() -> ! { */ let mut canvas = FrameBuf::new(); let white = RGB { r: 63, g: 63, b: 63 }; + let mut count = 0; loop { - print(&mut canvas, &font_sixteen, 1, 1, " !\"#$%&'<>*+,-./", &RGB{ r: 63, g: 63, b: 63 }); - print(&mut canvas, &font_sixteen, 1, 21, "0123456789| = ?", &RGB{ r: 63, g: 63, b: 63 }); - print(&mut canvas, &font_sixteen, 1, 41, "@ABCDEFGHIJKLMNO", &RGB{ r: 63, g: 63, b: 63 }); - print(&mut canvas, &font_sixteen, 1, 61, "PQRSTUVWXYZ[\\]^_", &RGB{ r: 63, g: 63, b: 63 }); - 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.fill(0, 10, 170, 20, &RGB{r: 0, g: 0, b: 0 }); + print(&mut canvas, &font_bitmap, 1, 10, &format(format_args!("COUNT: {:03}", count)), &RGB{ r: 32, g: 32, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 200, " !\"#$%&'<>*+,-./", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 220, "0123456789|: = ?", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 240, "@ABCDEFGHIJKLMNO", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 260, "PQRSTUVWXYZ[\\]^_", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 280, "`abcdefghijklmno", &RGB{ r: 63, g: 63, b: 63 }); + print(&mut canvas, &font_bitmap, 1, 300, "pqrstuvwxyz{|}", &RGB{ r: 63, g: 63, b: 63 }); // canvas.square(10, 70, 160, 310, &white); @@ -177,6 +170,7 @@ unsafe fn main() -> ! { display.send_buf(canvas.buf); let _ = led.set_low(); } + count = count + 1; /* for x in 80..90 { for y in 155..165 {