Skip to content

Commit

Permalink
Merge pull request #3 from lizard-brain/osc_timout-module
Browse files Browse the repository at this point in the history
OSC Timeout Encapsulated
  • Loading branch information
me-sideways authored Apr 28, 2023
2 parents 0415ec4 + 5f99990 commit 39504f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[Setup]
#
# IP Address of Headpat Device
device_ip = 192.168.1.151
device_ip = 192.168.1.157
#
# Port listening for OSC [Default 9001]
port_rx = 9100
Expand Down
2 changes: 2 additions & 0 deletions giggletech-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ lazy_static = "1.4.0"
tokio = { version = "1.13", features = ["full"] }
futures = "0.3"
#ini = "1.3.0"
anyhow = "1.0.44"




Expand Down
34 changes: 5 additions & 29 deletions giggletech-router/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,15 @@

use async_osc::{prelude::*, OscPacket, OscType, Result};
use async_std::{stream::StreamExt, task::{self}, sync::Arc,};
use lazy_static::lazy_static;
use std::{sync::Mutex, time::{Duration, Instant}};
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;


// TimeOut
lazy_static! {
static ref LAST_SIGNAL_TIME: Mutex<Instant> = Mutex::new(Instant::now());
}

async fn osc_timeout(device_ip: &str) -> Result<()> {
// Todo: Need to pull into module
// If no new osc signal is Rx for 5s, will send stop packets
// This loop can be used to implement Kays 'Soft Pat'
loop {
task::sleep(Duration::from_secs(1)).await;
let elapsed_time = Instant::now().duration_since(*LAST_SIGNAL_TIME.lock().unwrap());

if elapsed_time >= Duration::from_secs(5) {
// Send stop packet
//println!("Pat Timeout...");
giggletech_osc::send_data(device_ip, 0i32).await?;

let mut last_signal_time = LAST_SIGNAL_TIME.lock().unwrap();
*last_signal_time = Instant::now();
}
}
}
mod osc_timeout;

#[async_std::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -96,7 +72,7 @@ async fn main() -> Result<()> {

terminator::stop(running.clone()).await?;
// Update Last Signal Time for timeout clock
let mut last_signal_time = LAST_SIGNAL_TIME.lock().unwrap();
let mut last_signal_time = osc_timeout::LAST_SIGNAL_TIME.lock().unwrap();
*last_signal_time = Instant::now();

// Stop Function
Expand All @@ -120,4 +96,4 @@ async fn main() -> Result<()> {
}
}
Ok(())
}
}
26 changes: 26 additions & 0 deletions giggletech-router/src/osc_timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// osc_timeout.rs

use std::sync::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<Instant> = Mutex::new(Instant::now());
}

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());

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();
}
}
}

0 comments on commit 39504f4

Please sign in to comment.