518 lines
17 KiB
Rust
518 lines
17 KiB
Rust
use alloc::collections::btree_map::BTreeMap;
|
|
use bitflags::bitflags;
|
|
|
|
use crate::canvas::{Canvas, RGB};
|
|
|
|
use super::{Font, Glyph};
|
|
|
|
// Sixteen Segments
|
|
// https://www.partsnotincluded.com/segmented-led-display-ascii-library/
|
|
//
|
|
// a1 a2
|
|
// f h i jb
|
|
// f i j b
|
|
// f hij b
|
|
// g1 g2
|
|
// e klm c
|
|
// e k l m c
|
|
// ek l mc
|
|
// d1 d2
|
|
|
|
bitflags! {
|
|
pub struct SixteenSegmentGlyph: u32 {
|
|
const NONE = 0;
|
|
const A1 = 0x0001;
|
|
const A2 = 0x0001 << 1;
|
|
const B = 0x0001 << 2;
|
|
const C = 0x0001 << 3;
|
|
const D1 = 0x0001 << 4;
|
|
const D2 = 0x0001 << 5;
|
|
const E = 0x0001 << 6;
|
|
const F = 0x0001 << 7;
|
|
const G1 = 0x0001 << 8;
|
|
const G2 = 0x0001 << 9;
|
|
const H = 0x0001 << 10;
|
|
const I = 0x0001 << 11;
|
|
const J = 0x0001 << 12;
|
|
const K = 0x0001 << 13;
|
|
const L = 0x0001 << 14;
|
|
const M = 0x0001 << 15;
|
|
const DOT = 0x0001 << 16;
|
|
}
|
|
}
|
|
|
|
pub struct SixteenSegmentFont(BTreeMap<char, SixteenSegmentGlyph>);
|
|
|
|
impl SixteenSegmentFont {
|
|
pub fn new() -> Self {
|
|
let mut font = BTreeMap::new();
|
|
font.insert(' ', SixteenSegmentGlyph::NONE);
|
|
font.insert(
|
|
'!',
|
|
SixteenSegmentGlyph::B | SixteenSegmentGlyph::C | SixteenSegmentGlyph::DOT,
|
|
);
|
|
font.insert('"', SixteenSegmentGlyph::B | SixteenSegmentGlyph::I);
|
|
font.insert(
|
|
'0',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::J
|
|
| SixteenSegmentGlyph::K,
|
|
);
|
|
font.insert(
|
|
'1',
|
|
SixteenSegmentGlyph::B | SixteenSegmentGlyph::C | SixteenSegmentGlyph::J,
|
|
);
|
|
font.insert(
|
|
'2',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'3',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'4',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'5',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'6',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'7',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C,
|
|
);
|
|
font.insert(
|
|
'8',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'9',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'A',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'B',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::G2
|
|
| SixteenSegmentGlyph::I
|
|
| SixteenSegmentGlyph::L,
|
|
);
|
|
font.insert(
|
|
'C',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F,
|
|
);
|
|
font.insert(
|
|
'D',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::I
|
|
| SixteenSegmentGlyph::L,
|
|
);
|
|
font.insert(
|
|
'E',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1,
|
|
);
|
|
font.insert(
|
|
'F',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1,
|
|
);
|
|
font.insert(
|
|
'G',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'H',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'I',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::I
|
|
| SixteenSegmentGlyph::L,
|
|
);
|
|
font.insert(
|
|
'J',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E,
|
|
);
|
|
font.insert(
|
|
'K',
|
|
SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::J
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'L',
|
|
SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F,
|
|
);
|
|
font.insert(
|
|
'M',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::H
|
|
| SixteenSegmentGlyph::J,
|
|
);
|
|
font.insert(
|
|
'N',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::H
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'O',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F,
|
|
);
|
|
font.insert(
|
|
'P',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'Q',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'R',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'S',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'T',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::I
|
|
| SixteenSegmentGlyph::L,
|
|
);
|
|
font.insert(
|
|
'U',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F,
|
|
);
|
|
font.insert(
|
|
'V',
|
|
SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::J
|
|
| SixteenSegmentGlyph::K,
|
|
);
|
|
font.insert(
|
|
'W',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::E
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::K
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'X',
|
|
SixteenSegmentGlyph::H
|
|
| SixteenSegmentGlyph::J
|
|
| SixteenSegmentGlyph::K
|
|
| SixteenSegmentGlyph::M,
|
|
);
|
|
font.insert(
|
|
'Y',
|
|
SixteenSegmentGlyph::B
|
|
| SixteenSegmentGlyph::C
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::F
|
|
| SixteenSegmentGlyph::G1
|
|
| SixteenSegmentGlyph::G2,
|
|
);
|
|
font.insert(
|
|
'Z',
|
|
SixteenSegmentGlyph::A1
|
|
| SixteenSegmentGlyph::A2
|
|
| SixteenSegmentGlyph::D1
|
|
| SixteenSegmentGlyph::D2
|
|
| SixteenSegmentGlyph::J
|
|
| SixteenSegmentGlyph::K,
|
|
);
|
|
Self(font)
|
|
}
|
|
}
|
|
|
|
impl Font<SixteenSegmentGlyph> for SixteenSegmentFont {
|
|
fn glyph(&self, c: char) -> &SixteenSegmentGlyph {
|
|
self.0.get(&c).unwrap()
|
|
}
|
|
}
|
|
|
|
impl Glyph for SixteenSegmentGlyph {
|
|
fn draw(&self, canvas: &mut impl Canvas, x: usize, y: usize, color: &RGB) {
|
|
if self.contains(SixteenSegmentGlyph::A1) {
|
|
canvas.set_pixel(x + 1, y, color);
|
|
canvas.set_pixel(x + 2, y, color);
|
|
canvas.set_pixel(x + 3, y, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::A2) {
|
|
canvas.set_pixel(x + 5, y, color);
|
|
canvas.set_pixel(x + 6, y, color);
|
|
canvas.set_pixel(x + 7, y, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::B) {
|
|
canvas.set_pixel(x + 8, y + 1, color);
|
|
canvas.set_pixel(x + 8, y + 2, color);
|
|
canvas.set_pixel(x + 8, y + 3, color);
|
|
canvas.set_pixel(x + 8, y + 4, color);
|
|
canvas.set_pixel(x + 8, y + 5, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::C) {
|
|
canvas.set_pixel(x + 8, y + 7, color);
|
|
canvas.set_pixel(x + 8, y + 8, color);
|
|
canvas.set_pixel(x + 8, y + 9, color);
|
|
canvas.set_pixel(x + 8, y + 10, color);
|
|
canvas.set_pixel(x + 8, y + 11, color);
|
|
}
|
|
|
|
if self.contains(SixteenSegmentGlyph::D1) {
|
|
canvas.set_pixel(x + 1, y + 12, color);
|
|
canvas.set_pixel(x + 2, y + 12, color);
|
|
canvas.set_pixel(x + 3, y + 12, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::D2) {
|
|
canvas.set_pixel(x + 5, y + 12, color);
|
|
canvas.set_pixel(x + 6, y + 12, color);
|
|
canvas.set_pixel(x + 7, y + 12, color);
|
|
}
|
|
|
|
if self.contains(SixteenSegmentGlyph::E) {
|
|
canvas.set_pixel(x, y + 7, color);
|
|
canvas.set_pixel(x, y + 8, color);
|
|
canvas.set_pixel(x, y + 9, color);
|
|
canvas.set_pixel(x, y + 10, color);
|
|
canvas.set_pixel(x, y + 11, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::F) {
|
|
canvas.set_pixel(x, y + 1, color);
|
|
canvas.set_pixel(x, y + 2, color);
|
|
canvas.set_pixel(x, y + 3, color);
|
|
canvas.set_pixel(x, y + 4, color);
|
|
canvas.set_pixel(x, y + 5, color);
|
|
}
|
|
|
|
if self.contains(SixteenSegmentGlyph::G1) {
|
|
canvas.set_pixel(x + 1, y + 6, color);
|
|
canvas.set_pixel(x + 2, y + 6, color);
|
|
canvas.set_pixel(x + 3, y + 6, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::G2) {
|
|
canvas.set_pixel(x + 5, y + 6, color);
|
|
canvas.set_pixel(x + 6, y + 6, color);
|
|
canvas.set_pixel(x + 7, y + 6, color);
|
|
}
|
|
|
|
if self.contains(SixteenSegmentGlyph::H) {
|
|
canvas.set_pixel(x + 1, y + 1, color);
|
|
canvas.set_pixel(x + 1, y + 2, color);
|
|
canvas.set_pixel(x + 2, y + 3, color);
|
|
canvas.set_pixel(x + 3, y + 4, color);
|
|
canvas.set_pixel(x + 3, y + 5, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::I) {
|
|
canvas.set_pixel(x + 4, y + 1, color);
|
|
canvas.set_pixel(x + 4, y + 2, color);
|
|
canvas.set_pixel(x + 4, y + 3, color);
|
|
canvas.set_pixel(x + 4, y + 4, color);
|
|
canvas.set_pixel(x + 4, y + 5, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::J) {
|
|
canvas.set_pixel(x + 7, y + 1, color);
|
|
canvas.set_pixel(x + 7, y + 2, color);
|
|
canvas.set_pixel(x + 6, y + 3, color);
|
|
canvas.set_pixel(x + 5, y + 4, color);
|
|
canvas.set_pixel(x + 5, y + 5, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::K) {
|
|
canvas.set_pixel(x + 3, y + 7, color);
|
|
canvas.set_pixel(x + 3, y + 8, color);
|
|
canvas.set_pixel(x + 2, y + 9, color);
|
|
canvas.set_pixel(x + 1, y + 10, color);
|
|
canvas.set_pixel(x + 1, y + 11, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::L) {
|
|
canvas.set_pixel(x + 4, y + 7, color);
|
|
canvas.set_pixel(x + 4, y + 8, color);
|
|
canvas.set_pixel(x + 4, y + 9, color);
|
|
canvas.set_pixel(x + 4, y + 10, color);
|
|
canvas.set_pixel(x + 4, y + 11, color);
|
|
}
|
|
if self.contains(SixteenSegmentGlyph::M) {
|
|
canvas.set_pixel(x + 5, y + 7, color);
|
|
canvas.set_pixel(x + 5, y + 8, color);
|
|
canvas.set_pixel(x + 6, y + 9, color);
|
|
canvas.set_pixel(x + 7, y + 10, color);
|
|
canvas.set_pixel(x + 7, y + 11, color);
|
|
}
|
|
}
|
|
}
|
|
|