diff --git a/src/display.rs b/src/display.rs index 24bc249..4eb5d76 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,4 +1,4 @@ -use core::ops::Mul; +use core::ops::{Add, Mul}; use embedded_hal::{delay::DelayNs, spi::SpiBus}; use esp_hal::{DriverMode, gpio::Output, spi::master::Spi}; @@ -64,6 +64,14 @@ impl Mul for Rgb565 { } } +impl Add for Rgb565 { + type Output = Rgb565; + + fn add(self, rhs: Rgb565) -> Self::Output { + Rgb565(self.0 + rhs.0, self.1 + rhs.1, self.2 + rhs.2) + } +} + #[derive(Default, Clone, Copy, Debug)] pub struct Position { pub x: i16, diff --git a/src/font.rs b/src/font.rs index d82d325..c04c7b3 100644 --- a/src/font.rs +++ b/src/font.rs @@ -6,7 +6,7 @@ use alloc::vec; use alloc::vec::Vec; use alloc::{borrow::ToOwned, string::String}; -use crate::display::{self, Display, Position, Rgb565}; +use crate::display::{self, Color, Display, Position, Rgb565}; static OPEN_SANS: &'static [u8] = include_bytes!("./OpenSans_Condensed-Regular.ttf"); @@ -62,6 +62,8 @@ impl<'a> FontRenderer<'a> { position: Position, h_align: HorizontalAlignment, v_align: VerticalAlignment, + bg: Rgb565, + fg: Rgb565, ) { let text = text.into(); let rows = text.split("\n").collect::>(); @@ -82,7 +84,7 @@ impl<'a> FontRenderer<'a> { }; let y = start_y + (self.height * i as u32) as i16; - self.raw_render(display, data, Position { x, y }); + self.raw_render(display, data, Position { x, y }, bg, fg); } } @@ -112,6 +114,8 @@ impl<'a> FontRenderer<'a> { display: &mut Display<'d, DM, Delay>, data: RawRenderData<'a>, pos: Position, + bg: Rgb565, + fg: Rgb565, ) { let start_x = pos.x.clamp(0, 240); let start_y = pos.y.clamp(0, 240); @@ -129,7 +133,7 @@ impl<'a> FontRenderer<'a> { let width = end_x - start_x; let height = end_y - start_y; - let mut pixel_data = vec![Rgb565::white(); height as usize * width as usize]; + let mut pixel_data = vec![bg; height as usize * width as usize]; for g in data.glyphs { if let Some(bb) = g.pixel_bounding_box() { @@ -137,8 +141,8 @@ impl<'a> FontRenderer<'a> { let x = bb.min.x + x as i32 - clip_left as i32; let y = bb.min.y + y as i32 - clip_top as i32; if x >= 0 && y >= 0 && x < width as i32 && y < height as i32 { - let col = 255 - (255. * cov) as u8; - pixel_data[(x + y * width as i32) as usize] = Rgb565(col, col, col); + let col = bg * (1. - cov) + fg * cov; + pixel_data[(x + y * width as i32) as usize] = col; } }); } diff --git a/src/main.rs b/src/main.rs index c1fcb21..4028d47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,6 +117,8 @@ fn main() -> ! { Position::new(0, 0), HorizontalAlignment::LeftToRight, VerticalAlignment::TopToBottom, + Rgb565::black(), + Rgb565::white(), ); let sim_rst = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default());