From a2bc751c9a3de5ee485f0ca14290bbc6b7e4ba73 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 7 Dec 2024 09:00:35 -0600 Subject: [PATCH] Adds a couple of better docs --- README.md | 12 +++++++++++- src/main.rs | 38 ++++++++++++++++++-------------------- src/midi.rs | 45 +++++++++++++++++++++++---------------------- src/projector.rs | 19 ++++++++++++++++--- src/webtest.rs | 4 ++-- 5 files changed, 70 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index be34e3b..59b03a9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # 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. + + +# Installation +Depends on: + - `librust-alsa-dev` diff --git a/src/main.rs b/src/main.rs index 51347e0..fbe8b8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,15 @@ -// In this organization, src/main.rs is the entry point for the program. -// This file should set up the MIDI input and output devices, and then -// start a loop to receive and process MIDI messages. -// It should also use the commands module to send commands asynchronously. -// use crate::midi::print_midi_interfaces; -use crate::midi::cleanup; -use crate::midi::connect_loop_with_callback; -use crate::midi::create_output_interface; -use crate::midi::prepare; +//! In this organization, src/main.rs is the entry point for the program. +//! This file should set up the MIDI input and output devices, and then +//! start a loop to receive and process MIDI messages. +//! It should also use the commands module to send commands asynchronously. +//! use crate::midi::print_midi_interfaces; + +#![warn(clippy::missing_docs_in_private_items)] + use clap::Parser; +use midi::cleanup; +use midi::connect_loop_with_callback; +use midi::create_output_interface; use std::sync::Arc; use std::sync::Mutex; @@ -19,7 +21,7 @@ use midir::MidiOutputConnection; // use midir::MidiOutput; pub mod midi; mod projector; -/// Simple program to greet a person + #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { @@ -30,16 +32,12 @@ struct Args { fn main() { // Parse Arguments let args = Args::parse(); - match args { - Args { - midi_interface: None, - } => { - midi::print_midi_interfaces(); - return; - } - Args { - midi_interface: Some(_), - } => {} + if let Args { + midi_interface: None, + } = args + { + midi::print_midi_interfaces(); + return; } println!("{:?}", args); let proj = Arc::new(Mutex::new(projector::Projector::new( diff --git a/src/midi.rs b/src/midi.rs index cedec07..23b3918 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -7,6 +7,8 @@ use crate::MidiOutputConnection; use lazy_static::lazy_static; use std::sync::Arc; use std::sync::Mutex; +use std::thread::sleep; +use std::time::Duration; lazy_static! { static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)]; @@ -41,7 +43,7 @@ pub fn connect_loop_with_callback( // Connect to port let input_port = &input_ports[port]; - let conn_clone = Arc::clone(&out_port); + let conn_clone = Arc::clone(out_port); // let fn_clone = Arc::clone(&calls); let _connection = input_interface .connect( @@ -51,34 +53,33 @@ pub fn connect_loop_with_callback( // Handle MIDI messages here let (command, note, _value) = (message[0], message[1], message[2]); println!("Received MIDI message: {:?}", message); - 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(); - } + 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 { + 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(); } } - (_, _) => (), } }, (), ) .unwrap(); - loop {} + loop { + sleep(Duration::from_millis(300)); + } } pub fn create_output_interface(port: usize, name: &str) -> Arc> { diff --git a/src/projector.rs b/src/projector.rs index d3aedf4..91a988e 100644 --- a/src/projector.rs +++ b/src/projector.rs @@ -1,18 +1,24 @@ +//! Contains all projector related commands + use base64::{engine::general_purpose, Engine as _}; use reqwest::blocking::{Client, RequestBuilder}; -use std::error::Error; - -use std::time::Duration; +use std::{error::Error, time::Duration}; +/// The standard wait time static WAIT_TIME: f64 = 0.7; +/// A projector and all info to connect to it pub struct Projector { + /// The projector ip ip: String, + /// The username to connect to it username: String, + /// The password for the connection password: String, } impl Projector { + /// Create a new projector with the basic information pub fn new(ip: &str, username: &str, password: &str) -> Projector { Projector { ip: ip.to_string(), @@ -21,12 +27,14 @@ impl Projector { } } + /// Make the authentication header fn make_auth_header(&self) -> String { let auth = format!("{}:{}", self.username, self.password); let encoded_auth = general_purpose::STANDARD_NO_PAD.encode(auth); format!("Basic {}", encoded_auth) } + /// Make a control command fn make_url(&self, cmd: &str) -> String { format!( "http://{}/cgi-bin/proj_ctl.cgi?key={}&lang=e&osd=off", @@ -34,6 +42,7 @@ impl Projector { ) } + /// Make a request from a url fn make_request(&self, url: &str, headers: &str) -> Result> { let client = Client::new(); let builder = client @@ -43,6 +52,7 @@ impl Projector { Ok(builder) } + /// Make the actual request pub fn open_url(&self, cmd: &str) -> Result> { let headers = self.make_auth_header(); let url = self.make_url(cmd); @@ -52,14 +62,17 @@ impl Projector { Ok(body) } + /// A request to turn the projector on pub fn shutter_on(&self) -> Result> { self.open_url("shutter_on") } + /// A request to turn the projector off pub fn shutter_off(&self) -> Result> { self.open_url("shutter_off") } + /// The request to the the osd pub fn get_osd(&self) -> Result> { let headers = self.make_auth_header(); let url = format!("http://{}/cgi-bin/get_osd.cgi?lang=e", self.ip); diff --git a/src/webtest.rs b/src/webtest.rs index cf94de2..fa8fec0 100644 --- a/src/webtest.rs +++ b/src/webtest.rs @@ -2,13 +2,13 @@ use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder}; use base64::{engine::general_purpose, Engine as _}; async fn proj_ctl(_req: HttpRequest) -> impl Responder { - let response = format!("Projector received command"); + let response = "Projector received command".to_string(); println!("{}", response); HttpResponse::Ok().body(response) } async fn get_osd(_req: HttpRequest) -> impl Responder { - let response = format!("OSD status for projector: ON"); + let response = "OSD status for projector: ON".to_string(); println!("{}", response); HttpResponse::Ok().body(response) }