stop on hard reset

This commit is contained in:
Vinzenz Schroeter 2024-05-11 17:21:15 +02:00
parent 89c5c44e28
commit 69c07dd733
2 changed files with 25 additions and 16 deletions

View file

@ -2,7 +2,7 @@ use log::warn;
use pixels::wgpu::TextureFormat; use pixels::wgpu::TextureFormat;
use pixels::{Pixels, PixelsBuilder, SurfaceTexture}; use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
use servicepoint2::{ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE}; 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 std::sync::RwLock;
use winit::application::ApplicationHandler; use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, Size}; use winit::dpi::{LogicalSize, Size};
@ -16,6 +16,7 @@ pub struct App<'t> {
window: Option<Window>, window: Option<Window>,
pixels: Option<Pixels>, pixels: Option<Pixels>,
stop_ui_rx: Receiver<()>, stop_ui_rx: Receiver<()>,
stop_udp_tx: Sender<()>,
} }
impl<'t> App<'t> { impl<'t> App<'t> {
@ -23,11 +24,13 @@ impl<'t> App<'t> {
display: &'t RwLock<PixelGrid>, display: &'t RwLock<PixelGrid>,
luma: &'t RwLock<ByteGrid>, luma: &'t RwLock<ByteGrid>,
stop_ui_rx: Receiver<()>, stop_ui_rx: Receiver<()>,
stop_udp_tx: Sender<()>,
) -> Self { ) -> Self {
App { App {
display, display,
luma, luma,
stop_ui_rx, stop_ui_rx,
stop_udp_tx,
pixels: None, pixels: None,
window: None, window: None,
} }
@ -67,6 +70,9 @@ impl ApplicationHandler for App<'_> {
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
warn!("The close button was pressed; stopping"); warn!("The close button was pressed; stopping");
self.stop_udp_tx
.send(())
.expect("could not stop udp thread");
event_loop.exit(); event_loop.exit();
} }
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
@ -80,7 +86,7 @@ impl ApplicationHandler for App<'_> {
for y in 0..PIXEL_HEIGHT as usize { for y in 0..PIXEL_HEIGHT as usize {
for x in 0..PIXEL_WIDTH 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 brightness = luma.get(x / TILE_SIZE as usize, y / TILE_SIZE as usize);
let color = if is_set { let color = if is_set {

View file

@ -44,10 +44,15 @@ fn main() {
let luma = RwLock::new(luma); let luma = RwLock::new(luma);
let luma_ref = &luma; let luma_ref = &luma;
std::thread::scope(move |scope| {
let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); let (stop_udp_tx, stop_udp_rx) = mpsc::channel();
let (stop_ui_tx, stop_ui_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 udp_thread = scope.spawn(move || {
let mut buf = [0; 8985]; let mut buf = [0; 8985];
@ -71,23 +76,18 @@ fn main() {
let vec = buf[..amount].to_vec(); let vec = buf[..amount].to_vec();
let package = servicepoint2::Packet::from(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"); 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 event_loop
.run_app(&mut app) .run_app(&mut app)
.expect("could not run event loop"); .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"); udp_thread.join().expect("could not join udp thread");
}); });
} }
@ -97,15 +97,16 @@ fn handle_package(
font: &BitmapFont, font: &BitmapFont,
display_ref: &RwLock<PixelGrid>, display_ref: &RwLock<PixelGrid>,
luma_ref: &RwLock<ByteGrid>, luma_ref: &RwLock<ByteGrid>,
) { ) -> bool {
let command = match Command::try_from(received) { let command = match Command::try_from(received) {
Err(err) => { Err(err) => {
warn!("could not read command for packet: {:?}", err); warn!("could not read command for packet: {:?}", err);
return; return true;
} }
Ok(val) => val, Ok(val) => val,
}; };
debug!("received {command:?}");
match command { match command {
Command::Clear => { Command::Clear => {
info!("clearing display"); info!("clearing display");
@ -113,7 +114,7 @@ fn handle_package(
} }
Command::HardReset => { Command::HardReset => {
warn!("display shutting down"); warn!("display shutting down");
return; return false;
} }
Command::BitmapLinearWin(Origin(x, y), pixels) => { Command::BitmapLinearWin(Origin(x, y), pixels) => {
let mut display = display_ref.write().unwrap(); let mut display = display_ref.write().unwrap();
@ -187,6 +188,8 @@ fn handle_package(
error!("command not implemented: {command:?}") error!("command not implemented: {command:?}")
} }
}; };
true
} }
fn print_cp437_data( fn print_cp437_data(