diff --git a/rust/k210-console/src/color.rs b/rust/k210-console/src/color.rs new file mode 100644 index 0000000..a3eee29 --- /dev/null +++ b/rust/k210-console/src/color.rs @@ -0,0 +1,46 @@ +use k210_shared::board::lcd_colors::rgb565; + +/** Basic color math. */ +#[derive(Copy, Clone)] +pub struct Color { + pub r: u8, + pub g: u8, + pub b: u8, + #[allow(dead_code)] + pub a: u8, +} + +impl Color { + pub const fn new(r: u8, g: u8, b: u8) -> Color { + Color { r, g, b, a: 255 } + } + + pub const fn new_rgba(r: u8, g: u8, b: u8, a: u8) -> Color { + Color { r, g, b, a: a } + } + + pub const fn from_rgb565(val: u16) -> Color { + let rs = ((val >> 11) & 0x1f) as u8; + let gs = ((val >> 5) & 0x3f) as u8; + let bs = ((val >> 0) & 0x1f) as u8; + Color { + r: (rs << 3) | (rs >> 2), + g: (gs << 2) | (gs >> 4), + b: (bs << 3) | (bs >> 2), + a: 255, + } + } + + pub const fn from_rgba32(val: u32) -> Color { + Color { + r: ((val >> 24) & 0xff) as u8, + g: ((val >> 16) & 0xff) as u8, + b: ((val >> 8) & 0xff) as u8, + a: ((val >> 0) & 0xff) as u8, + } + } + + pub const fn to_rgb565(&self) -> u16 { + rgb565(self.r, self.g, self.b) + } +} diff --git a/rust/k210-console/src/console.rs b/rust/k210-console/src/console.rs index 8118b61..23fc1e0 100644 --- a/rust/k210-console/src/console.rs +++ b/rust/k210-console/src/console.rs @@ -1,6 +1,7 @@ use core::fmt; use k210_shared::board::lcd_colors::rgb565; +use crate::coord::Coord; use crate::palette_xterm256::PALETTE; pub use k210_shared::board::def::{DISP_WIDTH,DISP_HEIGHT,DISP_PIXELS}; @@ -12,63 +13,7 @@ const DEF_BG: u16 = rgb565(0, 0, 0); pub type ScreenImage = [u32; DISP_PIXELS / 2]; -/** Basic color math. */ -#[derive(Copy, Clone)] -pub struct Color { - r: u8, - g: u8, - b: u8, - #[allow(dead_code)] - a: u8, -} - -impl Color { - pub const fn new(r: u8, g: u8, b: u8) -> Color { - Color { r, g, b, a: 255 } - } - - pub const fn new_rgba(r: u8, g: u8, b: u8, a: u8) -> Color { - Color { r, g, b, a: a } - } - - pub const fn from_rgb565(val: u16) -> Color { - let rs = ((val >> 11) & 0x1f) as u8; - let gs = ((val >> 5) & 0x3f) as u8; - let bs = ((val >> 0) & 0x1f) as u8; - Color { - r: (rs << 3) | (rs >> 2), - g: (gs << 2) | (gs >> 4), - b: (bs << 3) | (bs >> 2), - a: 255, - } - } - - pub const fn from_rgba32(val: u32) -> Color { - Color { - r: ((val >> 24) & 0xff) as u8, - g: ((val >> 16) & 0xff) as u8, - b: ((val >> 8) & 0xff) as u8, - a: ((val >> 0) & 0xff) as u8, - } - } - - pub const fn to_rgb565(&self) -> u16 { - rgb565(self.r, self.g, self.b) - } -} - -/** Integer screen coordinate. */ -#[derive(Copy, Clone)] -pub struct Coord { - x: u16, - y: u16, -} - -impl Coord { - fn new(x: u16, y: u16) -> Self { - Self { x, y } - } -} +pub use crate::color::Color; /** Cell flags. */ #[allow(non_snake_case)] diff --git a/rust/k210-console/src/coord.rs b/rust/k210-console/src/coord.rs new file mode 100644 index 0000000..0fe9d25 --- /dev/null +++ b/rust/k210-console/src/coord.rs @@ -0,0 +1,12 @@ +/** Integer screen coordinate. */ +#[derive(Copy, Clone)] +pub struct Coord { + pub x: u16, + pub y: u16, +} + +impl Coord { + pub fn new(x: u16, y: u16) -> Self { + Self { x, y } + } +} diff --git a/rust/k210-console/src/lib.rs b/rust/k210-console/src/lib.rs index c99faf0..e58e675 100644 --- a/rust/k210-console/src/lib.rs +++ b/rust/k210-console/src/lib.rs @@ -1,6 +1,8 @@ #![no_std] +pub mod color; pub mod console; +pub mod coord; pub mod cp437; pub mod cp437_8x8; pub mod palette_xterm256;