From 4290e0da9ca78c4c09c5294b5a54fec40672b777 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 4 Apr 2023 11:50:13 +0200 Subject: [PATCH] Working Placeholder, now let's refactor EVERYTHING --- src/midi.rs | 36 ++++++++++++++++++++++++++---------- src/webtest.rs | 31 +++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/midi.rs b/src/midi.rs index 79e9958..cedec07 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -31,7 +31,7 @@ pub fn connect_loop_with_callback( port: usize, name: &str, out_port: &mut Arc>, - calls: Vec<(u8, Box ()>)>, + calls: Vec<(u8, Box)>, ) { // Create MIDI input instance let input_interface = MidiInput::new(name).unwrap(); @@ -42,22 +42,38 @@ pub fn connect_loop_with_callback( // Connect to port let input_port = &input_ports[port]; let conn_clone = Arc::clone(&out_port); + // let fn_clone = Arc::clone(&calls); let _connection = input_interface .connect( input_port, name, move |_timestamp, message, _| { // Handle MIDI messages here - let (a, note, value) = (message[0], message[1], message[2]); + let (command, note, _value) = (message[0], message[1], message[2]); println!("Received MIDI message: {:?}", message); - // for (id, _) in calls.iter() { - // println!("{:?}", id); - // } - conn_clone - .lock() - .unwrap() - .send(&[a, note, (value + 1) % 7]) - .unwrap(); + match (command, note) { + (144, 3..=4) => { + for (id, f) in calls.iter() { + if note == *id { + println!("match"); + 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/webtest.rs b/src/webtest.rs index 913e6bf..cf94de2 100644 --- a/src/webtest.rs +++ b/src/webtest.rs @@ -13,19 +13,30 @@ async fn get_osd(_req: HttpRequest) -> impl Responder { HttpResponse::Ok().body(response) } -async fn debug_handler(req: HttpRequest/*,config: web::Data*/) -> HttpResponse { +async fn debug_handler(req: HttpRequest /*,config: web::Data*/) -> HttpResponse { if let Some(header_value) = req.headers().get("Authorization") { if let Ok(header_value) = header_value.to_str() { if let Some(credentials) = header_value.strip_prefix("Basic ") { - // Authentication succeeded - println!("Debug message! {:?}", general_purpose::STANDARD_NO_PAD.decode(credentials).unwrap_or("".into())); - return HttpResponse::Ok().body(general_purpose::STANDARD_NO_PAD.decode(credentials).unwrap_or("".into())); - } + // Authentication succeeded + println!( + "Debug message! {:?}", + general_purpose::STANDARD_NO_PAD + .decode(credentials) + .unwrap_or("".into()) + ); + return HttpResponse::Ok().body( + general_purpose::STANDARD_NO_PAD + .decode(credentials) + .unwrap_or("".into()), + ); } } - + } + // Authentication failed - HttpResponse::Unauthorized().append_header(("WWW-Authenticate","Basic realm=\"Restricted area\"")).finish() + HttpResponse::Unauthorized() + .append_header(("WWW-Authenticate", "Basic realm=\"Restricted area\"")) + .finish() } // in your `App` configuration @@ -33,12 +44,12 @@ async fn debug_handler(req: HttpRequest/*,config: web::Data*/) -> HttpRe #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { - App::new().route("/debug", web::get().to(debug_handler)) + App::new() + .route("/debug", web::get().to(debug_handler)) .route("/cgi-bin/proj_ctl.cgi", web::get().to(proj_ctl)) .route("/cgi-bin/get_osd.cgi", web::get().to(get_osd)) - }) .bind("127.0.0.1:8080")? .run() .await -} \ No newline at end of file +}