Allow setting fg/bg-color

This commit is contained in:
Sofia 2026-05-16 20:13:05 +03:00
parent 15302ea96b
commit f5e2775a04
3 changed files with 20 additions and 6 deletions

View File

@ -1,4 +1,4 @@
use core::ops::Mul; use core::ops::{Add, Mul};
use embedded_hal::{delay::DelayNs, spi::SpiBus}; use embedded_hal::{delay::DelayNs, spi::SpiBus};
use esp_hal::{DriverMode, gpio::Output, spi::master::Spi}; use esp_hal::{DriverMode, gpio::Output, spi::master::Spi};
@ -64,6 +64,14 @@ impl Mul<f32> for Rgb565 {
} }
} }
impl Add<Rgb565> 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)] #[derive(Default, Clone, Copy, Debug)]
pub struct Position { pub struct Position {
pub x: i16, pub x: i16,

View File

@ -6,7 +6,7 @@ use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use alloc::{borrow::ToOwned, string::String}; 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"); static OPEN_SANS: &'static [u8] = include_bytes!("./OpenSans_Condensed-Regular.ttf");
@ -62,6 +62,8 @@ impl<'a> FontRenderer<'a> {
position: Position, position: Position,
h_align: HorizontalAlignment, h_align: HorizontalAlignment,
v_align: VerticalAlignment, v_align: VerticalAlignment,
bg: Rgb565,
fg: Rgb565,
) { ) {
let text = text.into(); let text = text.into();
let rows = text.split("\n").collect::<Vec<&str>>(); let rows = text.split("\n").collect::<Vec<&str>>();
@ -82,7 +84,7 @@ impl<'a> FontRenderer<'a> {
}; };
let y = start_y + (self.height * i as u32) as i16; 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>, display: &mut Display<'d, DM, Delay>,
data: RawRenderData<'a>, data: RawRenderData<'a>,
pos: Position, pos: Position,
bg: Rgb565,
fg: Rgb565,
) { ) {
let start_x = pos.x.clamp(0, 240); let start_x = pos.x.clamp(0, 240);
let start_y = pos.y.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 width = end_x - start_x;
let height = end_y - start_y; 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 { for g in data.glyphs {
if let Some(bb) = g.pixel_bounding_box() { 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 x = bb.min.x + x as i32 - clip_left as i32;
let y = bb.min.y + y as i32 - clip_top 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 { if x >= 0 && y >= 0 && x < width as i32 && y < height as i32 {
let col = 255 - (255. * cov) as u8; let col = bg * (1. - cov) + fg * cov;
pixel_data[(x + y * width as i32) as usize] = Rgb565(col, col, col); pixel_data[(x + y * width as i32) as usize] = col;
} }
}); });
} }

View File

@ -117,6 +117,8 @@ fn main() -> ! {
Position::new(0, 0), Position::new(0, 0),
HorizontalAlignment::LeftToRight, HorizontalAlignment::LeftToRight,
VerticalAlignment::TopToBottom, VerticalAlignment::TopToBottom,
Rgb565::black(),
Rgb565::white(),
); );
let sim_rst = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default()); let sim_rst = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default());