diff --git a/pico-st7789/src/canvas.rs b/pico-st7789/src/canvas.rs index cb2d05b..b154ebf 100644 --- a/pico-st7789/src/canvas.rs +++ b/pico-st7789/src/canvas.rs @@ -8,11 +8,6 @@ // e c // d d d -use alloc::collections::btree_map::BTreeMap; - -/* -*/ - pub struct RGB { pub r: u8, pub g: u8, pub b: u8 } pub trait Canvas { @@ -34,92 +29,3 @@ pub trait Canvas { } } -/* -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 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], - width: usize, - x: usize, - y: usize, - color: (u8, u8, u8), -) { - let segments = SEVEN_SEGMENT_FONT[digit as usize]; - if segments[0] { - 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, 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, 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, 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, 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, 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, width, x + 1, y + 4, color); - write_pixel(frame, width, x + 2, y + 4, color); - write_pixel(frame, width, x + 3, y + 4, color); - } -} -*/ diff --git a/pico-st7789/src/font/mod.rs b/pico-st7789/src/font/mod.rs index 126176d..3886765 100644 --- a/pico-st7789/src/font/mod.rs +++ b/pico-st7789/src/font/mod.rs @@ -1,5 +1,8 @@ use crate::canvas::{Canvas, RGB}; +mod seven_segment; +pub use seven_segment::SevenSegmentFont; + mod sixteen_segment; pub use sixteen_segment::SixteenSegmentFont; @@ -9,5 +12,6 @@ pub trait Font { pub trait Glyph { fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB); + fn extents(&self) -> (usize, usize); } diff --git a/pico-st7789/src/font/seven_segment.rs b/pico-st7789/src/font/seven_segment.rs new file mode 100644 index 0000000..94dd8cd --- /dev/null +++ b/pico-st7789/src/font/seven_segment.rs @@ -0,0 +1,211 @@ +use alloc::collections::btree_map::BTreeMap; +use bitflags::bitflags; + +use crate::canvas::{Canvas, RGB}; + +use super::{Font, Glyph}; + +// Seven Segments +// +// a a a +// f b +// f b +// f b +// g g g +// e c +// e c +// e c +// d d d + +bitflags! { + pub struct SevenSegmentGlyph: u8 { + const NONE = 0; + const A = 0x01; + const B = 0x01 << 1; + const C = 0x01 << 2; + const D = 0x01 << 3; + const E = 0x01 << 4; + const F = 0x01 << 5; + const G = 0x01 << 6; + const DOT = 0x01 << 7; + } +} + +pub struct SevenSegmentFont(BTreeMap); + +impl SevenSegmentFont { + pub fn new() -> Self { + let mut font = BTreeMap::new(); + font.insert(' ', SevenSegmentGlyph::NONE); + font.insert( + '0', + SevenSegmentGlyph::A + | SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F, + ); + font.insert('1', SevenSegmentGlyph::B | SevenSegmentGlyph::C); + font.insert( + '2', + SevenSegmentGlyph::A + | SevenSegmentGlyph::B + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::G, + ); + font.insert( + '3', + SevenSegmentGlyph::A + | SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::G + | SevenSegmentGlyph::D, + ); + font.insert( + '4', + SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + '5', + SevenSegmentGlyph::A + | SevenSegmentGlyph::C + | SevenSegmentGlyph::D + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + '6', + SevenSegmentGlyph::A + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G + | SevenSegmentGlyph::C + | SevenSegmentGlyph::D, + ); + font.insert( + '7', + SevenSegmentGlyph::A | SevenSegmentGlyph::B | SevenSegmentGlyph::C, + ); + font.insert( + '8', + SevenSegmentGlyph::A + | SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + '9', + SevenSegmentGlyph::A + | SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + 'A', + SevenSegmentGlyph::A + | SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + 'B', + SevenSegmentGlyph::C + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + 'C', + SevenSegmentGlyph::A + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F, + ); + font.insert( + 'D', + SevenSegmentGlyph::B + | SevenSegmentGlyph::C + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::G, + ); + font.insert( + 'E', + SevenSegmentGlyph::A + | SevenSegmentGlyph::D + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + font.insert( + 'F', + SevenSegmentGlyph::A + | SevenSegmentGlyph::E + | SevenSegmentGlyph::F + | SevenSegmentGlyph::G, + ); + Self(font) + } +} + +impl Font for SevenSegmentFont { + fn glyph(&self, c: char) -> &SevenSegmentGlyph { + self.0.get(&c).unwrap() + } +} + +impl Glyph for SevenSegmentGlyph { + fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) { + if self.contains(SevenSegmentGlyph::A) { + canvas.set_pixel(x + 1, y, color); + canvas.set_pixel(x + 2, y, color); + canvas.set_pixel(x + 3, y, color); + } + if self.contains(SevenSegmentGlyph::B) { + canvas.set_pixel(x + 4, y + 1, color); + canvas.set_pixel(x + 4, y + 2, color); + canvas.set_pixel(x + 4, y + 3, color); + } + if self.contains(SevenSegmentGlyph::C) { + canvas.set_pixel(x + 4, y + 5, color); + canvas.set_pixel(x + 4, y + 6, color); + canvas.set_pixel(x + 4, y + 7, color); + } + if self.contains(SevenSegmentGlyph::D) { + canvas.set_pixel(x + 1, y + 8, color); + canvas.set_pixel(x + 2, y + 8, color); + canvas.set_pixel(x + 3, y + 8, color); + } + if self.contains(SevenSegmentGlyph::E) { + canvas.set_pixel(x, y + 5, color); + canvas.set_pixel(x, y + 6, color); + canvas.set_pixel(x, y + 7, color); + } + if self.contains(SevenSegmentGlyph::F) { + canvas.set_pixel(x, y + 1, color); + canvas.set_pixel(x, y + 2, color); + canvas.set_pixel(x, y + 3, color); + } + if self.contains(SevenSegmentGlyph::G) { + canvas.set_pixel(x + 1, y + 4, color); + canvas.set_pixel(x + 2, y + 4, color); + canvas.set_pixel(x + 3, y + 4, color); + } + } + + fn extents(&self) -> (usize, usize) { + (5, 8) + } +} diff --git a/pico-st7789/src/font/sixteen_segment.rs b/pico-st7789/src/font/sixteen_segment.rs index fe021ff..62e1285 100644 --- a/pico-st7789/src/font/sixteen_segment.rs +++ b/pico-st7789/src/font/sixteen_segment.rs @@ -513,5 +513,9 @@ impl Glyph for SixteenSegmentGlyph { canvas.set_pixel(x + 7, y + 11, color); } } + + fn extents(&self) -> (usize, usize) { + (8, 12) + } } diff --git a/pico-st7789/src/main.rs b/pico-st7789/src/main.rs index e404ba7..68b0554 100644 --- a/pico-st7789/src/main.rs +++ b/pico-st7789/src/main.rs @@ -17,7 +17,7 @@ mod canvas; use canvas::{Canvas, RGB}; mod font; -use font::{Font, Glyph, SixteenSegmentFont}; +use font::{Font, Glyph, SevenSegmentFont, SixteenSegmentFont}; mod st7789; use st7789::{ST7789Display, SETUP_PROGRAM}; @@ -64,6 +64,7 @@ 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. @@ -163,119 +164,172 @@ unsafe fn main() -> ! { draw_seven_segment(9, &mut frame, 63, 0, (255, 255, 255)); */ - font_sixteen + font_seven .glyph(' ') .draw(&mut canvas, 0, 0, &white); - font_sixteen + font_seven .glyph('0') .draw(&mut canvas, 11, 0, &white); - font_sixteen + font_seven .glyph('1') .draw(&mut canvas, 22, 0, &white); - font_sixteen + font_seven .glyph('2') .draw(&mut canvas, 33, 0, &white); - font_sixteen + font_seven .glyph('3') .draw(&mut canvas, 44, 0, &white); - font_sixteen + font_seven .glyph('4') .draw(&mut canvas, 55, 0, &white); - font_sixteen + font_seven .glyph('5') .draw(&mut canvas, 66, 0, &white); - font_sixteen + font_seven .glyph('6') .draw(&mut canvas, 77, 0, &white); - font_sixteen + font_seven .glyph('7') .draw(&mut canvas, 88, 0, &white); - font_sixteen + font_seven .glyph('8') .draw(&mut canvas, 99, 0, &white); - font_sixteen + font_seven .glyph('9') .draw(&mut canvas, 110, 0, &white); - font_sixteen + 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('B') + .glyph('0') .draw(&mut canvas, 11, 20, &white); font_sixteen - .glyph('C') + .glyph('1') .draw(&mut canvas, 22, 20, &white); font_sixteen - .glyph('D') + .glyph('2') .draw(&mut canvas, 33, 20, &white); font_sixteen - .glyph('E') + .glyph('3') .draw(&mut canvas, 44, 20, &white); font_sixteen - .glyph('F') + .glyph('4') .draw(&mut canvas, 55, 20, &white); font_sixteen - .glyph('G') + .glyph('5') .draw(&mut canvas, 66, 20, &white); font_sixteen - .glyph('H') + .glyph('6') .draw(&mut canvas, 77, 20, &white); font_sixteen - .glyph('I') + .glyph('7') .draw(&mut canvas, 88, 20, &white); font_sixteen - .glyph('J') + .glyph('8') .draw(&mut canvas, 99, 20, &white); font_sixteen - .glyph('K') + .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, 20, &white); + .draw(&mut canvas, 121, 40, &white); font_sixteen .glyph('M') - .draw(&mut canvas, 132, 20, &white); + .draw(&mut canvas, 132, 40, &white); font_sixteen .glyph('N') - .draw(&mut canvas, 143, 20, &white); + .draw(&mut canvas, 143, 40, &white); font_sixteen .glyph('O') - .draw(&mut canvas, 154, 20, &white); + .draw(&mut canvas, 154, 40, &white); font_sixteen .glyph('P') - .draw(&mut canvas, 0, 40, &white); + .draw(&mut canvas, 0, 60, &white); font_sixteen .glyph('Q') - .draw(&mut canvas, 11, 40, &white); + .draw(&mut canvas, 11, 60, &white); font_sixteen .glyph('R') - .draw(&mut canvas, 22, 40, &white); + .draw(&mut canvas, 22, 60, &white); font_sixteen .glyph('S') - .draw(&mut canvas, 33, 40, &white); + .draw(&mut canvas, 33, 60, &white); font_sixteen .glyph('T') - .draw(&mut canvas, 44, 40, &white); + .draw(&mut canvas, 44, 60, &white); font_sixteen .glyph('U') - .draw(&mut canvas, 55, 40, &white); + .draw(&mut canvas, 55, 60, &white); font_sixteen .glyph('V') - .draw(&mut canvas, 66, 40, &white); + .draw(&mut canvas, 66, 60, &white); font_sixteen .glyph('W') - .draw(&mut canvas, 77, 40, &white); + .draw(&mut canvas, 77, 60, &white); font_sixteen .glyph('X') - .draw(&mut canvas, 88, 40, &white); + .draw(&mut canvas, 88, 60, &white); font_sixteen .glyph('Y') - .draw(&mut canvas, 99, 40, &white); + .draw(&mut canvas, 99, 60, &white); font_sixteen .glyph('Z') - .draw(&mut canvas, 110, 40, &white); + .draw(&mut canvas, 110, 60, &white); canvas.square(10, 70, 160, 310, &white);