diff --git a/src/main.rs b/src/main.rs index 220d443..8ccda34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,6 @@ use std::{ - sync::{ - Arc, Mutex, - mpsc::{self, Receiver, Sender}, - }, - thread::{self, Thread}, + sync::mpsc::{self, Receiver, Sender}, + thread::{self}, time::Duration, }; @@ -11,6 +8,7 @@ use anyhow::{Result, anyhow}; use macroquad::{ color::*, miniquad::{error, info}, + window::set_fullscreen, }; use midly::Smf; use serialport::SerialPortType; @@ -24,7 +22,7 @@ pub enum Instruction { } pub struct SerialDevice { - port: Box, + pub port: Box, } const CHANNEL_COUNT: u8 = 4; @@ -165,29 +163,48 @@ pub enum SerialCommand { Stop, } +pub enum SerialStatus { + SerialConnected { name: String }, + SerialDisconnected, +} + struct App { song_state: Option, tx: Sender, + rx: Receiver, + connected: Option, } impl App { pub fn new() -> Self { - let (tx, rx) = mpsc::channel(); - Self::spawn_serial_worker(rx); + let (tx_cmd, rx_cmd) = mpsc::channel(); + let (tx_status, rx_status) = mpsc::channel(); + Self::spawn_serial_worker(rx_cmd, tx_status); Self { song_state: None, - tx, + tx: tx_cmd, + rx: rx_status, + connected: None, } } - fn spawn_serial_worker(rx: Receiver) { + fn spawn_serial_worker(rx: Receiver, tx: Sender) { thread::spawn(move || { + let _ = tx.send(SerialStatus::SerialDisconnected); let mut device: Option = None; loop { if device.is_none() { match SerialDevice::new() { Ok(new_device) => { info!("Serial device connected"); + let _ = tx.send(SerialStatus::SerialConnected { + name: device + .as_ref() + .unwrap() + .port + .name() + .unwrap_or("".to_string()), + }); device = Some(new_device); } Err(_err) => { @@ -205,12 +222,14 @@ impl App { if let Err(e) = dev.execute(ch, instr) { error!("Disconnected: {e}"); device = None; + let _ = tx.send(SerialStatus::SerialDisconnected); } } SerialCommand::Stop => { if let Err(e) = dev.stop() { error!("Disconnected: {e}"); device = None; + let _ = tx.send(SerialStatus::SerialDisconnected); } } } @@ -223,13 +242,21 @@ impl App { }); } - pub async fn run(&self) { + pub async fn run(&mut self) { loop { self.update().await; } } - async fn update(&self) { + async fn update(&mut self) { + match self.rx.recv_timeout(Duration::from_millis(100)) { + Ok(status) => match status { + SerialStatus::SerialDisconnected => self.connected = None, + SerialStatus::SerialConnected { name } => self.connected = Some(name), + }, + _ => {} + } + macroquad::window::clear_background(WHITE); macroquad::shapes::draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE); @@ -241,7 +268,16 @@ impl App { GREEN, ); - macroquad::text::draw_text("Hello, Macroquad!", 20.0, 20.0, 30.0, DARKGRAY); + macroquad::text::draw_text( + match &self.connected { + Some(name) => &name, + None => "Serial disconnected", + }, + 20.0, + 20.0, + 30.0, + DARKGRAY, + ); macroquad::window::next_frame().await } @@ -249,6 +285,7 @@ impl App { #[macroquad::main("singer")] async fn main() { - let app = App::new(); + set_fullscreen(true); + let mut app = App::new(); app.run().await; }