diff --git a/Cargo.toml b/Cargo.toml index 42e6fa8..8dfc43c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,11 +12,21 @@ actix-web = "4.3.1" reqwest = { version = "0.11.16", features = ["blocking"] } base64 = "0.21.0" lazy_static = "1.4.0" +ratatui = "0.29.0" +serde = "1.0.215" +thiserror = "2.0.4" +tracing = "0.1.41" +tokio = "1.42.0" [[bin]] name = "rusty_midicommander" path = "src/main.rs" +[lib] +name = "rmc_lib" +path = "src/lib.rs" + [[bin]] name = "webtest" -path = "src/webtest.rs" \ No newline at end of file +path = "src/webtest.rs" + diff --git a/src/commands.rs b/src/commands.rs deleted file mode 100644 index 07ac577..0000000 --- a/src/commands.rs +++ /dev/null @@ -1,32 +0,0 @@ -// The commands module should define the commands that the program can send. -// This module should provide functions to send each command asynchronously. -// The functions should return immediately, without waiting for the command to complete. - -struct MidiData { - // Define your MIDI data struct here -} - -trait Command { - fn execute(&self, midi_data: &MidiData); -} - -struct CommandA; - -impl Command for CommandA { - fn execute(&self, midi_data: &MidiData) { - println!("Executing CommandA with MIDI data {:?}", midi_data); - } -} - -struct CommandB { - message: String, -} - -impl Command for CommandB { - fn execute(&self, midi_data: &MidiData) { - println!( - "Executing CommandB with message '{}' and MIDI data {:?}", - self.message, midi_data - ); - } -} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..0e79da7 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,12 @@ +//! Generic rusty midi commander error +//! + +use thiserror::Error; + +/// A generic error +/// +#[derive(Error, Debug)] +pub enum RustyMidiError { + #[error("network request failed {0}")] + NetworkRequest(#[from] reqwest::Error), +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b1e8c4e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,14 @@ +//! The Rusty Midi Commander lib holds the the following parts of the project +//! - The Async Web Requesting: Connect and Disconnect of VP and send requests +//! - The TUI (optionnal): To have a "nice" review of what is happening +//! - The Dispatch: In the middle to handle all orders and communications +//! - The CLI parser: Handle both cli and config +//! + +#![warn(clippy::missing_docs_in_private_items)] +// use midir::MidiOutput; +pub mod error; +pub mod midi; +pub mod projector; + +pub use error::RustyMidiError; diff --git a/src/main.rs b/src/main.rs index fbe8b8d..f803444 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,24 +6,17 @@ #![warn(clippy::missing_docs_in_private_items)] +use std::sync::{Arc, Mutex}; + 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; - -use midir::MidiInput; - -use midir::MidiOutput; -use midir::MidiOutputConnection; - -// use midir::MidiOutput; -pub mod midi; -mod projector; +use rmc_lib::{ + midi::{self, cleanup, connect_loop_with_callback, create_output_interface}, + projector, +}; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] +/// Description of the cli struct Args { /// The midi interface to select midi_interface: Option, @@ -40,7 +33,7 @@ fn main() { return; } println!("{:?}", args); - let proj = Arc::new(Mutex::new(projector::Projector::new( + let proj = Arc::new(Mutex::new(projector::panasonic::Projector::new( "127.0.0.1:8080", "admin1", "panasonic", diff --git a/src/midi.rs b/src/midi.rs index 23b3918..17a16c4 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -1,19 +1,24 @@ // The midi module should provide a high-level interface for receiving and sending MIDI messages. // It should handle the low-level details of communicating with the MIDI devices. -use crate::MidiInput; -use crate::MidiOutput; -use crate::MidiOutputConnection; -use lazy_static::lazy_static; -use std::sync::Arc; -use std::sync::Mutex; -use std::thread::sleep; -use std::time::Duration; +use std::{ + sync::{Arc, Mutex}, + thread::sleep, + time::Duration, +}; +use lazy_static::lazy_static; +use midir::{MidiInput, MidiOutput, MidiOutputConnection}; + +// TODO: All this should be generic with an impl for every midi pad, possibly from a config file +// As: GenericFromFile, GenericAkai, etc... + +// TODO: NO lazy_static! { static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)]; } +// TODO: Meh ? pub fn print_midi_interfaces() { // Create MIDI input and output instances let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap(); @@ -29,6 +34,7 @@ pub fn print_midi_interfaces() { } } +// Acceptable, should be more generic maybe ? pub fn connect_loop_with_callback( port: usize, name: &str, @@ -44,7 +50,8 @@ 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); + + // TODO: Unacceptable let _connection = input_interface .connect( input_port, @@ -61,6 +68,7 @@ pub fn connect_loop_with_callback( } let mut list = vec![]; if note == 3 { + // Meh list.push([command, 3, 2]); list.push([command, 4, 3]); } else { @@ -68,6 +76,7 @@ pub fn connect_loop_with_callback( list.push([command, 4, 4]); } for n in list.iter() { + // Meh conn_clone.lock().unwrap().send(n).unwrap(); conn_clone.lock().unwrap().send(n).unwrap(); } @@ -82,6 +91,7 @@ pub fn connect_loop_with_callback( } } +// TODO: Doc pub fn create_output_interface(port: usize, name: &str) -> Arc> { let output_interface = MidiOutput::new(name).unwrap(); let output_port = &output_interface.ports()[port]; @@ -92,6 +102,7 @@ pub fn create_output_interface(port: usize, name: &str) -> Arc*/ ) { @@ -109,11 +120,13 @@ pub fn cleanup( prepare(out_port, INIT_NOTES.to_vec()) } +// TODO: more generic interface pub fn send_note_on(out_port: &mut MidiOutputConnection, pitch: u8, velocity: u8) { let msg = &[0x90, pitch, velocity]; out_port.send(msg).unwrap(); } +// TODO: doc 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); diff --git a/src/projector/mod.rs b/src/projector/mod.rs new file mode 100644 index 0000000..132a896 --- /dev/null +++ b/src/projector/mod.rs @@ -0,0 +1,8 @@ +//! Contains all projector related commands + +pub mod panasonic; +pub mod proj_traits; + +/// The standard wait time +// TODO: NO +static WAIT_TIME: f64 = 0.7; diff --git a/src/projector.rs b/src/projector/panasonic.rs similarity index 78% rename from src/projector.rs rename to src/projector/panasonic.rs index 91a988e..ed5781e 100644 --- a/src/projector.rs +++ b/src/projector/panasonic.rs @@ -1,11 +1,14 @@ -//! Contains all projector related commands +//! A panasonic webpage interface implementation +//! -use base64::{engine::general_purpose, Engine as _}; +use std::time::Duration; + +use base64::{engine::general_purpose, Engine}; use reqwest::blocking::{Client, RequestBuilder}; -use std::{error::Error, time::Duration}; -/// The standard wait time -static WAIT_TIME: f64 = 0.7; +use crate::error::RustyMidiError; + +use super::WAIT_TIME; /// A projector and all info to connect to it pub struct Projector { @@ -15,8 +18,11 @@ pub struct Projector { username: String, /// The password for the connection password: String, + /// An optionnal port for connection + port: Option, } +// TODO: Trait impl Projector { /// Create a new projector with the basic information pub fn new(ip: &str, username: &str, password: &str) -> Projector { @@ -24,6 +30,7 @@ impl Projector { ip: ip.to_string(), username: username.to_string(), password: password.to_string(), + port: None, } } @@ -43,7 +50,7 @@ impl Projector { } /// Make a request from a url - fn make_request(&self, url: &str, headers: &str) -> Result> { + fn make_request(&self, url: &str, headers: &str) -> Result { let client = Client::new(); let builder = client .get(url) @@ -53,7 +60,7 @@ impl Projector { } /// Make the actual request - pub fn open_url(&self, cmd: &str) -> Result> { + pub fn open_url(&self, cmd: &str) -> Result { let headers = self.make_auth_header(); let url = self.make_url(cmd); let builder = self.make_request(&url, &headers)?; @@ -63,17 +70,17 @@ impl Projector { } /// A request to turn the projector on - pub fn shutter_on(&self) -> Result> { + pub fn shutter_on(&self) -> Result { self.open_url("shutter_on") } /// A request to turn the projector off - pub fn shutter_off(&self) -> Result> { + pub fn shutter_off(&self) -> Result { self.open_url("shutter_off") } /// The request to the the osd - pub fn get_osd(&self) -> Result> { + 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); let builder = self.make_request(&url, &headers)?; diff --git a/src/projector/proj_traits.rs b/src/projector/proj_traits.rs new file mode 100644 index 0000000..30f9413 --- /dev/null +++ b/src/projector/proj_traits.rs @@ -0,0 +1,9 @@ +//! Generic trait for a projector +//! +//! + +/// A generic Projector, contains all methods for a controllable vp +trait GenericProjector {} + +/// A generic Projector OSD, to retreive information from the vp +trait GenericProjectorOSD {} diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index 54f6aa7..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,2 +0,0 @@ -// The utils module should provide utility functions that are used throughout the program. -// These functions could include functions to convert MIDI messages to and from human-readable formats, or to handle errors.