move a bunch of stuff to shared

This commit is contained in:
Vinzenz Schroeter 2024-05-09 16:39:00 +02:00
parent 4f554bf60b
commit b56af905a0
4 changed files with 38 additions and 103 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "pixel-shared-rs"]
path = pixel-shared-rs
url = https://github.com/kaesaecracker/pixel-shared-rs.git

13
Cargo.lock generated
View file

@ -1561,14 +1561,21 @@ dependencies = [
"env_logger", "env_logger",
"image", "image",
"log", "log",
"num", "pixel-shared-rs",
"num-derive",
"num-traits",
"pixels", "pixels",
"raw-window-handle 0.6.1", "raw-window-handle 0.6.1",
"winit", "winit",
] ]
[[package]]
name = "pixel-shared-rs"
version = "0.1.0"
dependencies = [
"num",
"num-derive",
"num-traits",
]
[[package]] [[package]]
name = "pixels" name = "pixels"
version = "0.13.0" version = "0.13.0"

View file

@ -5,13 +5,11 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.5", features = ["derive"] }
num = "0.4"
num-derive = "0.4"
num-traits = "0.2"
image = "0.25" image = "0.25"
pixels = "0.13" pixels = "0.13"
winit = { version = "0.30", features = ["rwh_05"] } winit = { version = "0.30", features = ["rwh_05"] }
log = "0.4" log = "0.4"
raw-window-handle = "0.6" raw-window-handle = "0.6"
env_logger = "0.11" env_logger = "0.11"
pixel-shared-rs = { path = "pixel-shared-rs" }

View file

@ -1,6 +1,6 @@
use crate::{DISPLAY, PIXEL_WIDTH, TILE_SIZE}; use crate::{DISPLAY, PIXEL_WIDTH, TILE_SIZE};
use log::{error, info, warn}; 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::io::ErrorKind;
use std::mem::size_of; use std::mem::size_of;
use std::net::UdpSocket; use std::net::UdpSocket;
@ -30,8 +30,11 @@ pub fn start_udp_thread(bind: String, stop_receiver: Receiver<()>) -> JoinHandle
other => other.unwrap(), other => other.unwrap(),
}; };
if amount == buf.len(){ if amount == buf.len() {
warn!("the received package may have been truncated to a length of {}", amount); warn!(
"the received package may have been truncated to a length of {}",
amount
);
} }
handle_package(&mut buf[..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]) { fn handle_package(received: &mut [u8]) {
let header = match read_hdr_window(&received[..10]){ let header = match read_hdr_window(&received[..10]) {
None => return, Err(ReadHdrWindowError::BufferTooSmall) => {
Some(value) => value 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..]; let payload = &received[10..];
@ -122,50 +93,6 @@ fn handle_package(received: &mut [u8]) {
} }
} }
fn read_hdr_window(buffer: &[u8]) -> Option<HdrWindow> {
if buffer.len() < size_of::<HdrWindow>() {
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<DisplayCommand> = 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 { fn check_payload_size(buf: &[u8], expected: usize) -> bool {
let actual = buf.len(); let actual = buf.len();
if actual == expected { if actual == expected {