From de9f497a0023171dcbca8446c04a132c56de0749 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 20 Apr 2023 10:51:27 +0200 Subject: [PATCH] Added a macro --- README.md | 7 ++++++- config.toml | 8 ++++++++ src/main.rs | 59 +++++++++++++++++++++++++++-------------------------- src/midi.rs | 16 +++++++++++---- 4 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 config.toml diff --git a/README.md b/README.md index be34e3b..0b9c010 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # RustyMIDICommander -Rusty MIDI Commander is a lightweight and flexible Rust program for sending asynchronous commands in response to MIDI input. Built on top of popular Rust MIDI libraries, it provides a simple and modular framework for defining MIDI commands and executing them asynchronously. Whether you need to control web APIs, hardware devices, or other MIDI-enabled software, Rusty MIDI Commander makes it easy to build powerful and responsive MIDI control systems. \ No newline at end of file +Rusty MIDI Commander is a lightweight and flexible Rust program for sending asynchronous commands in response to MIDI input. Built on top of popular Rust MIDI libraries, it provides a simple and modular framework for defining MIDI commands and executing them asynchronously. Whether you need to control web APIs, hardware devices, or other MIDI-enabled software, Rusty MIDI Commander makes it easy to build powerful and responsive MIDI control systems. + + +# Macro Reading +https://veykril.github.io/tlborm/syntax-extensions/expansion.html +https://doc.rust-lang.org/book/ch19-06-macros.html \ No newline at end of file diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..0a11265 --- /dev/null +++ b/config.toml @@ -0,0 +1,8 @@ +[general] +midi_name = "Rusty MIDI Commander" + +[projector] +address = "127.0.0.1:8080" +login = "admin1" +password = "panasonic" + diff --git a/src/main.rs b/src/main.rs index efb4052..91e7076 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,31 @@ struct Args { midi_output: Option, } +macro_rules! list_actions { + ($($id1:literal , $proj1:ident, $action1:ident),+ ) => { + + + vec![ + $(( + $id1, + + Box::new(move || { + match $proj1.lock().unwrap().$action1() { + Ok(_) => {} + Err(e) => { + println!( + "[ERROR] An error occurend while sending a ({}) message", + stringify!($action1) + ); + println!(" {}", e) + } + }; + }), + ),)+ + ] + }; +} + fn main() { // Parse Arguments let args = Args::parse(); @@ -36,7 +61,7 @@ fn main() { midi_input: None, midi_output: None, } => { - midi::print_midi_interfaces(); + midi::print_midi_interfaces(&"No interface selected, please select one:".into()); return; } Args { @@ -47,12 +72,13 @@ fn main() { midi_input: _, midi_output: None, } => { - midi::print_midi_interfaces(); + midi::print_midi_interfaces(&"No interface selected, please select one:".into()); return; } _ => {} } - println!("{:?}", args); + + println!("[INFO] Connecting with args: {:?}", args); let proj = Arc::new(Mutex::new(projector::Projector::new( "127.0.0.1:8080", "admin1", @@ -72,31 +98,6 @@ fn main() { args.midi_input.unwrap(), "Rusty MIDI Commander Input", &mut output_interface, - vec![ - ( - 3, - Box::new(move || { - match proj1.lock().unwrap().shutter_on() { - Ok(_) => {} - Err(e) => { - println!( - "[ERROR] An error occurend while sending a (Shutter on) message" - ); - println!(" {}", e) - } - }; - }), - ), - ( - 4, - Box::new(move || match proj2.lock().unwrap().shutter_off() { - Ok(_) => {} - Err(e) => { - println!("[ERROR] An error occurend while sending a (Shutter off) message"); - println!(" {}", e) - } - }), - ), - ], + list_actions!(3, proj1, shutter_on, 4, proj2, shutter_off), ); } diff --git a/src/midi.rs b/src/midi.rs index afbdbf8..ff8f96d 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -12,7 +12,7 @@ lazy_static! { static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)]; } -pub fn print_midi_interfaces() { +pub fn print_midi_interfaces(error_message: &String) { // Create MIDI input and output instances let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap(); let midi_out = MidiOutput::new("Rusty MIDI Commander Output").unwrap(); @@ -36,7 +36,7 @@ pub fn print_midi_interfaces() { ); return; } - (_, _) => println!("No interface selected, please select one:"), + (_, _) => println!("{}", error_message), } println!("[Input]"); @@ -65,6 +65,10 @@ pub fn connect_loop_with_callback( let input_ports = input_interface.ports(); // Connect to port + if port >= input_ports.len() { + print_midi_interfaces(&"[ERROR] Midi input port is out of range".to_string()); + std::process::exit(1); + } let input_port = &input_ports[port]; let conn_clone = Arc::clone(&out_port); // let fn_clone = Arc::clone(&calls); @@ -115,6 +119,12 @@ pub fn create_output_interface( None => MidiOutput::new(name).unwrap(), Some(_) => MidiOutput::new(name).unwrap(), }; + if port.unwrap() >= output_interface.ports().len() { + print_midi_interfaces( + &"[ERROR] The midi output interface selected is out of bounds".into(), + ); + std::process::exit(1); + } let output_port = &output_interface.ports()[port.unwrap()]; Arc::new(Mutex::new( output_interface @@ -126,8 +136,6 @@ pub fn create_output_interface( pub fn cleanup( out_port: &mut MidiOutputConnection, /*, range: &dyn std::iter::Iterator*/ ) { - // let _messages = vec![[0xB0, 0x7B, 0x00], [0xB0, 0x79, 0x00]]; - // for message in messages.iter() { // co.send(message).unwrap(); // }