add warning for wrong endianness, clear pixel buffer

This commit is contained in:
Vinzenz Schroeter 2024-05-09 14:21:49 +02:00
parent 2fe6c3b6f1
commit 94868bbc73
2 changed files with 24 additions and 14 deletions

View file

@ -31,11 +31,9 @@ fn main() {
env_logger::init(); env_logger::init();
let cli = Cli::parse(); 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 (stop_udp_tx, stop_udp_rx) = mpsc::channel();
let thread = start_udp_thread(cli.bind, stop_udp_rx); let thread = start_udp_thread(cli.bind, stop_udp_rx);
let event_loop = EventLoop::new().expect("could not create event loop"); let event_loop = EventLoop::new().expect("could not create event loop");

View file

@ -82,13 +82,11 @@ enum DisplaySubcommand {
*/ */
fn handle_package(received: &mut [u8]) { fn handle_package(received: &mut [u8]) {
let header = read_hdr_window(&received[..10]); let header = match read_hdr_window(&received[..10]){
if let Err(err) = header { None => return,
warn!("could not read header: {}", err); Some(value) => value
return; };
}
let header = header.unwrap();
let payload = &received[10..]; let payload = &received[10..];
info!( info!(
@ -99,7 +97,10 @@ fn handle_package(received: &mut [u8]) {
match header.command { match header.command {
DisplayCommand::CmdClear => { DisplayCommand::CmdClear => {
info!("(imagine an empty screen now)") info!("clearing display");
for v in unsafe { DISPLAY.iter_mut() } {
*v = false;
}
} }
DisplayCommand::CmdHardReset => { DisplayCommand::CmdHardReset => {
warn!("display shutting down"); warn!("display shutting down");
@ -117,18 +118,29 @@ fn handle_package(received: &mut [u8]) {
} }
} }
fn read_hdr_window(buffer: &[u8]) -> Result<HdrWindow, String> { fn read_hdr_window(buffer: &[u8]) -> Option<HdrWindow> {
if buffer.len() < size_of::<HdrWindow>() { if buffer.len() < size_of::<HdrWindow>() {
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 command_u16 = read_beu16_from_buffer(&buffer[0..=1]);
let maybe_command = num::FromPrimitive::from_u16(command_u16); let maybe_command = num::FromPrimitive::from_u16(command_u16);
if maybe_command.is_none() { if maybe_command.is_none() {
return Err(format!("received invalid command {}", command_u16)); 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 Ok(HdrWindow { return Some(HdrWindow {
command: maybe_command.unwrap(), command: maybe_command.unwrap(),
x: read_beu16_from_buffer(&buffer[2..=3]), x: read_beu16_from_buffer(&buffer[2..=3]),
y: read_beu16_from_buffer(&buffer[4..=5]), y: read_beu16_from_buffer(&buffer[4..=5]),