diff --git a/Cargo.toml b/Cargo.toml index 8dfc43c..552a5ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,6 @@ path = "src/lib.rs" name = "webtest" path = "src/webtest.rs" +[[bin]] +name = "viewboard" +path = "src/viewboard.rs" diff --git a/src/main.rs b/src/main.rs index f803444..2ae34e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,14 +55,22 @@ fn main() { vec![ ( 3, - Box::new(move || { - proj1.lock().unwrap().shutter_on().unwrap(); + Box::new(move || match proj1.lock().unwrap().shutter_off() { + Ok(_) => println!("Receiving SHUTTER OFF message"), + Err(_e) => { + println!("Error receiving SHUTTER OFF message"); + "Error".to_string(); + } }), ), ( 4, - Box::new(move || { - proj2.lock().unwrap().shutter_on().unwrap(); + Box::new(move || match proj2.lock().unwrap().shutter_on() { + Ok(_) => println!("Receiving SHUTTER ON message"), + Err(_e) => { + println!("Error receiving SHUTTER ON message"); + "Error".to_string(); + } // proj2.lock().unwrap().unwrap_or((|| {})()); }), ), ], diff --git a/src/midi.rs b/src/midi.rs index 17a16c4..dd2528d 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -59,26 +59,25 @@ pub fn connect_loop_with_callback( move |_timestamp, message, _| { // Handle MIDI messages here let (command, note, _value) = (message[0], message[1], message[2]); - println!("Received MIDI message: {:?}", message); - if let (144, 3..=4) = (command, note) { - for (id, f) in calls.iter() { - if note == *id { - println!("match"); - f(); - } - let mut list = vec![]; - if note == 3 { - // Meh - list.push([command, 3, 2]); - list.push([command, 4, 3]); - } else { - list.push([command, 3, 1]); - list.push([command, 4, 4]); - } - for n in list.iter() { - // Meh - conn_clone.lock().unwrap().send(n).unwrap(); - conn_clone.lock().unwrap().send(n).unwrap(); + //println!("Received MIDI message: {:?}", message); + match (command, note) { + (144, 3..=4) => { + for (id, f) in calls.iter() { + if note == *id { + f(); + } + let mut list = vec![]; + if note == 3 { + list.push([command, 3, 2]); + list.push([command, 4, 3]); + } else { + list.push([command, 3, 1]); + list.push([command, 4, 4]); + } + for n in list.iter() { + conn_clone.lock().unwrap().send(n).unwrap(); + conn_clone.lock().unwrap().send(n).unwrap(); + } } } } diff --git a/src/viewboard.rs b/src/viewboard.rs new file mode 100644 index 0000000..baf6a27 --- /dev/null +++ b/src/viewboard.rs @@ -0,0 +1,77 @@ +#![allow(dead_code)] + +use crate::midi::create_output_interface; +use crate::midi::print_midi_interfaces; +use crate::midi::send_note_on; +use midir::MidiOutputConnection; +use std::sync::Arc; +use std::sync::Mutex; +use std::thread::sleep; +use std::time::Duration; + +/// A viewboard to represent the commanderpad +mod midi; +struct Placeholder {} + +struct RecvCommander<'a> { + /// The midi channel to communicate with the board + pub midi_address: &'a mut Arc>, + /// The list of available notes + pub list_notes: Vec, + /// The list of current notes values (if no other source has sent changes) + pub current_values: Vec, + /// The list of notes about to be sent + pub future_values: Vec, + /// The state of the board, wether it is visible or not + pub live: bool, +} + +impl RecvCommander<'_> { + fn new(output_interface: &mut Arc>) -> RecvCommander { + // Assert all vectors are of the same size + assert_eq!(70, 70); + RecvCommander { + midi_address: output_interface, + list_notes: (0..89).collect(), + current_values: vec![0; 89], + future_values: vec![0; 89], + live: false, + } + } + fn reset(&self) { + // let mut _addr = self.midi_address.lock().unwrap(); + for (i, note) in self.list_notes.iter().enumerate() { + send_note_on(&mut self.midi_address.lock().unwrap(), *note, 0); + if i % 20 == 0 { + sleep(Duration::from_millis(100)); + } + } + } + fn update(&self) { + for (i, note) in self.list_notes.iter().enumerate() { + let cr_note = self.current_values[i]; + let fu_note = self.future_values[i]; + if i % 20 == 0 { + sleep(Duration::from_millis(100)); + } + if fu_note == cr_note { + continue; + } + + send_note_on(&mut self.midi_address.lock().unwrap(), *note, fu_note); + } + } +} + +fn main() { + print_midi_interfaces(); + let output_interface = create_output_interface(3, "Rusty MIDI Commander Output"); + let mut binding = Arc::clone(&output_interface); + let board = RecvCommander::new(&mut binding); + // cleanup(&mut output_interface.lock().unwrap()); + board.reset(); + send_note_on(&mut output_interface.lock().unwrap(), 2, 1); + // sleep(Duration::from_millis(1000)); + board.update(); + loop {} +}