From 2441c5a13398f5a88989155874420974b6d84129 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 12 May 2024 00:40:46 +0200 Subject: [PATCH] individual synchronization of fixtures with timeout --- src/fixture.rs | 4 +-- src/main.rs | 37 ++++++++++++++++++++++------ src/patterns/pastel_fade.rs | 4 +-- src/patterns/strobe.rs | 1 - src/patterns/uboot_prüfstand_fade.rs | 4 +-- src/scene_library.rs | 19 ++++++-------- src/setup.rs | 23 ++++++----------- src/stripe_driver.rs | 37 ++++++++++++++-------------- 8 files changed, 71 insertions(+), 58 deletions(-) diff --git a/src/fixture.rs b/src/fixture.rs index 9cc82c0..b1178d7 100644 --- a/src/fixture.rs +++ b/src/fixture.rs @@ -6,8 +6,8 @@ use { }; pub trait FixtureDriver { - fn send(&self, pixels: &Vec>); - fn sync(&self); + fn send(&mut self, pixels: &Vec>); + fn sync(&mut self); } pub struct Fixture { diff --git a/src/main.rs b/src/main.rs index 28fa5b7..a4ec5b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,8 +61,8 @@ async fn main() { let dim = Arc::new(Mutex::new((1 as u32,1 as u32))); let socket = Arc::new(RwLock::new(std::net::UdpSocket::bind("0.0.0.0:4210").expect("failed to bind UDP socket"))); - socket.write().unwrap().set_read_timeout(Some(std::time::Duration::from_millis(500))); - socket.write().unwrap().set_write_timeout(Some(std::time::Duration::from_millis(50))); + socket.write().unwrap().set_read_timeout(Some(std::time::Duration::from_millis(1))); + socket.write().unwrap().set_write_timeout(Some(std::time::Duration::from_millis(1))); let inputs = Arc::new(RwLock::new( Inputs { @@ -93,11 +93,10 @@ async fn main() { Fixture::new_stripe() .with_driver( Box::new(StripeDriver::new("192.168.0.112:4210", socket.clone())) ) .offset(Vector2::new(-0.4, 0.0)), -/* + Fixture::new_stripe() .with_driver( Box::new(StripeDriver::new("192.168.0.113:4210", socket.clone())) ) .offset(Vector2::new(0.4, 0.0)), - */ Fixture::new_stripe() .with_driver( Box::new(StripeDriver::new("192.168.0.114:4210", socket.clone()))) @@ -112,20 +111,44 @@ async fn main() { let mut last_tap = std::time::Instant::now(); let mut z = 0; - let mut active = true; + let mut active = false; + + for i in 0..5 { + lighting_setup.sync_fixture(i); + } event_loop.run(move |event, elwt| { let tcur = std::time::Instant::now(); elwt.set_control_flow(ControlFlow:: WaitUntil( - tcur + Duration::from_millis(10) + tcur + Duration::from_millis(100) )); inputs.write().unwrap().t = tcur - tbegin; inputs.write().unwrap().transition_time = tcur - transition_begin; lighting_setup.update_buffers(); if active { - for i in 1..5 { + // sync + let mut rbuf = [0 as u8; 8]; + + while let Ok((n, src_addr)) = + socket.write().unwrap().recv_from(&mut rbuf) + { + if src_addr == "192.168.0.111:4210".parse().expect("parse socketaddr") { + lighting_setup.sync_fixture(1); + } + if src_addr == "192.168.0.112:4210".parse().expect("parse socketaddr") { + lighting_setup.sync_fixture(2); + } + if src_addr == "192.168.0.113:4210".parse().expect("parse socketaddr") { + lighting_setup.sync_fixture(3); + } + if src_addr == "192.168.0.114:4210".parse().expect("parse socketaddr") { + lighting_setup.sync_fixture(4); + } + } + + for i in 1 .. 5 { lighting_setup.update_outputs(i); } } diff --git a/src/patterns/pastel_fade.rs b/src/patterns/pastel_fade.rs index 0b6c6ff..7f2dcde 100644 --- a/src/patterns/pastel_fade.rs +++ b/src/patterns/pastel_fade.rs @@ -18,13 +18,13 @@ impl ColorGrid for PastelFade { fn get(&self, p: &Vector2) -> Rgb { let inputs = self.inputs.read().unwrap().clone(); - let i = ( inputs.t.as_millis() as f32 / (4.0*inputs.cycle_len.as_millis() as f32) ) % 1.0; + let i = ( inputs.t.as_millis() as f32 / (inputs.wheel as f32*inputs.cycle_len.as_millis() as f32) ) % 1.0; Rgb::from_color( &Hsv::>::new( Turns( i ), 0.5, - (4.0*i) %1.0 + 1.0 ) ) } diff --git a/src/patterns/strobe.rs b/src/patterns/strobe.rs index 185bd62..06f0f8b 100644 --- a/src/patterns/strobe.rs +++ b/src/patterns/strobe.rs @@ -15,7 +15,6 @@ use { pub struct Strobe { pub inputs: Arc>, - pub subdivision: u32, } impl ColorGrid for Strobe { diff --git a/src/patterns/uboot_prüfstand_fade.rs b/src/patterns/uboot_prüfstand_fade.rs index 2ab72f8..38108fc 100644 --- a/src/patterns/uboot_prüfstand_fade.rs +++ b/src/patterns/uboot_prüfstand_fade.rs @@ -28,11 +28,11 @@ impl ColorGrid for UbootPrüfstandFade { &Hsv::>::new( Turns( 0.65 ), 0.9, - 0.5 + 0.5*f32::sin(i*pi2),//(4.0*i) %1.0 + 1.0 ) ); - let col2 = Rgb::new(0.5+0.5*f32::sin(i*pi2), 0.0, 0.0); + let col2 = Rgb::new(0.8, 0.0, 0.0); let p = ( inputs.t.as_millis() as f32 / (32.0*inputs.cycle_len.as_millis() as f32)) % 1.0; if p >= 0.7 { diff --git a/src/scene_library.rs b/src/scene_library.rs index a6c2490..0c95232 100644 --- a/src/scene_library.rs +++ b/src/scene_library.rs @@ -32,22 +32,19 @@ impl SceneLibrary { // 0 Box::new( Breathing{ inputs: inputs.clone() } ), - // 1 - 4 - Box::new( Strobe{ inputs: inputs.clone(), subdivision: 4 } ), - Box::new( Strobe{ inputs: inputs.clone(), subdivision: 8 } ), - Box::new( Strobe{ inputs: inputs.clone(), subdivision: 12 } ), - Box::new( Strobe{ inputs: inputs.clone(), subdivision: 16 } ), - - // others + // 1 + Box::new( Strobe{ inputs: inputs.clone() } ), + // 2 Box::new( WaveFade{ inputs: inputs.clone(), hue: 0.5 } ), + // 3 Box::new( UbootPrüfstandFade{ inputs: inputs.clone() } ), + //4 Box::new( ArcticRain{ inputs: inputs.clone() } ), - - Box::new( GastelFade{ inputs: inputs.clone() } ), + //5 Box::new( Wheel{ inputs: inputs.clone() } ), - - + //6 Box::new( Alternate{ inputs: inputs.clone() } ), + //7 Box::new( PastelFade{ inputs: inputs.clone() } ), ], current_scene: RwLock::new(0), diff --git a/src/setup.rs b/src/setup.rs index 72c3aba..d346ada 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -32,25 +32,18 @@ impl LightingSetup { } pub fn update_outputs(&mut self, id: usize) { - /* - for fixture in self.fixtures.iter() { - if let Some(driver) = fixture.driver.as_ref() { - driver.send( &fixture.buffer ); - } - }*/ + let fixture = &mut self.fixtures[id]; - let fixture = &self.fixtures[id]; - if let Some(driver) = fixture.driver.as_ref() { + if let Some(mut driver) = fixture.driver.as_mut() { driver.send( &fixture.buffer ); - //driver.sync(); } - /* - for fixture in self.fixtures.iter() { - if let Some(driver) = fixture.driver.as_ref() { - driver.sync(); - } } - */ + + pub fn sync_fixture(&mut self, id: usize) { + let fixture = &mut self.fixtures[id]; + if let Some(mut driver) = fixture.driver.as_mut() { + driver.sync(); + } } pub fn draw_preview( diff --git a/src/stripe_driver.rs b/src/stripe_driver.rs index 49876cd..81771a2 100644 --- a/src/stripe_driver.rs +++ b/src/stripe_driver.rs @@ -6,6 +6,7 @@ use { pub struct StripeDriver { socket: Arc>, + timeout: usize, addr: String } @@ -13,34 +14,34 @@ impl StripeDriver { pub fn new(addr: &str, socket: Arc>) -> Self { StripeDriver { addr: addr.into(), + timeout: 0, socket } } } impl FixtureDriver for StripeDriver { - fn send(&self, pixels: &Vec>) { - const STRIPE_LEN : usize = 72; - let mut buf = [0 as u8; STRIPE_LEN*3]; + fn send(&mut self, pixels: &Vec>) { + if self.timeout == 0 { + const STRIPE_LEN : usize = 72; + let mut buf = [0 as u8; STRIPE_LEN*3]; - for x in 0 .. STRIPE_LEN { - buf[x*3+0] = pixels[x].green()/2; - buf[x*3+1] = pixels[x].red()/2; - buf[x*3+2] = pixels[x].blue()/2; + for x in 0 .. STRIPE_LEN { + buf[x*3+0] = pixels[x].green()/4; + buf[x*3+1] = pixels[x].red()/4; + buf[x*3+2] = pixels[x].blue()/4; + } + + self.socket.write().unwrap().send_to(&buf, &self.addr); + + self.timeout = 100; + } else { + self.timeout -= 1; } - - self.socket.write().unwrap().send_to(&buf, &self.addr); } - fn sync(&self) { - let mut rbuf = [0 as u8; 8]; - match - self.socket.write().unwrap().recv(&mut rbuf) { - Ok(_) => {} - Err(_) => { - eprintln!("missing response from stripe"); - } - } + fn sync(&mut self) { + self.timeout = 0; } }