From b56af905a088711fb8c2878c7fabc88152cf0c20 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 9 May 2024 16:39:00 +0200 Subject: [PATCH] move a bunch of stuff to shared --- .gitmodules | 3 ++ Cargo.lock | 13 ++++-- Cargo.toml | 6 +-- src/upd_loop.rs | 119 ++++++++++-------------------------------------- 4 files changed, 38 insertions(+), 103 deletions(-) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8c85e15 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "pixel-shared-rs"] + path = pixel-shared-rs + url = https://github.com/kaesaecracker/pixel-shared-rs.git diff --git a/Cargo.lock b/Cargo.lock index 56be9c5..907c4f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1561,14 +1561,21 @@ dependencies = [ "env_logger", "image", "log", - "num", - "num-derive", - "num-traits", + "pixel-shared-rs", "pixels", "raw-window-handle 0.6.1", "winit", ] +[[package]] +name = "pixel-shared-rs" +version = "0.1.0" +dependencies = [ + "num", + "num-derive", + "num-traits", +] + [[package]] name = "pixels" version = "0.13.0" diff --git a/Cargo.toml b/Cargo.toml index 6df50a4..439a432 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,11 @@ edition = "2021" publish = false [dependencies] -clap = { version = "4.0", features = ["derive"] } -num = "0.4" -num-derive = "0.4" -num-traits = "0.2" +clap = { version = "4.5", features = ["derive"] } image = "0.25" pixels = "0.13" winit = { version = "0.30", features = ["rwh_05"] } log = "0.4" raw-window-handle = "0.6" env_logger = "0.11" +pixel-shared-rs = { path = "pixel-shared-rs" } diff --git a/src/upd_loop.rs b/src/upd_loop.rs index e5f7ed0..821524f 100644 --- a/src/upd_loop.rs +++ b/src/upd_loop.rs @@ -1,6 +1,6 @@ use crate::{DISPLAY, PIXEL_WIDTH, TILE_SIZE}; use log::{error, info, warn}; -use num_derive::FromPrimitive; +use pixel_shared_rs::{read_hdr_window, DisplayCommand, HdrWindow, ReadHdrWindowError}; use std::io::ErrorKind; use std::mem::size_of; use std::net::UdpSocket; @@ -30,8 +30,11 @@ pub fn start_udp_thread(bind: String, stop_receiver: Receiver<()>) -> JoinHandle other => other.unwrap(), }; - if amount == buf.len(){ - warn!("the received package may have been truncated to a length of {}", amount); + if amount == buf.len() { + warn!( + "the received package may have been truncated to a length of {}", + amount + ); } handle_package(&mut buf[..amount]); @@ -39,56 +42,24 @@ pub fn start_udp_thread(bind: String, stop_receiver: Receiver<()>) -> JoinHandle }); } -#[derive(Debug, FromPrimitive)] -enum DisplayCommand { - CmdClear = 0x0002, - CmdCp437data = 0x0003, - CmdCharBrightness = 0x0005, - CmdBrightness = 0x0007, - CmdHardReset = 0x000b, - CmdFadeOut = 0x000d, - CmdBitmapLegacy = 0x0010, - CmdBitmapLinear = 0x0012, - CmdBitmapLinearWin = 0x0013, - CmdBitmapLinearAnd = 0x0014, - CmdBitmapLinearOr = 0x0015, - CmdBitmapLinearXor = 0x0016, -} - -#[repr(C)] -#[derive(Debug)] -struct HdrWindow { - command: DisplayCommand, - x: u16, - y: u16, - w: u16, - h: u16, -} - -/* needed for commands that are not implemented yet -#[repr(C)] -struct HdrBitmap { - command: DisplayCommand, - offset: u16, - length: u16, - subcommand: DisplaySubcommand, - reserved: u16, -} - -#[repr(u16)] -enum DisplaySubcommand { - SubCmdBitmapNormal = 0x0, - SubCmdBitmapCompressZ = 0x677a, - SubCmdBitmapCompressBz = 0x627a, - SubCmdBitmapCompressLz = 0x6c7a, - SubCmdBitmapCompressZs = 0x7a73, -} -*/ - fn handle_package(received: &mut [u8]) { - let header = match read_hdr_window(&received[..10]){ - None => return, - Some(value) => value + let header = match read_hdr_window(&received[..10]) { + Err(ReadHdrWindowError::BufferTooSmall) => { + error!("received a packet that is too small"); + return; + } + Err(ReadHdrWindowError::InvalidCommand(command_u16)) => { + error!("received invalid command {}", command_u16); + return; + } + Err(ReadHdrWindowError::WrongCommandEndianness(command_u16, command_swapped)) => { + error!( + "The reversed byte order of {} matches command {:?}, you are probably sending the wrong endianness", + command_u16, command_swapped + ); + return; + } + Ok(value) => value, }; let payload = &received[10..]; @@ -122,50 +93,6 @@ fn handle_package(received: &mut [u8]) { } } -fn read_hdr_window(buffer: &[u8]) -> Option { - if buffer.len() < size_of::() { - error!("received a packet that is too small"); - return None; - } - - let command_u16 = read_beu16_from_buffer(&buffer[0..=1]); - let maybe_command = num::FromPrimitive::from_u16(command_u16); - if maybe_command.is_none() { - error!("received invalid command {}", command_u16); - - let maybe_command: Option = num::FromPrimitive::from_u16(u16::swap_bytes(command_u16)); - if let Some(command) = maybe_command { - error!( - "The reversed byte order of {} matches command {:?}, you are probably sending the wrong endianness", - command_u16, command - ); - } - - return None; - } - - return Some(HdrWindow { - command: maybe_command.unwrap(), - x: read_beu16_from_buffer(&buffer[2..=3]), - y: read_beu16_from_buffer(&buffer[4..=5]), - w: read_beu16_from_buffer(&buffer[6..=7]), - h: read_beu16_from_buffer(&buffer[8..=9]), - }); -} - -fn read_beu16_from_buffer(buffer: &[u8]) -> u16 { - assert_eq!( - buffer.len(), - 2, - "cannot read u16 from buffer with size != 2" - ); - - let ptr = buffer.as_ptr() as *const u16; - let u16 = unsafe { *ptr }; - - return u16::from_be(u16); -} - fn check_payload_size(buf: &[u8], expected: usize) -> bool { let actual = buf.len(); if actual == expected {