From 40de106f46820ead1ecbc8694604acf4721d289e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 11 May 2024 12:43:17 +0200 Subject: [PATCH] add byte grid --- src/byte_grid.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/command.rs | 36 +++++++++++++++++++++--------------- src/lib.rs | 10 ++++++---- 3 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 src/byte_grid.rs diff --git a/src/byte_grid.rs b/src/byte_grid.rs new file mode 100644 index 0000000..ce7b76b --- /dev/null +++ b/src/byte_grid.rs @@ -0,0 +1,43 @@ +#[derive(Debug)] +pub struct ByteGrid { + pub width: usize, + pub height: usize, + data: Vec, +} + +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> for ByteGrid { + fn into(self) -> Vec { + self.data + } +} diff --git a/src/command.rs b/src/command.rs index 243d4db..4583fe8 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,6 +1,4 @@ -use crate::{BitVec, Packet, PixelGrid, TILE_SIZE}; -use crate::command_codes::CommandCode; -use crate::packet::Header; +use crate::{BitVec, ByteGrid, CommandCode, Header, Packet, PixelGrid, TILE_SIZE}; /// A window #[derive(Debug, Copy, Clone)] @@ -24,7 +22,7 @@ pub enum Command { Clear, HardReset, FadeOut, - CharBrightness(Window, Vec), + CharBrightness(Origin, ByteGrid), Brightness(Brightness), #[deprecated] BitmapLegacy, @@ -32,7 +30,7 @@ pub enum Command { BitmapLinearAnd(Offset, BitVec), BitmapLinearOr(Offset, BitVec), BitmapLinearXor(Offset, BitVec), - Cp437Data(Window, Vec), + Cp437Data(Origin, ByteGrid), BitmapLinearWin(Origin, PixelGrid), } @@ -58,8 +56,10 @@ impl Into for Command { #[allow(deprecated)] Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy), - Command::CharBrightness(window, payload) => { - window_and_payload(CommandCode::CharBrightness, window, payload) + Command::CharBrightness(origin, grid) => { + window_and_payload(CommandCode::CharBrightness, + Window(origin, Size(grid.width as u16, grid.height as u16)), + grid.into()) } Command::Brightness(brightness) => { Packet(Header(CommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness)) @@ -72,7 +72,11 @@ impl Into for Command { debug_assert_eq!(pixel_x % 8, 0); debug_assert_eq!(pixels.width % 8, 0); 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()) } Command::BitmapLinearAnd(offset, bits) => { @@ -84,8 +88,10 @@ impl Into for Command { Command::BitmapLinearXor(offset, bits) => { offset_and_payload(CommandCode::BitmapLinearXor, offset, bits.into()) } - Command::Cp437Data(window, payload) => { - window_and_payload(CommandCode::Cp437Data, window, payload) + Command::Cp437Data(origin, grid) => { + 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 { } fn check_linear_bitmap(packet: &Packet) -> Option { - let Packet(Header(_, offset, length, sub, reserved), payload) = packet; + let Packet(Header(_, _, length, sub, reserved), payload) = packet; if *reserved != 0 { return Some(TryFromPacketError::ExtraneousHeaderValues); } @@ -168,14 +174,14 @@ impl TryFrom for Command { } CommandCode::Cp437Data => { Ok(Command::Cp437Data( - Window(Origin(*a, *b), Size(*c, *d)), - payload.clone(), + Origin(*a, *b), + ByteGrid::load(*c as usize, *d as usize, payload), )) } CommandCode::CharBrightness => { Ok(Command::CharBrightness( - Window(Origin(*a, *b), Size(*c, *d)), - payload.clone(), + Origin(*a, *b), + ByteGrid::load(*c as usize, *d as usize, payload), )) } #[allow(deprecated)] diff --git a/src/lib.rs b/src/lib.rs index 1289b41..132b1d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,13 +4,15 @@ mod bit_vec; mod packet; mod command; mod command_codes; +mod byte_grid; -pub use crate::connection::{Connection}; -pub use crate::pixel_grid::{PixelGrid}; -pub use crate::bit_vec::{BitVec}; +pub use crate::connection::Connection; +pub use crate::pixel_grid::PixelGrid; +pub use crate::bit_vec::BitVec; pub use crate::packet::{Packet, Header, Payload}; 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_WIDTH: u16 = 56;