add byte grid

This commit is contained in:
Vinzenz Schroeter 2024-05-11 12:43:17 +02:00
parent 8ceaef72fa
commit 40de106f46
3 changed files with 70 additions and 19 deletions

43
src/byte_grid.rs Normal file
View file

@ -0,0 +1,43 @@
#[derive(Debug)]
pub struct ByteGrid {
pub width: usize,
pub height: usize,
data: Vec<u8>,
}
impl ByteGrid {
pub fn new(width: usize, height: usize) -> Self {
Self {
data: vec![0; width * height],
width,
height,
}
}
pub fn load(width: usize, height: usize, data: &[u8]) -> Self {
assert_eq!(width * height, data.len());
Self {
data: Vec::from(data),
width,
height,
}
}
pub fn get(&self, x: usize, y: usize) -> u8 {
self.data[x + y * self.width]
}
pub fn set(&mut self, x: usize, y: usize, value: u8) {
self.data[x + y * self.width] = value;
}
pub fn fill(&mut self, value: u8){
self.data.fill(value)
}
}
impl Into<Vec<u8>> for ByteGrid {
fn into(self) -> Vec<u8> {
self.data
}
}

View file

@ -1,6 +1,4 @@
use crate::{BitVec, Packet, PixelGrid, TILE_SIZE}; use crate::{BitVec, ByteGrid, CommandCode, Header, Packet, PixelGrid, TILE_SIZE};
use crate::command_codes::CommandCode;
use crate::packet::Header;
/// A window /// A window
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -24,7 +22,7 @@ pub enum Command {
Clear, Clear,
HardReset, HardReset,
FadeOut, FadeOut,
CharBrightness(Window, Vec<Brightness>), CharBrightness(Origin, ByteGrid),
Brightness(Brightness), Brightness(Brightness),
#[deprecated] #[deprecated]
BitmapLegacy, BitmapLegacy,
@ -32,7 +30,7 @@ pub enum Command {
BitmapLinearAnd(Offset, BitVec), BitmapLinearAnd(Offset, BitVec),
BitmapLinearOr(Offset, BitVec), BitmapLinearOr(Offset, BitVec),
BitmapLinearXor(Offset, BitVec), BitmapLinearXor(Offset, BitVec),
Cp437Data(Window, Vec<u8>), Cp437Data(Origin, ByteGrid),
BitmapLinearWin(Origin, PixelGrid), BitmapLinearWin(Origin, PixelGrid),
} }
@ -58,8 +56,10 @@ impl Into<Packet> for Command {
#[allow(deprecated)] #[allow(deprecated)]
Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy), Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy),
Command::CharBrightness(window, payload) => { Command::CharBrightness(origin, grid) => {
window_and_payload(CommandCode::CharBrightness, window, payload) window_and_payload(CommandCode::CharBrightness,
Window(origin, Size(grid.width as u16, grid.height as u16)),
grid.into())
} }
Command::Brightness(brightness) => { Command::Brightness(brightness) => {
Packet(Header(CommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness)) Packet(Header(CommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness))
@ -72,7 +72,11 @@ impl Into<Packet> for Command {
debug_assert_eq!(pixel_x % 8, 0); debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0); debug_assert_eq!(pixels.width % 8, 0);
Packet( Packet(
Header(CommandCode::BitmapLinearWin.to_primitive(), pixel_x / TILE_SIZE, pixel_y, pixels.width as u16 / TILE_SIZE, pixels.height as u16), Header(CommandCode::BitmapLinearWin.to_primitive(),
pixel_x / TILE_SIZE,
pixel_y,
pixels.width as u16 / TILE_SIZE,
pixels.height as u16),
pixels.into()) pixels.into())
} }
Command::BitmapLinearAnd(offset, bits) => { Command::BitmapLinearAnd(offset, bits) => {
@ -84,8 +88,10 @@ impl Into<Packet> for Command {
Command::BitmapLinearXor(offset, bits) => { Command::BitmapLinearXor(offset, bits) => {
offset_and_payload(CommandCode::BitmapLinearXor, offset, bits.into()) offset_and_payload(CommandCode::BitmapLinearXor, offset, bits.into())
} }
Command::Cp437Data(window, payload) => { Command::Cp437Data(origin, grid) => {
window_and_payload(CommandCode::Cp437Data, window, payload) window_and_payload(CommandCode::Cp437Data,
Window(origin, Size(grid.width as u16, grid.height as u16)),
grid.into())
} }
} }
} }
@ -118,7 +124,7 @@ fn check_command_only(packet: &Packet) -> Option<TryFromPacketError> {
} }
fn check_linear_bitmap(packet: &Packet) -> Option<TryFromPacketError> { fn check_linear_bitmap(packet: &Packet) -> Option<TryFromPacketError> {
let Packet(Header(_, offset, length, sub, reserved), payload) = packet; let Packet(Header(_, _, length, sub, reserved), payload) = packet;
if *reserved != 0 { if *reserved != 0 {
return Some(TryFromPacketError::ExtraneousHeaderValues); return Some(TryFromPacketError::ExtraneousHeaderValues);
} }
@ -168,14 +174,14 @@ impl TryFrom<Packet> for Command {
} }
CommandCode::Cp437Data => { CommandCode::Cp437Data => {
Ok(Command::Cp437Data( Ok(Command::Cp437Data(
Window(Origin(*a, *b), Size(*c, *d)), Origin(*a, *b),
payload.clone(), ByteGrid::load(*c as usize, *d as usize, payload),
)) ))
} }
CommandCode::CharBrightness => { CommandCode::CharBrightness => {
Ok(Command::CharBrightness( Ok(Command::CharBrightness(
Window(Origin(*a, *b), Size(*c, *d)), Origin(*a, *b),
payload.clone(), ByteGrid::load(*c as usize, *d as usize, payload),
)) ))
} }
#[allow(deprecated)] #[allow(deprecated)]

View file

@ -4,13 +4,15 @@ mod bit_vec;
mod packet; mod packet;
mod command; mod command;
mod command_codes; mod command_codes;
mod byte_grid;
pub use crate::connection::{Connection}; pub use crate::connection::Connection;
pub use crate::pixel_grid::{PixelGrid}; pub use crate::pixel_grid::PixelGrid;
pub use crate::bit_vec::{BitVec}; pub use crate::bit_vec::BitVec;
pub use crate::packet::{Packet, Header, Payload}; pub use crate::packet::{Packet, Header, Payload};
pub use crate::command::{Command, Size, Origin, Window}; pub use crate::command::{Command, Size, Origin, Window};
pub use crate::command_codes::{CommandCode}; pub use crate::command_codes::CommandCode;
pub use crate::byte_grid::ByteGrid;
pub const TILE_SIZE: u16 = 8; pub const TILE_SIZE: u16 = 8;
pub const TILE_WIDTH: u16 = 56; pub const TILE_WIDTH: u16 = 56;