From 69c07dd733c1591d1f08a373de0bc3a8df9457eb Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 11 May 2024 17:21:15 +0200 Subject: [PATCH] stop on hard reset --- src/gui.rs | 10 ++++++++-- src/main.rs | 31 +++++++++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 859419c..295bf33 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -2,7 +2,7 @@ use log::warn; use pixels::wgpu::TextureFormat; use pixels::{Pixels, PixelsBuilder, SurfaceTexture}; use servicepoint2::{ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE}; -use std::sync::mpsc::Receiver; +use std::sync::mpsc::{Receiver, Sender}; use std::sync::RwLock; use winit::application::ApplicationHandler; use winit::dpi::{LogicalSize, Size}; @@ -16,6 +16,7 @@ pub struct App<'t> { window: Option, pixels: Option, stop_ui_rx: Receiver<()>, + stop_udp_tx: Sender<()>, } impl<'t> App<'t> { @@ -23,11 +24,13 @@ impl<'t> App<'t> { display: &'t RwLock, luma: &'t RwLock, stop_ui_rx: Receiver<()>, + stop_udp_tx: Sender<()>, ) -> Self { App { display, luma, stop_ui_rx, + stop_udp_tx, pixels: None, window: None, } @@ -67,6 +70,9 @@ impl ApplicationHandler for App<'_> { match event { WindowEvent::CloseRequested => { warn!("The close button was pressed; stopping"); + self.stop_udp_tx + .send(()) + .expect("could not stop udp thread"); event_loop.exit(); } WindowEvent::RedrawRequested => { @@ -80,7 +86,7 @@ impl ApplicationHandler for App<'_> { for y in 0..PIXEL_HEIGHT as usize { for x in 0..PIXEL_WIDTH as usize { - let is_set = display.get(x , y ); + let is_set = display.get(x, y); let brightness = luma.get(x / TILE_SIZE as usize, y / TILE_SIZE as usize); let color = if is_set { diff --git a/src/main.rs b/src/main.rs index d50ec02..39f3452 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,10 +44,15 @@ fn main() { let luma = RwLock::new(luma); let luma_ref = &luma; - std::thread::scope(move |scope| { - let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); - let (stop_ui_tx, stop_ui_rx) = mpsc::channel(); + let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); + let (stop_ui_tx, stop_ui_rx) = mpsc::channel(); + let mut app = App::new(display_ref, luma_ref, stop_ui_rx, stop_udp_tx); + + let event_loop = EventLoop::new().expect("could not create event loop"); + event_loop.set_control_flow(ControlFlow::Poll); + + std::thread::scope(move |scope| { let udp_thread = scope.spawn(move || { let mut buf = [0; 8985]; @@ -71,23 +76,18 @@ fn main() { let vec = buf[..amount].to_vec(); let package = servicepoint2::Packet::from(vec); - handle_package(package, &font, display_ref, luma_ref); + if !handle_package(package, &font, display_ref, luma_ref) { + break; // hard reset + } } stop_ui_tx.send(()).expect("could not stop ui thread"); }); - let mut app = App::new(display_ref, luma_ref, stop_ui_rx); - - let event_loop = EventLoop::new().expect("could not create event loop"); - event_loop.set_control_flow(ControlFlow::Poll); - event_loop .run_app(&mut app) .expect("could not run event loop"); - stop_udp_tx.send(()).expect("could not stop udp thread"); - udp_thread.join().expect("could not join udp thread"); }); } @@ -97,15 +97,16 @@ fn handle_package( font: &BitmapFont, display_ref: &RwLock, luma_ref: &RwLock, -) { +) -> bool { let command = match Command::try_from(received) { Err(err) => { warn!("could not read command for packet: {:?}", err); - return; + return true; } Ok(val) => val, }; + debug!("received {command:?}"); match command { Command::Clear => { info!("clearing display"); @@ -113,7 +114,7 @@ fn handle_package( } Command::HardReset => { warn!("display shutting down"); - return; + return false; } Command::BitmapLinearWin(Origin(x, y), pixels) => { let mut display = display_ref.write().unwrap(); @@ -187,6 +188,8 @@ fn handle_package( error!("command not implemented: {command:?}") } }; + + true } fn print_cp437_data(