reformat with max width
This commit is contained in:
parent
0a3f400e92
commit
362426c758
13 changed files with 225 additions and 122 deletions
179
src/command.rs
179
src/command.rs
|
@ -1,7 +1,6 @@
|
|||
use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
|
||||
use crate::command_codes::{CommandCode, CompressionCode};
|
||||
use crate::compression::{into_compressed, into_decompressed};
|
||||
|
||||
use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
|
||||
|
||||
/// An origin marks the top left position of the
|
||||
/// data sent to the display.
|
||||
|
@ -46,45 +45,77 @@ impl Into<Packet> for Command {
|
|||
Command::FadeOut => command_code_only(CommandCode::FadeOut),
|
||||
Command::HardReset => command_code_only(CommandCode::HardReset),
|
||||
#[allow(deprecated)]
|
||||
Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy),
|
||||
Command::CharBrightness(origin, grid) => {
|
||||
origin_size_payload(CommandCode::CharBrightness,
|
||||
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))
|
||||
Command::BitmapLegacy => {
|
||||
command_code_only(CommandCode::BitmapLegacy)
|
||||
}
|
||||
Command::CharBrightness(origin, grid) => origin_size_payload(
|
||||
CommandCode::CharBrightness,
|
||||
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],
|
||||
),
|
||||
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels) => {
|
||||
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),
|
||||
pixels.into())
|
||||
Header(
|
||||
CommandCode::BitmapLinearWin.to_primitive(),
|
||||
pixel_x / TILE_SIZE,
|
||||
pixel_y,
|
||||
pixels.width as u16 / TILE_SIZE,
|
||||
pixels.height as u16,
|
||||
),
|
||||
pixels.into(),
|
||||
)
|
||||
}
|
||||
Command::BitmapLinear(offset, bits, compression) => {
|
||||
bitmap_linear_into_packet(CommandCode::BitmapLinear, offset, compression, bits.into())
|
||||
bitmap_linear_into_packet(
|
||||
CommandCode::BitmapLinear,
|
||||
offset,
|
||||
compression,
|
||||
bits.into(),
|
||||
)
|
||||
}
|
||||
Command::BitmapLinearAnd(offset, bits, compression) => {
|
||||
bitmap_linear_into_packet(CommandCode::BitmapLinearAnd, offset, compression, bits.into())
|
||||
bitmap_linear_into_packet(
|
||||
CommandCode::BitmapLinearAnd,
|
||||
offset,
|
||||
compression,
|
||||
bits.into(),
|
||||
)
|
||||
}
|
||||
Command::BitmapLinearOr(offset, bits, compression) => {
|
||||
bitmap_linear_into_packet(CommandCode::BitmapLinearOr, offset, compression, bits.into())
|
||||
bitmap_linear_into_packet(
|
||||
CommandCode::BitmapLinearOr,
|
||||
offset,
|
||||
compression,
|
||||
bits.into(),
|
||||
)
|
||||
}
|
||||
Command::BitmapLinearXor(offset, bits, compression) => {
|
||||
bitmap_linear_into_packet(CommandCode::BitmapLinearXor, offset, compression, bits.into())
|
||||
}
|
||||
Command::Cp437Data(origin, grid) => {
|
||||
origin_size_payload(CommandCode::Cp437Data,
|
||||
origin,
|
||||
Size(grid.width as u16, grid.height as u16),
|
||||
grid.into())
|
||||
bitmap_linear_into_packet(
|
||||
CommandCode::BitmapLinearXor,
|
||||
offset,
|
||||
compression,
|
||||
bits.into(),
|
||||
)
|
||||
}
|
||||
Command::Cp437Data(origin, grid) => origin_size_payload(
|
||||
CommandCode::Cp437Data,
|
||||
origin,
|
||||
Size(grid.width as u16, grid.height as u16),
|
||||
grid.into(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -104,21 +135,24 @@ impl TryFrom<Packet> for Command {
|
|||
fn try_from(value: Packet) -> Result<Self, Self::Error> {
|
||||
let Packet(Header(command_u16, a, b, c, d), _) = value;
|
||||
let command_code = match CommandCode::from_primitive(command_u16) {
|
||||
None => return Err(TryFromPacketError::InvalidCommand(command_u16)),
|
||||
Some(value) => value
|
||||
None => {
|
||||
return Err(TryFromPacketError::InvalidCommand(command_u16))
|
||||
}
|
||||
Some(value) => value,
|
||||
};
|
||||
|
||||
match command_code {
|
||||
CommandCode::Clear => {
|
||||
match check_command_only(value) {
|
||||
Some(err) => Err(err),
|
||||
None => Ok(Command::Clear),
|
||||
}
|
||||
}
|
||||
CommandCode::Clear => match check_command_only(value) {
|
||||
Some(err) => Err(err),
|
||||
None => Ok(Command::Clear),
|
||||
},
|
||||
CommandCode::Brightness => {
|
||||
let Packet(header, payload) = value;
|
||||
if payload.len() != 1 {
|
||||
return Err(TryFromPacketError::UnexpectedPayloadSize(1, payload.len()));
|
||||
return Err(TryFromPacketError::UnexpectedPayloadSize(
|
||||
1,
|
||||
payload.len(),
|
||||
));
|
||||
}
|
||||
|
||||
match check_empty_header(header) {
|
||||
|
@ -126,18 +160,14 @@ impl TryFrom<Packet> for Command {
|
|||
None => Ok(Command::Brightness(payload[0])),
|
||||
}
|
||||
}
|
||||
CommandCode::HardReset => {
|
||||
match check_command_only(value) {
|
||||
Some(err) => Err(err),
|
||||
None => Ok(Command::HardReset),
|
||||
}
|
||||
}
|
||||
CommandCode::FadeOut => {
|
||||
match check_command_only(value) {
|
||||
Some(err) => Err(err),
|
||||
None => Ok(Command::FadeOut),
|
||||
}
|
||||
}
|
||||
CommandCode::HardReset => match check_command_only(value) {
|
||||
Some(err) => Err(err),
|
||||
None => Ok(Command::HardReset),
|
||||
},
|
||||
CommandCode::FadeOut => match check_command_only(value) {
|
||||
Some(err) => Err(err),
|
||||
None => Ok(Command::FadeOut),
|
||||
},
|
||||
CommandCode::Cp437Data => {
|
||||
let Packet(_, payload) = value;
|
||||
Ok(Command::Cp437Data(
|
||||
|
@ -153,14 +183,16 @@ impl TryFrom<Packet> for Command {
|
|||
))
|
||||
}
|
||||
#[allow(deprecated)]
|
||||
CommandCode::BitmapLegacy => {
|
||||
Ok(Command::BitmapLegacy)
|
||||
}
|
||||
CommandCode::BitmapLegacy => Ok(Command::BitmapLegacy),
|
||||
CommandCode::BitmapLinearWin => {
|
||||
let Packet(_, payload) = value;
|
||||
Ok(Command::BitmapLinearWin(
|
||||
Origin(a * TILE_SIZE, b),
|
||||
PixelGrid::load(c as usize * TILE_SIZE as usize, d as usize, &payload),
|
||||
PixelGrid::load(
|
||||
c as usize * TILE_SIZE as usize,
|
||||
d as usize,
|
||||
&payload,
|
||||
),
|
||||
))
|
||||
}
|
||||
CommandCode::BitmapLinear => {
|
||||
|
@ -183,20 +215,42 @@ impl TryFrom<Packet> for Command {
|
|||
}
|
||||
}
|
||||
|
||||
fn bitmap_linear_into_packet(command: CommandCode, offset: Offset, compression: CompressionCode, payload: Vec<u8>) -> Packet {
|
||||
fn bitmap_linear_into_packet(
|
||||
command: CommandCode,
|
||||
offset: Offset,
|
||||
compression: CompressionCode,
|
||||
payload: Vec<u8>,
|
||||
) -> Packet {
|
||||
let payload = into_compressed(compression, payload);
|
||||
let compression = CompressionCode::to_primitive(&compression);
|
||||
Packet(Header(command.to_primitive(), offset, payload.len() as u16, compression, 0), payload)
|
||||
Packet(
|
||||
Header(
|
||||
command.to_primitive(),
|
||||
offset,
|
||||
payload.len() as u16,
|
||||
compression,
|
||||
0,
|
||||
),
|
||||
payload,
|
||||
)
|
||||
}
|
||||
|
||||
fn origin_size_payload(command: CommandCode, origin: Origin, size: Size, payload: Vec<u8>) -> Packet {
|
||||
fn origin_size_payload(
|
||||
command: CommandCode,
|
||||
origin: Origin,
|
||||
size: Size,
|
||||
payload: Vec<u8>,
|
||||
) -> Packet {
|
||||
let Origin(x, y) = origin;
|
||||
let Size(w, h) = size;
|
||||
Packet(Header(command.to_primitive(), x, y, w, h), payload.into())
|
||||
}
|
||||
|
||||
fn command_code_only(code: CommandCode) -> Packet {
|
||||
Packet(Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000), vec!())
|
||||
Packet(
|
||||
Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000),
|
||||
vec![],
|
||||
)
|
||||
}
|
||||
|
||||
fn check_empty_header(header: Header) -> Option<TryFromPacketError> {
|
||||
|
@ -219,21 +273,26 @@ fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
|
|||
}
|
||||
}
|
||||
|
||||
fn packet_into_linear_bitmap(packet: Packet) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
|
||||
fn packet_into_linear_bitmap(
|
||||
packet: Packet,
|
||||
) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
|
||||
let Packet(Header(_, _, length, sub, reserved), payload) = packet;
|
||||
if reserved != 0 {
|
||||
return Err(TryFromPacketError::ExtraneousHeaderValues);
|
||||
}
|
||||
if payload.len() != length as usize {
|
||||
return Err(TryFromPacketError::UnexpectedPayloadSize(length as usize, payload.len()));
|
||||
return Err(TryFromPacketError::UnexpectedPayloadSize(
|
||||
length as usize,
|
||||
payload.len(),
|
||||
));
|
||||
}
|
||||
let sub = match CompressionCode::from_primitive(sub) {
|
||||
None => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
|
||||
Some(value) => value
|
||||
Some(value) => value,
|
||||
};
|
||||
let payload = match into_decompressed(sub, payload) {
|
||||
None => return Err(TryFromPacketError::DecompressionFailed),
|
||||
Some(value) => value
|
||||
Some(value) => value,
|
||||
};
|
||||
Ok((BitVec::load(&payload), sub))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue