From 9175b9d4ccb57aa58d0cb72f3301c036432800fe Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Fri, 7 Mar 2025 22:23:03 -0500 Subject: [PATCH] Create a print function and fill out more sixteen-segment glyphs --- pico-st7789/src/canvas.rs | 33 +++- pico-st7789/src/font/sixteen_segment.rs | 174 +++++++++++++++++++++- pico-st7789/src/main.rs | 190 +----------------------- 3 files changed, 207 insertions(+), 190 deletions(-) diff --git a/pico-st7789/src/canvas.rs b/pico-st7789/src/canvas.rs index b154ebf..a695b86 100644 --- a/pico-st7789/src/canvas.rs +++ b/pico-st7789/src/canvas.rs @@ -8,24 +8,47 @@ // e c // d d d -pub struct RGB { pub r: u8, pub g: u8, pub b: u8 } +use crate::font::{Font, Glyph}; + +pub struct RGB { + pub r: u8, + pub g: u8, + pub b: u8, +} pub trait Canvas { fn set_pixel(&mut self, x: usize, y: usize, color: &RGB); fn square(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) { - for x in x1..x2+1 { + for x in x1..x2 + 1 { self.set_pixel(x, y1, color); } - for x in x1..x2+1 { + for x in x1..x2 + 1 { self.set_pixel(x, y2, color); } - for y in y1..y2+1 { + for y in y1..y2 + 1 { self.set_pixel(x1, y, color); } - for y in y1..y2+1 { + for y in y1..y2 + 1 { self.set_pixel(x2, y, color); } } } +pub fn print( + canvas: &mut impl Canvas, + font: &impl Font, + sx: usize, + sy: usize, + text: &str, + color: &RGB, +) where + A: Glyph, +{ + let mut x = sx; + for c in text.chars().map(|c| font.glyph(c)) { + c.draw(canvas, x, sy, color); + let (dx, _) = c.extents(); + x = x + dx + 1; + } +} diff --git a/pico-st7789/src/font/sixteen_segment.rs b/pico-st7789/src/font/sixteen_segment.rs index 62e1285..eb8cb29 100644 --- a/pico-st7789/src/font/sixteen_segment.rs +++ b/pico-st7789/src/font/sixteen_segment.rs @@ -396,13 +396,182 @@ impl SixteenSegmentFont { | SixteenSegmentGlyph::J | SixteenSegmentGlyph::K, ); + + font.insert( + 'a', + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::L, + ); + font.insert( + 'b', + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::L, + ); + font.insert( + 'c', + SixteenSegmentGlyph::D1 | SixteenSegmentGlyph::E | SixteenSegmentGlyph::G1, + ); + font.insert( + 'd', + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::G2 + | SixteenSegmentGlyph::L, + ); + font.insert( + 'e', + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::K, + ); + font.insert( + 'f', + SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, + ); + font.insert( + 'g', + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, + ); + font.insert( + 'h', + SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::L, + ); + font.insert('i', SixteenSegmentGlyph::L); + font.insert( + 'j', + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, + ); + font.insert( + 'k', + SixteenSegmentGlyph::I + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::L + | SixteenSegmentGlyph::M, + ); + font.insert('l', SixteenSegmentGlyph::E | SixteenSegmentGlyph::F); + font.insert( + 'm', + SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::L + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, + ); + font.insert( + 'n', + SixteenSegmentGlyph::E | SixteenSegmentGlyph::L | SixteenSegmentGlyph::G1, + ); + font.insert( + 'o', + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::L, + ); + font.insert( + 'p', + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::I, + ); + font.insert( + 'q', + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, + ); + font.insert('r', SixteenSegmentGlyph::E | SixteenSegmentGlyph::G1); + font.insert( + 's', + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::L, + ); + font.insert( + 't', + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1, + ); + font.insert( + 'u', + SixteenSegmentGlyph::D1 | SixteenSegmentGlyph::E | SixteenSegmentGlyph::L, + ); + font.insert('v', SixteenSegmentGlyph::E | SixteenSegmentGlyph::K); + font.insert( + 'w', + SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::K + | SixteenSegmentGlyph::M, + ); + font.insert( + 'x', + SixteenSegmentGlyph::H + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::K + | SixteenSegmentGlyph::M, + ); + font.insert( + 'y', + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::G2, + ); + font.insert( + 'z', + SixteenSegmentGlyph::D1 | SixteenSegmentGlyph::G1 | SixteenSegmentGlyph::K, + ); + font.insert( + '@', + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G2, + ); Self(font) } } impl Font for SixteenSegmentFont { fn glyph(&self, c: char) -> &SixteenSegmentGlyph { - self.0.get(&c).unwrap() + self.0.get(&c).unwrap_or(&SixteenSegmentGlyph::NONE) } } @@ -515,7 +684,6 @@ impl Glyph for SixteenSegmentGlyph { } fn extents(&self) -> (usize, usize) { - (8, 12) + (9, 13) } } - diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index 68b0554..79da94f 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -14,7 +14,7 @@ use rp_pico::{ }; mod canvas; -use canvas::{Canvas, RGB}; +use canvas::{Canvas, RGB, print}; mod font; use font::{Font, Glyph, SevenSegmentFont, SixteenSegmentFont}; @@ -64,7 +64,6 @@ unsafe fn main() -> ! { unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } } - let font_seven = SevenSegmentFont::new(); let font_sixteen = SixteenSegmentFont::new(); // rp_pico::pac::Peripherals is a reference to physical hardware defined on the Pico. @@ -151,187 +150,14 @@ unsafe fn main() -> ! { let white = RGB { r: 63, g: 63, b: 63 }; loop { - /* - draw_seven_segment(0, &mut frame, 0, 0, (255, 255, 255)); - draw_seven_segment(1, &mut frame, 7, 0, (255, 255, 255)); - draw_seven_segment(2, &mut frame, 14, 0, (255, 255, 255)); - draw_seven_segment(3, &mut frame, 21, 0, (255, 255, 255)); - draw_seven_segment(4, &mut frame, 28, 0, (255, 255, 255)); - draw_seven_segment(5, &mut frame, 35, 0, (255, 255, 255)); - draw_seven_segment(6, &mut frame, 42, 0, (255, 255, 255)); - draw_seven_segment(7, &mut frame, 49, 0, (255, 255, 255)); - draw_seven_segment(8, &mut frame, 56, 0, (255, 255, 255)); - draw_seven_segment(9, &mut frame, 63, 0, (255, 255, 255)); - */ + 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 }); - font_seven - .glyph(' ') - .draw(&mut canvas, 0, 0, &white); - font_seven - .glyph('0') - .draw(&mut canvas, 11, 0, &white); - font_seven - .glyph('1') - .draw(&mut canvas, 22, 0, &white); - font_seven - .glyph('2') - .draw(&mut canvas, 33, 0, &white); - font_seven - .glyph('3') - .draw(&mut canvas, 44, 0, &white); - font_seven - .glyph('4') - .draw(&mut canvas, 55, 0, &white); - font_seven - .glyph('5') - .draw(&mut canvas, 66, 0, &white); - font_seven - .glyph('6') - .draw(&mut canvas, 77, 0, &white); - font_seven - .glyph('7') - .draw(&mut canvas, 88, 0, &white); - font_seven - .glyph('8') - .draw(&mut canvas, 99, 0, &white); - font_seven - .glyph('9') - .draw(&mut canvas, 110, 0, &white); - - font_seven - .glyph('A') - .draw(&mut canvas, 121, 0, &white); - font_seven - .glyph('B') - .draw(&mut canvas, 132, 0, &white); - font_seven - .glyph('C') - .draw(&mut canvas, 143, 0, &white); - font_seven - .glyph('D') - .draw(&mut canvas, 154, 0, &white); - font_seven - .glyph('E') - .draw(&mut canvas, 165, 0, &white); - font_seven - .glyph('F') - .draw(&mut canvas, 0, 20, &white); - - font_sixteen - .glyph(' ') - .draw(&mut canvas, 0, 20, &white); - font_sixteen - .glyph('0') - .draw(&mut canvas, 11, 20, &white); - font_sixteen - .glyph('1') - .draw(&mut canvas, 22, 20, &white); - font_sixteen - .glyph('2') - .draw(&mut canvas, 33, 20, &white); - font_sixteen - .glyph('3') - .draw(&mut canvas, 44, 20, &white); - font_sixteen - .glyph('4') - .draw(&mut canvas, 55, 20, &white); - font_sixteen - .glyph('5') - .draw(&mut canvas, 66, 20, &white); - font_sixteen - .glyph('6') - .draw(&mut canvas, 77, 20, &white); - font_sixteen - .glyph('7') - .draw(&mut canvas, 88, 20, &white); - font_sixteen - .glyph('8') - .draw(&mut canvas, 99, 20, &white); - font_sixteen - .glyph('9') - .draw(&mut canvas, 110, 20, &white); - - font_sixteen - .glyph('A') - .draw(&mut canvas, 0, 40, &white); - font_sixteen - .glyph('B') - .draw(&mut canvas, 11, 40, &white); - font_sixteen - .glyph('C') - .draw(&mut canvas, 22, 40, &white); - font_sixteen - .glyph('D') - .draw(&mut canvas, 33, 40, &white); - font_sixteen - .glyph('E') - .draw(&mut canvas, 44, 40, &white); - font_sixteen - .glyph('F') - .draw(&mut canvas, 55, 40, &white); - font_sixteen - .glyph('G') - .draw(&mut canvas, 66, 40, &white); - font_sixteen - .glyph('H') - .draw(&mut canvas, 77, 40, &white); - font_sixteen - .glyph('I') - .draw(&mut canvas, 88, 40, &white); - font_sixteen - .glyph('J') - .draw(&mut canvas, 99, 40, &white); - font_sixteen - .glyph('K') - .draw(&mut canvas, 110, 40, &white); - font_sixteen - .glyph('L') - .draw(&mut canvas, 121, 40, &white); - font_sixteen - .glyph('M') - .draw(&mut canvas, 132, 40, &white); - font_sixteen - .glyph('N') - .draw(&mut canvas, 143, 40, &white); - font_sixteen - .glyph('O') - .draw(&mut canvas, 154, 40, &white); - - font_sixteen - .glyph('P') - .draw(&mut canvas, 0, 60, &white); - font_sixteen - .glyph('Q') - .draw(&mut canvas, 11, 60, &white); - font_sixteen - .glyph('R') - .draw(&mut canvas, 22, 60, &white); - font_sixteen - .glyph('S') - .draw(&mut canvas, 33, 60, &white); - font_sixteen - .glyph('T') - .draw(&mut canvas, 44, 60, &white); - font_sixteen - .glyph('U') - .draw(&mut canvas, 55, 60, &white); - font_sixteen - .glyph('V') - .draw(&mut canvas, 66, 60, &white); - font_sixteen - .glyph('W') - .draw(&mut canvas, 77, 60, &white); - font_sixteen - .glyph('X') - .draw(&mut canvas, 88, 60, &white); - font_sixteen - .glyph('Y') - .draw(&mut canvas, 99, 60, &white); - font_sixteen - .glyph('Z') - .draw(&mut canvas, 110, 60, &white); - - canvas.square(10, 70, 160, 310, &white); + // canvas.square(10, 70, 160, 310, &white); { let display = display.acquire();