From f31e9cc290df0281bfd7c6c93af64d41dc798afd Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 09:12:28 +1000 Subject: [PATCH 01/17] config.rs update to accept multiple uris --- config.ini | 1 + giggletech-router/src/config.rs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/config.ini b/config.ini index 5723284..228b8e9 100644 --- a/config.ini +++ b/config.ini @@ -12,6 +12,7 @@ # IP Address of Headpat Device device_ip = 192.168.1.157 # + device_uris = 192.168.1.157 192.168.1.158 # Port listening for OSC [Default 9001] port_rx = 9100 # diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index e4e5f8a..4209e7a 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -33,6 +33,16 @@ pub(crate) fn load_config() -> ( } const MAX_SPEED_LOW_LIMIT_CONST: f32 = 0.05; + // ----------------------------------------------------------------------------------------------------------- TODO error if incorrect format + let headpat_device_uris: Vec = config.get("Setup", "device_uris").unwrap() + .split_whitespace() + .map(|s| s.to_string()) // convert &str to String + .collect(); + + println!("Device URIs: {:?}", headpat_device_uris); + + + let headpat_device_ip = config.get("Setup", "device_ip").unwrap(); let headpat_device_port = "8888".to_string(); let min_speed = config.get("Haptic_Config", "min_speed").unwrap(); From f94c6bc77ad58595dc771de403a17711c5313034 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 09:20:39 +1000 Subject: [PATCH 02/17] URI error check working --- config.ini | 2 +- giggletech-router/src/config.rs | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/config.ini b/config.ini index 228b8e9..ad7b749 100644 --- a/config.ini +++ b/config.ini @@ -12,7 +12,7 @@ # IP Address of Headpat Device device_ip = 192.168.1.157 # - device_uris = 192.168.1.157 192.168.1.158 + device_uris = #192.168.1.157 192.168.1.157 # Port listening for OSC [Default 9001] port_rx = 9100 # diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index 4209e7a..382f2d6 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -1,4 +1,5 @@ use configparser::ini::Ini; +use std::net::IpAddr; // Banner fn banner_txt(){ @@ -33,11 +34,27 @@ pub(crate) fn load_config() -> ( } const MAX_SPEED_LOW_LIMIT_CONST: f32 = 0.05; - // ----------------------------------------------------------------------------------------------------------- TODO error if incorrect format - let headpat_device_uris: Vec = config.get("Setup", "device_uris").unwrap() - .split_whitespace() - .map(|s| s.to_string()) // convert &str to String - .collect(); + + + // Check the format of the URIs + let headpat_device_uris: Vec = config.get("Setup", "device_uris") + .unwrap() + .split_whitespace() + .map(|s| s.to_string()) // convert &str to String + .filter_map(|s| { + match s.parse::() { + Ok(_) => Some(s), + Err(_) => { + println!("Invalid IP address format: {}", s); + None + } + } + }) + .collect(); + if headpat_device_uris.is_empty() { + eprintln!("Error: no device URIs specified in config file"); + // handle error here, e.g. return early from the function or exit the program + } println!("Device URIs: {:?}", headpat_device_uris); From 7ec40ed33dcf23b65f27dd4f4ca80fc9d9f7831f Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 09:26:52 +1000 Subject: [PATCH 03/17] added URIs import --- giggletech-router/src/config.rs | 2 ++ giggletech-router/src/main.rs | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index 382f2d6..f1b925b 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -18,6 +18,7 @@ fn banner_txt(){ pub(crate) fn load_config() -> ( String, // headpat_device_ip + Vec, // headpat_device_URIs f32, // min_speed_float f32, // max_speed_float f32, // speed_scale_float @@ -92,6 +93,7 @@ pub(crate) fn load_config() -> ( ( headpat_device_ip, + headpat_device_uris, min_speed_float, max_speed_float, speed_scale_float, diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index ba4104d..f603c6a 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -22,6 +22,7 @@ async fn main() -> Result<()> { // Import Config // Todo: Refactor let (headpat_device_ip, + headpat_device_uris, min_speed, mut max_speed, speed_scale, @@ -46,6 +47,13 @@ async fn main() -> Result<()> { let running = Arc::new(AtomicBool::new(false)); let headpat_device_ip_arc = Arc::new(headpat_device_ip); + + + + + + + // Listen for OSC Packets while let Some(packet) = rx_socket.next().await { let (packet, _peer_addr) = packet?; From 2ec18c5fb61f05cc04e33edcef74ce64f6ca6171 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 09:46:45 +1000 Subject: [PATCH 04/17] added mutli prox parameter --- config.ini | 4 +++- giggletech-router/src/config.rs | 32 +++++++++++++++++++++++++++++++- giggletech-router/src/main.rs | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/config.ini b/config.ini index ad7b749..a1c50e6 100644 --- a/config.ini +++ b/config.ini @@ -12,12 +12,14 @@ # IP Address of Headpat Device device_ip = 192.168.1.157 # - device_uris = #192.168.1.157 192.168.1.157 + device_uris = 192.168.1.157 192.168.1.157 # Port listening for OSC [Default 9001] port_rx = 9100 # # proximity_parameter = /avatar/parameters/proximity_01 + + proximity_parameters_multi = proximity_01 proximity_02 # max_speed_parameter = /avatar/parameters/max_speed # diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index f1b925b..fca6092 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -24,6 +24,7 @@ pub(crate) fn load_config() -> ( f32, // speed_scale_float String, // port_rx String, // proximity_parameter_address + Vec, // proximity_parameters_multi String, // max_speed_parameter_address f32, // Max Speed Low Limit ) { @@ -37,7 +38,7 @@ pub(crate) fn load_config() -> ( - // Check the format of the URIs + // Check the format of the IP URIs let headpat_device_uris: Vec = config.get("Setup", "device_uris") .unwrap() .split_whitespace() @@ -59,6 +60,34 @@ pub(crate) fn load_config() -> ( println!("Device URIs: {:?}", headpat_device_uris); + // Multi Device + + + let proximity_parameters_multi: Vec = config.get("Setup", "proximity_parameters_multi") + .unwrap() + .split_whitespace() + .map(|s| format!("/avatar/parameters/{}", s)) // add "/avatar/parameters/" prefix to each string + .collect(); + + println!("Device URIs: {:?}", proximity_parameters_multi); + + + if headpat_device_uris.len() != proximity_parameters_multi.len() { + eprintln!("Error: number of device URIs does not match number of proximity parameters"); + // handle error here, e.g. return early from the function or exit the program + } + + + + + + + + + + + + let headpat_device_ip = config.get("Setup", "device_ip").unwrap(); @@ -99,6 +128,7 @@ pub(crate) fn load_config() -> ( speed_scale_float, port_rx, proximity_parameter_address, + proximity_parameters_multi, max_speed_parameter_address, max_speed_low_limit, ) diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index f603c6a..e3dcb72 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -28,6 +28,7 @@ async fn main() -> Result<()> { speed_scale, port_rx, proximity_parameter_address, + proximity_parameters_multi, max_speed_parameter_address, max_speed_low_limit, From 3d9b427ff4a8185411310d10d3dd630918621c72 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 11:39:30 +1000 Subject: [PATCH 05/17] working but jank --- giggletech-router/src/giggletech_osc.rs | 2 +- giggletech-router/src/main.rs | 59 +++++++++++++++++++++---- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/giggletech-router/src/giggletech_osc.rs b/giggletech-router/src/giggletech_osc.rs index fe232c0..b9095ad 100644 --- a/giggletech-router/src/giggletech_osc.rs +++ b/giggletech-router/src/giggletech_osc.rs @@ -26,7 +26,7 @@ pub(crate) async fn setup_tx_socket(address: std::string::String) -> Result Result<()> { - //println!("Sending Value:{} to IP: {}", value, device_ip); + println!("Sending Value:{} to IP: {}", value, device_ip); let tx_socket_address = create_socket_address(device_ip, "8888"); // ------------------- Port to Send OSC Data Too let tx_socket = setup_tx_socket(tx_socket_address.clone()).await?; tx_socket.connect(tx_socket_address).await?; diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index e3dcb72..6de8247 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -16,6 +16,12 @@ mod giggletech_osc; mod terminator; mod osc_timeout; + + + + + + #[async_std::main] async fn main() -> Result<()> { @@ -34,6 +40,10 @@ async fn main() -> Result<()> { ) = config::load_config(); + let headpat_device_ip_arc_2_orig = Arc::new("192.168.1.153".to_string()); + let headpat_device_ip_arc_2 = &Arc::clone(&headpat_device_ip_arc_2_orig); + + let proximity_parameter_address_2 = "/avatar/parameters/proximity_02"; // Rx/Tx Socket Setup let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; @@ -44,17 +54,12 @@ async fn main() -> Result<()> { osc_timeout(&headpat_device_ip_clone).await.unwrap(); }); + + // Start/ Stop Function Setup let running = Arc::new(AtomicBool::new(false)); let headpat_device_ip_arc = Arc::new(headpat_device_ip); - - - - - - - // Listen for OSC Packets while let Some(packet) = rx_socket.next().await { let (packet, _peer_addr) = packet?; @@ -76,7 +81,7 @@ async fn main() -> Result<()> { max_speed = value.max(max_speed_low_limit); } - // Prox Parmeter + // Prox Parmeter 1 else if address == proximity_parameter_address { terminator::stop(running.clone()).await?; @@ -98,6 +103,42 @@ async fn main() -> Result<()> { data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; } } + + // Prox Parmeter 2 + else if address == proximity_parameter_address_2 { + //else if address == proximity_parameter_address { + + terminator::stop(running.clone()).await?; + // Update Last Signal Time for timeout clock + let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); + *last_signal_time = Instant::now(); + + // Stop Function + if value == 0.0 { + println!("Stopping pats..."); + terminator::start(running.clone(), &headpat_device_ip_arc_2).await?; + + for _ in 0..5 { + giggletech_osc::send_data(&headpat_device_ip_arc_2, 0i32).await?; + } + + } else { + giggletech_osc::send_data(&headpat_device_ip_arc_2, + data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; + } + } + + + + + + + + + + + + else { //eprintln!("Unknown Address") // Have a debug mode, print if debug mode } @@ -105,4 +146,4 @@ async fn main() -> Result<()> { } } Ok(()) -} \ No newline at end of file +} From 9d8e386dbc93a9111d4685e5ffb8fcc37d043104 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 13:47:35 +1000 Subject: [PATCH 06/17] Multi Device Working - Removed Timeout --- config.ini | 2 +- giggletech-router/src/main.rs | 110 +++++++++++++++++++++++++++++++--- 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/config.ini b/config.ini index a1c50e6..639257a 100644 --- a/config.ini +++ b/config.ini @@ -12,7 +12,7 @@ # IP Address of Headpat Device device_ip = 192.168.1.157 # - device_uris = 192.168.1.157 192.168.1.157 + device_uris = 192.168.1.157 192.168.1.153 # Port listening for OSC [Default 9001] port_rx = 9100 # diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index 6de8247..acd5768 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -19,9 +19,8 @@ mod osc_timeout; +/* - - #[async_std::main] async fn main() -> Result<()> { @@ -60,6 +59,8 @@ async fn main() -> Result<()> { let running = Arc::new(AtomicBool::new(false)); let headpat_device_ip_arc = Arc::new(headpat_device_ip); + // create iterators that iterartos over list + // Listen for OSC Packets while let Some(packet) = rx_socket.next().await { let (packet, _peer_addr) = packet?; @@ -128,22 +129,117 @@ async fn main() -> Result<()> { } } + else { + //eprintln!("Unknown Address") // Have a debug mode, print if debug mode + } + } + } + } + Ok(()) +} +*/ + +async fn handle_proximity_parameter( + running: Arc, + headpat_device_ip_arc: &Arc, + value: f32, + max_speed: f32, + min_speed: f32, + speed_scale: f32, + proximity_parameter_address: &str, +) -> Result<()> { + terminator::stop(running.clone()).await?; + + // Update Last Signal Time for timeout clock + let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); + *last_signal_time = Instant::now(); + + // Stop Function + if value == 0.0 { + println!("Stopping pats..."); + terminator::start(running.clone(), &headpat_device_ip_arc).await?; + + for _ in 0..5 { + giggletech_osc::send_data(&headpat_device_ip_arc, 0i32).await?; + } + } else { + giggletech_osc::send_data(&headpat_device_ip_arc, + data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; + } + Ok(()) +} +#[async_std::main] +async fn main() -> Result<()> { + // Import Config + // Todo: Refactor + let ( + headpat_device_ip, + headpat_device_uris, + min_speed, + mut max_speed, + speed_scale, + port_rx, + proximity_parameter_addresses, + proximity_parameters_multi, + max_speed_parameter_address, + max_speed_low_limit, + ) = config::load_config(); + let headpat_device_ip_arc = Arc::new(headpat_device_ip); + let running = Arc::new(AtomicBool::new(false)); + // Rx/Tx Socket Setup + let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; + // Timeout + //let headpat_device_ip_clone = headpat_device_ip.clone(); + //task::spawn(async move { + // osc_timeout(&headpat_device_ip_clone).await.unwrap(); + //}); + // Listen for OSC Packets + while let Some(packet) = rx_socket.next().await { + let (packet, _peer_addr) = packet?; + // Filter OSC Signals: Headpat Max & Headpat Prox + match packet { + OscPacket::Bundle(_) => {} + OscPacket::Message(message) => { + let (address, osc_value) = message.as_tuple(); + let value = match osc_value.first().unwrap_or(&OscType::Nil).clone().float() { + Some(v) => v, + None => continue, + }; - - else { - //eprintln!("Unknown Address") // Have a debug mode, print if debug mode + // Max Speed Setting + if address == max_speed_parameter_address { + data_processing::print_speed_limit(value); + max_speed = value.max(max_speed_low_limit); + } else { + let index = proximity_parameters_multi.iter().position(|a| *a == address); + + match index { + Some(i) => { + handle_proximity_parameter( + running.clone(), + &Arc::new(headpat_device_uris[i].clone()), + value, + max_speed, + min_speed, + speed_scale, + &proximity_parameters_multi[i], + ) + .await? + } + None => {} + } } - } - } + } + } } Ok(()) } From a969631d1c461357b5b72b8f7ba9780c966237bf Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 19:20:41 +1000 Subject: [PATCH 07/17] Refactor --- giggletech-router/src/giggletech_osc.rs | 2 +- giggletech-router/src/main.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/giggletech-router/src/giggletech_osc.rs b/giggletech-router/src/giggletech_osc.rs index b9095ad..fe232c0 100644 --- a/giggletech-router/src/giggletech_osc.rs +++ b/giggletech-router/src/giggletech_osc.rs @@ -26,7 +26,7 @@ pub(crate) async fn setup_tx_socket(address: std::string::String) -> Result Result<()> { - println!("Sending Value:{} to IP: {}", value, device_ip); + //println!("Sending Value:{} to IP: {}", value, device_ip); let tx_socket_address = create_socket_address(device_ip, "8888"); // ------------------- Port to Send OSC Data Too let tx_socket = setup_tx_socket(tx_socket_address.clone()).await?; tx_socket.connect(tx_socket_address).await?; diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index acd5768..fefe340 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -142,7 +142,7 @@ async fn main() -> Result<()> { async fn handle_proximity_parameter( running: Arc, - headpat_device_ip_arc: &Arc, + device_ip: &Arc, value: f32, max_speed: f32, min_speed: f32, @@ -158,13 +158,13 @@ async fn handle_proximity_parameter( // Stop Function if value == 0.0 { println!("Stopping pats..."); - terminator::start(running.clone(), &headpat_device_ip_arc).await?; + terminator::start(running.clone(), &device_ip).await?; for _ in 0..5 { - giggletech_osc::send_data(&headpat_device_ip_arc, 0i32).await?; + giggletech_osc::send_data(&device_ip, 0i32).await?; } } else { - giggletech_osc::send_data(&headpat_device_ip_arc, + giggletech_osc::send_data(&device_ip, data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; } Ok(()) @@ -189,17 +189,17 @@ async fn main() -> Result<()> { max_speed_low_limit, ) = config::load_config(); - let headpat_device_ip_arc = Arc::new(headpat_device_ip); + //let headpat_device_ip_arc = Arc::new(headpat_device_ip); let running = Arc::new(AtomicBool::new(false)); // Rx/Tx Socket Setup let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; // Timeout - //let headpat_device_ip_clone = headpat_device_ip.clone(); - //task::spawn(async move { - // osc_timeout(&headpat_device_ip_clone).await.unwrap(); - //}); + let headpat_device_ip_clone = headpat_device_ip.clone(); + task::spawn(async move { + osc_timeout(&headpat_device_ip_clone).await.unwrap(); + }); // Listen for OSC Packets while let Some(packet) = rx_socket.next().await { From 7ab582894c3040851d07792d887030931547cc93 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Sat, 29 Apr 2023 19:57:35 +1000 Subject: [PATCH 08/17] Kind of working with timeout needs work --- giggletech-router/Cargo.toml | 5 ++++- giggletech-router/src/main.rs | 12 ++++++++---- giggletech-router/src/osc_timeout.rs | 28 +++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/giggletech-router/Cargo.toml b/giggletech-router/Cargo.toml index 0c18282..fd8817c 100644 --- a/giggletech-router/Cargo.toml +++ b/giggletech-router/Cargo.toml @@ -12,7 +12,8 @@ authors = ["Giggle Tech " [dependencies] rosc = "0.4.2" -async-std = "1.12.0" +async-std = { version = "1.8.0", features = ["attributes", "unstable"] } +#async-std = "1.12.0" log = "0.4.14" futures-lite = "1.11.3" thiserror = "1.0.24" @@ -22,6 +23,8 @@ tokio = { version = "1.13", features = ["full"] } futures = "0.3" #ini = "1.3.0" anyhow = "1.0.44" +once_cell = "1.8.0" + diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index fefe340..f932318 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -140,6 +140,11 @@ async fn main() -> Result<()> { */ +/* + +*/ + + async fn handle_proximity_parameter( running: Arc, device_ip: &Arc, @@ -152,10 +157,9 @@ async fn handle_proximity_parameter( terminator::stop(running.clone()).await?; // Update Last Signal Time for timeout clock + let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); *last_signal_time = Instant::now(); - - // Stop Function if value == 0.0 { println!("Stopping pats..."); terminator::start(running.clone(), &device_ip).await?; @@ -189,7 +193,7 @@ async fn main() -> Result<()> { max_speed_low_limit, ) = config::load_config(); - //let headpat_device_ip_arc = Arc::new(headpat_device_ip); + // Setup Start / Stop of Terminiator let running = Arc::new(AtomicBool::new(false)); // Rx/Tx Socket Setup @@ -225,7 +229,7 @@ async fn main() -> Result<()> { match index { Some(i) => { handle_proximity_parameter( - running.clone(), + running.clone(), // Terminator &Arc::new(headpat_device_uris[i].clone()), value, max_speed, diff --git a/giggletech-router/src/osc_timeout.rs b/giggletech-router/src/osc_timeout.rs index 541a04a..e62d5e4 100644 --- a/giggletech-router/src/osc_timeout.rs +++ b/giggletech-router/src/osc_timeout.rs @@ -15,7 +15,7 @@ pub async fn osc_timeout(device_ip: &str) -> Result<()> { loop { async_std::task::sleep(Duration::from_secs(1)).await; let elapsed_time = Instant::now().duration_since(*LAST_SIGNAL_TIME.lock().unwrap()); - + println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); if elapsed_time >= Duration::from_secs(5) { giggletech_osc::send_data(device_ip, 0i32).await?; @@ -24,3 +24,29 @@ pub async fn osc_timeout(device_ip: &str) -> Result<()> { } } } + +/* +use std::collections::HashMap; +use std::time::{Duration, Instant}; +use anyhow::Result; +use crate::giggletech_osc; + +pub async fn osc_timeout(device_ips: Vec) -> Result<()> { + let mut last_signal_times: HashMap = HashMap::new(); + + loop { + async_std::task::sleep(Duration::from_secs(1)).await; + + for device_ip in &device_ips { + let elapsed_time = Instant::now().duration_since(*last_signal_times.entry(device_ip.clone()).or_insert(Instant::now())); + + println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); + + if elapsed_time >= Duration::from_secs(5) { + giggletech_osc::send_data(device_ip, 0i32).await?; + last_signal_times.insert(device_ip.clone(), Instant::now()); + } + } + } +} +*/ \ No newline at end of file From 3bad625ca37de2bd6fdb411aad2a4c2986d26e5a Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 07:25:16 +1000 Subject: [PATCH 09/17] Working Clean --- config.ini | 4 +- giggletech-router/src/giggletech_osc.rs | 2 +- giggletech-router/src/main.rs | 126 +----------------------- 3 files changed, 5 insertions(+), 127 deletions(-) diff --git a/config.ini b/config.ini index 639257a..06f6f85 100644 --- a/config.ini +++ b/config.ini @@ -12,9 +12,9 @@ # IP Address of Headpat Device device_ip = 192.168.1.157 # - device_uris = 192.168.1.157 192.168.1.153 + device_uris = 192.168.1.151 192.168.1.157 # Port listening for OSC [Default 9001] - port_rx = 9100 + port_rx = 9001 # # proximity_parameter = /avatar/parameters/proximity_01 diff --git a/giggletech-router/src/giggletech_osc.rs b/giggletech-router/src/giggletech_osc.rs index fe232c0..b9095ad 100644 --- a/giggletech-router/src/giggletech_osc.rs +++ b/giggletech-router/src/giggletech_osc.rs @@ -26,7 +26,7 @@ pub(crate) async fn setup_tx_socket(address: std::string::String) -> Result Result<()> { - //println!("Sending Value:{} to IP: {}", value, device_ip); + println!("Sending Value:{} to IP: {}", value, device_ip); let tx_socket_address = create_socket_address(device_ip, "8888"); // ------------------- Port to Send OSC Data Too let tx_socket = setup_tx_socket(tx_socket_address.clone()).await?; tx_socket.connect(tx_socket_address).await?; diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index f932318..932d38d 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -19,130 +19,6 @@ mod osc_timeout; -/* - -#[async_std::main] -async fn main() -> Result<()> { - - // Import Config - // Todo: Refactor - let (headpat_device_ip, - headpat_device_uris, - min_speed, - mut max_speed, - speed_scale, - port_rx, - proximity_parameter_address, - proximity_parameters_multi, - max_speed_parameter_address, - max_speed_low_limit, - - ) = config::load_config(); - - let headpat_device_ip_arc_2_orig = Arc::new("192.168.1.153".to_string()); - let headpat_device_ip_arc_2 = &Arc::clone(&headpat_device_ip_arc_2_orig); - - let proximity_parameter_address_2 = "/avatar/parameters/proximity_02"; - // Rx/Tx Socket Setup - let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; - - - // Timeout - let headpat_device_ip_clone = headpat_device_ip.clone(); - task::spawn(async move { - osc_timeout(&headpat_device_ip_clone).await.unwrap(); - }); - - - - // Start/ Stop Function Setup - let running = Arc::new(AtomicBool::new(false)); - let headpat_device_ip_arc = Arc::new(headpat_device_ip); - - // create iterators that iterartos over list - - // Listen for OSC Packets - while let Some(packet) = rx_socket.next().await { - let (packet, _peer_addr) = packet?; - - // Filter OSC Signals : Headpat Max & Headpat Prox - match packet { - OscPacket::Bundle(_) => {} - OscPacket::Message(message) => { - - let (address, osc_value) = message.as_tuple(); - let value = match osc_value.first().unwrap_or(&OscType::Nil).clone().float(){ - Some(v) => v, - None => continue, - }; - - // Max Speed Setting - if address == max_speed_parameter_address { - data_processing::print_speed_limit(value); - max_speed = value.max(max_speed_low_limit); - } - - // Prox Parmeter 1 - else if address == proximity_parameter_address { - - terminator::stop(running.clone()).await?; - // Update Last Signal Time for timeout clock - let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); - *last_signal_time = Instant::now(); - - // Stop Function - if value == 0.0 { - println!("Stopping pats..."); - terminator::start(running.clone(), &headpat_device_ip_arc).await?; - - for _ in 0..5 { - giggletech_osc::send_data(&headpat_device_ip_arc, 0i32).await?; - } - - } else { - giggletech_osc::send_data(&headpat_device_ip_arc, - data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; - } - } - - // Prox Parmeter 2 - else if address == proximity_parameter_address_2 { - //else if address == proximity_parameter_address { - - terminator::stop(running.clone()).await?; - // Update Last Signal Time for timeout clock - let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); - *last_signal_time = Instant::now(); - - // Stop Function - if value == 0.0 { - println!("Stopping pats..."); - terminator::start(running.clone(), &headpat_device_ip_arc_2).await?; - - for _ in 0..5 { - giggletech_osc::send_data(&headpat_device_ip_arc_2, 0i32).await?; - } - - } else { - giggletech_osc::send_data(&headpat_device_ip_arc_2, - data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; - } - } - - else { - //eprintln!("Unknown Address") // Have a debug mode, print if debug mode - } - } - } - } - Ok(()) -} - -*/ - -/* - -*/ async fn handle_proximity_parameter( @@ -168,6 +44,7 @@ async fn handle_proximity_parameter( giggletech_osc::send_data(&device_ip, 0i32).await?; } } else { + //start_timer(value); giggletech_osc::send_data(&device_ip, data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; } @@ -176,6 +53,7 @@ async fn handle_proximity_parameter( + #[async_std::main] async fn main() -> Result<()> { // Import Config From 601c78c0c492b087c716b50ffdb2262654b1cba3 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 09:14:47 +1000 Subject: [PATCH 10/17] Working Timeout for multiple devices --- giggletech-router/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index 932d38d..dd0f560 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -78,10 +78,22 @@ async fn main() -> Result<()> { let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; // Timeout + /* let headpat_device_ip_clone = headpat_device_ip.clone(); task::spawn(async move { osc_timeout(&headpat_device_ip_clone).await.unwrap(); }); +*/ + // Timeout + for ip in &headpat_device_uris { + let headpat_device_ip_clone = ip.clone(); + task::spawn(async move { + osc_timeout(&headpat_device_ip_clone).await.unwrap(); + }); + } + + + // Listen for OSC Packets while let Some(packet) = rx_socket.next().await { From 44ba918930141a07d985eefaceac1efbffecd17b Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 09:23:00 +1000 Subject: [PATCH 11/17] Working timeout - but shared timeout clock for all devices --- giggletech-router/src/main.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index dd0f560..7d82e2f 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -32,7 +32,7 @@ async fn handle_proximity_parameter( ) -> Result<()> { terminator::stop(running.clone()).await?; - // Update Last Signal Time for timeout clock + // Update Last Signal Time for timeout clock --------------------------------------------------------------- this has to be changed let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); *last_signal_time = Instant::now(); @@ -77,13 +77,7 @@ async fn main() -> Result<()> { // Rx/Tx Socket Setup let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; - // Timeout - /* - let headpat_device_ip_clone = headpat_device_ip.clone(); - task::spawn(async move { - osc_timeout(&headpat_device_ip_clone).await.unwrap(); - }); -*/ + // Timeout for ip in &headpat_device_uris { let headpat_device_ip_clone = ip.clone(); From 7cc517c5a0c8527ada35a960a3cb33d8573cb493 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 13:29:10 +1000 Subject: [PATCH 12/17] Working with independent timeouts --- giggletech-router/src/main.rs | 7 +++-- giggletech-router/src/osc_timeout.rs | 40 +++++----------------------- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index 7d82e2f..5b3528c 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -19,8 +19,6 @@ mod osc_timeout; - - async fn handle_proximity_parameter( running: Arc, device_ip: &Arc, @@ -34,8 +32,9 @@ async fn handle_proximity_parameter( // Update Last Signal Time for timeout clock --------------------------------------------------------------- this has to be changed - let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap(); - *last_signal_time = Instant::now(); + let mut device_last_signal_times = osc_timeout::DEVICE_LAST_SIGNAL_TIME.lock().unwrap(); + device_last_signal_times.insert(device_ip.to_string(), Instant::now()); + if value == 0.0 { println!("Stopping pats..."); terminator::start(running.clone(), &device_ip).await?; diff --git a/giggletech-router/src/osc_timeout.rs b/giggletech-router/src/osc_timeout.rs index e62d5e4..15d20f4 100644 --- a/giggletech-router/src/osc_timeout.rs +++ b/giggletech-router/src/osc_timeout.rs @@ -1,52 +1,26 @@ // osc_timeout.rs -use std::sync::Mutex; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; use anyhow::Result; use lazy_static::lazy_static; use crate::giggletech_osc; - lazy_static! { - pub static ref LAST_SIGNAL_TIME: Mutex = Mutex::new(Instant::now()); + pub static ref DEVICE_LAST_SIGNAL_TIME: Arc>> = + Arc::new(Mutex::new(HashMap::new())); } pub async fn osc_timeout(device_ip: &str) -> Result<()> { loop { async_std::task::sleep(Duration::from_secs(1)).await; - let elapsed_time = Instant::now().duration_since(*LAST_SIGNAL_TIME.lock().unwrap()); + let elapsed_time = Instant::now().duration_since(*DEVICE_LAST_SIGNAL_TIME.lock().unwrap().get(device_ip).unwrap_or(&Instant::now())); println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); if elapsed_time >= Duration::from_secs(5) { giggletech_osc::send_data(device_ip, 0i32).await?; - - let mut last_signal_time = LAST_SIGNAL_TIME.lock().unwrap(); - *last_signal_time = Instant::now(); - } - } -} - -/* -use std::collections::HashMap; -use std::time::{Duration, Instant}; -use anyhow::Result; -use crate::giggletech_osc; - -pub async fn osc_timeout(device_ips: Vec) -> Result<()> { - let mut last_signal_times: HashMap = HashMap::new(); - - loop { - async_std::task::sleep(Duration::from_secs(1)).await; - - for device_ip in &device_ips { - let elapsed_time = Instant::now().duration_since(*last_signal_times.entry(device_ip.clone()).or_insert(Instant::now())); - - println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); - - if elapsed_time >= Duration::from_secs(5) { - giggletech_osc::send_data(device_ip, 0i32).await?; - last_signal_times.insert(device_ip.clone(), Instant::now()); - } + let mut device_last_signal_times = DEVICE_LAST_SIGNAL_TIME.lock().unwrap(); + device_last_signal_times.insert(device_ip.to_string(), Instant::now()); } } } -*/ \ No newline at end of file From c1da70db8a28d020c5920b4fefe2900c8a804718 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 14:19:57 +1000 Subject: [PATCH 13/17] Refactor --- config.ini | 10 ++-- giggletech-router/Cargo.toml | 1 + giggletech-router/src/config.rs | 44 +++++----------- .../src/handle_proximity_parameter.rs | 41 +++++++++++++++ giggletech-router/src/main.rs | 51 ++----------------- 5 files changed, 62 insertions(+), 85 deletions(-) create mode 100644 giggletech-router/src/handle_proximity_parameter.rs diff --git a/config.ini b/config.ini index 06f6f85..161ba51 100644 --- a/config.ini +++ b/config.ini @@ -10,16 +10,14 @@ [Setup] # # IP Address of Headpat Device - device_ip = 192.168.1.157 # - device_uris = 192.168.1.151 192.168.1.157 + device_uris = 192.168.1.151 192.168.1.157 192.168.1.153 +# # Port listening for OSC [Default 9001] - port_rx = 9001 # + port_rx = 9001 # - proximity_parameter = /avatar/parameters/proximity_01 - - proximity_parameters_multi = proximity_01 proximity_02 + proximity_parameters_multi = proximity_01 proximity_02 proximity_03 # max_speed_parameter = /avatar/parameters/max_speed # diff --git a/giggletech-router/Cargo.toml b/giggletech-router/Cargo.toml index fd8817c..55f16b8 100644 --- a/giggletech-router/Cargo.toml +++ b/giggletech-router/Cargo.toml @@ -29,5 +29,6 @@ once_cell = "1.8.0" + [dev-dependencies] async-std = { version = "1.9.0", features = ["unstable", "attributes"] } diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index fca6092..adefdbc 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -1,6 +1,7 @@ use configparser::ini::Ini; use std::net::IpAddr; + // Banner fn banner_txt(){ // https://fsymbols.com/generators/carty/ @@ -17,13 +18,11 @@ fn banner_txt(){ } pub(crate) fn load_config() -> ( - String, // headpat_device_ip Vec, // headpat_device_URIs f32, // min_speed_float f32, // max_speed_float f32, // speed_scale_float String, // port_rx - String, // proximity_parameter_address Vec, // proximity_parameters_multi String, // max_speed_parameter_address f32, // Max Speed Low Limit @@ -36,8 +35,6 @@ pub(crate) fn load_config() -> ( } const MAX_SPEED_LOW_LIMIT_CONST: f32 = 0.05; - - // Check the format of the IP URIs let headpat_device_uris: Vec = config.get("Setup", "device_uris") .unwrap() @@ -58,40 +55,23 @@ pub(crate) fn load_config() -> ( // handle error here, e.g. return early from the function or exit the program } - println!("Device URIs: {:?}", headpat_device_uris); + // Multi Device - - let proximity_parameters_multi: Vec = config.get("Setup", "proximity_parameters_multi") + let proximity_parameters_multi: Vec = config + .get("Setup", "proximity_parameters_multi") .unwrap() .split_whitespace() - .map(|s| format!("/avatar/parameters/{}", s)) // add "/avatar/parameters/" prefix to each string + .map(|s| format!("/avatar/parameters/{}", s)) .collect(); - println!("Device URIs: {:?}", proximity_parameters_multi); - if headpat_device_uris.len() != proximity_parameters_multi.len() { eprintln!("Error: number of device URIs does not match number of proximity parameters"); // handle error here, e.g. return early from the function or exit the program } - - - - - - - - - - - - - - let headpat_device_ip = config.get("Setup", "device_ip").unwrap(); - let headpat_device_port = "8888".to_string(); let min_speed = config.get("Haptic_Config", "min_speed").unwrap(); let min_speed_float = min_speed.parse::().unwrap() / 100.0; let max_speed = config.get("Haptic_Config", "max_speed").unwrap(); @@ -102,9 +82,7 @@ pub(crate) fn load_config() -> ( let speed_scale_float = speed_scale.parse::().unwrap() / 100.0; let port_rx = config.get("Setup", "port_rx").unwrap(); - let proximity_parameter_address = config - .get("Setup", "proximity_parameter") - .unwrap_or_else(|| "/avatar/parameters/proximity_01".into()); + let max_speed_parameter_address = config .get("Setup", "max_speed_parameter") .unwrap_or_else(|| "/avatar/parameters/max_speed".into()); @@ -112,8 +90,12 @@ pub(crate) fn load_config() -> ( println!("\n"); banner_txt(); println!("\n"); - println!(" Haptic Device: {}:{}", headpat_device_ip, headpat_device_port); - println!(" Listening for OSC on port: {}", port_rx); + println!(" Device Maps"); + for (i, parameter) in proximity_parameters_multi.iter().enumerate() { + println!(" {} => {}", parameter.trim_start_matches("/avatar/parameters/"), headpat_device_uris[i]); + } + + println!("\n Listening for OSC on port: {}", port_rx); println!("\n Vibration Configuration"); println!(" Min Speed: {}%", min_speed); println!(" Max Speed: {:?}%", max_speed_float * 100.0); @@ -121,13 +103,11 @@ pub(crate) fn load_config() -> ( println!("\nWaiting for pats..."); ( - headpat_device_ip, headpat_device_uris, min_speed_float, max_speed_float, speed_scale_float, port_rx, - proximity_parameter_address, proximity_parameters_multi, max_speed_parameter_address, max_speed_low_limit, diff --git a/giggletech-router/src/handle_proximity_parameter.rs b/giggletech-router/src/handle_proximity_parameter.rs new file mode 100644 index 0000000..98b2337 --- /dev/null +++ b/giggletech-router/src/handle_proximity_parameter.rs @@ -0,0 +1,41 @@ +use async_osc::Result; +use async_std::sync::Arc; +use std::{ + sync::atomic::{AtomicBool}, + time::Instant, +}; + + +use crate::osc_timeout; +use crate::terminator; +use crate::giggletech_osc; +use crate::data_processing; + + +pub(crate) async fn handle_proximity_parameter( + running: Arc, + device_ip: &Arc, + value: f32, + max_speed: f32, + min_speed: f32, + speed_scale: f32, +) -> Result<()> { + terminator::stop(running.clone()).await?; + + // Update Last Signal Time for timeout clock + let mut device_last_signal_times = osc_timeout::DEVICE_LAST_SIGNAL_TIME.lock().unwrap(); + device_last_signal_times.insert(device_ip.to_string(), Instant::now()); + + if value == 0.0 { + println!("Stopping pats..."); + terminator::start(running.clone(), &device_ip).await?; + + for _ in 0..5 { + giggletech_osc::send_data(&device_ip, 0i32).await?; + } + } else { + giggletech_osc::send_data(&device_ip, + data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; + } + Ok(()) +} diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index 5b3528c..1856267 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -6,65 +6,27 @@ use async_osc::{prelude::*, OscPacket, OscType, Result}; use async_std::{stream::StreamExt, task::{self}, sync::Arc,}; -use std::{time::{Instant}}; use std::sync::atomic::{AtomicBool}; use crate::osc_timeout::osc_timeout; + mod data_processing; mod config; mod giggletech_osc; mod terminator; mod osc_timeout; - - - - -async fn handle_proximity_parameter( - running: Arc, - device_ip: &Arc, - value: f32, - max_speed: f32, - min_speed: f32, - speed_scale: f32, - proximity_parameter_address: &str, -) -> Result<()> { - terminator::stop(running.clone()).await?; - - // Update Last Signal Time for timeout clock --------------------------------------------------------------- this has to be changed - - let mut device_last_signal_times = osc_timeout::DEVICE_LAST_SIGNAL_TIME.lock().unwrap(); - device_last_signal_times.insert(device_ip.to_string(), Instant::now()); - - if value == 0.0 { - println!("Stopping pats..."); - terminator::start(running.clone(), &device_ip).await?; - - for _ in 0..5 { - giggletech_osc::send_data(&device_ip, 0i32).await?; - } - } else { - //start_timer(value); - giggletech_osc::send_data(&device_ip, - data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; - } - Ok(()) -} - - +mod handle_proximity_parameter; #[async_std::main] async fn main() -> Result<()> { - // Import Config - // Todo: Refactor + let ( - headpat_device_ip, headpat_device_uris, min_speed, mut max_speed, speed_scale, port_rx, - proximity_parameter_addresses, proximity_parameters_multi, max_speed_parameter_address, max_speed_low_limit, @@ -84,10 +46,6 @@ async fn main() -> Result<()> { osc_timeout(&headpat_device_ip_clone).await.unwrap(); }); } - - - - // Listen for OSC Packets while let Some(packet) = rx_socket.next().await { let (packet, _peer_addr) = packet?; @@ -111,14 +69,13 @@ async fn main() -> Result<()> { match index { Some(i) => { - handle_proximity_parameter( + handle_proximity_parameter::handle_proximity_parameter( running.clone(), // Terminator &Arc::new(headpat_device_uris[i].clone()), value, max_speed, min_speed, speed_scale, - &proximity_parameters_multi[i], ) .await? } From 1ec5dcfa94aba3c5cbdf015eac3928da36227c96 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 14:52:19 +1000 Subject: [PATCH 14/17] Refactor / New print displays --- giggletech-router/src/data_processing.rs | 6 +++--- giggletech-router/src/giggletech_osc.rs | 2 +- giggletech-router/src/handle_proximity_parameter.rs | 4 +++- giggletech-router/src/main.rs | 7 ++++--- giggletech-router/src/osc_timeout.rs | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/giggletech-router/src/data_processing.rs b/giggletech-router/src/data_processing.rs index f71f111..059c064 100644 --- a/giggletech-router/src/data_processing.rs +++ b/giggletech-router/src/data_processing.rs @@ -1,3 +1,4 @@ + pub fn proximity_graph(proximity_signal: f32) -> String { let num_dashes = (proximity_signal * 10.0) as usize; let graph = "-".repeat(num_dashes) + ">"; @@ -19,12 +20,11 @@ pub fn print_speed_limit(headpat_max_rx: f32) { // Pat Processor const MOTOR_SPEED_SCALE: f32 = 0.66; // Overvolt Here, OEM config 0.66 going higher than this value will reduce your vibrator motor life -pub fn process_pat(proximity_signal: f32, max_speed: f32, min_speed: f32, speed_scale: f32) -> i32 { +pub fn process_pat(proximity_signal: f32, max_speed: f32, min_speed: f32, speed_scale: f32, proximity_parameter: &String) -> i32 { let graph_str = proximity_graph(proximity_signal); let headpat_tx = (((max_speed - min_speed) * proximity_signal + min_speed) * MOTOR_SPEED_SCALE * speed_scale * 255.0).round() as i32; let proximity_signal = format!("{:.2}", proximity_signal); - let max_speed = format!("{:.2}", max_speed); - eprintln!("Prox: {:5} Motor Tx: {:3} Max Speed: {:5} |{:11}|", proximity_signal, headpat_tx, max_speed, graph_str); + eprintln!("{} Prox: {:5} Motor Tx: {:3} |{:11}|", proximity_parameter.trim_start_matches("/avatar/parameters/") , proximity_signal, headpat_tx, graph_str); headpat_tx } diff --git a/giggletech-router/src/giggletech_osc.rs b/giggletech-router/src/giggletech_osc.rs index b9095ad..fe232c0 100644 --- a/giggletech-router/src/giggletech_osc.rs +++ b/giggletech-router/src/giggletech_osc.rs @@ -26,7 +26,7 @@ pub(crate) async fn setup_tx_socket(address: std::string::String) -> Result Result<()> { - println!("Sending Value:{} to IP: {}", value, device_ip); + //println!("Sending Value:{} to IP: {}", value, device_ip); let tx_socket_address = create_socket_address(device_ip, "8888"); // ------------------- Port to Send OSC Data Too let tx_socket = setup_tx_socket(tx_socket_address.clone()).await?; tx_socket.connect(tx_socket_address).await?; diff --git a/giggletech-router/src/handle_proximity_parameter.rs b/giggletech-router/src/handle_proximity_parameter.rs index 98b2337..c1cffce 100644 --- a/giggletech-router/src/handle_proximity_parameter.rs +++ b/giggletech-router/src/handle_proximity_parameter.rs @@ -19,6 +19,7 @@ pub(crate) async fn handle_proximity_parameter( max_speed: f32, min_speed: f32, speed_scale: f32, + proximity_parameters_multi: &String, ) -> Result<()> { terminator::stop(running.clone()).await?; @@ -35,7 +36,8 @@ pub(crate) async fn handle_proximity_parameter( } } else { giggletech_osc::send_data(&device_ip, - data_processing::process_pat(value, max_speed, min_speed, speed_scale)).await?; + data_processing::process_pat(value, max_speed, min_speed, speed_scale, proximity_parameters_multi)).await?; + } Ok(()) } diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index 1856267..3c50456 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -9,7 +9,6 @@ use async_std::{stream::StreamExt, task::{self}, sync::Arc,}; use std::sync::atomic::{AtomicBool}; use crate::osc_timeout::osc_timeout; - mod data_processing; mod config; mod giggletech_osc; @@ -38,7 +37,6 @@ async fn main() -> Result<()> { // Rx/Tx Socket Setup let mut rx_socket = giggletech_osc::setup_rx_socket(port_rx).await?; - // Timeout for ip in &headpat_device_uris { let headpat_device_ip_clone = ip.clone(); @@ -50,7 +48,7 @@ async fn main() -> Result<()> { while let Some(packet) = rx_socket.next().await { let (packet, _peer_addr) = packet?; - // Filter OSC Signals: Headpat Max & Headpat Prox + // Filter OSC Signals match packet { OscPacket::Bundle(_) => {} OscPacket::Message(message) => { @@ -66,9 +64,11 @@ async fn main() -> Result<()> { max_speed = value.max(max_speed_low_limit); } else { let index = proximity_parameters_multi.iter().position(|a| *a == address); + match index { Some(i) => { + handle_proximity_parameter::handle_proximity_parameter( running.clone(), // Terminator &Arc::new(headpat_device_uris[i].clone()), @@ -76,6 +76,7 @@ async fn main() -> Result<()> { max_speed, min_speed, speed_scale, + &proximity_parameters_multi[i], ) .await? } diff --git a/giggletech-router/src/osc_timeout.rs b/giggletech-router/src/osc_timeout.rs index 15d20f4..d6af287 100644 --- a/giggletech-router/src/osc_timeout.rs +++ b/giggletech-router/src/osc_timeout.rs @@ -16,7 +16,7 @@ pub async fn osc_timeout(device_ip: &str) -> Result<()> { loop { async_std::task::sleep(Duration::from_secs(1)).await; let elapsed_time = Instant::now().duration_since(*DEVICE_LAST_SIGNAL_TIME.lock().unwrap().get(device_ip).unwrap_or(&Instant::now())); - println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); + //println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); if elapsed_time >= Duration::from_secs(5) { giggletech_osc::send_data(device_ip, 0i32).await?; let mut device_last_signal_times = DEVICE_LAST_SIGNAL_TIME.lock().unwrap(); From 65222130051e2c2fa0baabc950431a3419390722 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 16:07:39 +1000 Subject: [PATCH 15/17] Timeout configurable from config.ini --- config.ini | 5 ++++- giggletech-router/src/config.rs | 13 +++++++++---- giggletech-router/src/main.rs | 4 ++-- giggletech-router/src/osc_timeout.rs | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/config.ini b/config.ini index 161ba51..2dbb15a 100644 --- a/config.ini +++ b/config.ini @@ -21,7 +21,7 @@ # max_speed_parameter = /avatar/parameters/max_speed # - [Haptic_Config] + [Config] # # Min Speed of Haptic Motor [5-100] min_speed = 2 @@ -31,4 +31,7 @@ # # Max Speed Scalar [10-100] max_speed_scale = 100 +# +# OSC Timeout [seconds] + timeout = 5 diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index adefdbc..3e64c6c 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -1,5 +1,5 @@ use configparser::ini::Ini; -use std::net::IpAddr; +use std::{net::IpAddr}; // Banner @@ -26,6 +26,7 @@ pub(crate) fn load_config() -> ( Vec, // proximity_parameters_multi String, // max_speed_parameter_address f32, // Max Speed Low Limit + u64, // Timeout Setting ) { let mut config = Ini::new(); @@ -72,15 +73,18 @@ pub(crate) fn load_config() -> ( // handle error here, e.g. return early from the function or exit the program } - let min_speed = config.get("Haptic_Config", "min_speed").unwrap(); + let min_speed = config.get("Config", "min_speed").unwrap(); let min_speed_float = min_speed.parse::().unwrap() / 100.0; - let max_speed = config.get("Haptic_Config", "max_speed").unwrap(); + let max_speed = config.get("Config", "max_speed").unwrap(); let max_speed_float = max_speed.parse::().unwrap() / 100.0; let max_speed_low_limit = MAX_SPEED_LOW_LIMIT_CONST; let max_speed_float = max_speed_float.max(max_speed_low_limit); - let speed_scale = config.get("Haptic_Config", "max_speed_scale").unwrap(); + let speed_scale = config.get("Config", "max_speed_scale").unwrap(); let speed_scale_float = speed_scale.parse::().unwrap() / 100.0; let port_rx = config.get("Setup", "port_rx").unwrap(); + let timeout_str = config.get("Config", "timeout").unwrap(); + let timeout = timeout_str.parse::().unwrap_or(0); + println!("\n Timeout: {}", timeout); let max_speed_parameter_address = config @@ -111,5 +115,6 @@ pub(crate) fn load_config() -> ( proximity_parameters_multi, max_speed_parameter_address, max_speed_low_limit, + timeout, ) } \ No newline at end of file diff --git a/giggletech-router/src/main.rs b/giggletech-router/src/main.rs index 3c50456..3f7ba75 100644 --- a/giggletech-router/src/main.rs +++ b/giggletech-router/src/main.rs @@ -29,6 +29,7 @@ async fn main() -> Result<()> { proximity_parameters_multi, max_speed_parameter_address, max_speed_low_limit, + timeout, ) = config::load_config(); // Setup Start / Stop of Terminiator @@ -41,7 +42,7 @@ async fn main() -> Result<()> { for ip in &headpat_device_uris { let headpat_device_ip_clone = ip.clone(); task::spawn(async move { - osc_timeout(&headpat_device_ip_clone).await.unwrap(); + osc_timeout(&headpat_device_ip_clone, timeout).await.unwrap(); }); } // Listen for OSC Packets @@ -64,7 +65,6 @@ async fn main() -> Result<()> { max_speed = value.max(max_speed_low_limit); } else { let index = proximity_parameters_multi.iter().position(|a| *a == address); - match index { Some(i) => { diff --git a/giggletech-router/src/osc_timeout.rs b/giggletech-router/src/osc_timeout.rs index d6af287..920ad03 100644 --- a/giggletech-router/src/osc_timeout.rs +++ b/giggletech-router/src/osc_timeout.rs @@ -12,12 +12,12 @@ lazy_static! { Arc::new(Mutex::new(HashMap::new())); } -pub async fn osc_timeout(device_ip: &str) -> Result<()> { +pub async fn osc_timeout(device_ip: &str, timeout: u64) -> Result<()> { loop { async_std::task::sleep(Duration::from_secs(1)).await; let elapsed_time = Instant::now().duration_since(*DEVICE_LAST_SIGNAL_TIME.lock().unwrap().get(device_ip).unwrap_or(&Instant::now())); //println!("Device Ip {} Elapsed Time {:?}", device_ip, elapsed_time); - if elapsed_time >= Duration::from_secs(5) { + if elapsed_time >= Duration::from_secs(timeout) { giggletech_osc::send_data(device_ip, 0i32).await?; let mut device_last_signal_times = DEVICE_LAST_SIGNAL_TIME.lock().unwrap(); device_last_signal_times.insert(device_ip.to_string(), Instant::now()); From 168e0336f951d5af23b9ae2ad2e0a2833c262822 Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 16:20:36 +1000 Subject: [PATCH 16/17] Clean up Working --- config.ini | 4 ++-- giggletech-router/src/config.rs | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/config.ini b/config.ini index 2dbb15a..1d5197b 100644 --- a/config.ini +++ b/config.ini @@ -11,7 +11,7 @@ # # IP Address of Headpat Device # - device_uris = 192.168.1.151 192.168.1.157 192.168.1.153 + device_ips = 192.168.1.151 192.168.1.157 192.168.1.153 # # Port listening for OSC [Default 9001] # @@ -19,7 +19,7 @@ # proximity_parameters_multi = proximity_01 proximity_02 proximity_03 # - max_speed_parameter = /avatar/parameters/max_speed + max_speed_parameter = max_speed # [Config] # diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index 3e64c6c..e076365 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -37,7 +37,7 @@ pub(crate) fn load_config() -> ( const MAX_SPEED_LOW_LIMIT_CONST: f32 = 0.05; // Check the format of the IP URIs - let headpat_device_uris: Vec = config.get("Setup", "device_uris") + let headpat_device_uris: Vec = config.get("Setup", "device_ips") .unwrap() .split_whitespace() .map(|s| s.to_string()) // convert &str to String @@ -87,9 +87,8 @@ pub(crate) fn load_config() -> ( println!("\n Timeout: {}", timeout); - let max_speed_parameter_address = config - .get("Setup", "max_speed_parameter") - .unwrap_or_else(|| "/avatar/parameters/max_speed".into()); + let max_speed_parameter_address = format!("/avatar/parameters/{}", config.get("Setup", "max_speed_parameter").unwrap_or_else(|| "/avatar/parameters/max_speed".into())); + println!("\n"); banner_txt(); From 1292930eb6c875647af5b723332637f9c59e6dce Mon Sep 17 00:00:00 2001 From: lizard-brain <37095041+lizard-brain@users.noreply.github.com> Date: Wed, 3 May 2023 16:58:37 +1000 Subject: [PATCH 17/17] Working and Ready --- config.ini | 10 +-- giggletech-router/src/config.rs | 64 ++++++++++--------- giggletech-router/src/data_processing.rs | 2 + giggletech-router/src/giggletech_osc.rs | 2 + .../src/handle_proximity_parameter.rs | 2 + giggletech-router/src/terminator.rs | 2 + 6 files changed, 46 insertions(+), 36 deletions(-) diff --git a/config.ini b/config.ini index 1d5197b..c423635 100644 --- a/config.ini +++ b/config.ini @@ -9,15 +9,15 @@ # [Setup] # -# IP Address of Headpat Device -# - device_ips = 192.168.1.151 192.168.1.157 192.168.1.153 -# # Port listening for OSC [Default 9001] # port_rx = 9001 # - proximity_parameters_multi = proximity_01 proximity_02 proximity_03 +# IP Address of Headpat Device + device_ips = 192.168.1.151 192.168.1.157 192.168.1.153 +# +# Unity Proximity parameter[s] + proximity_parameters_multi = proximity_01 proximity_02 proximity_03 # max_speed_parameter = max_speed # diff --git a/giggletech-router/src/config.rs b/giggletech-router/src/config.rs index e076365..0278f62 100644 --- a/giggletech-router/src/config.rs +++ b/giggletech-router/src/config.rs @@ -1,7 +1,8 @@ +// config.rs + use configparser::ini::Ini; use std::{net::IpAddr}; - // Banner fn banner_txt(){ // https://fsymbols.com/generators/carty/ @@ -18,15 +19,15 @@ fn banner_txt(){ } pub(crate) fn load_config() -> ( - Vec, // headpat_device_URIs - f32, // min_speed_float - f32, // max_speed_float - f32, // speed_scale_float - String, // port_rx - Vec, // proximity_parameters_multi - String, // max_speed_parameter_address - f32, // Max Speed Low Limit - u64, // Timeout Setting + Vec, // headpat_device_URIs + f32, // min_speed_float + f32, // max_speed_float + f32, // speed_scale_float + String, // port_rx + Vec, // proximity_parameters_multi + String, // max_speed_parameter_address + f32, // Max Speed Low Limit + u64, // Timeout Setting ) { let mut config = Ini::new(); @@ -34,8 +35,7 @@ pub(crate) fn load_config() -> ( Err(why) => panic!("{}", why), Ok(_) => {} } - const MAX_SPEED_LOW_LIMIT_CONST: f32 = 0.05; - + // Check the format of the IP URIs let headpat_device_uris: Vec = config.get("Setup", "device_ips") .unwrap() @@ -56,10 +56,6 @@ pub(crate) fn load_config() -> ( // handle error here, e.g. return early from the function or exit the program } - - - // Multi Device - let proximity_parameters_multi: Vec = config .get("Setup", "proximity_parameters_multi") .unwrap() @@ -73,23 +69,25 @@ pub(crate) fn load_config() -> ( // handle error here, e.g. return early from the function or exit the program } - let min_speed = config.get("Config", "min_speed").unwrap(); - let min_speed_float = min_speed.parse::().unwrap() / 100.0; - let max_speed = config.get("Config", "max_speed").unwrap(); - let max_speed_float = max_speed.parse::().unwrap() / 100.0; - let max_speed_low_limit = MAX_SPEED_LOW_LIMIT_CONST; - let max_speed_float = max_speed_float.max(max_speed_low_limit); - let speed_scale = config.get("Config", "max_speed_scale").unwrap(); - let speed_scale_float = speed_scale.parse::().unwrap() / 100.0; - let port_rx = config.get("Setup", "port_rx").unwrap(); - let timeout_str = config.get("Config", "timeout").unwrap(); - let timeout = timeout_str.parse::().unwrap_or(0); - println!("\n Timeout: {}", timeout); - + const MAX_SPEED_LOW_LIMIT_CONST: f32 = 0.05; + let min_speed = config.get("Config", "min_speed").unwrap(); + let min_speed_float = min_speed.parse::().unwrap() / 100.0; + + let max_speed = config.get("Config", "max_speed").unwrap().parse::().unwrap() / 100.0; + let max_speed_low_limit = MAX_SPEED_LOW_LIMIT_CONST; + let max_speed_float = max_speed.max(max_speed_low_limit); + + let speed_scale = config.get("Config", "max_speed_scale").unwrap(); + let speed_scale_float = speed_scale.parse::().unwrap() / 100.0; + + let port_rx = config.get("Setup", "port_rx").unwrap(); + + let timeout_str = config.get("Config", "timeout").unwrap(); + let timeout = timeout_str.parse::().unwrap_or(0); + let max_speed_parameter_address = format!("/avatar/parameters/{}", config.get("Setup", "max_speed_parameter").unwrap_or_else(|| "/avatar/parameters/max_speed".into())); - println!("\n"); banner_txt(); println!("\n"); @@ -103,6 +101,7 @@ pub(crate) fn load_config() -> ( println!(" Min Speed: {}%", min_speed); println!(" Max Speed: {:?}%", max_speed_float * 100.0); println!(" Scale Factor: {}%", speed_scale); + println!(" Timeout: {}s", timeout); println!("\nWaiting for pats..."); ( @@ -116,4 +115,7 @@ pub(crate) fn load_config() -> ( max_speed_low_limit, timeout, ) -} \ No newline at end of file +} + + + diff --git a/giggletech-router/src/data_processing.rs b/giggletech-router/src/data_processing.rs index 059c064..32175a9 100644 --- a/giggletech-router/src/data_processing.rs +++ b/giggletech-router/src/data_processing.rs @@ -1,3 +1,5 @@ +// data_processing.rs + pub fn proximity_graph(proximity_signal: f32) -> String { let num_dashes = (proximity_signal * 10.0) as usize; diff --git a/giggletech-router/src/giggletech_osc.rs b/giggletech-router/src/giggletech_osc.rs index fe232c0..e2ba009 100644 --- a/giggletech-router/src/giggletech_osc.rs +++ b/giggletech-router/src/giggletech_osc.rs @@ -1,3 +1,5 @@ +// giggletech_osc.rs + // GiggleTech OSC Module // Data Sender, Tx & Rx Socket Setup diff --git a/giggletech-router/src/handle_proximity_parameter.rs b/giggletech-router/src/handle_proximity_parameter.rs index c1cffce..f84f7e2 100644 --- a/giggletech-router/src/handle_proximity_parameter.rs +++ b/giggletech-router/src/handle_proximity_parameter.rs @@ -1,3 +1,5 @@ +// handle_proximity_parameter.rs + use async_osc::Result; use async_std::sync::Arc; use std::{ diff --git a/giggletech-router/src/terminator.rs b/giggletech-router/src/terminator.rs index a15a8d0..3af9ad0 100644 --- a/giggletech-router/src/terminator.rs +++ b/giggletech-router/src/terminator.rs @@ -1,3 +1,5 @@ +// terminator.rs + use async_osc::{Result}; use async_std::{task::{self},sync::Arc,};