Serial thread and tx channel, more App state work
Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
88
src/main.rs
88
src/main.rs
@@ -1,5 +1,18 @@
|
|||||||
|
use std::{
|
||||||
|
sync::{
|
||||||
|
Arc, Mutex,
|
||||||
|
mpsc::{self, Receiver, Sender},
|
||||||
|
},
|
||||||
|
thread::{self, Thread},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use macroquad::color::*;
|
use macroquad::{
|
||||||
|
color::*,
|
||||||
|
miniquad::{error, info},
|
||||||
|
};
|
||||||
|
use midly::Smf;
|
||||||
use serialport::SerialPortType;
|
use serialport::SerialPortType;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
@@ -133,14 +146,81 @@ impl Song {
|
|||||||
pattern_order: Vec::new(),
|
pattern_order: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_midi(_smf: Smf) -> Result<Self> {
|
||||||
|
todo!("TBD");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
struct AppSongState {
|
||||||
struct App {}
|
pub song: Song,
|
||||||
|
pub current_pattern: usize,
|
||||||
|
pub current_row: usize,
|
||||||
|
pub current_column: u8,
|
||||||
|
pub playing: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum SerialCommand {
|
||||||
|
Instruction(u8, Instruction),
|
||||||
|
Stop,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct App {
|
||||||
|
song_state: Option<AppSongState>,
|
||||||
|
tx: Sender<SerialCommand>,
|
||||||
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { ..App::default() }
|
let (tx, rx) = mpsc::channel();
|
||||||
|
Self::spawn_serial_worker(rx);
|
||||||
|
Self {
|
||||||
|
song_state: None,
|
||||||
|
tx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_serial_worker(rx: Receiver<SerialCommand>) {
|
||||||
|
thread::spawn(move || {
|
||||||
|
let mut device: Option<SerialDevice> = None;
|
||||||
|
loop {
|
||||||
|
if device.is_none() {
|
||||||
|
match SerialDevice::new() {
|
||||||
|
Ok(new_device) => {
|
||||||
|
info!("Serial device connected");
|
||||||
|
device = Some(new_device);
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match rx.recv_timeout(Duration::from_millis(100)) {
|
||||||
|
Ok(command) => {
|
||||||
|
if let Some(dev) = device.as_mut() {
|
||||||
|
match command {
|
||||||
|
SerialCommand::Instruction(ch, instr) => {
|
||||||
|
if let Err(e) = dev.execute(ch, instr) {
|
||||||
|
error!("Disconnected: {e}");
|
||||||
|
device = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SerialCommand::Stop => {
|
||||||
|
if let Err(e) = dev.stop() {
|
||||||
|
error!("Disconnected: {e}");
|
||||||
|
device = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(mpsc::RecvTimeoutError::Timeout) => {}
|
||||||
|
Err(_) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(&self) {
|
pub async fn run(&self) {
|
||||||
|
|||||||
Reference in New Issue
Block a user