Add formatting for numbers too

This commit is contained in:
Sofia 2025-09-13 19:10:45 +03:00
parent c78db0c81a
commit df146d7c0a
2 changed files with 123 additions and 27 deletions

View File

@ -127,6 +127,46 @@ pub static COMMA: [u8; 8] = [
pub static EMPTY: [u8; 8] = [ pub static EMPTY: [u8; 8] = [
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
]; ];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_1: [u8; 8] = [
0b00011000, 0b00111000, 0b01111000, 0b00011000, 0b00011000, 0b00011000, 0b00011000, 0b00011000,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_2: [u8; 8] = [
0b11111110, 0b11110110, 0b00001100, 0b00011000, 0b00110000, 0b01100000, 0b11111111, 0b11111111,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_3: [u8; 8] = [
0b01111110, 0b10000001, 0b00011110, 0b00011110, 0b00000001, 0b10000001, 0b11111111, 0b01111110,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_4: [u8; 8] = [
0b11000110, 0b11000110, 0b11000110, 0b11111111, 0b11111110, 0b00000110, 0b00000110, 0b00000110,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_5: [u8; 8] = [
0b11111111, 0b11111111, 0b11000000, 0b11111100, 0b00000110, 0b11000011, 0b11111111, 0b01111110,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_6: [u8; 8] = [
0b01111110, 0b11000011, 0b11000000, 0b11111110, 0b11000011, 0b11000011, 0b11000011, 0b01111110,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_7: [u8; 8] = [
0b11111111, 0b11111111, 0b00000110, 0b00001100, 0b00011000, 0b00110000, 0b01100000, 0b11000000,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_8: [u8; 8] = [
0b01111110, 0b11111111, 0b11000011, 0b01111110, 0b11000011, 0b11000011, 0b11111111, 0b01111110,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_9: [u8; 8] = [
0b01111110, 0b11000011, 0b11000011, 0b01111111, 0b00000011, 0b11000011, 0b11000011, 0b01111110,
];
#[unsafe(link_section = ".progmem.data")]
pub static NUM_0: [u8; 8] = [
0b01111110, 0b11000111, 0b11001011, 0b11001011, 0b11010011, 0b11010011, 0b11100011, 0b01111110,
];
fn letter(character: char) -> *const [u8; 8] { fn letter(character: char) -> *const [u8; 8] {
match character { match character {
@ -156,6 +196,16 @@ fn letter(character: char) -> *const [u8; 8] {
'x' => addr_of!(X), 'x' => addr_of!(X),
'y' => addr_of!(Y), 'y' => addr_of!(Y),
'z' => addr_of!(Z), 'z' => addr_of!(Z),
'0' => addr_of!(NUM_0),
'1' => addr_of!(NUM_1),
'2' => addr_of!(NUM_2),
'3' => addr_of!(NUM_3),
'4' => addr_of!(NUM_4),
'5' => addr_of!(NUM_5),
'6' => addr_of!(NUM_6),
'7' => addr_of!(NUM_7),
'8' => addr_of!(NUM_8),
'9' => addr_of!(NUM_9),
'!' => addr_of!(EXCLAMATION), '!' => addr_of!(EXCLAMATION),
',' => addr_of!(COMMA), ',' => addr_of!(COMMA),
_ => addr_of!(EMPTY), _ => addr_of!(EMPTY),
@ -167,43 +217,83 @@ pub fn draw_text<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
text: &str, text: &str,
fg: Rgb565, fg: Rgb565,
bg: Rgb565, bg: Rgb565,
position: Vec2, position: &mut Vec2,
scale: u16, scale: u16,
) { ) {
let mut curr_pos = position;
let kerning = 9 * scale; let kerning = 9 * scale;
let original_x = position.x; let original_x = position.x;
for line in text.split('\n') { for line in text.split('\n') {
for word in line.split(' ') { for word in line.split(' ') {
let word_length = word.len() as u16 * kerning; let word_length = word.len() as u16 * kerning;
if curr_pos.x + word_length > 240 { if position.x + word_length > 240 {
curr_pos.x = original_x; position.x = original_x;
curr_pos.y += kerning; position.y += kerning;
} }
for c in word.chars() { for c in word.chars() {
if curr_pos.x + kerning > 240 { draw_character(display, c, fg, bg, original_x, position, scale);
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;
} }
curr_pos.x += kerning; position.x += kerning;
} }
curr_pos.y += kerning; position.y += kerning;
position.x = original_x;
} }
} }
pub fn draw_character<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
display: &mut Display<T, DCPin, RSTPin>,
character: char,
fg: Rgb565,
bg: Rgb565,
original_x: u16,
position: &mut Vec2,
scale: u16,
) {
let kerning = 9 * scale;
if position.x + kerning > 240 {
position.x = original_x;
position.y += kerning;
}
if position.y + kerning > 240 {
position.y = 0;
}
Letter::from(character, fg, bg)
.iter()
.draw(display, *position, scale);
position.x += kerning;
}
pub fn draw_number<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
display: &mut Display<T, DCPin, RSTPin>,
number: u32,
fg: Rgb565,
bg: Rgb565,
position: &mut Vec2,
scale: u16,
) {
if number > 10 {
draw_number(display, number / 10, fg, bg, position, scale);
}
let character = match number % 10 {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
_ => 'X',
};
draw_character(display, character, fg, bg, position.x, position, scale);
}
pub struct Letter { pub struct Letter {
base: [u8; 8], base: [u8; 8],
pub fg: Rgb565, pub fg: Rgb565,

View File

@ -19,7 +19,7 @@ use panic_halt as _;
use crate::{ use crate::{
display::{Display, Rgb565, Vec2}, display::{Display, Rgb565, Vec2},
font::{Letter, draw_text}, font::{Letter, draw_number, 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,
}; };
@ -82,17 +82,23 @@ fn main() -> ! {
let images = [Image::from(addr_of!(LARGE_CAT_UNSAFE))]; let images = [Image::from(addr_of!(LARGE_CAT_UNSAFE))];
let len = images.len(); let len = images.len();
ufmt::uwriteln!(serial, "Drawing text").unwrap(); let mut position = Vec2 { x: 10, y: 10 };
draw_text( draw_text(
&mut display, &mut display,
"hi,my name is adafruit!nice to meet you!", "hi,my name is adafruit!nice to meet you!\n",
Rgb565::white(), Rgb565::white(),
Rgb565::black(), Rgb565::black(),
Vec2 { x: 10, y: 10 }, &mut position,
3,
);
draw_number(
&mut display,
1234,
Rgb565::white(),
Rgb565::black(),
&mut position,
3, 3,
); );
ufmt::uwriteln!(serial, "Text drawn!").unwrap();
loop { loop {
if button.poll() { if button.poll() {
@ -111,7 +117,7 @@ fn main() -> ! {
"you just lost the game!", "you just lost the game!",
Rgb565::white(), Rgb565::white(),
Rgb565::black(), Rgb565::black(),
Vec2 { x: 10, y: 10 }, &mut Vec2 { x: 10, y: 10 },
3, 3,
); );