rust: Pass in font mapping to k210-console

Support other unicode to font mappings (such as application specific
ones) besides cp437.
This commit is contained in:
Wladimir J. van der Laan 2020-02-03 16:13:33 +00:00
parent fa38e28491
commit 69636c4005
6 changed files with 14 additions and 16 deletions

View File

@ -5,6 +5,7 @@
#![no_main]
use k210_console::console::{Console, ScreenImage};
use k210_console::cp437;
use k210_console::cp437_8x8::{FONT, GLYPH_BY_FILL};
use k210_hal::Peripherals;
use k210_hal::prelude::*;
@ -107,7 +108,7 @@ fn main() -> ! {
dvp.set_auto(false);
let mut image: ScreenImage = [0; DISP_PIXELS / 2];
let mut console: Console = Console::new(&FONT, None);
let mut console: Console = Console::new(&cp437::to, &FONT, None);
writeln!(stdout, "Starting frame loop").unwrap();
loop {
dvp.get_image();

View File

@ -1,7 +1,6 @@
use core::fmt;
use k210_shared::board::lcd_colors::rgb565;
use crate::cp437;
use crate::palette_xterm256::PALETTE;
pub use k210_shared::board::def::{DISP_WIDTH,DISP_HEIGHT,DISP_PIXELS};
@ -13,10 +12,6 @@ const DEF_BG: u16 = rgb565(0, 0, 0);
pub type ScreenImage = [u32; DISP_PIXELS / 2];
/* TODO
* - pass in unicode to font mapping instead of hardcoding cp437
*/
/** Basic color math. */
#[derive(Copy, Clone)]
pub struct Color {
@ -118,6 +113,8 @@ enum Sgr {
/** Visual attributes of console */
pub struct Console {
/** Map unicode character to font index */
map_utf: &'static dyn Fn(char) -> u16,
/** Standard font */
pub font: &'static [[u8; 8]],
/** Color font */
@ -148,9 +145,9 @@ pub struct Console {
impl Console {
/** Create new, empty console */
pub fn new(font: &'static [[u8; 8]], color_font: Option<&'static [[u32; 32]]>) -> Console {
pub fn new(map_utf: &'static dyn Fn(char) -> u16, font: &'static [[u8; 8]], color_font: Option<&'static [[u32; 32]]>) -> Console {
Console {
font, color_font,
map_utf, font, color_font,
dirty: false,
cells: [Cell {
fg: DEF_FG,
@ -241,7 +238,7 @@ impl Console {
self.cells[usize::from(y) * usize::from(GRID_WIDTH) + usize::from(x)] = Cell {
fg: rgb565(fg.r, fg.g, fg.b),
bg: rgb565(bg.r, bg.g, bg.b),
ch: u16::from(cp437::to(ch)),
ch: (self.map_utf)(ch),
flags: 0,
};
}
@ -359,7 +356,7 @@ impl Console {
self.scroll();
}
self.put_raw(self.cursor_pos.x, self.cursor_pos.y, self.cur_fg, self.cur_bg, cp437::to(ch).into(), 0);
self.put_raw(self.cursor_pos.x, self.cursor_pos.y, self.cur_fg, self.cur_bg, (self.map_utf)(ch), 0);
self.cursor_pos.x += 1;
}
}

View File

@ -38,7 +38,7 @@ pub fn from(ch: u8) -> char {
FROM[usize::from(ch)]
}
pub fn to(ch: char) -> u8 {
pub fn to(ch: char) -> u16 {
match ch {
'\u{0000}' => 0x00, // NUL
'\u{263a}' => 0x01, // WHITE SMILING FACE

View File

@ -133,7 +133,7 @@ fn main() -> ! {
lcd.clear(lcd_colors::PURPLE);
let mut image: ScreenImage = [0; DISP_PIXELS / 2];
let mut console: Console = Console::new(&cp437_8x8::FONT, Some(&example_colorfont::FONT));
let mut console: Console = Console::new(&cp437::to, &cp437_8x8::FONT, Some(&example_colorfont::FONT));
/* Make a border */
let fg = Color::new(0x40, 0x40, 0x40);

View File

@ -22,7 +22,7 @@ use k210_shared::soc::spi::SPIExt;
use k210_shared::soc::sysctl;
use riscv_rt::entry;
use k210_console::console::{Console, ScreenImage, DISP_HEIGHT, DISP_WIDTH, DISP_PIXELS};
use k210_console::cp437_8x8;
use k210_console::{cp437, cp437_8x8};
use buffered_uart;
mod config;
@ -101,7 +101,7 @@ fn main() -> ! {
let mut lcd = LCD::new(spi, &dmac, dma_channel::CHANNEL0);
lcd.init();
lcd.set_direction(lcd::direction::YX_LRUD);
let mut console: Console = Console::new(&cp437_8x8::FONT, None);
let mut console: Console = Console::new(&cp437::to, &cp437_8x8::FONT, None);
writeln!(console, "\x1b[48;2;128;192;255;38;5;0m TERMINAL \x1b[0m \x1b[38;2;128;128;128m\x1b[0m").unwrap();

View File

@ -22,7 +22,7 @@ use k210_shared::soc::spi::SPIExt;
use k210_shared::soc::sysctl;
use riscv_rt::entry;
use k210_console::console::{Console, ScreenImage, DISP_HEIGHT, DISP_WIDTH, DISP_PIXELS};
use k210_console::cp437_8x8;
use k210_console::{cp437, cp437_8x8};
use buffered_uart;
mod config;
@ -101,7 +101,7 @@ fn main() -> ! {
let mut lcd = LCD::new(spi, &dmac, dma_channel::CHANNEL0);
lcd.init();
lcd.set_direction(lcd::direction::YX_LRUD);
let mut console: Console = Console::new(&cp437_8x8::FONT, None);
let mut console: Console = Console::new(&cp437::to, &cp437_8x8::FONT, None);
writeln!(console, "\x1b[48;2;128;192;255;38;5;0m WEATHER \x1b[0m \x1b[38;2;128;128;128m\x1b[0m").unwrap();