mirror of
https://github.com/kaesaecracker/servicepoint-simulator.git
synced 2025-01-18 10:30:14 +01:00
stop on hard reset
This commit is contained in:
parent
89c5c44e28
commit
69c07dd733
10
src/gui.rs
10
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<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 => {
|
||||
|
@ -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 {
|
||||
|
|
31
src/main.rs
31
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<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(
|
||||
|
|
Loading…
Reference in a new issue