From 69636c40056c5fa71894f168516d41f99d1c3ac3 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 3 Feb 2020 16:13:33 +0000 Subject: [PATCH] rust: Pass in font mapping to k210-console Support other unicode to font mappings (such as application specific ones) besides cp437. --- rust/glyph-mapping/src/main.rs | 3 ++- rust/k210-console/src/console.rs | 15 ++++++--------- rust/k210-console/src/cp437.rs | 2 +- rust/k210-console/src/main.rs | 2 +- rust/term-server/src/main.rs | 4 ++-- rust/weather/src/main.rs | 4 ++-- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/rust/glyph-mapping/src/main.rs b/rust/glyph-mapping/src/main.rs index 97e6e77..3a3ea9c 100644 --- a/rust/glyph-mapping/src/main.rs +++ b/rust/glyph-mapping/src/main.rs @@ -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(); diff --git a/rust/k210-console/src/console.rs b/rust/k210-console/src/console.rs index caea731..8118b61 100644 --- a/rust/k210-console/src/console.rs +++ b/rust/k210-console/src/console.rs @@ -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; } } diff --git a/rust/k210-console/src/cp437.rs b/rust/k210-console/src/cp437.rs index 9e8f244..c2b6adc 100644 --- a/rust/k210-console/src/cp437.rs +++ b/rust/k210-console/src/cp437.rs @@ -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 diff --git a/rust/k210-console/src/main.rs b/rust/k210-console/src/main.rs index 11eef38..67ab9c9 100644 --- a/rust/k210-console/src/main.rs +++ b/rust/k210-console/src/main.rs @@ -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); diff --git a/rust/term-server/src/main.rs b/rust/term-server/src/main.rs index 835098f..9f1b22d 100644 --- a/rust/term-server/src/main.rs +++ b/rust/term-server/src/main.rs @@ -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(); diff --git a/rust/weather/src/main.rs b/rust/weather/src/main.rs index 01e00d7..fd36f70 100644 --- a/rust/weather/src/main.rs +++ b/rust/weather/src/main.rs @@ -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();