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::{
|
use std::{
|
||||||
sync::{
|
sync::mpsc::{self, Receiver, Sender},
|
||||||
Arc, Mutex,
|
thread::{self},
|
||||||
mpsc::{self, Receiver, Sender},
|
|
||||||
},
|
|
||||||
thread::{self, Thread},
|
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -11,6 +8,7 @@ use anyhow::{Result, anyhow};
|
|||||||
use macroquad::{
|
use macroquad::{
|
||||||
color::*,
|
color::*,
|
||||||
miniquad::{error, info},
|
miniquad::{error, info},
|
||||||
|
window::set_fullscreen,
|
||||||
};
|
};
|
||||||
use midly::Smf;
|
use midly::Smf;
|
||||||
use serialport::SerialPortType;
|
use serialport::SerialPortType;
|
||||||
@@ -24,7 +22,7 @@ pub enum Instruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct SerialDevice {
|
pub struct SerialDevice {
|
||||||
port: Box<dyn serialport::SerialPort>,
|
pub port: Box<dyn serialport::SerialPort>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const CHANNEL_COUNT: u8 = 4;
|
const CHANNEL_COUNT: u8 = 4;
|
||||||
@@ -165,29 +163,48 @@ pub enum SerialCommand {
|
|||||||
Stop,
|
Stop,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum SerialStatus {
|
||||||
|
SerialConnected { name: String },
|
||||||
|
SerialDisconnected,
|
||||||
|
}
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
song_state: Option<AppSongState>,
|
song_state: Option<AppSongState>,
|
||||||
tx: Sender<SerialCommand>,
|
tx: Sender<SerialCommand>,
|
||||||
|
rx: Receiver<SerialStatus>,
|
||||||
|
connected: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx_cmd, rx_cmd) = mpsc::channel();
|
||||||
Self::spawn_serial_worker(rx);
|
let (tx_status, rx_status) = mpsc::channel();
|
||||||
|
Self::spawn_serial_worker(rx_cmd, tx_status);
|
||||||
Self {
|
Self {
|
||||||
song_state: None,
|
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 || {
|
thread::spawn(move || {
|
||||||
|
let _ = tx.send(SerialStatus::SerialDisconnected);
|
||||||
let mut device: Option<SerialDevice> = None;
|
let mut device: Option<SerialDevice> = None;
|
||||||
loop {
|
loop {
|
||||||
if device.is_none() {
|
if device.is_none() {
|
||||||
match SerialDevice::new() {
|
match SerialDevice::new() {
|
||||||
Ok(new_device) => {
|
Ok(new_device) => {
|
||||||
info!("Serial device connected");
|
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);
|
device = Some(new_device);
|
||||||
}
|
}
|
||||||
Err(_err) => {
|
Err(_err) => {
|
||||||
@@ -205,12 +222,14 @@ impl App {
|
|||||||
if let Err(e) = dev.execute(ch, instr) {
|
if let Err(e) = dev.execute(ch, instr) {
|
||||||
error!("Disconnected: {e}");
|
error!("Disconnected: {e}");
|
||||||
device = None;
|
device = None;
|
||||||
|
let _ = tx.send(SerialStatus::SerialDisconnected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SerialCommand::Stop => {
|
SerialCommand::Stop => {
|
||||||
if let Err(e) = dev.stop() {
|
if let Err(e) = dev.stop() {
|
||||||
error!("Disconnected: {e}");
|
error!("Disconnected: {e}");
|
||||||
device = None;
|
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 {
|
loop {
|
||||||
self.update().await;
|
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::window::clear_background(WHITE);
|
||||||
|
|
||||||
macroquad::shapes::draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
|
macroquad::shapes::draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
|
||||||
@@ -241,7 +268,16 @@ impl App {
|
|||||||
GREEN,
|
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
|
macroquad::window::next_frame().await
|
||||||
}
|
}
|
||||||
@@ -249,6 +285,7 @@ impl App {
|
|||||||
|
|
||||||
#[macroquad::main("singer")]
|
#[macroquad::main("singer")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let app = App::new();
|
set_fullscreen(true);
|
||||||
|
let mut app = App::new();
|
||||||
app.run().await;
|
app.run().await;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user