servicepoint-binding-uniffi/src/command.rs

319 lines
11 KiB
Rust
Raw Normal View History

2024-05-12 01:30:55 +02:00
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-11 23:16:41 +02:00
use crate::compression::{into_compressed, into_decompressed};
2024-05-10 00:53:12 +02:00
2024-05-12 01:30:55 +02:00
/// An origin marks the top left position of a window 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 {
2024-05-12 01:30:55 +02:00
/// Set all pixels to the off state
2024-05-10 00:53:12 +02:00
Clear,
2024-05-12 01:30:55 +02:00
/// Kills the udp daemon, usually results in a reboot of the display.
2024-05-10 00:53:12 +02:00
HardReset,
FadeOut,
2024-05-12 01:30:55 +02:00
/// Set the brightness of tiles
2024-05-11 12:43:17 +02:00
CharBrightness(Origin, ByteGrid),
2024-05-12 01:30:55 +02:00
/// Set the brightness of all tiles
2024-05-10 00:53:12 +02:00
Brightness(Brightness),
2024-05-10 21:27:34 +02:00
#[deprecated]
BitmapLegacy,
2024-05-12 01:30:55 +02:00
/// Set pixel data starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinear(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an and-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearAnd(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an or-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearOr(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an xor-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearXor(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Show text on the screen. Note that the byte data has to be CP437 encoded.
2024-05-11 12:43:17 +02:00
Cp437Data(Origin, ByteGrid),
2024-05-12 01:30:55 +02:00
/// Sets a window of pixels to the specified values
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)]
2024-05-11 23:28:08 +02:00
Command::BitmapLegacy => {
command_code_only(CommandCode::BitmapLegacy)
2024-05-10 18:33:51 +02:00
}
2024-05-11 23:28:08 +02:00
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],
),
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 23:28:08 +02:00
Header(
CommandCode::BitmapLinearWin.to_primitive(),
pixel_x / TILE_SIZE,
pixel_y,
pixels.width as u16 / TILE_SIZE,
pixels.height as u16,
),
pixels.into(),
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinear(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
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) => {
2024-05-11 23:28:08 +02:00
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) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearOr,
offset,
compression,
bits.into(),
)
2024-05-11 21:14:20 +02:00
}
Command::BitmapLinearXor(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearXor,
offset,
compression,
bits.into(),
)
2024-05-10 12:24:07 +02:00
}
2024-05-11 23:28:08 +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 00:53:12 +02:00
}
}
}
2024-05-10 19:55:18 +02:00
#[derive(Debug)]
pub enum TryFromPacketError {
2024-05-12 01:30:55 +02:00
/// the contained command code does not correspond to a known command
2024-05-10 19:55:18 +02:00
InvalidCommand(u16),
2024-05-12 01:30:55 +02:00
/// the expected payload size was n, but size m was found
2024-05-10 21:45:33 +02:00
UnexpectedPayloadSize(usize, usize),
2024-05-12 01:30:55 +02:00
/// Header fields not needed for the command have been used.
///
/// Note that these commands would usually still work on the actual display.
2024-05-10 19:55:18 +02:00
ExtraneousHeaderValues,
2024-05-12 01:30:55 +02:00
/// The contained compression code is not known. This could be of disabled features.
2024-05-11 21:14:20 +02:00
InvalidCompressionCode(u16),
2024-05-12 01:30:55 +02:00
/// Decompression of the payload failed. This can be caused by corrupted packets.
2024-05-11 22:21:27 +02:00
DecompressionFailed,
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) {
2024-05-11 23:28:08 +02:00
None => {
2024-05-12 01:30:55 +02:00
return Err(TryFromPacketError::InvalidCommand(command_u16));
2024-05-11 23:28:08 +02:00
}
Some(value) => value,
2024-05-10 19:55:18 +02:00
};
match command_code {
2024-05-11 23:28:08 +02:00
CommandCode::Clear => 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 {
2024-05-11 23:28:08 +02:00
return Err(TryFromPacketError::UnexpectedPayloadSize(
1,
payload.len(),
));
2024-05-11 21:14:20 +02:00
}
match check_empty_header(header) {
Some(err) => Err(err),
None => Ok(Command::Brightness(payload[0])),
2024-05-10 19:55:18 +02:00
}
}
2024-05-11 23:28:08 +02:00
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),
},
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)]
2024-05-11 23:28:08 +02:00
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),
2024-05-11 23:28:08 +02:00
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))
}
}
}
}
2024-05-11 23:28:08 +02:00
fn bitmap_linear_into_packet(
command: CommandCode,
offset: Offset,
compression: CompressionCode,
payload: Vec<u8>,
) -> Packet {
2024-05-11 22:21:27 +02:00
let payload = into_compressed(compression, payload);
2024-05-11 21:14:20 +02:00
let compression = CompressionCode::to_primitive(&compression);
2024-05-11 23:28:08 +02:00
Packet(
Header(
command.to_primitive(),
offset,
payload.len() as u16,
compression,
0,
),
payload,
)
2024-05-11 21:14:20 +02:00
}
2024-05-11 23:28:08 +02:00
fn origin_size_payload(
command: CommandCode,
origin: Origin,
size: Size,
payload: Vec<u8>,
) -> Packet {
2024-05-11 21:14:20 +02:00
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 {
2024-05-11 23:28:08 +02:00
Packet(
Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000),
vec![],
)
2024-05-11 21:14:20 +02:00
}
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
}
}
2024-05-11 23:28:08 +02:00
fn packet_into_linear_bitmap(
packet: Packet,
) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
2024-05-11 21:14:20 +02:00
let Packet(Header(_, _, length, sub, reserved), payload) = packet;
if reserved != 0 {
return Err(TryFromPacketError::ExtraneousHeaderValues);
}
if payload.len() != length as usize {
2024-05-11 23:28:08 +02:00
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
2024-05-11 21:14:20 +02:00
}
let sub = match CompressionCode::from_primitive(sub) {
None => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
2024-05-11 23:28:08 +02:00
Some(value) => value,
2024-05-11 21:14:20 +02:00
};
2024-05-11 22:21:27 +02:00
let payload = match into_decompressed(sub, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),
2024-05-11 23:28:08 +02:00
Some(value) => value,
2024-05-11 22:21:27 +02:00
};
2024-05-12 01:30:55 +02:00
Ok((BitVec::from(&*payload), sub))
2024-05-11 22:21:27 +02:00
}