servicepoint-binding-uniffi/src/command.rs

268 lines
9.9 KiB
Rust
Raw Normal View History

2024-05-11 21:14:20 +02:00
use std::io::Read;
use flate2::bufread::GzEncoder;
use flate2::Compression;
use flate2::read::GzDecoder;
use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
2024-05-11 21:14:20 +02:00
use crate::command_codes::{CommandCode, CompressionCode};
2024-05-10 00:53:12 +02:00
/// An origin marks the top left position of the
/// data sent to the display.
2024-05-10 12:24:07 +02:00
#[derive(Debug, Clone, Copy)]
2024-05-10 00:53:12 +02:00
pub struct Origin(pub u16, pub u16);
impl Origin {
pub fn top_left() -> Self {
Self(0, 0)
}
}
2024-05-10 00:53:12 +02:00
/// Size defines the width and height of a window
2024-05-10 12:24:07 +02:00
#[derive(Debug, Clone, Copy)]
2024-05-10 00:53:12 +02:00
pub struct Size(pub u16, pub u16);
type Offset = u16;
type Brightness = u8;
#[derive(Debug)]
pub enum Command {
Clear,
HardReset,
FadeOut,
2024-05-11 12:43:17 +02:00
CharBrightness(Origin, ByteGrid),
2024-05-10 00:53:12 +02:00
Brightness(Brightness),
2024-05-10 21:27:34 +02:00
#[deprecated]
BitmapLegacy,
2024-05-11 21:14:20 +02:00
BitmapLinear(Offset, BitVec, CompressionCode),
BitmapLinearAnd(Offset, BitVec, CompressionCode),
BitmapLinearOr(Offset, BitVec, CompressionCode),
BitmapLinearXor(Offset, BitVec, CompressionCode),
2024-05-11 12:43:17 +02:00
Cp437Data(Origin, ByteGrid),
2024-05-10 12:24:07 +02:00
BitmapLinearWin(Origin, PixelGrid),
2024-05-10 00:53:12 +02:00
}
2024-05-10 19:55:18 +02:00
impl Into<Packet> for Command {
fn into(self) -> Packet {
2024-05-10 00:53:12 +02:00
match self {
2024-05-10 19:55:18 +02:00
Command::Clear => command_code_only(CommandCode::Clear),
Command::FadeOut => command_code_only(CommandCode::FadeOut),
Command::HardReset => command_code_only(CommandCode::HardReset),
2024-05-10 21:27:34 +02:00
#[allow(deprecated)]
Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy),
2024-05-11 12:43:17 +02:00
Command::CharBrightness(origin, grid) => {
origin_size_payload(CommandCode::CharBrightness,
origin,
Size(grid.width as u16, grid.height as u16),
grid.into())
2024-05-10 18:33:51 +02:00
}
Command::Brightness(brightness) => {
2024-05-10 19:55:18 +02:00
Packet(Header(CommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness))
2024-05-10 18:33:51 +02:00
}
2024-05-10 12:24:07 +02:00
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels) => {
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0);
2024-05-10 19:55:18 +02:00
Packet(
2024-05-11 12:43:17 +02:00
Header(CommandCode::BitmapLinearWin.to_primitive(),
pixel_x / TILE_SIZE,
pixel_y,
pixels.width as u16 / TILE_SIZE,
pixels.height as u16),
2024-05-10 19:55:18 +02:00
pixels.into())
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinear(offset, bits, compression) => {
bitmap_linear_into_packet(CommandCode::BitmapLinear, offset, compression, bits.into())
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinearAnd(offset, bits, compression) => {
bitmap_linear_into_packet(CommandCode::BitmapLinearAnd, offset, compression, bits.into())
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinearOr(offset, bits, compression) => {
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())
2024-05-10 18:33:51 +02:00
}
2024-05-11 12:43:17 +02:00
Command::Cp437Data(origin, grid) => {
origin_size_payload(CommandCode::Cp437Data,
origin,
Size(grid.width as u16, grid.height as u16),
grid.into())
2024-05-10 12:24:07 +02:00
}
2024-05-10 00:53:12 +02:00
}
}
}
2024-05-10 19:55:18 +02:00
#[derive(Debug)]
pub enum TryFromPacketError {
InvalidCommand(u16),
2024-05-10 21:45:33 +02:00
UnexpectedPayloadSize(usize, usize),
2024-05-10 19:55:18 +02:00
ExtraneousHeaderValues,
2024-05-11 21:14:20 +02:00
InvalidCompressionCode(u16),
DecompressionFailed(std::io::Error),
2024-05-10 21:45:33 +02:00
}
2024-05-10 19:55:18 +02:00
impl TryFrom<Packet> for Command {
type Error = TryFromPacketError;
fn try_from(value: Packet) -> Result<Self, Self::Error> {
2024-05-11 21:14:20 +02:00
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)),
2024-05-10 19:55:18 +02:00
Some(value) => value
};
match command_code {
CommandCode::Clear => {
2024-05-11 21:14:20 +02:00
match check_command_only(value) {
Some(err) => Err(err),
None => Ok(Command::Clear),
2024-05-10 19:55:18 +02:00
}
}
CommandCode::Brightness => {
2024-05-11 21:14:20 +02:00
let Packet(header, payload) = value;
if payload.len() != 1 {
return Err(TryFromPacketError::UnexpectedPayloadSize(1, payload.len()));
}
match check_empty_header(header) {
Some(err) => Err(err),
None => Ok(Command::Brightness(payload[0])),
2024-05-10 19:55:18 +02:00
}
}
CommandCode::HardReset => {
2024-05-11 21:14:20 +02:00
match check_command_only(value) {
Some(err) => Err(err),
None => Ok(Command::HardReset),
2024-05-10 19:55:18 +02:00
}
}
CommandCode::FadeOut => {
2024-05-11 21:14:20 +02:00
match check_command_only(value) {
Some(err) => Err(err),
None => Ok(Command::FadeOut),
2024-05-10 19:55:18 +02:00
}
}
CommandCode::Cp437Data => {
2024-05-11 21:14:20 +02:00
let Packet(_, payload) = value;
2024-05-10 19:55:18 +02:00
Ok(Command::Cp437Data(
2024-05-11 21:14:20 +02:00
Origin(a, b),
ByteGrid::load(c as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
CommandCode::CharBrightness => {
2024-05-11 21:14:20 +02:00
let Packet(_, payload) = value;
2024-05-10 19:55:18 +02:00
Ok(Command::CharBrightness(
2024-05-11 21:14:20 +02:00
Origin(a, b),
ByteGrid::load(c as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
2024-05-10 21:27:34 +02:00
#[allow(deprecated)]
CommandCode::BitmapLegacy => {
Ok(Command::BitmapLegacy)
}
2024-05-10 19:55:18 +02:00
CommandCode::BitmapLinearWin => {
2024-05-11 21:14:20 +02:00
let Packet(_, payload) = value;
2024-05-10 19:55:18 +02:00
Ok(Command::BitmapLinearWin(
2024-05-11 21:14:20 +02:00
Origin(a * TILE_SIZE, b),
PixelGrid::load(c as usize * TILE_SIZE as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
2024-05-10 21:45:33 +02:00
CommandCode::BitmapLinear => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinear(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearAnd => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinearAnd(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearOr => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinearOr(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearXor => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinearXor(a, vec, compression))
}
}
}
}
fn bitmap_linear_into_packet(command: CommandCode, offset: Offset, compression: CompressionCode, payload: Vec<u8>) -> Packet {
let payload = match compression {
CompressionCode::None => payload,
CompressionCode::Gz => {
let mut encoder = GzEncoder::new(&*payload, Compression::best());
let mut compressed = vec!();
match encoder.read_to_end(&mut compressed) {
Err(err) => panic!("could not compress payload: {}", err),
Ok(_) => compressed,
2024-05-10 21:45:33 +02:00
}
2024-05-10 19:55:18 +02:00
}
2024-05-11 21:14:20 +02:00
CompressionCode::Bz => todo!(),
CompressionCode::Lz => todo!(),
CompressionCode::Zs => todo!(),
};
let compression = CompressionCode::to_primitive(&compression);
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 {
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!())
}
fn check_empty_header(header: Header) -> Option<TryFromPacketError> {
let Header(_, a, b, c, d) = header;
if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
2024-05-10 19:55:18 +02:00
}
}
2024-05-11 21:14:20 +02:00
fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
let Packet(Header(_, a, b, c, d), payload) = packet;
if payload.len() != 0 {
Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
} else if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
}
}
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()));
}
let sub = match CompressionCode::from_primitive(sub) {
None => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Some(value) => value
};
let payload = match sub {
CompressionCode::None => payload,
CompressionCode::Gz => {
let mut decoder = GzDecoder::new(&*payload);
let mut decompressed = vec!();
match decoder.read_to_end(&mut decompressed) {
Err(err) => return Err(TryFromPacketError::DecompressionFailed(err)),
Ok(_) => {}
}
decompressed
}
CompressionCode::Bz => todo!(),
CompressionCode::Lz => todo!(),
CompressionCode::Zs => todo!(),
};
Ok((BitVec::load(&payload), sub))
}