reformat with max width
This commit is contained in:
parent
0a3f400e92
commit
362426c758
13 changed files with 225 additions and 122 deletions
|
@ -7,11 +7,15 @@ pub struct BitVec {
|
|||
impl BitVec {
|
||||
pub fn new(size: usize) -> BitVec {
|
||||
assert_eq!(size % 8, 0);
|
||||
Self { data: vec!(0; size / 8) }
|
||||
Self {
|
||||
data: vec![0; size / 8],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(data: &[u8]) -> BitVec {
|
||||
Self { data: Vec::from(data) }
|
||||
Self {
|
||||
data: Vec::from(data),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, index: usize, value: bool) -> bool {
|
||||
|
|
|
@ -31,7 +31,7 @@ impl ByteGrid {
|
|||
self.data[x + y * self.width] = value;
|
||||
}
|
||||
|
||||
pub fn fill(&mut self, value: u8){
|
||||
pub fn fill(&mut self, value: u8) {
|
||||
self.data.fill(value)
|
||||
}
|
||||
}
|
||||
|
|
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))
|
||||
}
|
||||
|
|
|
@ -1,70 +1,78 @@
|
|||
use crate::{CompressionCode, Payload};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
#[cfg(feature = "compression-bz")]
|
||||
use bzip2::read::{BzEncoder, BzDecoder};
|
||||
use bzip2::read::{BzDecoder, BzEncoder};
|
||||
#[cfg(feature = "compression-gz")]
|
||||
use flate2::read::{GzEncoder, GzDecoder};
|
||||
use flate2::read::{GzDecoder, GzEncoder};
|
||||
#[cfg(feature = "compression-lz")]
|
||||
use lz4::{EncoderBuilder as Lz4EncoderBuilder, Decoder as Lz4Decoder};
|
||||
use lz4::{Decoder as Lz4Decoder, EncoderBuilder as Lz4EncoderBuilder};
|
||||
#[cfg(feature = "compression-zs")]
|
||||
use zstd::{Encoder as ZstdEncoder, Decoder as ZstdDecoder};
|
||||
use zstd::{Decoder as ZstdDecoder, Encoder as ZstdEncoder};
|
||||
|
||||
pub(crate) fn into_decompressed(kind: CompressionCode, payload: Payload) -> Option<Payload> {
|
||||
use crate::{CompressionCode, Payload};
|
||||
|
||||
pub(crate) fn into_decompressed(
|
||||
kind: CompressionCode,
|
||||
payload: Payload,
|
||||
) -> Option<Payload> {
|
||||
match kind {
|
||||
CompressionCode::None => Some(payload),
|
||||
#[cfg(feature = "compression-gz")]
|
||||
CompressionCode::Gz => {
|
||||
let mut decoder = GzDecoder::new(&*payload);
|
||||
let mut decompressed = vec!();
|
||||
let mut decompressed = vec![];
|
||||
match decoder.read_to_end(&mut decompressed) {
|
||||
Err(_) => None,
|
||||
Ok(_) => Some(decompressed)
|
||||
Ok(_) => Some(decompressed),
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "compression-bz")]
|
||||
CompressionCode::Bz => {
|
||||
let mut decoder = BzDecoder::new(&*payload);
|
||||
let mut decompressed = vec!();
|
||||
let mut decompressed = vec![];
|
||||
match decoder.read_to_end(&mut decompressed) {
|
||||
Err(_) => None,
|
||||
Ok(_) => Some(decompressed)
|
||||
Ok(_) => Some(decompressed),
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "compression-lz")]
|
||||
CompressionCode::Lz => {
|
||||
let mut decoder = match Lz4Decoder::new(&*payload) {
|
||||
Err(_) => return None,
|
||||
Ok(value) => value
|
||||
Ok(value) => value,
|
||||
};
|
||||
let mut decompressed = vec!();
|
||||
let mut decompressed = vec![];
|
||||
match decoder.read_to_end(&mut decompressed) {
|
||||
Err(_) => None,
|
||||
Ok(_) => Some(decompressed)
|
||||
Ok(_) => Some(decompressed),
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "compression-zs")]
|
||||
CompressionCode::Zs => {
|
||||
let mut decoder = match ZstdDecoder::new(&*payload) {
|
||||
Err(_) => return None,
|
||||
Ok(value) => value
|
||||
Ok(value) => value,
|
||||
};
|
||||
let mut decompressed = vec!();
|
||||
let mut decompressed = vec![];
|
||||
match decoder.read_to_end(&mut decompressed) {
|
||||
Err(_) => None,
|
||||
Ok(_) => Some(decompressed)
|
||||
Ok(_) => Some(decompressed),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payload {
|
||||
pub(crate) fn into_compressed(
|
||||
kind: CompressionCode,
|
||||
payload: Payload,
|
||||
) -> Payload {
|
||||
match kind {
|
||||
CompressionCode::None => payload,
|
||||
#[cfg(feature = "compression-gz")]
|
||||
CompressionCode::Gz => {
|
||||
let mut encoder = GzEncoder::new(&*payload, flate2::Compression::fast());
|
||||
let mut compressed = vec!();
|
||||
let mut encoder =
|
||||
GzEncoder::new(&*payload, flate2::Compression::fast());
|
||||
let mut compressed = vec![];
|
||||
match encoder.read_to_end(&mut compressed) {
|
||||
Err(err) => panic!("could not compress payload: {}", err),
|
||||
Ok(_) => compressed,
|
||||
|
@ -72,8 +80,9 @@ pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payloa
|
|||
}
|
||||
#[cfg(feature = "compression-bz")]
|
||||
CompressionCode::Bz => {
|
||||
let mut encoder = BzEncoder::new(&*payload, bzip2::Compression::fast());
|
||||
let mut compressed = vec!();
|
||||
let mut encoder =
|
||||
BzEncoder::new(&*payload, bzip2::Compression::fast());
|
||||
let mut compressed = vec![];
|
||||
match encoder.read_to_end(&mut compressed) {
|
||||
Err(err) => panic!("could not compress payload: {}", err),
|
||||
Ok(_) => compressed,
|
||||
|
@ -82,22 +91,21 @@ pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payloa
|
|||
#[cfg(feature = "compression-lz")]
|
||||
CompressionCode::Lz => {
|
||||
let mut encoder = Lz4EncoderBuilder::new()
|
||||
.build(vec!())
|
||||
.build(vec![])
|
||||
.expect("could not create encoder");
|
||||
encoder.write(&*payload)
|
||||
.expect("could not write payload");
|
||||
encoder.write(&*payload).expect("could not write payload");
|
||||
let (payload, _) = encoder.finish();
|
||||
payload
|
||||
}
|
||||
#[cfg(feature = "compression-zs")]
|
||||
CompressionCode::Zs => {
|
||||
let mut encoder = ZstdEncoder::new(vec!(), zstd::DEFAULT_COMPRESSION_LEVEL)
|
||||
.expect("could not create encoder");
|
||||
encoder.write(&*payload)
|
||||
let mut encoder =
|
||||
ZstdEncoder::new(vec![], zstd::DEFAULT_COMPRESSION_LEVEL)
|
||||
.expect("could not create encoder");
|
||||
encoder
|
||||
.write(&*payload)
|
||||
.expect("could not compress payload");
|
||||
encoder.finish()
|
||||
.expect("could not finish encoding")
|
||||
encoder.finish().expect("could not finish encoding")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use std::fmt::Debug;
|
||||
use std::net::{ToSocketAddrs, UdpSocket};
|
||||
|
||||
use log::{debug, info};
|
||||
|
||||
use crate::Packet;
|
||||
|
||||
pub struct Connection {
|
||||
|
@ -17,7 +19,10 @@ impl Connection {
|
|||
}
|
||||
|
||||
/// Send a command to the display
|
||||
pub fn send(&self, packet: impl Into<Packet> + Debug) -> Result<(), std::io::Error> {
|
||||
pub fn send(
|
||||
&self,
|
||||
packet: impl Into<Packet> + Debug,
|
||||
) -> Result<(), std::io::Error> {
|
||||
debug!("sending {packet:?}");
|
||||
let packet: Packet = packet.into();
|
||||
let data: Vec<u8> = packet.into();
|
||||
|
|
24
src/lib.rs
24
src/lib.rs
|
@ -1,19 +1,19 @@
|
|||
mod connection;
|
||||
mod pixel_grid;
|
||||
pub use crate::bit_vec::BitVec;
|
||||
pub use crate::byte_grid::ByteGrid;
|
||||
pub use crate::command::{Command, Origin, Size};
|
||||
pub use crate::command_codes::{CommandCode, CompressionCode};
|
||||
pub use crate::connection::Connection;
|
||||
pub use crate::packet::{Header, Packet, Payload};
|
||||
pub use crate::pixel_grid::PixelGrid;
|
||||
|
||||
mod bit_vec;
|
||||
mod packet;
|
||||
mod byte_grid;
|
||||
mod command;
|
||||
mod command_codes;
|
||||
mod byte_grid;
|
||||
mod compression;
|
||||
|
||||
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};
|
||||
pub use crate::command_codes::{CommandCode, CompressionCode};
|
||||
pub use crate::byte_grid::ByteGrid;
|
||||
mod connection;
|
||||
mod packet;
|
||||
mod pixel_grid;
|
||||
|
||||
pub const TILE_SIZE: u16 = 8;
|
||||
pub const TILE_WIDTH: u16 = 56;
|
||||
|
|
|
@ -10,7 +10,7 @@ impl Into<Vec<u8>> for Packet {
|
|||
fn into(self) -> Vec<u8> {
|
||||
let Packet(Header(mode, a, b, c, d), payload) = self;
|
||||
|
||||
let mut packet = vec!(0u8; 10 + payload.len());
|
||||
let mut packet = vec![0u8; 10 + payload.len()];
|
||||
packet[0..=1].copy_from_slice(&u16::to_be_bytes(mode));
|
||||
packet[2..=3].copy_from_slice(&u16::to_be_bytes(a));
|
||||
packet[4..=5].copy_from_slice(&u16::to_be_bytes(b));
|
||||
|
@ -41,4 +41,4 @@ impl From<Vec<u8>> for Packet {
|
|||
|
||||
Packet(Header(mode, a, b, c, d), payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue