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::{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<Window>,
pixels: Option<Pixels>,
stop_ui_rx: Receiver<()>,
stop_udp_tx: Sender<()>,
}
impl<'t> App<'t> {
@ -23,11 +24,13 @@ impl<'t> App<'t> {
display: &'t RwLock<PixelGrid>,
luma: &'t RwLock<ByteGrid>,
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 => {

View file

@ -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 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<PixelGrid>,
luma_ref: &RwLock<ByteGrid>,
) {
) -> 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(