From 85e5d0bb5e99acbfe2b2f9716dc0e7fb6a1ec9cd Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Mon, 3 Mar 2025 10:24:50 -0500 Subject: [PATCH 1/7] Worked out a font and a canvas I'm adding a 16-segment-based font here, and have encoded the numbers 0-10. I've also worked out a way to make a Canvas structure not crash the pico. --- Cargo.lock | 52 ++++- pico-st7789/Cargo.toml | 1 + pico-st7789/src/drawing.rs | 125 ++++++++++++ pico-st7789/src/font.rs | 398 +++++++++++++++++++++++++++++++++++++ pico-st7789/src/main.rs | 88 +++++--- 5 files changed, 634 insertions(+), 30 deletions(-) create mode 100644 pico-st7789/src/drawing.rs create mode 100644 pico-st7789/src/font.rs diff --git a/Cargo.lock b/Cargo.lock index bb828ca..1772741 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -693,6 +693,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "const-default" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b396d1f76d455557e1218ec8066ae14bba60b4b36ecd55577ba979f5db7ecaa" + [[package]] name = "const-oid" version = "0.9.6" @@ -1057,6 +1063,18 @@ dependencies = [ "serde 1.0.218", ] +[[package]] +name = "embedded-alloc" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f2de9133f68db0d4627ad69db767726c99ff8585272716708227008d3f1bddd" +dependencies = [ + "const-default", + "critical-section", + "linked_list_allocator", + "rlsf", +] + [[package]] name = "embedded-dma" version = "0.2.0" @@ -2870,6 +2888,12 @@ dependencies = [ "fixed", ] +[[package]] +name = "linked_list_allocator" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286" + [[package]] name = "linux-raw-sys" version = "0.4.15" @@ -3540,6 +3564,7 @@ name = "pico-st7789" version = "0.1.0" dependencies = [ "cortex-m-rt", + "embedded-alloc", "embedded-hal 1.0.0", "fugit", "panic-halt", @@ -4131,6 +4156,18 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "rlsf" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222fb240c3286247ecdee6fa5341e7cdad0ffdf8e7e401d9937f2d58482a20bf" +dependencies = [ + "cfg-if", + "const-default", + "libc", + "svgbobdoc", +] + [[package]] name = "rp-pico" version = "0.9.0" @@ -4844,6 +4881,19 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "svgbobdoc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2c04b93fc15d79b39c63218f15e3fdffaa4c227830686e3b7c5f41244eb3e50" +dependencies = [ + "base64 0.9.3", + "proc-macro2", + "quote", + "syn 1.0.109", + "unicode-width", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/pico-st7789/Cargo.toml b/pico-st7789/Cargo.toml index 6d8012e..001c708 100644 --- a/pico-st7789/Cargo.toml +++ b/pico-st7789/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] cortex-m-rt = "0.7.3" +embedded-alloc = "0.6.0" embedded-hal = "1.0.0" fugit = "0.3.7" panic-halt = "1.0.0" diff --git a/pico-st7789/src/drawing.rs b/pico-st7789/src/drawing.rs new file mode 100644 index 0000000..e02e868 --- /dev/null +++ b/pico-st7789/src/drawing.rs @@ -0,0 +1,125 @@ +// a a a +// f b +// f b +// f b +// g g g +// e c +// e c +// e c +// d d d + +use alloc::{boxed::Box, vec::Vec}; + +pub struct Canvas<'a> { + pub buf: &'a mut [u8], + pub width: usize, +} + +impl<'a> Canvas<'a> { + pub fn new(buf: &'a mut [u8], columns: usize) -> Self { + Self { + buf, + width: columns, + } + } + + pub fn set_pixel(&mut self, x: usize, y: usize, color: (u8, u8, u8)) { + self.buf[(y * self.width + x) * 3 + 0] = color.0 << 2; + self.buf[(y * self.width + x) * 3 + 1] = color.1 << 2; + self.buf[(y * self.width + x) * 3 + 2] = color.2 << 2; + } +} + +/* +pub const DIGIT_WIDTH: usize = 5; +pub const DIGIT_HEIGHT: usize = 9; + +pub const ZERO_SEVEN_SEGMENT: [bool; 7] = [true, true, true, true, true, true, false]; + +pub const ONE_SEVEN_SEGMENT: [bool; 7] = [false, true, true, false, false, false, false]; + +pub const TWO_SEVEN_SEGMENT: [bool; 7] = [true, true, false, true, true, false, true]; + +pub const THREE_SEVEN_SEGMENT: [bool; 7] = [true, true, true, true, false, false, true]; + +pub const FOUR_SEVEN_SEGMENT: [bool; 7] = [false, true, true, false, false, true, true]; + +pub const FIVE_SEVEN_SEGMENT: [bool; 7] = [true, false, true, true, false, true, true]; + +pub const SIX_SEVEN_SEGMENT: [bool; 7] = [true, false, true, true, true, true, true]; + +pub const SEVEN_SEVEN_SEGMENT: [bool; 7] = [true, true, true, false, false, false, false]; + +pub const EIGHT_SEVEN_SEGMENT: [bool; 7] = [true, true, true, true, true, true, true]; + +pub const NINE_SEVEN_SEGMENT: [bool; 7] = [true, true, true, false, false, true, true]; + +pub const SEVEN_SEGMENT_FONT: [[bool; 7]; 10] = [ + ZERO_SEVEN_SEGMENT, + ONE_SEVEN_SEGMENT, + TWO_SEVEN_SEGMENT, + THREE_SEVEN_SEGMENT, + FOUR_SEVEN_SEGMENT, + FIVE_SEVEN_SEGMENT, + SIX_SEVEN_SEGMENT, + SEVEN_SEVEN_SEGMENT, + EIGHT_SEVEN_SEGMENT, + NINE_SEVEN_SEGMENT, +]; +*/ + +/* +pub fn draw_pixel(frame: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)) { + frame.buf[(y * frame.width + x) * 3 + 0] = color.0 << 2; + frame.buf[(y * frame.width + x) * 3 + 1] = color.1 << 2; + frame.buf[(y * frame.width + x) * 3 + 2] = color.2 << 2; +} +*/ + +/* +pub fn draw_seven_segment( + digit: u8, + frame: &mut [u8], + width: usize, + x: usize, + y: usize, + color: (u8, u8, u8), +) { + let segments = SEVEN_SEGMENT_FONT[digit as usize]; + if segments[0] { + write_pixel(frame, x + 1, y, color); + write_pixel(frame, x + 2, y, color); + write_pixel(frame, x + 3, y, color); + } + if segments[1] { + write_pixel(frame, x + 4, y + 1, color); + write_pixel(frame, x + 4, y + 2, color); + write_pixel(frame, x + 4, y + 3, color); + } + if segments[2] { + write_pixel(frame, x + 4, y + 5, color); + write_pixel(frame, x + 4, y + 6, color); + write_pixel(frame, x + 4, y + 7, color); + } + if segments[3] { + write_pixel(frame, x + 1, y + 8, color); + write_pixel(frame, x + 2, y + 8, color); + write_pixel(frame, x + 3, y + 8, color); + } + if segments[4] { + write_pixel(frame, x, y + 5, color); + write_pixel(frame, x, y + 6, color); + write_pixel(frame, x, y + 7, color); + } + if segments[5] { + write_pixel(frame, x, y + 1, color); + write_pixel(frame, x, y + 2, color); + write_pixel(frame, x, y + 3, color); + } + if segments[6] { + write_pixel(frame, x + 1, y + 4, color); + write_pixel(frame, x + 2, y + 4, color); + write_pixel(frame, x + 3, y + 4, color); + } +} +*/ diff --git a/pico-st7789/src/font.rs b/pico-st7789/src/font.rs new file mode 100644 index 0000000..a2f4c3e --- /dev/null +++ b/pico-st7789/src/font.rs @@ -0,0 +1,398 @@ +use alloc::collections::btree_map::BTreeMap; + +use crate::drawing::Canvas; + +pub trait Font<A> { + fn glyph(&self, c: char) -> &A; +} + +pub trait Glyph { + fn draw(&self, canvas: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)); +} + +// Sixteen Segments +// +// a1 a2 +// f h i jb +// f i j b +// f hij b +// g1 g2 +// e klm c +// e k l m c +// ek l mc +// d1 d2 + +pub struct SixteenSegmentGlyph { + a1: bool, + a2: bool, + b: bool, + c: bool, + d1: bool, + d2: bool, + e: bool, + f: bool, + g1: bool, + g2: bool, + h: bool, + i: bool, + j: bool, + k: bool, + l: bool, + m: bool, +} + +pub struct SixteenSegmentFont(BTreeMap<char, SixteenSegmentGlyph>); + +impl SixteenSegmentFont { + pub fn new() -> Self { + let mut font = BTreeMap::new(); + font.insert( + '*', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: true, + g2: true, + h: true, + i: true, + j: true, + k: true, + l: true, + m: true, + }, + ); + font.insert( + '0', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: true, + k: true, + l: false, + m: false, + }, + ); + font.insert( + '1', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: true, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '2', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: false, + d1: true, + d2: true, + e: true, + f: false, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '3', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: false, + f: false, + g1: false, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '4', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: false, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '5', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: true, + d2: true, + e: false, + f: true, + g1: true, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: true, + }, + ); + font.insert( + '6', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '7', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '8', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '9', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: false, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + + Self(font) + } +} + +impl Font<SixteenSegmentGlyph> for SixteenSegmentFont { + fn glyph(&self, c: char) -> &SixteenSegmentGlyph { + self.0.get(&c).unwrap() + } +} + +impl Glyph for SixteenSegmentGlyph { + fn draw(&self, canvas: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)) { + if self.a1 { + canvas.set_pixel(x + 1, y, color); + canvas.set_pixel(x + 2, y, color); + canvas.set_pixel(x + 3, y, color); + } + if self.a2 { + canvas.set_pixel(x + 5, y, color); + canvas.set_pixel(x + 6, y, color); + canvas.set_pixel(x + 7, y, color); + } + if self.b { + canvas.set_pixel(x + 8, y + 1, color); + canvas.set_pixel(x + 8, y + 2, color); + canvas.set_pixel(x + 8, y + 3, color); + canvas.set_pixel(x + 8, y + 4, color); + canvas.set_pixel(x + 8, y + 5, color); + } + if self.c { + canvas.set_pixel(x + 8, y + 7, color); + canvas.set_pixel(x + 8, y + 8, color); + canvas.set_pixel(x + 8, y + 9, color); + canvas.set_pixel(x + 8, y + 10, color); + canvas.set_pixel(x + 8, y + 11, color); + } + + if self.d1 { + canvas.set_pixel(x + 1, y + 12, color); + canvas.set_pixel(x + 2, y + 12, color); + canvas.set_pixel(x + 3, y + 12, color); + } + if self.d2 { + canvas.set_pixel(x + 5, y + 12, color); + canvas.set_pixel(x + 6, y + 12, color); + canvas.set_pixel(x + 7, y + 12, color); + } + + if self.e { + canvas.set_pixel(x, y + 7, color); + canvas.set_pixel(x, y + 8, color); + canvas.set_pixel(x, y + 9, color); + canvas.set_pixel(x, y + 10, color); + canvas.set_pixel(x, y + 11, color); + } + if self.f { + canvas.set_pixel(x, y + 1, color); + canvas.set_pixel(x, y + 2, color); + canvas.set_pixel(x, y + 3, color); + canvas.set_pixel(x, y + 4, color); + canvas.set_pixel(x, y + 5, color); + } + + if self.g1 { + canvas.set_pixel(x + 1, y + 6, color); + canvas.set_pixel(x + 2, y + 6, color); + canvas.set_pixel(x + 3, y + 6, color); + } + if self.g2 { + canvas.set_pixel(x + 5, y + 6, color); + canvas.set_pixel(x + 6, y + 6, color); + canvas.set_pixel(x + 7, y + 6, color); + } + + if self.h { + canvas.set_pixel(x + 1, y + 1, color); + canvas.set_pixel(x + 1, y + 2, color); + canvas.set_pixel(x + 2, y + 3, color); + canvas.set_pixel(x + 3, y + 4, color); + canvas.set_pixel(x + 3, y + 5, color); + } + if self.i { + canvas.set_pixel(x + 4, y + 1, color); + canvas.set_pixel(x + 4, y + 2, color); + canvas.set_pixel(x + 4, y + 3, color); + canvas.set_pixel(x + 4, y + 4, color); + canvas.set_pixel(x + 4, y + 5, color); + } + if self.j { + canvas.set_pixel(x + 7, y + 1, color); + canvas.set_pixel(x + 7, y + 2, color); + canvas.set_pixel(x + 6, y + 3, color); + canvas.set_pixel(x + 5, y + 4, color); + canvas.set_pixel(x + 5, y + 5, color); + } + if self.k { + canvas.set_pixel(x + 3, y + 7, color); + canvas.set_pixel(x + 3, y + 8, color); + canvas.set_pixel(x + 2, y + 9, color); + canvas.set_pixel(x + 1, y + 10, color); + canvas.set_pixel(x + 1, y + 11, color); + } + if self.l { + canvas.set_pixel(x + 4, y + 7, color); + canvas.set_pixel(x + 4, y + 8, color); + canvas.set_pixel(x + 4, y + 9, color); + canvas.set_pixel(x + 4, y + 10, color); + canvas.set_pixel(x + 4, y + 11, color); + } + if self.m { + canvas.set_pixel(x + 5, y + 7, color); + canvas.set_pixel(x + 5, y + 8, color); + canvas.set_pixel(x + 6, y + 9, color); + canvas.set_pixel(x + 7, y + 10, color); + canvas.set_pixel(x + 7, y + 11, color); + } + } +} diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index cdb6130..33deb64 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -1,23 +1,26 @@ #![no_main] #![no_std] -use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiBus}; +extern crate alloc; + +use embedded_alloc::LlffHeap as Heap; +use embedded_hal::{delay::DelayNs, digital::OutputPin}; use fugit::RateExtU32; use panic_halt as _; use rp_pico::{ entry, - hal::{ - clocks::init_clocks_and_plls, - gpio::{FunctionSio, Pin, PinId, PullDown, SioOutput}, - spi::{Enabled, Spi, SpiDevice, ValidSpiPinout}, - Clock, Sio, Timer, Watchdog, - }, + hal::{clocks::init_clocks_and_plls, spi::Spi, Clock, Sio, Timer, Watchdog}, pac, Pins, }; +mod drawing; +use drawing::{Canvas}; + +mod font; +use font::{Font, Glyph, SixteenSegmentFont}; + mod st7789; use st7789::{ST7789Display, SETUP_PROGRAM}; -pub use st7789::Step; const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // MHz, https://forums.raspberrypi.com/viewtopic.php?t=356764 @@ -25,15 +28,21 @@ const ROWS: usize = 320; const COLUMNS: usize = 170; const FRAMEBUF: usize = ROWS * COLUMNS * 3; -/* -struct Frame { - columns: usize, - buf: [u8; FRAMEBUF], -} -*/ +#[global_allocator] +static HEAP: Heap = Heap::empty(); + #[entry] unsafe fn main() -> ! { + { + use core::mem::MaybeUninit; + const HEAP_SIZE: usize = 1024; + static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; + unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } + } + + let font_sixteen = SixteenSegmentFont::new(); + // rp_pico::pac::Peripherals is a reference to physical hardware defined on the Pico. let mut peripherals = pac::Peripherals::take().unwrap(); @@ -92,8 +101,8 @@ unsafe fn main() -> ! { &mut peripherals.RESETS, // The SPI system uses the peripheral clock clocks.peripheral_clock.freq(), - // Transmit data at a rate of 1Mbit. - 1_u32.MHz(), + // Transmit data at a rate of 32Mbit. + 32_u32.MHz(), // Run with SPI Mode 1. This means that the clock line should start high and that data will // be sampled starting at the first falling edge. embedded_hal::spi::MODE_3, @@ -110,31 +119,52 @@ unsafe fn main() -> ! { timer.delay_ms(1000); - let mut frame: [u8; FRAMEBUF] = [0; FRAMEBUF]; + let mut framebuf = [0; FRAMEBUF]; + let mut canvas = Canvas::new(&mut framebuf, COLUMNS); - let mut strength = 0; loop { - led.set_high(); { let display = display.acquire(); - display.send_buf(&frame); + let _ = led.set_high(); + timer.delay_ms(100); + display.send_buf(&canvas.buf); + let _ = led.set_low(); } + /* + 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)); + */ + + font_sixteen.glyph('*').draw(&mut canvas, 0, 10, (255, 255, 255)); + font_sixteen.glyph('0').draw(&mut canvas, 11, 10, (255, 255, 255)); + font_sixteen.glyph('1').draw(&mut canvas, 22, 10, (255, 255, 255)); + font_sixteen.glyph('2').draw(&mut canvas, 33, 10, (255, 255, 255)); + font_sixteen.glyph('3').draw(&mut canvas, 44, 10, (255, 255, 255)); + font_sixteen.glyph('4').draw(&mut canvas, 55, 10, (255, 255, 255)); + font_sixteen.glyph('5').draw(&mut canvas, 66, 10, (255, 255, 255)); + font_sixteen.glyph('6').draw(&mut canvas, 77, 10, (255, 255, 255)); + font_sixteen.glyph('7').draw(&mut canvas, 88, 10, (255, 255, 255)); + font_sixteen.glyph('8').draw(&mut canvas, 99, 10, (255, 255, 255)); + font_sixteen.glyph('9').draw(&mut canvas, 110, 10, (255, 255, 255)); + + /* for x in 80..90 { for y in 155..165 { - write_pixel(&mut frame, x, y, (0, 0, 63)); + draw_pixel(&mut frame, x, y, (0, 0, 63)); } } - - timer.delay_ms(10); - led.set_low(); + */ timer.delay_ms(1000); } } -fn write_pixel(framebuf: &mut [u8], x: usize, y: usize, color: (u8, u8, u8)) { - framebuf[(y * COLUMNS + x) * 3 + 0] = color.0 << 2; - framebuf[(y * COLUMNS + x) * 3 + 1] = color.1 << 2; - framebuf[(y * COLUMNS + x) * 3 + 2] = color.2 << 2; -} -- 2.47.2 From aea858dd1738da36510ff2f681555dea3029064c Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Mon, 3 Mar 2025 23:18:28 -0500 Subject: [PATCH 2/7] Do a static buffer allocation for the Canvas --- pico-st7789/src/drawing.rs | 14 ++++++++------ pico-st7789/src/main.rs | 5 ++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pico-st7789/src/drawing.rs b/pico-st7789/src/drawing.rs index e02e868..d8e780f 100644 --- a/pico-st7789/src/drawing.rs +++ b/pico-st7789/src/drawing.rs @@ -10,16 +10,18 @@ use alloc::{boxed::Box, vec::Vec}; -pub struct Canvas<'a> { - pub buf: &'a mut [u8], +static mut BUF: [u8; 163200] = [0; 163200]; + +pub struct Canvas { + pub buf: &'static mut [u8; 163200], pub width: usize, } -impl<'a> Canvas<'a> { - pub fn new(buf: &'a mut [u8], columns: usize) -> Self { +impl Canvas { + pub fn new() -> Self { Self { - buf, - width: columns, + buf: unsafe { &mut BUF }, + width: 170, } } diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index 33deb64..90d3789 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -119,15 +119,18 @@ unsafe fn main() -> ! { timer.delay_ms(1000); + /* let mut framebuf = [0; FRAMEBUF]; let mut canvas = Canvas::new(&mut framebuf, COLUMNS); + */ + let mut canvas = Canvas::new(); loop { { let display = display.acquire(); let _ = led.set_high(); timer.delay_ms(100); - display.send_buf(&canvas.buf); + display.send_buf(canvas.buf); let _ = led.set_low(); } -- 2.47.2 From 132c85e99d21f8c06bdfb414a140c8173f5f42ac Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Wed, 5 Mar 2025 00:50:17 -0500 Subject: [PATCH 3/7] Fix a bunch of the letters. Add a dot. --- pico-st7789/src/drawing.rs | 295 +++++++++++++++-- pico-st7789/src/font.rs | 657 ++++++++++++++++++++++++++++++++++++- pico-st7789/src/main.rs | 51 ++- 3 files changed, 944 insertions(+), 59 deletions(-) diff --git a/pico-st7789/src/drawing.rs b/pico-st7789/src/drawing.rs index d8e780f..8917586 100644 --- a/pico-st7789/src/drawing.rs +++ b/pico-st7789/src/drawing.rs @@ -8,7 +8,7 @@ // e c // d d d -use alloc::{boxed::Box, vec::Vec}; +use alloc::collections::btree_map::BTreeMap; static mut BUF: [u8; 163200] = [0; 163200]; @@ -32,7 +32,6 @@ impl Canvas { } } -/* pub const DIGIT_WIDTH: usize = 5; pub const DIGIT_HEIGHT: usize = 9; @@ -68,17 +67,13 @@ pub const SEVEN_SEGMENT_FONT: [[bool; 7]; 10] = [ EIGHT_SEVEN_SEGMENT, NINE_SEVEN_SEGMENT, ]; -*/ -/* -pub fn draw_pixel(frame: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)) { - frame.buf[(y * frame.width + x) * 3 + 0] = color.0 << 2; - frame.buf[(y * frame.width + x) * 3 + 1] = color.1 << 2; - frame.buf[(y * frame.width + x) * 3 + 2] = color.2 << 2; +pub fn write_pixel(framebuf: &mut [u8], width: usize, x: usize, y: usize, color: (u8, u8, u8)) { + framebuf[(y * width + x) * 3 + 0] = color.0 << 2; + framebuf[(y * width + x) * 3 + 1] = color.1 << 2; + framebuf[(y * width + x) * 3 + 2] = color.2 << 2; } -*/ -/* pub fn draw_seven_segment( digit: u8, frame: &mut [u8], @@ -89,39 +84,275 @@ pub fn draw_seven_segment( ) { let segments = SEVEN_SEGMENT_FONT[digit as usize]; if segments[0] { - write_pixel(frame, x + 1, y, color); - write_pixel(frame, x + 2, y, color); - write_pixel(frame, x + 3, y, color); + write_pixel(frame, width, x + 1, y, color); + write_pixel(frame, width, x + 2, y, color); + write_pixel(frame, width, x + 3, y, color); } if segments[1] { - write_pixel(frame, x + 4, y + 1, color); - write_pixel(frame, x + 4, y + 2, color); - write_pixel(frame, x + 4, y + 3, color); + write_pixel(frame, width, x + 4, y + 1, color); + write_pixel(frame, width, x + 4, y + 2, color); + write_pixel(frame, width, x + 4, y + 3, color); } if segments[2] { - write_pixel(frame, x + 4, y + 5, color); - write_pixel(frame, x + 4, y + 6, color); - write_pixel(frame, x + 4, y + 7, color); + write_pixel(frame, width, x + 4, y + 5, color); + write_pixel(frame, width, x + 4, y + 6, color); + write_pixel(frame, width, x + 4, y + 7, color); } if segments[3] { - write_pixel(frame, x + 1, y + 8, color); - write_pixel(frame, x + 2, y + 8, color); - write_pixel(frame, x + 3, y + 8, color); + write_pixel(frame, width, x + 1, y + 8, color); + write_pixel(frame, width, x + 2, y + 8, color); + write_pixel(frame, width, x + 3, y + 8, color); } if segments[4] { - write_pixel(frame, x, y + 5, color); - write_pixel(frame, x, y + 6, color); - write_pixel(frame, x, y + 7, color); + write_pixel(frame, width, x, y + 5, color); + write_pixel(frame, width, x, y + 6, color); + write_pixel(frame, width, x, y + 7, color); } if segments[5] { - write_pixel(frame, x, y + 1, color); - write_pixel(frame, x, y + 2, color); - write_pixel(frame, x, y + 3, color); + write_pixel(frame, width, x, y + 1, color); + write_pixel(frame, width, x, y + 2, color); + write_pixel(frame, width, x, y + 3, color); } if segments[6] { - write_pixel(frame, x + 1, y + 4, color); - write_pixel(frame, x + 2, y + 4, color); - write_pixel(frame, x + 3, y + 4, color); + write_pixel(frame, width, x + 1, y + 4, color); + write_pixel(frame, width, x + 2, y + 4, color); + write_pixel(frame, width, x + 3, y + 4, color); + } +} + +// Sixteen Segments +// +// a1 a2 +// f h i jb +// f i j b +// f hij b +// g1 g2 +// e klm c +// e k l m c +// ek l mc +// d1 d2 + +pub struct SixteenSegment { + a1: bool, + a2: bool, + b: bool, + c: bool, + d1: bool, + d2: bool, + e: bool, + f: bool, + g1: bool, + g2: bool, + h: bool, + i: bool, + j: bool, + k: bool, + l: bool, + m: bool, +} + +pub fn sixteen_segment_font() -> BTreeMap<char, SixteenSegment> { + let mut font = BTreeMap::new(); + font.insert( + '*', + SixteenSegment { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: true, + g2: true, + h: true, + i: true, + j: true, + k: true, + l: true, + m: true, + }, + ); + font.insert( + '0', + SixteenSegment { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '1', + SixteenSegment { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: true, + k: false, + l: false, + m: false, + }, + ); + font.insert( + '2', + SixteenSegment { + a1: true, + a2: true, + b: false, + c: false, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: true, + k: false, + l: false, + m: false, + }, + ); + + font +} + +pub fn draw_sixteen_segment( + c: char, + font: &BTreeMap<char, SixteenSegment>, + frame: &mut [u8], + width: usize, + x: usize, + y: usize, + color: (u8, u8, u8), +) { + let segments = font.get(&c).unwrap(); + if segments.a1 { + write_pixel(frame, width, x + 1, y, color); + write_pixel(frame, width, x + 2, y, color); + write_pixel(frame, width, x + 3, y, color); + } + if segments.a2 { + write_pixel(frame, width, x + 5, y, color); + write_pixel(frame, width, x + 6, y, color); + write_pixel(frame, width, x + 7, y, color); + } + if segments.b { + write_pixel(frame, width, x + 8, y + 1, color); + write_pixel(frame, width, x + 8, y + 2, color); + write_pixel(frame, width, x + 8, y + 3, color); + write_pixel(frame, width, x + 8, y + 4, color); + write_pixel(frame, width, x + 8, y + 5, color); + } + if segments.c { + write_pixel(frame, width, x + 8, y + 7, color); + write_pixel(frame, width, x + 8, y + 8, color); + write_pixel(frame, width, x + 8, y + 9, color); + write_pixel(frame, width, x + 8, y + 10, color); + write_pixel(frame, width, x + 8, y + 11, color); + } + + if segments.d1 { + write_pixel(frame, width, x + 1, y + 12, color); + write_pixel(frame, width, x + 2, y + 12, color); + write_pixel(frame, width, x + 3, y + 12, color); + } + if segments.d2 { + write_pixel(frame, width, x + 5, y + 12, color); + write_pixel(frame, width, x + 6, y + 12, color); + write_pixel(frame, width, x + 7, y + 12, color); + } + + if segments.e { + write_pixel(frame, width, x, y + 7, color); + write_pixel(frame, width, x, y + 8, color); + write_pixel(frame, width, x, y + 9, color); + write_pixel(frame, width, x, y + 10, color); + write_pixel(frame, width, x, y + 11, color); + } + if segments.f { + write_pixel(frame, width, x, y + 1, color); + write_pixel(frame, width, x, y + 2, color); + write_pixel(frame, width, x, y + 3, color); + write_pixel(frame, width, x, y + 4, color); + write_pixel(frame, width, x, y + 5, color); + } + + if segments.g1 { + write_pixel(frame, width, x + 1, y + 6, color); + write_pixel(frame, width, x + 2, y + 6, color); + write_pixel(frame, width, x + 3, y + 6, color); + } + if segments.g2 { + write_pixel(frame, width, x + 5, y + 6, color); + write_pixel(frame, width, x + 6, y + 6, color); + write_pixel(frame, width, x + 7, y + 6, color); + } + + if segments.h { + write_pixel(frame, width, x + 1, y + 1, color); + write_pixel(frame, width, x + 1, y + 2, color); + write_pixel(frame, width, x + 2, y + 3, color); + write_pixel(frame, width, x + 3, y + 4, color); + write_pixel(frame, width, x + 3, y + 5, color); + } + if segments.i { + write_pixel(frame, width, x + 4, y + 1, color); + write_pixel(frame, width, x + 4, y + 2, color); + write_pixel(frame, width, x + 4, y + 3, color); + write_pixel(frame, width, x + 4, y + 4, color); + write_pixel(frame, width, x + 4, y + 5, color); + } + if segments.j { + write_pixel(frame, width, x + 7, y + 1, color); + write_pixel(frame, width, x + 7, y + 2, color); + write_pixel(frame, width, x + 6, y + 3, color); + write_pixel(frame, width, x + 5, y + 4, color); + write_pixel(frame, width, x + 5, y + 5, color); + } + if segments.k { + write_pixel(frame, width, x + 3, y + 7, color); + write_pixel(frame, width, x + 3, y + 8, color); + write_pixel(frame, width, x + 2, y + 9, color); + write_pixel(frame, width, x + 1, y + 10, color); + write_pixel(frame, width, x + 1, y + 11, color); + } + if segments.l { + write_pixel(frame, width, x + 4, y + 7, color); + write_pixel(frame, width, x + 4, y + 8, color); + write_pixel(frame, width, x + 4, y + 9, color); + write_pixel(frame, width, x + 4, y + 10, color); + write_pixel(frame, width, x + 4, y + 11, color); + } + if segments.m { + write_pixel(frame, width, x + 5, y + 7, color); + write_pixel(frame, width, x + 5, y + 8, color); + write_pixel(frame, width, x + 6, y + 9, color); + write_pixel(frame, width, x + 7, y + 10, color); + write_pixel(frame, width, x + 7, y + 11, color); } } -*/ diff --git a/pico-st7789/src/font.rs b/pico-st7789/src/font.rs index a2f4c3e..d7b3d5a 100644 --- a/pico-st7789/src/font.rs +++ b/pico-st7789/src/font.rs @@ -39,6 +39,7 @@ pub struct SixteenSegmentGlyph { k: bool, l: bool, m: bool, + dot: bool, } pub struct SixteenSegmentFont(BTreeMap<char, SixteenSegmentGlyph>); @@ -47,24 +48,69 @@ impl SixteenSegmentFont { pub fn new() -> Self { let mut font = BTreeMap::new(); font.insert( - '*', + ' ', SixteenSegmentGlyph { - a1: true, - a2: true, + a1: false, + a2: false, + b: false, + c: false, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + '!', + SixteenSegmentGlyph { + a1: false, + a2: false, b: true, c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: true, - g2: true, - h: true, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: true, + }, + ); + font.insert( + '"', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: false, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, i: true, - j: true, - k: true, - l: true, - m: true, + j: false, + k: false, + l: false, + m: false, + dot: false, }, ); font.insert( @@ -86,6 +132,7 @@ impl SixteenSegmentFont { k: true, l: false, m: false, + dot: false, }, ); font.insert( @@ -107,6 +154,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -128,6 +176,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -149,6 +198,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -170,6 +220,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -191,6 +242,7 @@ impl SixteenSegmentFont { k: false, l: false, m: true, + dot: false, }, ); font.insert( @@ -212,6 +264,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -233,6 +286,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -254,6 +308,7 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, }, ); font.insert( @@ -275,9 +330,581 @@ impl SixteenSegmentFont { k: false, l: false, m: false, + dot: false, + }, + ); + font.insert( + 'A', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: false, + d2: false, + e: true, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'B', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: false, + f: false, + g1: false, + g2: true, + h: false, + i: true, + j: false, + k: false, + l: true, + m: false, + dot: false, + }, + ); + font.insert( + 'C', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'D', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: true, + j: false, + k: false, + l: true, + m: false, + dot: false, + }, + ); + font.insert( + 'E', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: true, + d2: true, + e: true, + f: true, + g1: true, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'F', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: false, + d2: false, + e: true, + f: true, + g1: true, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'G', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'H', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: true, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'I', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: true, + d2: true, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: true, + j: false, + k: false, + l: true, + m: false, + dot: false, + }, + ); + font.insert( + 'J', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'K', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: false, + c: false, + d1: false, + d2: false, + e: true, + f: true, + g1: true, + g2: false, + h: false, + i: false, + j: true, + k: false, + l: false, + m: true, + dot: false, + }, + ); + font.insert( + 'L', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: false, + c: false, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'M', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: true, + f: true, + g1: false, + g2: false, + h: true, + i: false, + j: true, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'N', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: true, + f: true, + g1: false, + g2: false, + h: true, + i: false, + j: false, + k: false, + l: false, + m: true, + dot: false, + }, + ); + font.insert( + 'O', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'P', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: false, + d1: false, + d2: false, + e: true, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'Q', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: true, + dot: false, + }, + ); + font.insert( + 'R', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: true, + c: false, + d1: false, + d2: false, + e: true, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: true, + dot: false, + }, + ); + font.insert( + 'S', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: true, + d1: true, + d2: true, + e: false, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'T', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: true, + j: false, + k: false, + l: true, + m: false, + dot: false, + }, + ); + font.insert( + 'U', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: true, + d2: true, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'V', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: false, + c: false, + d1: false, + d2: false, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: true, + k: true, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'W', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: false, + d2: false, + e: true, + f: true, + g1: false, + g2: false, + h: false, + i: false, + j: false, + k: true, + l: false, + m: true, + dot: false, + }, + ); + font.insert( + 'X', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: false, + c: false, + d1: false, + d2: false, + e: false, + f: false, + g1: false, + g2: false, + h: true, + i: false, + j: true, + k: true, + l: false, + m: true, + dot: false, + }, + ); + font.insert( + 'Y', + SixteenSegmentGlyph { + a1: false, + a2: false, + b: true, + c: true, + d1: true, + d2: true, + e: false, + f: true, + g1: true, + g2: true, + h: false, + i: false, + j: false, + k: false, + l: false, + m: false, + dot: false, + }, + ); + font.insert( + 'Z', + SixteenSegmentGlyph { + a1: true, + a2: true, + b: false, + c: false, + d1: true, + d2: true, + e: false, + f: false, + g1: false, + g2: false, + h: false, + i: false, + j: true, + k: true, + l: false, + m: false, + dot: false, }, ); - Self(font) } } diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index 90d3789..2a11a68 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -36,7 +36,7 @@ static HEAP: Heap = Heap::empty(); unsafe fn main() -> ! { { use core::mem::MaybeUninit; - const HEAP_SIZE: usize = 1024; + const HEAP_SIZE: usize = 16 * 1024; static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } } @@ -147,18 +147,45 @@ unsafe fn main() -> ! { draw_seven_segment(9, &mut frame, 63, 0, (255, 255, 255)); */ - font_sixteen.glyph('*').draw(&mut canvas, 0, 10, (255, 255, 255)); - font_sixteen.glyph('0').draw(&mut canvas, 11, 10, (255, 255, 255)); - font_sixteen.glyph('1').draw(&mut canvas, 22, 10, (255, 255, 255)); - font_sixteen.glyph('2').draw(&mut canvas, 33, 10, (255, 255, 255)); - font_sixteen.glyph('3').draw(&mut canvas, 44, 10, (255, 255, 255)); - font_sixteen.glyph('4').draw(&mut canvas, 55, 10, (255, 255, 255)); - font_sixteen.glyph('5').draw(&mut canvas, 66, 10, (255, 255, 255)); - font_sixteen.glyph('6').draw(&mut canvas, 77, 10, (255, 255, 255)); - font_sixteen.glyph('7').draw(&mut canvas, 88, 10, (255, 255, 255)); - font_sixteen.glyph('8').draw(&mut canvas, 99, 10, (255, 255, 255)); - font_sixteen.glyph('9').draw(&mut canvas, 110, 10, (255, 255, 255)); + font_sixteen.glyph(' ').draw(&mut canvas, 0, 0, (255, 255, 255)); + font_sixteen.glyph('0').draw(&mut canvas, 11, 0, (255, 255, 255)); + font_sixteen.glyph('1').draw(&mut canvas, 22, 0, (255, 255, 255)); + font_sixteen.glyph('2').draw(&mut canvas, 33, 0, (255, 255, 255)); + font_sixteen.glyph('3').draw(&mut canvas, 44, 0, (255, 255, 255)); + font_sixteen.glyph('4').draw(&mut canvas, 55, 0, (255, 255, 255)); + font_sixteen.glyph('5').draw(&mut canvas, 66, 0, (255, 255, 255)); + font_sixteen.glyph('6').draw(&mut canvas, 77, 0, (255, 255, 255)); + font_sixteen.glyph('7').draw(&mut canvas, 88, 0, (255, 255, 255)); + font_sixteen.glyph('8').draw(&mut canvas, 99, 0, (255, 255, 255)); + font_sixteen.glyph('9').draw(&mut canvas, 110, 0, (255, 255, 255)); + font_sixteen.glyph('A').draw(&mut canvas, 0, 20, (255, 255, 255)); + font_sixteen.glyph('B').draw(&mut canvas, 11, 20, (255, 255, 255)); + font_sixteen.glyph('C').draw(&mut canvas, 22, 20, (255, 255, 255)); + font_sixteen.glyph('D').draw(&mut canvas, 33, 20, (255, 255, 255)); + font_sixteen.glyph('E').draw(&mut canvas, 44, 20, (255, 255, 255)); + font_sixteen.glyph('F').draw(&mut canvas, 55, 20, (255, 255, 255)); + font_sixteen.glyph('G').draw(&mut canvas, 66, 20, (255, 255, 255)); + font_sixteen.glyph('H').draw(&mut canvas, 77, 20, (255, 255, 255)); + font_sixteen.glyph('I').draw(&mut canvas, 88, 20, (255, 255, 255)); + font_sixteen.glyph('J').draw(&mut canvas, 99, 20, (255, 255, 255)); + font_sixteen.glyph('K').draw(&mut canvas, 110, 20, (255, 255, 255)); + font_sixteen.glyph('L').draw(&mut canvas, 121, 20, (255, 255, 255)); + font_sixteen.glyph('M').draw(&mut canvas, 132, 20, (255, 255, 255)); + font_sixteen.glyph('N').draw(&mut canvas, 143, 20, (255, 255, 255)); + font_sixteen.glyph('O').draw(&mut canvas, 154, 20, (255, 255, 255)); + + font_sixteen.glyph('P').draw(&mut canvas, 0, 40, (255, 255, 255)); + font_sixteen.glyph('Q').draw(&mut canvas, 11, 40, (255, 255, 255)); + font_sixteen.glyph('R').draw(&mut canvas, 22, 40, (255, 255, 255)); + font_sixteen.glyph('S').draw(&mut canvas, 33, 40, (255, 255, 255)); + font_sixteen.glyph('T').draw(&mut canvas, 44, 40, (255, 255, 255)); + font_sixteen.glyph('U').draw(&mut canvas, 55, 40, (255, 255, 255)); + font_sixteen.glyph('V').draw(&mut canvas, 66, 40, (255, 255, 255)); + font_sixteen.glyph('W').draw(&mut canvas, 77, 40, (255, 255, 255)); + font_sixteen.glyph('X').draw(&mut canvas, 88, 40, (255, 255, 255)); + font_sixteen.glyph('Y').draw(&mut canvas, 99, 40, (255, 255, 255)); + font_sixteen.glyph('Z').draw(&mut canvas, 110, 40, (255, 255, 255)); /* for x in 80..90 { for y in 155..165 { -- 2.47.2 From 155d2ba18edb9ee875c0412bad9f281183371d94 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Wed, 5 Mar 2025 09:25:15 -0500 Subject: [PATCH 4/7] Use bitflags to represent the font --- Cargo.lock | 35 +- pico-st7789/Cargo.toml | 1 + pico-st7789/src/font.rs | 1052 +++++++++++---------------------------- pico-st7789/src/main.rs | 29 +- 4 files changed, 324 insertions(+), 793 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1772741..d871276 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -365,7 +365,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" dependencies = [ "annotate-snippets", - "bitflags 2.8.0", + "bitflags 2.9.0", "cexpr", "clang-sys", "itertools 0.12.1", @@ -420,9 +420,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" dependencies = [ "serde 1.0.218", ] @@ -485,7 +485,7 @@ version = "0.18.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ca26ef0159422fb77631dc9d17b102f253b876fe1586b03b803e63a309b4ee2" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "cairo-sys-rs", "glib 0.18.5", "libc", @@ -1815,7 +1815,7 @@ version = "0.18.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "233daaf6e83ae6a12a52055f568f9d7cf4671dabb78ff9560ab6da230ce00ee5" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "futures-channel", "futures-core", "futures-executor", @@ -1838,7 +1838,7 @@ version = "0.20.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707b819af8059ee5395a2de9f2317d87a53dbad8846a2f089f0bb44703f37686" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "futures-channel", "futures-core", "futures-executor", @@ -2837,7 +2837,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65f3a4b81b2a2d8c7f300643676202debd1b7c929dbf5c9bb89402ea11d19810" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "cc", "convert_case", "cookie-factory", @@ -3142,7 +3142,7 @@ version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "cfg-if", "libc", ] @@ -3286,7 +3286,7 @@ version = "0.10.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e14130c6a98cd258fdcb0fb6d744152343ff729cbfcb28c656a9d12b999fbcd" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "cfg-if", "foreign-types", "libc", @@ -3563,6 +3563,7 @@ dependencies = [ name = "pico-st7789" version = "0.1.0" dependencies = [ + "bitflags 2.9.0", "cortex-m-rt", "embedded-alloc", "embedded-hal 1.0.0", @@ -3632,7 +3633,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08e645ba5c45109106d56610b3ee60eb13a6f2beb8b74f8dc8186cf261788dda" dependencies = [ "anyhow", - "bitflags 2.8.0", + "bitflags 2.9.0", "libc", "libspa", "libspa-sys", @@ -3829,7 +3830,7 @@ checksum = "14cae93065090804185d3b75f0bf93b8eeda30c7a9b4a33d3bdb3988d6229e50" dependencies = [ "bit-set", "bit-vec", - "bitflags 2.8.0", + "bitflags 2.9.0", "lazy_static", "num-traits", "rand 0.8.5", @@ -4068,7 +4069,7 @@ version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82b568323e98e49e2a0899dcee453dd679fae22d69adf9b11dd508d1549b7e2f" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", ] [[package]] @@ -4299,7 +4300,7 @@ version = "0.38.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "errno", "libc", "linux-raw-sys", @@ -4387,7 +4388,7 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", "core-foundation", "core-foundation-sys", "libc", @@ -4758,7 +4759,7 @@ checksum = "4560278f0e00ce64938540546f59f590d60beee33fffbd3b9cd47851e5fff233" dependencies = [ "atoi", "base64 0.22.1", - "bitflags 2.8.0", + "bitflags 2.9.0", "byteorder", "bytes", "crc", @@ -4800,7 +4801,7 @@ checksum = "c5b98a57f363ed6764d5b3a12bfedf62f07aa16e1856a7ddc2a0bb190a959613" dependencies = [ "atoi", "base64 0.22.1", - "bitflags 2.8.0", + "bitflags 2.9.0", "byteorder", "crc", "dotenvy", @@ -6035,7 +6036,7 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" dependencies = [ - "bitflags 2.8.0", + "bitflags 2.9.0", ] [[package]] diff --git a/pico-st7789/Cargo.toml b/pico-st7789/Cargo.toml index 001c708..ff98242 100644 --- a/pico-st7789/Cargo.toml +++ b/pico-st7789/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +bitflags = "2.9.0" cortex-m-rt = "0.7.3" embedded-alloc = "0.6.0" embedded-hal = "1.0.0" diff --git a/pico-st7789/src/font.rs b/pico-st7789/src/font.rs index d7b3d5a..d4f8b88 100644 --- a/pico-st7789/src/font.rs +++ b/pico-st7789/src/font.rs @@ -1,4 +1,5 @@ use alloc::collections::btree_map::BTreeMap; +use bitflags::bitflags; use crate::drawing::Canvas; @@ -22,6 +23,7 @@ pub trait Glyph { // ek l mc // d1 d2 +/* pub struct SixteenSegmentGlyph { a1: bool, a2: bool, @@ -41,869 +43,393 @@ pub struct SixteenSegmentGlyph { m: bool, dot: bool, } +*/ + +bitflags! { + pub struct SixteenSegmentGlyph: u32 { + const NONE = 0; + const A1 = 0x0001; + const A2 = 0x0001 << 1; + const B = 0x0001 << 2; + const C = 0x0001 << 3; + const D1 = 0x0001 << 4; + const D2 = 0x0001 << 5; + const E = 0x0001 << 6; + const F = 0x0001 << 7; + const G1 = 0x0001 << 8; + const G2 = 0x0001 << 9; + const H = 0x0001 << 10; + const I = 0x0001 << 11; + const J = 0x0001 << 12; + const K = 0x0001 << 13; + const L = 0x0001 << 14; + const M = 0x0001 << 15; + const DOT = 0x0001 << 16; + } +} + +/* +impl Default for SixteenSegmentGlyph { + fn default() -> Self { + 0_u32 + } +} +*/ pub struct SixteenSegmentFont(BTreeMap<char, SixteenSegmentGlyph>); impl SixteenSegmentFont { pub fn new() -> Self { let mut font = BTreeMap::new(); - font.insert( - ' ', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: false, - c: false, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, - ); + font.insert(' ', SixteenSegmentGlyph::NONE); font.insert( '!', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: true, - }, - ); - font.insert( - '"', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: false, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: true, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B | SixteenSegmentGlyph::C | SixteenSegmentGlyph::DOT, ); + font.insert('"', SixteenSegmentGlyph::B | SixteenSegmentGlyph::I); font.insert( '0', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: true, - k: true, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::K, ); font.insert( '1', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: true, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B | SixteenSegmentGlyph::C | SixteenSegmentGlyph::J, ); font.insert( '2', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: false, - d1: true, - d2: true, - e: true, - f: false, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( '3', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: false, - f: false, - g1: false, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::G2, ); font.insert( '4', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: false, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( '5', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: true, - d2: true, - e: false, - f: true, - g1: true, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::M, ); font.insert( '6', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( '7', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C, ); font.insert( '8', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( '9', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: false, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( 'A', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: false, - d2: false, - e: true, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( 'B', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: false, - f: false, - g1: false, - g2: true, - h: false, - i: true, - j: false, - k: false, - l: true, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::G2 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, ); font.insert( 'C', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F, ); font.insert( 'D', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: true, - j: false, - k: false, - l: true, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, ); font.insert( 'E', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: true, - d2: true, - e: true, - f: true, - g1: true, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1, ); font.insert( 'F', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: false, - d2: false, - e: true, - f: true, - g1: true, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1, ); font.insert( 'G', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G2, ); font.insert( 'H', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: true, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( 'I', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: true, - d2: true, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: true, - j: false, - k: false, - l: true, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, ); font.insert( 'J', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E, ); font.insert( 'K', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: false, - c: false, - d1: false, - d2: false, - e: true, - f: true, - g1: true, - g2: false, - h: false, - i: false, - j: true, - k: false, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::M, ); font.insert( 'L', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: false, - c: false, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F, ); font.insert( 'M', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: true, - f: true, - g1: false, - g2: false, - h: true, - i: false, - j: true, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::H + | SixteenSegmentGlyph::J, ); font.insert( 'N', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: true, - f: true, - g1: false, - g2: false, - h: true, - i: false, - j: false, - k: false, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::H + | SixteenSegmentGlyph::M, ); font.insert( 'O', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F, ); font.insert( 'P', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: false, - d1: false, - d2: false, - e: true, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( 'Q', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::M, ); font.insert( 'R', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: true, - c: false, - d1: false, - d2: false, - e: true, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::B + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2 + | SixteenSegmentGlyph::M, ); font.insert( 'S', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: true, - d1: true, - d2: true, - e: false, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( 'T', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: true, - j: false, - k: false, - l: true, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::I + | SixteenSegmentGlyph::L, ); font.insert( 'U', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F, ); font.insert( 'V', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: false, - c: false, - d1: false, - d2: false, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: true, - k: true, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::K, ); font.insert( 'W', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: true, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::E + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::K + | SixteenSegmentGlyph::M, ); font.insert( 'X', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: false, - c: false, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: true, - i: false, - j: true, - k: true, - l: false, - m: true, - dot: false, - }, + SixteenSegmentGlyph::H + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::K + | SixteenSegmentGlyph::M, ); font.insert( 'Y', - SixteenSegmentGlyph { - a1: false, - a2: false, - b: true, - c: true, - d1: true, - d2: true, - e: false, - f: true, - g1: true, - g2: true, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::B + | SixteenSegmentGlyph::C + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::F + | SixteenSegmentGlyph::G1 + | SixteenSegmentGlyph::G2, ); font.insert( 'Z', - SixteenSegmentGlyph { - a1: true, - a2: true, - b: false, - c: false, - d1: true, - d2: true, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: true, - k: true, - l: false, - m: false, - dot: false, - }, + SixteenSegmentGlyph::A1 + | SixteenSegmentGlyph::A2 + | SixteenSegmentGlyph::D1 + | SixteenSegmentGlyph::D2 + | SixteenSegmentGlyph::J + | SixteenSegmentGlyph::K, ); Self(font) } @@ -917,24 +443,24 @@ impl Font<SixteenSegmentGlyph> for SixteenSegmentFont { impl Glyph for SixteenSegmentGlyph { fn draw(&self, canvas: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)) { - if self.a1 { + if self.contains(SixteenSegmentGlyph::A1) { canvas.set_pixel(x + 1, y, color); canvas.set_pixel(x + 2, y, color); canvas.set_pixel(x + 3, y, color); } - if self.a2 { + if self.contains(SixteenSegmentGlyph::A2) { canvas.set_pixel(x + 5, y, color); canvas.set_pixel(x + 6, y, color); canvas.set_pixel(x + 7, y, color); } - if self.b { + if self.contains(SixteenSegmentGlyph::B) { canvas.set_pixel(x + 8, y + 1, color); canvas.set_pixel(x + 8, y + 2, color); canvas.set_pixel(x + 8, y + 3, color); canvas.set_pixel(x + 8, y + 4, color); canvas.set_pixel(x + 8, y + 5, color); } - if self.c { + if self.contains(SixteenSegmentGlyph::C) { canvas.set_pixel(x + 8, y + 7, color); canvas.set_pixel(x + 8, y + 8, color); canvas.set_pixel(x + 8, y + 9, color); @@ -942,25 +468,25 @@ impl Glyph for SixteenSegmentGlyph { canvas.set_pixel(x + 8, y + 11, color); } - if self.d1 { + if self.contains(SixteenSegmentGlyph::D1) { canvas.set_pixel(x + 1, y + 12, color); canvas.set_pixel(x + 2, y + 12, color); canvas.set_pixel(x + 3, y + 12, color); } - if self.d2 { + if self.contains(SixteenSegmentGlyph::D2) { canvas.set_pixel(x + 5, y + 12, color); canvas.set_pixel(x + 6, y + 12, color); canvas.set_pixel(x + 7, y + 12, color); } - if self.e { + if self.contains(SixteenSegmentGlyph::E) { canvas.set_pixel(x, y + 7, color); canvas.set_pixel(x, y + 8, color); canvas.set_pixel(x, y + 9, color); canvas.set_pixel(x, y + 10, color); canvas.set_pixel(x, y + 11, color); } - if self.f { + if self.contains(SixteenSegmentGlyph::F) { canvas.set_pixel(x, y + 1, color); canvas.set_pixel(x, y + 2, color); canvas.set_pixel(x, y + 3, color); @@ -968,53 +494,53 @@ impl Glyph for SixteenSegmentGlyph { canvas.set_pixel(x, y + 5, color); } - if self.g1 { + if self.contains(SixteenSegmentGlyph::G1) { canvas.set_pixel(x + 1, y + 6, color); canvas.set_pixel(x + 2, y + 6, color); canvas.set_pixel(x + 3, y + 6, color); } - if self.g2 { + if self.contains(SixteenSegmentGlyph::G2) { canvas.set_pixel(x + 5, y + 6, color); canvas.set_pixel(x + 6, y + 6, color); canvas.set_pixel(x + 7, y + 6, color); } - if self.h { + if self.contains(SixteenSegmentGlyph::H) { canvas.set_pixel(x + 1, y + 1, color); canvas.set_pixel(x + 1, y + 2, color); canvas.set_pixel(x + 2, y + 3, color); canvas.set_pixel(x + 3, y + 4, color); canvas.set_pixel(x + 3, y + 5, color); } - if self.i { + if self.contains(SixteenSegmentGlyph::I) { canvas.set_pixel(x + 4, y + 1, color); canvas.set_pixel(x + 4, y + 2, color); canvas.set_pixel(x + 4, y + 3, color); canvas.set_pixel(x + 4, y + 4, color); canvas.set_pixel(x + 4, y + 5, color); } - if self.j { + if self.contains(SixteenSegmentGlyph::J) { canvas.set_pixel(x + 7, y + 1, color); canvas.set_pixel(x + 7, y + 2, color); canvas.set_pixel(x + 6, y + 3, color); canvas.set_pixel(x + 5, y + 4, color); canvas.set_pixel(x + 5, y + 5, color); } - if self.k { + if self.contains(SixteenSegmentGlyph::K) { canvas.set_pixel(x + 3, y + 7, color); canvas.set_pixel(x + 3, y + 8, color); canvas.set_pixel(x + 2, y + 9, color); canvas.set_pixel(x + 1, y + 10, color); canvas.set_pixel(x + 1, y + 11, color); } - if self.l { + if self.contains(SixteenSegmentGlyph::L) { canvas.set_pixel(x + 4, y + 7, color); canvas.set_pixel(x + 4, y + 8, color); canvas.set_pixel(x + 4, y + 9, color); canvas.set_pixel(x + 4, y + 10, color); canvas.set_pixel(x + 4, y + 11, color); } - if self.m { + if self.contains(SixteenSegmentGlyph::M) { canvas.set_pixel(x + 5, y + 7, color); canvas.set_pixel(x + 5, y + 8, color); canvas.set_pixel(x + 6, y + 9, color); diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index 2a11a68..d0bdad6 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -14,7 +14,7 @@ use rp_pico::{ }; mod drawing; -use drawing::{Canvas}; +use drawing::Canvas; mod font; use font::{Font, Glyph, SixteenSegmentFont}; @@ -31,12 +31,11 @@ const FRAMEBUF: usize = ROWS * COLUMNS * 3; #[global_allocator] static HEAP: Heap = Heap::empty(); - #[entry] unsafe fn main() -> ! { { use core::mem::MaybeUninit; - const HEAP_SIZE: usize = 16 * 1024; + const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } } @@ -126,13 +125,6 @@ unsafe fn main() -> ! { let mut canvas = Canvas::new(); loop { - { - let display = display.acquire(); - let _ = led.set_high(); - timer.delay_ms(100); - display.send_buf(canvas.buf); - let _ = led.set_low(); - } /* draw_seven_segment(0, &mut frame, 0, 0, (255, 255, 255)); @@ -147,8 +139,12 @@ unsafe fn main() -> ! { draw_seven_segment(9, &mut frame, 63, 0, (255, 255, 255)); */ - font_sixteen.glyph(' ').draw(&mut canvas, 0, 0, (255, 255, 255)); - font_sixteen.glyph('0').draw(&mut canvas, 11, 0, (255, 255, 255)); + font_sixteen + .glyph(' ') + .draw(&mut canvas, 0, 0, (255, 255, 255)); + font_sixteen + .glyph('0') + .draw(&mut canvas, 11, 0, (255, 255, 255)); font_sixteen.glyph('1').draw(&mut canvas, 22, 0, (255, 255, 255)); font_sixteen.glyph('2').draw(&mut canvas, 33, 0, (255, 255, 255)); font_sixteen.glyph('3').draw(&mut canvas, 44, 0, (255, 255, 255)); @@ -186,6 +182,14 @@ unsafe fn main() -> ! { font_sixteen.glyph('X').draw(&mut canvas, 88, 40, (255, 255, 255)); font_sixteen.glyph('Y').draw(&mut canvas, 99, 40, (255, 255, 255)); font_sixteen.glyph('Z').draw(&mut canvas, 110, 40, (255, 255, 255)); + + { + let display = display.acquire(); + let _ = led.set_high(); + timer.delay_ms(100); + display.send_buf(canvas.buf); + let _ = led.set_low(); + } /* for x in 80..90 { for y in 155..165 { @@ -197,4 +201,3 @@ unsafe fn main() -> ! { timer.delay_ms(1000); } } - -- 2.47.2 From 8288fdbb6b7a50a87dc966e23b016889b4ba5ea5 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Wed, 5 Mar 2025 09:47:00 -0500 Subject: [PATCH 5/7] Refactor the canvas and font --- pico-st7789/src/{drawing.rs => canvas.rs} | 36 ++-- pico-st7789/src/font/mod.rs | 13 ++ .../src/{font.rs => font/sixteen_segment.rs} | 43 +---- pico-st7789/src/main.rs | 178 ++++++++++++++---- 4 files changed, 174 insertions(+), 96 deletions(-) rename pico-st7789/src/{drawing.rs => canvas.rs} (94%) create mode 100644 pico-st7789/src/font/mod.rs rename pico-st7789/src/{font.rs => font/sixteen_segment.rs} (96%) diff --git a/pico-st7789/src/drawing.rs b/pico-st7789/src/canvas.rs similarity index 94% rename from pico-st7789/src/drawing.rs rename to pico-st7789/src/canvas.rs index 8917586..e7d6f30 100644 --- a/pico-st7789/src/drawing.rs +++ b/pico-st7789/src/canvas.rs @@ -10,28 +10,31 @@ use alloc::collections::btree_map::BTreeMap; -static mut BUF: [u8; 163200] = [0; 163200]; +/* +*/ -pub struct Canvas { - pub buf: &'static mut [u8; 163200], - pub width: usize, -} +pub struct RGB { pub r: u8, pub g: u8, pub b: u8 } -impl Canvas { - pub fn new() -> Self { - Self { - buf: unsafe { &mut BUF }, - width: 170, +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 { + self.set_pixel(x, y1, color); + } + for x in x1..x2+1 { + self.set_pixel(x, y2, color); + } + for y in y1..y2+1 { + self.set_pixel(x1, y, color); + } + for y in y1..y2+1 { + self.set_pixel(x2, y, color); } } - - pub fn set_pixel(&mut self, x: usize, y: usize, color: (u8, u8, u8)) { - self.buf[(y * self.width + x) * 3 + 0] = color.0 << 2; - self.buf[(y * self.width + x) * 3 + 1] = color.1 << 2; - self.buf[(y * self.width + x) * 3 + 2] = color.2 << 2; - } } +/* pub const DIGIT_WIDTH: usize = 5; pub const DIGIT_HEIGHT: usize = 9; @@ -356,3 +359,4 @@ pub fn draw_sixteen_segment( write_pixel(frame, width, x + 7, y + 11, color); } } +*/ diff --git a/pico-st7789/src/font/mod.rs b/pico-st7789/src/font/mod.rs new file mode 100644 index 0000000..126176d --- /dev/null +++ b/pico-st7789/src/font/mod.rs @@ -0,0 +1,13 @@ +use crate::canvas::{Canvas, RGB}; + +mod sixteen_segment; +pub use sixteen_segment::SixteenSegmentFont; + +pub trait Font<A> { + fn glyph(&self, c: char) -> &A; +} + +pub trait Glyph { + fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB); +} + diff --git a/pico-st7789/src/font.rs b/pico-st7789/src/font/sixteen_segment.rs similarity index 96% rename from pico-st7789/src/font.rs rename to pico-st7789/src/font/sixteen_segment.rs index d4f8b88..44061ce 100644 --- a/pico-st7789/src/font.rs +++ b/pico-st7789/src/font/sixteen_segment.rs @@ -1,15 +1,9 @@ use alloc::collections::btree_map::BTreeMap; use bitflags::bitflags; -use crate::drawing::Canvas; +use crate::canvas::{Canvas, RGB}; -pub trait Font<A> { - fn glyph(&self, c: char) -> &A; -} - -pub trait Glyph { - fn draw(&self, canvas: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)); -} +use super::{Font, Glyph}; // Sixteen Segments // @@ -23,28 +17,6 @@ pub trait Glyph { // ek l mc // d1 d2 -/* -pub struct SixteenSegmentGlyph { - a1: bool, - a2: bool, - b: bool, - c: bool, - d1: bool, - d2: bool, - e: bool, - f: bool, - g1: bool, - g2: bool, - h: bool, - i: bool, - j: bool, - k: bool, - l: bool, - m: bool, - dot: bool, -} -*/ - bitflags! { pub struct SixteenSegmentGlyph: u32 { const NONE = 0; @@ -68,14 +40,6 @@ bitflags! { } } -/* -impl Default for SixteenSegmentGlyph { - fn default() -> Self { - 0_u32 - } -} -*/ - pub struct SixteenSegmentFont(BTreeMap<char, SixteenSegmentGlyph>); impl SixteenSegmentFont { @@ -442,7 +406,7 @@ impl Font<SixteenSegmentGlyph> for SixteenSegmentFont { } impl Glyph for SixteenSegmentGlyph { - fn draw(&self, canvas: &mut Canvas, x: usize, y: usize, color: (u8, u8, u8)) { + fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) { if self.contains(SixteenSegmentGlyph::A1) { canvas.set_pixel(x + 1, y, color); canvas.set_pixel(x + 2, y, color); @@ -549,3 +513,4 @@ impl Glyph for SixteenSegmentGlyph { } } } + diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index d0bdad6..e404ba7 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -13,8 +13,8 @@ use rp_pico::{ pac, Pins, }; -mod drawing; -use drawing::Canvas; +mod canvas; +use canvas::{Canvas, RGB}; mod font; use font::{Font, Glyph, SixteenSegmentFont}; @@ -31,6 +31,30 @@ const FRAMEBUF: usize = ROWS * COLUMNS * 3; #[global_allocator] static HEAP: Heap = Heap::empty(); +static mut BUF: [u8; 163200] = [0; 163200]; + +pub struct FrameBuf { + pub buf: &'static mut [u8; 163200], + pub width: usize, +} + +impl FrameBuf { + pub fn new() -> Self { + Self { + buf: unsafe { &mut BUF }, + width: 170, + } + } +} + +impl Canvas for FrameBuf { + fn set_pixel(&mut self, x: usize, y: usize, color: &RGB) { + self.buf[(y * self.width + x) * 3 + 0] = color.r << 2; + self.buf[(y * self.width + x) * 3 + 1] = color.g << 2; + self.buf[(y * self.width + x) * 3 + 2] = color.b << 2; + } +} + #[entry] unsafe fn main() -> ! { { @@ -122,10 +146,10 @@ unsafe fn main() -> ! { let mut framebuf = [0; FRAMEBUF]; let mut canvas = Canvas::new(&mut framebuf, COLUMNS); */ - let mut canvas = Canvas::new(); + let mut canvas = FrameBuf::new(); + 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)); @@ -141,47 +165,119 @@ unsafe fn main() -> ! { font_sixteen .glyph(' ') - .draw(&mut canvas, 0, 0, (255, 255, 255)); + .draw(&mut canvas, 0, 0, &white); font_sixteen .glyph('0') - .draw(&mut canvas, 11, 0, (255, 255, 255)); - font_sixteen.glyph('1').draw(&mut canvas, 22, 0, (255, 255, 255)); - font_sixteen.glyph('2').draw(&mut canvas, 33, 0, (255, 255, 255)); - font_sixteen.glyph('3').draw(&mut canvas, 44, 0, (255, 255, 255)); - font_sixteen.glyph('4').draw(&mut canvas, 55, 0, (255, 255, 255)); - font_sixteen.glyph('5').draw(&mut canvas, 66, 0, (255, 255, 255)); - font_sixteen.glyph('6').draw(&mut canvas, 77, 0, (255, 255, 255)); - font_sixteen.glyph('7').draw(&mut canvas, 88, 0, (255, 255, 255)); - font_sixteen.glyph('8').draw(&mut canvas, 99, 0, (255, 255, 255)); - font_sixteen.glyph('9').draw(&mut canvas, 110, 0, (255, 255, 255)); + .draw(&mut canvas, 11, 0, &white); + font_sixteen + .glyph('1') + .draw(&mut canvas, 22, 0, &white); + font_sixteen + .glyph('2') + .draw(&mut canvas, 33, 0, &white); + font_sixteen + .glyph('3') + .draw(&mut canvas, 44, 0, &white); + font_sixteen + .glyph('4') + .draw(&mut canvas, 55, 0, &white); + font_sixteen + .glyph('5') + .draw(&mut canvas, 66, 0, &white); + font_sixteen + .glyph('6') + .draw(&mut canvas, 77, 0, &white); + font_sixteen + .glyph('7') + .draw(&mut canvas, 88, 0, &white); + font_sixteen + .glyph('8') + .draw(&mut canvas, 99, 0, &white); + font_sixteen + .glyph('9') + .draw(&mut canvas, 110, 0, &white); - font_sixteen.glyph('A').draw(&mut canvas, 0, 20, (255, 255, 255)); - font_sixteen.glyph('B').draw(&mut canvas, 11, 20, (255, 255, 255)); - font_sixteen.glyph('C').draw(&mut canvas, 22, 20, (255, 255, 255)); - font_sixteen.glyph('D').draw(&mut canvas, 33, 20, (255, 255, 255)); - font_sixteen.glyph('E').draw(&mut canvas, 44, 20, (255, 255, 255)); - font_sixteen.glyph('F').draw(&mut canvas, 55, 20, (255, 255, 255)); - font_sixteen.glyph('G').draw(&mut canvas, 66, 20, (255, 255, 255)); - font_sixteen.glyph('H').draw(&mut canvas, 77, 20, (255, 255, 255)); - font_sixteen.glyph('I').draw(&mut canvas, 88, 20, (255, 255, 255)); - font_sixteen.glyph('J').draw(&mut canvas, 99, 20, (255, 255, 255)); - font_sixteen.glyph('K').draw(&mut canvas, 110, 20, (255, 255, 255)); - font_sixteen.glyph('L').draw(&mut canvas, 121, 20, (255, 255, 255)); - font_sixteen.glyph('M').draw(&mut canvas, 132, 20, (255, 255, 255)); - font_sixteen.glyph('N').draw(&mut canvas, 143, 20, (255, 255, 255)); - font_sixteen.glyph('O').draw(&mut canvas, 154, 20, (255, 255, 255)); + font_sixteen + .glyph('A') + .draw(&mut canvas, 0, 20, &white); + font_sixteen + .glyph('B') + .draw(&mut canvas, 11, 20, &white); + font_sixteen + .glyph('C') + .draw(&mut canvas, 22, 20, &white); + font_sixteen + .glyph('D') + .draw(&mut canvas, 33, 20, &white); + font_sixteen + .glyph('E') + .draw(&mut canvas, 44, 20, &white); + font_sixteen + .glyph('F') + .draw(&mut canvas, 55, 20, &white); + font_sixteen + .glyph('G') + .draw(&mut canvas, 66, 20, &white); + font_sixteen + .glyph('H') + .draw(&mut canvas, 77, 20, &white); + font_sixteen + .glyph('I') + .draw(&mut canvas, 88, 20, &white); + font_sixteen + .glyph('J') + .draw(&mut canvas, 99, 20, &white); + font_sixteen + .glyph('K') + .draw(&mut canvas, 110, 20, &white); + font_sixteen + .glyph('L') + .draw(&mut canvas, 121, 20, &white); + font_sixteen + .glyph('M') + .draw(&mut canvas, 132, 20, &white); + font_sixteen + .glyph('N') + .draw(&mut canvas, 143, 20, &white); + font_sixteen + .glyph('O') + .draw(&mut canvas, 154, 20, &white); - font_sixteen.glyph('P').draw(&mut canvas, 0, 40, (255, 255, 255)); - font_sixteen.glyph('Q').draw(&mut canvas, 11, 40, (255, 255, 255)); - font_sixteen.glyph('R').draw(&mut canvas, 22, 40, (255, 255, 255)); - font_sixteen.glyph('S').draw(&mut canvas, 33, 40, (255, 255, 255)); - font_sixteen.glyph('T').draw(&mut canvas, 44, 40, (255, 255, 255)); - font_sixteen.glyph('U').draw(&mut canvas, 55, 40, (255, 255, 255)); - font_sixteen.glyph('V').draw(&mut canvas, 66, 40, (255, 255, 255)); - font_sixteen.glyph('W').draw(&mut canvas, 77, 40, (255, 255, 255)); - font_sixteen.glyph('X').draw(&mut canvas, 88, 40, (255, 255, 255)); - font_sixteen.glyph('Y').draw(&mut canvas, 99, 40, (255, 255, 255)); - font_sixteen.glyph('Z').draw(&mut canvas, 110, 40, (255, 255, 255)); + font_sixteen + .glyph('P') + .draw(&mut canvas, 0, 40, &white); + font_sixteen + .glyph('Q') + .draw(&mut canvas, 11, 40, &white); + font_sixteen + .glyph('R') + .draw(&mut canvas, 22, 40, &white); + font_sixteen + .glyph('S') + .draw(&mut canvas, 33, 40, &white); + font_sixteen + .glyph('T') + .draw(&mut canvas, 44, 40, &white); + font_sixteen + .glyph('U') + .draw(&mut canvas, 55, 40, &white); + font_sixteen + .glyph('V') + .draw(&mut canvas, 66, 40, &white); + font_sixteen + .glyph('W') + .draw(&mut canvas, 77, 40, &white); + font_sixteen + .glyph('X') + .draw(&mut canvas, 88, 40, &white); + font_sixteen + .glyph('Y') + .draw(&mut canvas, 99, 40, &white); + font_sixteen + .glyph('Z') + .draw(&mut canvas, 110, 40, &white); + + canvas.square(10, 70, 160, 310, &white); { let display = display.acquire(); -- 2.47.2 From b444326c1c418e05217dc57761aed0c0b70d9d7a Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Wed, 5 Mar 2025 09:48:33 -0500 Subject: [PATCH 6/7] Add a link to the sixteen-segment font source --- pico-st7789/src/font/sixteen_segment.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pico-st7789/src/font/sixteen_segment.rs b/pico-st7789/src/font/sixteen_segment.rs index 44061ce..fe021ff 100644 --- a/pico-st7789/src/font/sixteen_segment.rs +++ b/pico-st7789/src/font/sixteen_segment.rs @@ -6,6 +6,7 @@ use crate::canvas::{Canvas, RGB}; use super::{Font, Glyph}; // Sixteen Segments +// https://www.partsnotincluded.com/segmented-led-display-ascii-library/ // // a1 a2 // f h i jb -- 2.47.2 From bc9e24c0c95c78d62d4c358f790a268e34e05f08 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel <savanni@luminescent-dreams.com> Date: Wed, 5 Mar 2025 09:58:42 -0500 Subject: [PATCH 7/7] Remove dead code --- pico-st7789/src/canvas.rs | 237 -------------------------------------- 1 file changed, 237 deletions(-) diff --git a/pico-st7789/src/canvas.rs b/pico-st7789/src/canvas.rs index e7d6f30..cb2d05b 100644 --- a/pico-st7789/src/canvas.rs +++ b/pico-st7789/src/canvas.rs @@ -122,241 +122,4 @@ pub fn draw_seven_segment( write_pixel(frame, width, x + 3, y + 4, color); } } - -// Sixteen Segments -// -// a1 a2 -// f h i jb -// f i j b -// f hij b -// g1 g2 -// e klm c -// e k l m c -// ek l mc -// d1 d2 - -pub struct SixteenSegment { - a1: bool, - a2: bool, - b: bool, - c: bool, - d1: bool, - d2: bool, - e: bool, - f: bool, - g1: bool, - g2: bool, - h: bool, - i: bool, - j: bool, - k: bool, - l: bool, - m: bool, -} - -pub fn sixteen_segment_font() -> BTreeMap<char, SixteenSegment> { - let mut font = BTreeMap::new(); - font.insert( - '*', - SixteenSegment { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: true, - g2: true, - h: true, - i: true, - j: true, - k: true, - l: true, - m: true, - }, - ); - font.insert( - '0', - SixteenSegment { - a1: true, - a2: true, - b: true, - c: true, - d1: true, - d2: true, - e: true, - f: true, - g1: false, - g2: false, - h: false, - i: false, - j: false, - k: false, - l: false, - m: false, - }, - ); - font.insert( - '1', - SixteenSegment { - a1: false, - a2: false, - b: true, - c: true, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: true, - k: false, - l: false, - m: false, - }, - ); - font.insert( - '2', - SixteenSegment { - a1: true, - a2: true, - b: false, - c: false, - d1: false, - d2: false, - e: false, - f: false, - g1: false, - g2: false, - h: false, - i: false, - j: true, - k: false, - l: false, - m: false, - }, - ); - - font -} - -pub fn draw_sixteen_segment( - c: char, - font: &BTreeMap<char, SixteenSegment>, - frame: &mut [u8], - width: usize, - x: usize, - y: usize, - color: (u8, u8, u8), -) { - let segments = font.get(&c).unwrap(); - if segments.a1 { - write_pixel(frame, width, x + 1, y, color); - write_pixel(frame, width, x + 2, y, color); - write_pixel(frame, width, x + 3, y, color); - } - if segments.a2 { - write_pixel(frame, width, x + 5, y, color); - write_pixel(frame, width, x + 6, y, color); - write_pixel(frame, width, x + 7, y, color); - } - if segments.b { - write_pixel(frame, width, x + 8, y + 1, color); - write_pixel(frame, width, x + 8, y + 2, color); - write_pixel(frame, width, x + 8, y + 3, color); - write_pixel(frame, width, x + 8, y + 4, color); - write_pixel(frame, width, x + 8, y + 5, color); - } - if segments.c { - write_pixel(frame, width, x + 8, y + 7, color); - write_pixel(frame, width, x + 8, y + 8, color); - write_pixel(frame, width, x + 8, y + 9, color); - write_pixel(frame, width, x + 8, y + 10, color); - write_pixel(frame, width, x + 8, y + 11, color); - } - - if segments.d1 { - write_pixel(frame, width, x + 1, y + 12, color); - write_pixel(frame, width, x + 2, y + 12, color); - write_pixel(frame, width, x + 3, y + 12, color); - } - if segments.d2 { - write_pixel(frame, width, x + 5, y + 12, color); - write_pixel(frame, width, x + 6, y + 12, color); - write_pixel(frame, width, x + 7, y + 12, color); - } - - if segments.e { - write_pixel(frame, width, x, y + 7, color); - write_pixel(frame, width, x, y + 8, color); - write_pixel(frame, width, x, y + 9, color); - write_pixel(frame, width, x, y + 10, color); - write_pixel(frame, width, x, y + 11, color); - } - if segments.f { - write_pixel(frame, width, x, y + 1, color); - write_pixel(frame, width, x, y + 2, color); - write_pixel(frame, width, x, y + 3, color); - write_pixel(frame, width, x, y + 4, color); - write_pixel(frame, width, x, y + 5, color); - } - - if segments.g1 { - write_pixel(frame, width, x + 1, y + 6, color); - write_pixel(frame, width, x + 2, y + 6, color); - write_pixel(frame, width, x + 3, y + 6, color); - } - if segments.g2 { - write_pixel(frame, width, x + 5, y + 6, color); - write_pixel(frame, width, x + 6, y + 6, color); - write_pixel(frame, width, x + 7, y + 6, color); - } - - if segments.h { - write_pixel(frame, width, x + 1, y + 1, color); - write_pixel(frame, width, x + 1, y + 2, color); - write_pixel(frame, width, x + 2, y + 3, color); - write_pixel(frame, width, x + 3, y + 4, color); - write_pixel(frame, width, x + 3, y + 5, color); - } - if segments.i { - write_pixel(frame, width, x + 4, y + 1, color); - write_pixel(frame, width, x + 4, y + 2, color); - write_pixel(frame, width, x + 4, y + 3, color); - write_pixel(frame, width, x + 4, y + 4, color); - write_pixel(frame, width, x + 4, y + 5, color); - } - if segments.j { - write_pixel(frame, width, x + 7, y + 1, color); - write_pixel(frame, width, x + 7, y + 2, color); - write_pixel(frame, width, x + 6, y + 3, color); - write_pixel(frame, width, x + 5, y + 4, color); - write_pixel(frame, width, x + 5, y + 5, color); - } - if segments.k { - write_pixel(frame, width, x + 3, y + 7, color); - write_pixel(frame, width, x + 3, y + 8, color); - write_pixel(frame, width, x + 2, y + 9, color); - write_pixel(frame, width, x + 1, y + 10, color); - write_pixel(frame, width, x + 1, y + 11, color); - } - if segments.l { - write_pixel(frame, width, x + 4, y + 7, color); - write_pixel(frame, width, x + 4, y + 8, color); - write_pixel(frame, width, x + 4, y + 9, color); - write_pixel(frame, width, x + 4, y + 10, color); - write_pixel(frame, width, x + 4, y + 11, color); - } - if segments.m { - write_pixel(frame, width, x + 5, y + 7, color); - write_pixel(frame, width, x + 5, y + 8, color); - write_pixel(frame, width, x + 6, y + 9, color); - write_pixel(frame, width, x + 7, y + 10, color); - write_pixel(frame, width, x + 7, y + 11, color); - } -} */ -- 2.47.2