Adds a couple of better docs

This commit is contained in:
Dandaedre 2024-12-07 09:00:35 -06:00
parent 94c22474ad
commit a2bc751c9a
5 changed files with 70 additions and 48 deletions

View File

@ -1,3 +1,13 @@
# RustyMIDICommander # 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. 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`

View File

@ -1,13 +1,15 @@
// In this organization, src/main.rs is the entry point for the program. //! 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 //! This file should set up the MIDI input and output devices, and then
// start a loop to receive and process MIDI messages. //! start a loop to receive and process MIDI messages.
// It should also use the commands module to send commands asynchronously. //! It should also use the commands module to send commands asynchronously.
// use crate::midi::print_midi_interfaces; //! use crate::midi::print_midi_interfaces;
use crate::midi::cleanup;
use crate::midi::connect_loop_with_callback; #![warn(clippy::missing_docs_in_private_items)]
use crate::midi::create_output_interface;
use crate::midi::prepare;
use clap::Parser; use clap::Parser;
use midi::cleanup;
use midi::connect_loop_with_callback;
use midi::create_output_interface;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
@ -19,7 +21,7 @@ use midir::MidiOutputConnection;
// use midir::MidiOutput; // use midir::MidiOutput;
pub mod midi; pub mod midi;
mod projector; mod projector;
/// Simple program to greet a person
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Args { struct Args {
@ -30,16 +32,12 @@ struct Args {
fn main() { fn main() {
// Parse Arguments // Parse Arguments
let args = Args::parse(); let args = Args::parse();
match args { if let Args {
Args { midi_interface: None,
midi_interface: None, } = args
} => { {
midi::print_midi_interfaces(); midi::print_midi_interfaces();
return; return;
}
Args {
midi_interface: Some(_),
} => {}
} }
println!("{:?}", args); println!("{:?}", args);
let proj = Arc::new(Mutex::new(projector::Projector::new( let proj = Arc::new(Mutex::new(projector::Projector::new(

View File

@ -7,6 +7,8 @@ use crate::MidiOutputConnection;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
use std::thread::sleep;
use std::time::Duration;
lazy_static! { lazy_static! {
static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)]; 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 // Connect to port
let input_port = &input_ports[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 fn_clone = Arc::clone(&calls);
let _connection = input_interface let _connection = input_interface
.connect( .connect(
@ -51,34 +53,33 @@ pub fn connect_loop_with_callback(
// Handle MIDI messages here // Handle MIDI messages here
let (command, note, _value) = (message[0], message[1], message[2]); let (command, note, _value) = (message[0], message[1], message[2]);
println!("Received MIDI message: {:?}", message); println!("Received MIDI message: {:?}", message);
match (command, note) { if let (144, 3..=4) = (command, note) {
(144, 3..=4) => { for (id, f) in calls.iter() {
for (id, f) in calls.iter() { if note == *id {
if note == *id { println!("match");
println!("match"); f();
f(); }
} let mut list = vec![];
let mut list = vec![]; if note == 3 {
if note == 3 { list.push([command, 3, 2]);
list.push([command, 3, 2]); list.push([command, 4, 3]);
list.push([command, 4, 3]); } else {
} else { list.push([command, 3, 1]);
list.push([command, 3, 1]); list.push([command, 4, 4]);
list.push([command, 4, 4]); }
} for n in list.iter() {
for n in list.iter() { conn_clone.lock().unwrap().send(n).unwrap();
conn_clone.lock().unwrap().send(n).unwrap(); conn_clone.lock().unwrap().send(n).unwrap();
conn_clone.lock().unwrap().send(n).unwrap();
}
} }
} }
(_, _) => (),
} }
}, },
(), (),
) )
.unwrap(); .unwrap();
loop {} loop {
sleep(Duration::from_millis(300));
}
} }
pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> { pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> {

View File

@ -1,18 +1,24 @@
//! Contains all projector related commands
use base64::{engine::general_purpose, Engine as _}; use base64::{engine::general_purpose, Engine as _};
use reqwest::blocking::{Client, RequestBuilder}; use reqwest::blocking::{Client, RequestBuilder};
use std::error::Error; use std::{error::Error, time::Duration};
use std::time::Duration;
/// The standard wait time
static WAIT_TIME: f64 = 0.7; static WAIT_TIME: f64 = 0.7;
/// A projector and all info to connect to it
pub struct Projector { pub struct Projector {
/// The projector ip
ip: String, ip: String,
/// The username to connect to it
username: String, username: String,
/// The password for the connection
password: String, password: String,
} }
impl Projector { impl Projector {
/// Create a new projector with the basic information
pub fn new(ip: &str, username: &str, password: &str) -> Projector { pub fn new(ip: &str, username: &str, password: &str) -> Projector {
Projector { Projector {
ip: ip.to_string(), ip: ip.to_string(),
@ -21,12 +27,14 @@ impl Projector {
} }
} }
/// Make the authentication header
fn make_auth_header(&self) -> String { fn make_auth_header(&self) -> String {
let auth = format!("{}:{}", self.username, self.password); let auth = format!("{}:{}", self.username, self.password);
let encoded_auth = general_purpose::STANDARD_NO_PAD.encode(auth); let encoded_auth = general_purpose::STANDARD_NO_PAD.encode(auth);
format!("Basic {}", encoded_auth) format!("Basic {}", encoded_auth)
} }
/// Make a control command
fn make_url(&self, cmd: &str) -> String { fn make_url(&self, cmd: &str) -> String {
format!( format!(
"http://{}/cgi-bin/proj_ctl.cgi?key={}&lang=e&osd=off", "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<RequestBuilder, Box<dyn Error>> { fn make_request(&self, url: &str, headers: &str) -> Result<RequestBuilder, Box<dyn Error>> {
let client = Client::new(); let client = Client::new();
let builder = client let builder = client
@ -43,6 +52,7 @@ impl Projector {
Ok(builder) Ok(builder)
} }
/// Make the actual request
pub fn open_url(&self, cmd: &str) -> Result<String, Box<dyn Error>> { pub fn open_url(&self, cmd: &str) -> Result<String, Box<dyn Error>> {
let headers = self.make_auth_header(); let headers = self.make_auth_header();
let url = self.make_url(cmd); let url = self.make_url(cmd);
@ -52,14 +62,17 @@ impl Projector {
Ok(body) Ok(body)
} }
/// A request to turn the projector on
pub fn shutter_on(&self) -> Result<String, Box<dyn Error>> { pub fn shutter_on(&self) -> Result<String, Box<dyn Error>> {
self.open_url("shutter_on") self.open_url("shutter_on")
} }
/// A request to turn the projector off
pub fn shutter_off(&self) -> Result<String, Box<dyn Error>> { pub fn shutter_off(&self) -> Result<String, Box<dyn Error>> {
self.open_url("shutter_off") self.open_url("shutter_off")
} }
/// The request to the the osd
pub fn get_osd(&self) -> Result<String, Box<dyn Error>> { pub fn get_osd(&self) -> Result<String, Box<dyn Error>> {
let headers = self.make_auth_header(); let headers = self.make_auth_header();
let url = format!("http://{}/cgi-bin/get_osd.cgi?lang=e", self.ip); let url = format!("http://{}/cgi-bin/get_osd.cgi?lang=e", self.ip);

View File

@ -2,13 +2,13 @@ use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use base64::{engine::general_purpose, Engine as _}; use base64::{engine::general_purpose, Engine as _};
async fn proj_ctl(_req: HttpRequest) -> impl Responder { async fn proj_ctl(_req: HttpRequest) -> impl Responder {
let response = format!("Projector received command"); let response = "Projector received command".to_string();
println!("{}", response); println!("{}", response);
HttpResponse::Ok().body(response) HttpResponse::Ok().body(response)
} }
async fn get_osd(_req: HttpRequest) -> impl Responder { 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); println!("{}", response);
HttpResponse::Ok().body(response) HttpResponse::Ok().body(response)
} }