Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
65
src/main.rs
65
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<dyn serialport::SerialPort>,
|
||||
pub port: Box<dyn serialport::SerialPort>,
|
||||
}
|
||||
|
||||
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<AppSongState>,
|
||||
tx: Sender<SerialCommand>,
|
||||
rx: Receiver<SerialStatus>,
|
||||
connected: Option<String>,
|
||||
}
|
||||
|
||||
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<SerialCommand>) {
|
||||
fn spawn_serial_worker(rx: Receiver<SerialCommand>, tx: Sender<SerialStatus>) {
|
||||
thread::spawn(move || {
|
||||
let _ = tx.send(SerialStatus::SerialDisconnected);
|
||||
let mut device: Option<SerialDevice> = 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("<unknown serial port>".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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user