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 { + 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 { - 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); impl SixteenSegmentFont { @@ -442,7 +406,7 @@ impl Font 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();