Fill out an alphabet and animate the screen

This commit is contained in:
Savanni D'Gerinel 2025-03-08 01:49:43 -05:00
parent 596de6525f
commit 36eef45971
3 changed files with 374 additions and 39 deletions

View File

@ -19,6 +19,14 @@ pub struct RGB {
pub trait Canvas { pub trait Canvas {
fn set_pixel(&mut self, x: usize, y: usize, color: &RGB); fn set_pixel(&mut self, x: usize, y: usize, color: &RGB);
fn fill(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) {
for x in x1..x2 + 1 {
for y in y1..y2 + 1 {
self.set_pixel(x, y, color);
}
}
}
fn square(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) { fn square(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) {
for x in x1..x2 + 1 { for x in x1..x2 + 1 {
self.set_pixel(x, y1, color); self.set_pixel(x, y1, color);

View File

@ -4,7 +4,7 @@ use crate::canvas::{Canvas, RGB};
use super::{Font, Glyph}; use super::{Font, Glyph};
pub struct BitmapGlyph([u8; 8]); pub struct BitmapGlyph([u8; 7]);
pub struct BitmapFont(BTreeMap<char, BitmapGlyph>); pub struct BitmapFont(BTreeMap<char, BitmapGlyph>);
@ -12,15 +12,26 @@ impl BitmapFont {
pub fn new() -> Self { pub fn new() -> Self {
let mut font = BTreeMap::new(); let mut font = BTreeMap::new();
font.insert(' ', BitmapGlyph([ font.insert(' ', BitmapGlyph([
0b10101, 0b00000,
0b01010, 0b00000,
0b10101, 0b00000,
0b01010, 0b00000,
0b10101, 0b00000,
0b01010, 0b00000,
0b10101, 0b00000,
0b01010,
])); ]));
font.insert(
':',
BitmapGlyph([
0b00000,
0b00100,
0b00100,
0b00000,
0b00100,
0b00100,
0b00000,
]),
);
font.insert( font.insert(
'/', '/',
BitmapGlyph([ BitmapGlyph([
@ -28,7 +39,6 @@ impl BitmapFont {
0b00010, 0b00010,
0b00010, 0b00010,
0b00100, 0b00100,
0b00100,
0b01000, 0b01000,
0b01000, 0b01000,
0b10000, 0b10000,
@ -41,7 +51,6 @@ impl BitmapFont {
0b10001, 0b10001,
0b10001, 0b10001,
0b10101, 0b10101,
0b10101,
0b10001, 0b10001,
0b10001, 0b10001,
0b01110, 0b01110,
@ -50,19 +59,37 @@ impl BitmapFont {
font.insert( font.insert(
'1', '1',
BitmapGlyph([ BitmapGlyph([
0b00001, 0b00011, 0b00101, 0b00001, 0b00001, 0b00001, 0b00001, 0b00001, 0b00001,
0b00011,
0b00101,
0b00001,
0b00001,
0b00001,
0b00001,
]), ]),
); );
font.insert( font.insert(
'2', '2',
BitmapGlyph([ BitmapGlyph([
0b01110, 0b10001, 0b00001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111, 0b01110,
0b10001,
0b00001,
0b00010,
0b00100,
0b01000,
0b11111,
]), ]),
); );
font.insert( font.insert(
'3', '3',
BitmapGlyph([ BitmapGlyph([
0b01110, 0b10001, 0b00001, 0b01110, 0b00001, 0b00001, 0b10001, 0b01110, 0b01110,
0b10001,
0b00001,
0b01110,
0b00001,
0b10001,
0b01110,
]), ]),
); );
font.insert( font.insert(
@ -75,7 +102,6 @@ impl BitmapFont {
0b00001, 0b00001,
0b00001, 0b00001,
0b00001, 0b00001,
0b00001,
]), ]),
); );
font.insert( font.insert(
@ -86,7 +112,6 @@ impl BitmapFont {
0b10000, 0b10000,
0b11111, 0b11111,
0b00001, 0b00001,
0b00001,
0b10001, 0b10001,
0b01110, 0b01110,
]), ]),
@ -97,7 +122,6 @@ impl BitmapFont {
0b01110, 0b01110,
0b10001, 0b10001,
0b10000, 0b10000,
0b10000,
0b11110, 0b11110,
0b10001, 0b10001,
0b10001, 0b10001,
@ -109,7 +133,6 @@ impl BitmapFont {
BitmapGlyph([ BitmapGlyph([
0b11111, 0b11111,
0b00001, 0b00001,
0b00001,
0b00010, 0b00010,
0b00110, 0b00110,
0b00100, 0b00100,
@ -124,7 +147,6 @@ impl BitmapFont {
0b10001, 0b10001,
0b10001, 0b10001,
0b01110, 0b01110,
0b01110,
0b10001, 0b10001,
0b10001, 0b10001,
0b01110, 0b01110,
@ -136,13 +158,324 @@ impl BitmapFont {
0b01110, 0b01110,
0b10001, 0b10001,
0b10001, 0b10001,
0b10001,
0b01111, 0b01111,
0b00001, 0b00001,
0b00001, 0b00001,
0b01110, 0b01110,
]), ]),
); );
font.insert(
'A',
BitmapGlyph([
0b00100,
0b01010,
0b01010,
0b10001,
0b11111,
0b10001,
0b10001,
]),
);
font.insert(
'B',
BitmapGlyph([
0b11110,
0b10001,
0b10001,
0b1111 ,
0b10001,
0b10001,
0b11110,
]),
);
font.insert(
'C',
BitmapGlyph([
0b01110,
0b10001,
0b10000,
0b10000,
0b10000,
0b10001,
0b01110,
]),
);
font.insert(
'D',
BitmapGlyph([
0b11110,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b11110,
]),
);
font.insert(
'E',
BitmapGlyph([
0b11111,
0b10000,
0b10000,
0b11111,
0b10000,
0b10000,
0b11111,
]),
);
font.insert(
'F',
BitmapGlyph([
0b11111,
0b10000,
0b10000,
0b11110,
0b10000,
0b10000,
0b10000,
]),
);
font.insert(
'G',
BitmapGlyph([
0b01110,
0b10001,
0b10000,
0b10011,
0b10001,
0b10001,
0b01110,
]),
);
font.insert(
'H',
BitmapGlyph([
0b10001,
0b10001,
0b10001,
0b11111,
0b10001,
0b10001,
0b10001,
]),
);
font.insert(
'I',
BitmapGlyph([
0b11111,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b11111,
]),
);
font.insert(
'J',
BitmapGlyph([
0b00111,
0b00001,
0b00001,
0b00001,
0b00001,
0b10001,
0b01110,
]),
);
font.insert(
'K',
BitmapGlyph([
0b10001,
0b10010,
0b10100,
0b11000,
0b10100,
0b10010,
0b10001,
]),
);
font.insert(
'L',
BitmapGlyph([
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b11111,
]),
);
font.insert(
'M',
BitmapGlyph([
0b10001,
0b11011,
0b10101,
0b10001,
0b10001,
0b10001,
0b10001,
]),
);
font.insert(
'N',
BitmapGlyph([
0b10001,
0b11001,
0b11001,
0b10101,
0b10011,
0b10011,
0b10001,
]),
);
font.insert(
'O',
BitmapGlyph([
0b01110,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b01110,
]),
);
font.insert(
'P',
BitmapGlyph([
0b11110,
0b10001,
0b10001,
0b11110,
0b10000,
0b10000,
0b10000,
]),
);
font.insert(
'Q',
BitmapGlyph([
0b01110,
0b10001,
0b10001,
0b10001,
0b10101,
0b10011,
0b01110,
]),
);
font.insert(
'R',
BitmapGlyph([
0b11110,
0b10001,
0b10001,
0b11110,
0b10100,
0b10010,
0b10001,
]),
);
font.insert(
'S',
BitmapGlyph([
0b01110,
0b10001,
0b10000,
0b01110,
0b00001,
0b10001,
0b01110,
]),
);
font.insert(
'T',
BitmapGlyph([
0b11111,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
]),
);
font.insert(
'U',
BitmapGlyph([
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b01110,
]),
);
font.insert(
'V',
BitmapGlyph([
0b10001,
0b10001,
0b10001,
0b10001,
0b01010,
0b01010,
0b00100,
]),
);
font.insert(
'W',
BitmapGlyph([
0b10001,
0b10001,
0b10001,
0b10001,
0b10101,
0b10101,
0b01010,
]),
);
font.insert(
'X',
BitmapGlyph([
0b10001,
0b10001,
0b01010,
0b00100,
0b01010,
0b10001,
0b10001,
]),
);
font.insert(
'Y',
BitmapGlyph([
0b10001,
0b10001,
0b01010,
0b00100,
0b00100,
0b00100,
0b00100,
]),
);
font.insert(
'Z',
BitmapGlyph([
0b11111,
0b00001,
0b00010,
0b00100,
0b01000,
0b10000,
0b11111,
]),
);
Self(font) Self(font)
} }
} }
@ -155,7 +488,7 @@ impl Font<BitmapGlyph> for BitmapFont {
impl Glyph for BitmapGlyph { impl Glyph for BitmapGlyph {
fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) { fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) {
for row in 0..8 { for row in 0..7 {
if self.0[row] & (1 << 4) > 0 { if self.0[row] & (1 << 4) > 0 {
canvas.set_pixel(x, y + row, color); canvas.set_pixel(x, y + row, color);
} }
@ -175,6 +508,6 @@ impl Glyph for BitmapGlyph {
} }
fn extents(&self) -> (usize, usize) { fn extents(&self) -> (usize, usize) {
(5, 8) (5, 7)
} }
} }

View File

@ -3,6 +3,7 @@
extern crate alloc; extern crate alloc;
use alloc::fmt::format;
use embedded_alloc::LlffHeap as Heap; use embedded_alloc::LlffHeap as Heap;
use embedded_hal::{delay::DelayNs, digital::OutputPin}; use embedded_hal::{delay::DelayNs, digital::OutputPin};
use fugit::RateExtU32; use fugit::RateExtU32;
@ -64,7 +65,6 @@ unsafe fn main() -> ! {
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
} }
let font_sixteen = SixteenSegmentFont::new();
let font_bitmap = BitmapFont::new(); let font_bitmap = BitmapFont::new();
// rp_pico::pac::Peripherals is a reference to physical hardware defined on the Pico. // rp_pico::pac::Peripherals is a reference to physical hardware defined on the Pico.
@ -149,24 +149,17 @@ unsafe fn main() -> ! {
*/ */
let mut canvas = FrameBuf::new(); let mut canvas = FrameBuf::new();
let white = RGB { r: 63, g: 63, b: 63 }; let white = RGB { r: 63, g: 63, b: 63 };
let mut count = 0;
loop { loop {
print(&mut canvas, &font_sixteen, 1, 1, " !\"#$%&'<>*+,-./", &RGB{ r: 63, g: 63, b: 63 }); canvas.fill(0, 10, 170, 20, &RGB{r: 0, g: 0, b: 0 });
print(&mut canvas, &font_sixteen, 1, 21, "0123456789| = ?", &RGB{ r: 63, g: 63, b: 63 }); print(&mut canvas, &font_bitmap, 1, 10, &format(format_args!("COUNT: {:03}", count)), &RGB{ r: 32, g: 32, b: 63 });
print(&mut canvas, &font_sixteen, 1, 41, "@ABCDEFGHIJKLMNO", &RGB{ r: 63, g: 63, b: 63 }); print(&mut canvas, &font_bitmap, 1, 200, " !\"#$%&'<>*+,-./", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_sixteen, 1, 61, "PQRSTUVWXYZ[\\]^_", &RGB{ r: 63, g: 63, b: 63 }); print(&mut canvas, &font_bitmap, 1, 220, "0123456789|: = ?", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_sixteen, 1, 81, "`abcdefghijklmno", &RGB{ r: 63, g: 63, b: 63 }); print(&mut canvas, &font_bitmap, 1, 240, "@ABCDEFGHIJKLMNO", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_sixteen, 1, 101, "pqrstuvwxyz{|}", &RGB{ r: 63, g: 63, b: 63 }); print(&mut canvas, &font_bitmap, 1, 260, "PQRSTUVWXYZ[\\]^_", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 280, "`abcdefghijklmno", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 300, "pqrstuvwxyz{|}", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 121, " !\"#$%&'<>*+,-./", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 141, "0123456789| = ?", &RGB{ r: 63, g: 63, b: 63 });
/*
print(&mut canvas, &font_bitmap, 1, 161, "@ABCDEFGHIJKLMNO", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 181, "PQRSTUVWXYZ[\\]^_", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 201, "`abcdefghijklmno", &RGB{ r: 63, g: 63, b: 63 });
print(&mut canvas, &font_bitmap, 1, 221, "pqrstuvwxyz{|}", &RGB{ r: 63, g: 63, b: 63 });
*/
// canvas.square(10, 70, 160, 310, &white); // canvas.square(10, 70, 160, 310, &white);
@ -177,6 +170,7 @@ unsafe fn main() -> ! {
display.send_buf(canvas.buf); display.send_buf(canvas.buf);
let _ = led.set_low(); let _ = led.set_low();
} }
count = count + 1;
/* /*
for x in 80..90 { for x in 80..90 {
for y in 155..165 { for y in 155..165 {