Add simple text rendering

This commit is contained in:
Sofia 2025-09-13 15:00:42 +03:00
parent 549ebdc37c
commit 3a827cfde7
2 changed files with 56 additions and 4 deletions

View File

@ -78,7 +78,7 @@ fn letter(character: char) -> [u8; 8] {
], ],
'r' => [ 'r' => [
0b11111111, 0b10000001, 0b10000001, 0b11111111, 0b11111100, 0b10000010, 0b10000001, 0b11111111, 0b10000001, 0b10000001, 0b11111111, 0b11111100, 0b10000010, 0b10000001,
0b00000001, 0b10000001,
], ],
's' => [ 's' => [
0b01111111, 0b10000000, 0b10000000, 0b01111110, 0b00000001, 0b00000001, 0b00000001, 0b01111111, 0b10000000, 0b10000000, 0b01111110, 0b00000001, 0b00000001, 0b00000001,
@ -112,10 +112,55 @@ fn letter(character: char) -> [u8; 8] {
0b11111111, 0b00000111, 0b00001100, 0b00011000, 0b00110000, 0b01100000, 0b11000000, 0b11111111, 0b00000111, 0b00001100, 0b00011000, 0b00110000, 0b01100000, 0b11000000,
0b11111111, 0b11111111,
], ],
'!' => [
0b01000000, 0b01000000, 0b01000000, 0b01000000, 0b01000000, 0b01000000, 0b00000000,
0b01000000,
],
',' => [
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b01000000,
0b10000000,
],
_ => [0; 8], _ => [0; 8],
} }
} }
pub fn draw_text<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
display: &mut Display<T, DCPin, RSTPin>,
text: &str,
fg: Rgb565,
bg: Rgb565,
position: Vec2,
scale: u16,
) {
let mut curr_pos = position;
let kerning = 9 * scale;
let original_x = position.x;
for c in text.chars() {
if c == '\n' {
curr_pos.y += kerning;
continue;
}
if c == ' ' {
curr_pos.x += kerning;
continue;
}
if curr_pos.x + kerning > 240 {
curr_pos.x = original_x;
curr_pos.y += kerning;
}
if curr_pos.y + kerning > 240 {
curr_pos.y = 0;
}
Letter::from(c, fg, bg)
.iter()
.draw(display, curr_pos, scale);
curr_pos.x += kerning;
}
}
pub struct Letter { pub struct Letter {
base: [u8; 8], base: [u8; 8],
pub fg: Rgb565, pub fg: Rgb565,
@ -160,7 +205,7 @@ impl<'a> Iterator for LetterIter<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let byte_idx = self.idx / 8; let byte_idx = self.idx / 8;
let bit_idx = self.idx % 8; let bit_idx = 7 - (self.idx % 8);
if byte_idx >= 8 { if byte_idx >= 8 {
None None
} else { } else {

View File

@ -19,7 +19,7 @@ use panic_halt as _;
use crate::{ use crate::{
display::{Display, Rgb565, Vec2}, display::{Display, Rgb565, Vec2},
font::Letter, font::{Letter, draw_text},
graphics::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image}, graphics::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image},
peripherals::Button, peripherals::Button,
}; };
@ -94,7 +94,14 @@ fn main() -> ! {
let letter = Letter::from('h', Rgb565::white(), Rgb565::black()); let letter = Letter::from('h', Rgb565::white(), Rgb565::black());
letter.iter().draw(&mut display, Vec2 { x: 10, y: 10 }, 4); draw_text(
&mut display,
"hello there!",
Rgb565::white(),
Rgb565::red(),
Vec2 { x: 10, y: 10 },
3,
);
loop { loop {
if button.poll() { if button.poll() {