From 94868bbc73ce13ba322065afb8b0e40c4fd543f2 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 9 May 2024 14:21:49 +0200 Subject: [PATCH] add warning for wrong endianness, clear pixel buffer --- src/main.rs | 4 +--- src/upd_loop.rs | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c1ee7b..22b2c54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,11 +31,9 @@ fn main() { env_logger::init(); let cli = Cli::parse(); - info!("running with args: {:?}", &cli); + info!("starting with args: {:?}", &cli); - info!("display booting up"); let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); - let thread = start_udp_thread(cli.bind, stop_udp_rx); let event_loop = EventLoop::new().expect("could not create event loop"); diff --git a/src/upd_loop.rs b/src/upd_loop.rs index abced91..adc579a 100644 --- a/src/upd_loop.rs +++ b/src/upd_loop.rs @@ -82,13 +82,11 @@ enum DisplaySubcommand { */ fn handle_package(received: &mut [u8]) { - let header = read_hdr_window(&received[..10]); - if let Err(err) = header { - warn!("could not read header: {}", err); - return; - } + let header = match read_hdr_window(&received[..10]){ + None => return, + Some(value) => value + }; - let header = header.unwrap(); let payload = &received[10..]; info!( @@ -99,7 +97,10 @@ fn handle_package(received: &mut [u8]) { match header.command { DisplayCommand::CmdClear => { - info!("(imagine an empty screen now)") + info!("clearing display"); + for v in unsafe { DISPLAY.iter_mut() } { + *v = false; + } } DisplayCommand::CmdHardReset => { warn!("display shutting down"); @@ -117,18 +118,29 @@ fn handle_package(received: &mut [u8]) { } } -fn read_hdr_window(buffer: &[u8]) -> Result { +fn read_hdr_window(buffer: &[u8]) -> Option { if buffer.len() < size_of::() { - return Err("received a packet that is too small".into()); + 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() { - return Err(format!("received invalid command {}", command_u16)); + 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 Ok(HdrWindow { + return Some(HdrWindow { command: maybe_command.unwrap(), x: read_beu16_from_buffer(&buffer[2..=3]), y: read_beu16_from_buffer(&buffer[4..=5]),