rust: Split out some structures from k210-console

This commit is contained in:
Wladimir J. van der Laan 2020-02-03 16:39:14 +00:00
parent 69636c4005
commit 0fbf997b0d
4 changed files with 62 additions and 57 deletions

View File

@ -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)
}
}

View File

@ -1,6 +1,7 @@
use core::fmt; use core::fmt;
use k210_shared::board::lcd_colors::rgb565; use k210_shared::board::lcd_colors::rgb565;
use crate::coord::Coord;
use crate::palette_xterm256::PALETTE; use crate::palette_xterm256::PALETTE;
pub use k210_shared::board::def::{DISP_WIDTH,DISP_HEIGHT,DISP_PIXELS}; 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]; pub type ScreenImage = [u32; DISP_PIXELS / 2];
/** Basic color math. */ pub use crate::color::Color;
#[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 }
}
}
/** Cell flags. */ /** Cell flags. */
#[allow(non_snake_case)] #[allow(non_snake_case)]

View File

@ -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 }
}
}

View File

@ -1,6 +1,8 @@
#![no_std] #![no_std]
pub mod color;
pub mod console; pub mod console;
pub mod coord;
pub mod cp437; pub mod cp437;
pub mod cp437_8x8; pub mod cp437_8x8;
pub mod palette_xterm256; pub mod palette_xterm256;