move DisplayCommandCode into shared lib

This commit is contained in:
Vinzenz Schroeter 2024-05-10 18:43:00 +02:00
parent ae12c82c2e
commit 63520f5708
3 changed files with 54 additions and 73 deletions

23
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]]
name = "ab_glyph"
version = "0.2.25"
version = "0.2.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f90148830dac590fac7ccfe78ec4a8ea404c60f75a24e16407a71f0f40de775"
checksum = "2e53b0a3d5760cd2ba9b787ae0c6440ad18ee294ff71b05e3381c900a7d16cfd"
dependencies = [
"ab_glyph_rasterizer",
"owned_ttf_parser",
@ -403,9 +403,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.0"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77e53693616d3075149f4ead59bdeecd204ac6b8192d8969757601b74bddf00f"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "clap"
@ -1608,9 +1608,9 @@ dependencies = [
[[package]]
name = "owned_ttf_parser"
version = "0.20.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7"
checksum = "6b41438d2fc63c46c74a2203bf5ccd82c41ba04347b2fcf5754f230b167067d5"
dependencies = [
"ttf-parser",
]
@ -2151,7 +2151,12 @@ dependencies = [
[[package]]
name = "servicepoint2"
version = "0.1.0"
source = "git+https://github.com/kaesaecracker/servicepoint.git#a23ca55f607b64324eda02f27ae347998d227deb"
source = "git+https://github.com/kaesaecracker/servicepoint.git#06d3a99659dc32060c7a302fa8d7147c5227dda6"
dependencies = [
"num",
"num-derive",
"num-traits",
]
[[package]]
name = "simd-adler32"
@ -2421,9 +2426,9 @@ checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
[[package]]
name = "ttf-parser"
version = "0.20.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4"
checksum = "eb71ffab0ca84cecd986dd52e873c8d0b013f3d5d9ce25a6f7d0513ed933d562"
[[package]]
name = "ultraviolet"

View file

@ -1,36 +1,9 @@
use num_derive::{FromPrimitive, ToPrimitive};
use servicepoint2::DisplayCommandCode;
use std::mem::size_of;
use num_derive::{FromPrimitive, ToPrimitive};
#[repr(u16)]
#[derive(Debug, FromPrimitive, ToPrimitive, Default)]
pub enum DisplayCommandCode {
#[default]
Clear = 0x0002,
Cp437data = 0x0003,
CharBrightness = 0x0005,
Brightness = 0x0007,
HardReset = 0x000b,
FadeOut = 0x000d,
BitmapLegacy = 0x0010,
BitmapLinear = 0x0012,
BitmapLinearWin = 0x0013,
BitmapLinearAnd = 0x0014,
BitmapLinearOr = 0x0015,
BitmapLinearXor = 0x0016,
}
impl DisplayCommandCode {
pub fn from_primitive(value: u16) -> Option<Self> {
num::FromPrimitive::from_u16(value)
}
pub fn to_primitive(&self) -> u16 {
num::ToPrimitive::to_u16(self).unwrap()
}
}
#[repr(C)]
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct HdrWindow {
pub command: DisplayCommandCode,
pub x: u16,
@ -67,38 +40,41 @@ pub enum ReadHeaderError {
InvalidCommand(u16),
}
pub fn read_header(buffer: &[u8]) -> Result<HdrWindow, ReadHeaderError> {
assert_eq!(size_of::<HdrWindow>(), 10, "invalid struct size");
impl HdrWindow {
pub fn from_buffer(buffer: &[u8]) -> Result<HdrWindow, ReadHeaderError> {
assert_eq!(size_of::<HdrWindow>(), 10, "invalid struct size");
if buffer.len() < size_of::<HdrWindow>() {
return Err(ReadHeaderError::BufferTooSmall);
if buffer.len() < size_of::<HdrWindow>() {
return Err(ReadHeaderError::BufferTooSmall);
}
let command_u16 = Self::read_beu16(&buffer[0..=1]);
return match DisplayCommandCode::from_primitive(command_u16) {
Some(command) => Ok(HdrWindow {
command,
x: Self::read_beu16(&buffer[2..=3]),
y: Self::read_beu16(&buffer[4..=5]),
w: Self::read_beu16(&buffer[6..=7]),
h: Self::read_beu16(&buffer[8..=9]),
}),
None => {
let maybe_command =
DisplayCommandCode::from_primitive(u16::swap_bytes(command_u16));
return match maybe_command {
None => Err(ReadHeaderError::InvalidCommand(command_u16)),
Some(command) => Err(ReadHeaderError::WrongCommandEndianness(
command_u16,
command,
)),
};
}
};
}
let command_u16 = read_beu16(&buffer[0..=1]);
return match DisplayCommandCode::from_primitive(command_u16) {
Some(command) => Ok(HdrWindow {
command,
x: read_beu16(&buffer[2..=3]),
y: read_beu16(&buffer[4..=5]),
w: read_beu16(&buffer[6..=7]),
h: read_beu16(&buffer[8..=9]),
}),
None => {
let maybe_command = DisplayCommandCode::from_primitive(u16::swap_bytes(command_u16));
return match maybe_command {
None => Err(ReadHeaderError::InvalidCommand(command_u16)),
Some(command) => Err(ReadHeaderError::WrongCommandEndianness(
command_u16,
command,
)),
};
}
};
}
fn read_beu16(buffer: &[u8]) -> u16 {
let buffer: [u8; 2] = buffer
.try_into()
.expect("cannot read u16 from buffer with size != 2");
return u16::from_be_bytes(buffer);
fn read_beu16(buffer: &[u8]) -> u16 {
let buffer: [u8; 2] = buffer
.try_into()
.expect("cannot read u16 from buffer with size != 2");
return u16::from_be_bytes(buffer);
}
}

View file

@ -1,8 +1,8 @@
use crate::font::BitmapFont;
use crate::protocol::{read_header, DisplayCommandCode, HdrWindow, ReadHeaderError};
use crate::protocol::{HdrWindow, ReadHeaderError};
use crate::DISPLAY;
use log::{debug, error, info, warn};
use servicepoint2::{PixelGrid, PIXEL_WIDTH, TILE_SIZE};
use servicepoint2::{PixelGrid, PIXEL_WIDTH, TILE_SIZE, DisplayCommandCode};
use std::io::ErrorKind;
use std::net::{ToSocketAddrs, UdpSocket};
use std::sync::mpsc;
@ -60,7 +60,7 @@ impl UdpThread {
}
fn handle_package(received: &mut [u8], font: &BitmapFont) {
let header = match read_header(&received[..10]) {
let header = match HdrWindow::from_buffer(&received[..10]) {
Err(ReadHeaderError::BufferTooSmall) => {
error!("received a packet that is too small");
return;