Blinking lights
This commit is contained in:
parent
4078b602ec
commit
f0b63d0572
14
Cargo.toml
14
Cargo.toml
|
|
@ -2,9 +2,21 @@
|
|||
name = "rusty_midicommander"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
default-run = "rusty_midicommander"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
midir = "0.9.1"
|
||||
clap = { version = "4.2.1", features = ["derive","color"] }
|
||||
actix-web = "4.3.1"
|
||||
reqwest = { version = "0.11.16", features = ["blocking"] }
|
||||
base64 = "0.21.0"
|
||||
lazy_static = "1.4.0"
|
||||
|
||||
[[bin]]
|
||||
name = "rusty_midicommander"
|
||||
path = "src/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "webtest"
|
||||
path = "src/webtest.rs"
|
||||
110
src/main.rs
110
src/main.rs
|
|
@ -3,19 +3,22 @@
|
|||
// 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;
|
||||
use clap::Parser;
|
||||
|
||||
use midir::MidiInput;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use midir::MidiInput;
|
||||
|
||||
use midir::MidiOutput;
|
||||
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)]
|
||||
|
|
@ -38,82 +41,39 @@ fn main() {
|
|||
midi_interface: Some(_),
|
||||
} => {}
|
||||
}
|
||||
// println!("{:?}", args);
|
||||
println!("{:?}", args);
|
||||
let proj = Arc::new(Mutex::new(projector::Projector::new(
|
||||
"127.0.0.1:8080",
|
||||
"admin1",
|
||||
"panasonic",
|
||||
)));
|
||||
let proj1 = Arc::clone(&proj);
|
||||
let proj2 = Arc::clone(&proj);
|
||||
|
||||
// proj.lock().unwrap().shutter_on().unwrap();
|
||||
// proj.lock().unwrap().shutter_off().unwrap();
|
||||
// proj.lock().unwrap().get_osd().unwrap();
|
||||
|
||||
let mut output_interface =
|
||||
create_output_interface(args.midi_interface.unwrap(), "Rusty MIDI Commander Output");
|
||||
cleanup(&mut output_interface.lock().unwrap());
|
||||
connect_with_callback(
|
||||
connect_loop_with_callback(
|
||||
args.midi_interface.unwrap(),
|
||||
"Rusty MIDI Commander Input",
|
||||
&mut output_interface,
|
||||
vec![
|
||||
(
|
||||
3,
|
||||
Box::new(move || {
|
||||
proj1.lock().unwrap().shutter_on().unwrap();
|
||||
}),
|
||||
),
|
||||
(
|
||||
4,
|
||||
Box::new(move || {
|
||||
proj2.lock().unwrap().shutter_on().unwrap();
|
||||
}),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
pub fn connect_with_callback(
|
||||
port: usize,
|
||||
name: &str,
|
||||
out_port: &mut Arc<Mutex<MidiOutputConnection>>,
|
||||
)
|
||||
// where
|
||||
// F: FnMut(u64, &[u8], &mut T) + Send + 'static,
|
||||
{
|
||||
// Create MIDI input instance
|
||||
let input_interface = MidiInput::new(name).unwrap();
|
||||
|
||||
// Get available MIDI input ports
|
||||
let input_ports = input_interface.ports();
|
||||
|
||||
// Connect to port
|
||||
let input_port = &input_ports[port];
|
||||
let conn_clone = Arc::clone(&out_port);
|
||||
let _connection = input_interface
|
||||
.connect(
|
||||
input_port,
|
||||
name,
|
||||
move |_timestamp, message, _| {
|
||||
// Handle MIDI messages here
|
||||
println!("Received MIDI message: {:?}", message);
|
||||
conn_clone.lock().unwrap().send(message).unwrap();
|
||||
},
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
loop {}
|
||||
}
|
||||
|
||||
pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> {
|
||||
let output_interface = MidiOutput::new(name).unwrap();
|
||||
let output_port = &output_interface.ports()[port];
|
||||
Arc::new(Mutex::new(
|
||||
output_interface
|
||||
.connect(output_port, "Rusty MIDI Commander Output")
|
||||
.unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn cleanup(
|
||||
out_port: &mut MidiOutputConnection, /*, range: &dyn std::iter::Iterator<Item = usize>*/
|
||||
) {
|
||||
// let _messages = vec![[0xB0, 0x7B, 0x00], [0xB0, 0x79, 0x00]];
|
||||
|
||||
// for message in messages.iter() {
|
||||
// co.send(message).unwrap();
|
||||
// }
|
||||
for n in 0..=63 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
for n in 82..=89 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_note_on(out_port: &mut MidiOutputConnection, pitch: u8, velocity: u8) {
|
||||
let msg = &[0x90, pitch, velocity];
|
||||
out_port.send(msg).unwrap();
|
||||
}
|
||||
|
||||
pub fn prepare(out_port: &mut MidiOutputConnection, notes_values: Vec<(u8, u8)>) {
|
||||
for (note, val) in notes_values.iter() {
|
||||
send_note_on(out_port, *note, *val);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
src/midi.rs
27
src/midi.rs
|
|
@ -4,10 +4,14 @@
|
|||
use crate::MidiInput;
|
||||
use crate::MidiOutput;
|
||||
use crate::MidiOutputConnection;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
lazy_static! {
|
||||
static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)];
|
||||
}
|
||||
|
||||
pub fn print_midi_interfaces() {
|
||||
// Create MIDI input and output instances
|
||||
let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap();
|
||||
|
|
@ -23,14 +27,12 @@ pub fn print_midi_interfaces() {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn connect_with_callback(
|
||||
pub fn connect_loop_with_callback(
|
||||
port: usize,
|
||||
name: &str,
|
||||
out_port: &mut Arc<Mutex<MidiOutputConnection>>,
|
||||
)
|
||||
// where
|
||||
// F: FnMut(u64, &[u8], &mut T) + Send + 'static,
|
||||
{
|
||||
calls: Vec<(u8, Box<dyn Fn() -> ()>)>,
|
||||
) {
|
||||
// Create MIDI input instance
|
||||
let input_interface = MidiInput::new(name).unwrap();
|
||||
|
||||
|
|
@ -46,8 +48,16 @@ pub fn connect_with_callback(
|
|||
name,
|
||||
move |_timestamp, message, _| {
|
||||
// Handle MIDI messages here
|
||||
let (a, note, value) = (message[0], message[1], message[2]);
|
||||
println!("Received MIDI message: {:?}", message);
|
||||
conn_clone.lock().unwrap().send(message).unwrap();
|
||||
// for (id, _) in calls.iter() {
|
||||
// println!("{:?}", id);
|
||||
// }
|
||||
conn_clone
|
||||
.lock()
|
||||
.unwrap()
|
||||
.send(&[a, note, (value + 1) % 7])
|
||||
.unwrap();
|
||||
},
|
||||
(),
|
||||
)
|
||||
|
|
@ -73,12 +83,13 @@ pub fn cleanup(
|
|||
// for message in messages.iter() {
|
||||
// co.send(message).unwrap();
|
||||
// }
|
||||
for n in 0..=63 {
|
||||
for n in 0..=71 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
for n in 82..=89 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
prepare(out_port, INIT_NOTES.to_vec())
|
||||
}
|
||||
|
||||
pub fn send_note_on(out_port: &mut MidiOutputConnection, pitch: u8, velocity: u8) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
use base64::{engine::general_purpose, Engine as _};
|
||||
use reqwest::blocking::{Client, RequestBuilder};
|
||||
use std::error::Error;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
static WAIT_TIME: f64 = 0.7;
|
||||
|
||||
pub struct Projector {
|
||||
ip: String,
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
impl Projector {
|
||||
pub fn new(ip: &str, username: &str, password: &str) -> Projector {
|
||||
Projector {
|
||||
ip: ip.to_string(),
|
||||
username: username.to_string(),
|
||||
password: password.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
fn make_url(&self, cmd: &str) -> String {
|
||||
format!(
|
||||
"http://{}/cgi-bin/proj_ctl.cgi?key={}&lang=e&osd=off",
|
||||
self.ip, cmd
|
||||
)
|
||||
}
|
||||
|
||||
fn make_request(&self, url: &str, headers: &str) -> Result<RequestBuilder, Box<dyn Error>> {
|
||||
let client = Client::new();
|
||||
let builder = client
|
||||
.get(url)
|
||||
.header("Authorization", headers)
|
||||
.timeout(Duration::from_secs_f64(WAIT_TIME));
|
||||
Ok(builder)
|
||||
}
|
||||
|
||||
pub fn open_url(&self, cmd: &str) -> Result<String, Box<dyn Error>> {
|
||||
let headers = self.make_auth_header();
|
||||
let url = self.make_url(cmd);
|
||||
let builder = self.make_request(&url, &headers)?;
|
||||
let response = builder.send()?;
|
||||
let body = response.text()?;
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
pub fn shutter_on(&self) -> Result<String, Box<dyn Error>> {
|
||||
self.open_url("shutter_on")
|
||||
}
|
||||
|
||||
pub fn shutter_off(&self) -> Result<String, Box<dyn Error>> {
|
||||
self.open_url("shutter_off")
|
||||
}
|
||||
|
||||
pub fn get_osd(&self) -> Result<String, Box<dyn Error>> {
|
||||
let headers = self.make_auth_header();
|
||||
let url = format!("http://{}/cgi-bin/get_osd.cgi?lang=e", self.ip);
|
||||
let builder = self.make_request(&url, &headers)?;
|
||||
let response = builder.send()?;
|
||||
let body = response.text()?;
|
||||
Ok(body)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
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");
|
||||
println!("{}", response);
|
||||
HttpResponse::Ok().body(response)
|
||||
}
|
||||
|
||||
async fn get_osd(_req: HttpRequest) -> impl Responder {
|
||||
let response = format!("OSD status for projector: ON");
|
||||
println!("{}", response);
|
||||
HttpResponse::Ok().body(response)
|
||||
}
|
||||
|
||||
async fn debug_handler(req: HttpRequest/*,config: web::Data<Config>*/) -> 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 failed
|
||||
HttpResponse::Unauthorized().append_header(("WWW-Authenticate","Basic realm=\"Restricted area\"")).finish()
|
||||
}
|
||||
|
||||
// in your `App` configuration
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
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
|
||||
}
|
||||
Reference in New Issue