add byte grid
This commit is contained in:
		
							parent
							
								
									8ceaef72fa
								
							
						
					
					
						commit
						40de106f46
					
				
					 3 changed files with 70 additions and 19 deletions
				
			
		
							
								
								
									
										43
									
								
								src/byte_grid.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/byte_grid.rs
									
										
									
									
									
										Normal 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 | ||||
|     } | ||||
| } | ||||
|  | @ -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<Brightness>), | ||||
|     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<u8>), | ||||
|     Cp437Data(Origin, ByteGrid), | ||||
|     BitmapLinearWin(Origin, PixelGrid), | ||||
| } | ||||
| 
 | ||||
|  | @ -58,8 +56,10 @@ impl Into<Packet> 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<Packet> 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<Packet> 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<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 { | ||||
|         return Some(TryFromPacketError::ExtraneousHeaderValues); | ||||
|     } | ||||
|  | @ -168,14 +174,14 @@ impl TryFrom<Packet> 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)] | ||||
|  |  | |||
							
								
								
									
										10
									
								
								src/lib.rs
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								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; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter