individual synchronization of fixtures with timeout

This commit is contained in:
Michael Sippel 2024-05-12 00:40:46 +02:00
parent 1b94695d57
commit 2441c5a133
Signed by: senvas
GPG key ID: 060F22F65102F95C
8 changed files with 71 additions and 58 deletions

View file

@ -6,8 +6,8 @@ use {
}; };
pub trait FixtureDriver { pub trait FixtureDriver {
fn send(&self, pixels: &Vec<Rgb<u8>>); fn send(&mut self, pixels: &Vec<Rgb<u8>>);
fn sync(&self); fn sync(&mut self);
} }
pub struct Fixture { pub struct Fixture {

View file

@ -61,8 +61,8 @@ async fn main() {
let dim = Arc::new(Mutex::new((1 as u32,1 as u32))); 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"))); 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_read_timeout(Some(std::time::Duration::from_millis(1)));
socket.write().unwrap().set_write_timeout(Some(std::time::Duration::from_millis(50))); socket.write().unwrap().set_write_timeout(Some(std::time::Duration::from_millis(1)));
let inputs = Arc::new(RwLock::new( let inputs = Arc::new(RwLock::new(
Inputs { Inputs {
@ -93,11 +93,10 @@ async fn main() {
Fixture::new_stripe() Fixture::new_stripe()
.with_driver( Box::new(StripeDriver::new("192.168.0.112:4210", socket.clone())) ) .with_driver( Box::new(StripeDriver::new("192.168.0.112:4210", socket.clone())) )
.offset(Vector2::new(-0.4, 0.0)), .offset(Vector2::new(-0.4, 0.0)),
/*
Fixture::new_stripe() Fixture::new_stripe()
.with_driver( Box::new(StripeDriver::new("192.168.0.113:4210", socket.clone())) ) .with_driver( Box::new(StripeDriver::new("192.168.0.113:4210", socket.clone())) )
.offset(Vector2::new(0.4, 0.0)), .offset(Vector2::new(0.4, 0.0)),
*/
Fixture::new_stripe() Fixture::new_stripe()
.with_driver( Box::new(StripeDriver::new("192.168.0.114:4210", socket.clone()))) .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 last_tap = std::time::Instant::now();
let mut z = 0; 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| { event_loop.run(move |event, elwt| {
let tcur = std::time::Instant::now(); let tcur = std::time::Instant::now();
elwt.set_control_flow(ControlFlow:: elwt.set_control_flow(ControlFlow::
WaitUntil( WaitUntil(
tcur + Duration::from_millis(10) tcur + Duration::from_millis(100)
)); ));
inputs.write().unwrap().t = tcur - tbegin; inputs.write().unwrap().t = tcur - tbegin;
inputs.write().unwrap().transition_time = tcur - transition_begin; inputs.write().unwrap().transition_time = tcur - transition_begin;
lighting_setup.update_buffers(); lighting_setup.update_buffers();
if active { 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); lighting_setup.update_outputs(i);
} }
} }

View file

@ -18,13 +18,13 @@ impl ColorGrid for PastelFade {
fn get(&self, p: &Vector2<f32>) -> Rgb<f32> { fn get(&self, p: &Vector2<f32>) -> Rgb<f32> {
let inputs = self.inputs.read().unwrap().clone(); 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( Rgb::from_color(
&Hsv::<f32, Turns<f32>>::new( &Hsv::<f32, Turns<f32>>::new(
Turns( i ), Turns( i ),
0.5, 0.5,
(4.0*i) %1.0 1.0
) )
) )
} }

View file

@ -15,7 +15,6 @@ use {
pub struct Strobe { pub struct Strobe {
pub inputs: Arc<RwLock< Inputs >>, pub inputs: Arc<RwLock< Inputs >>,
pub subdivision: u32,
} }
impl ColorGrid for Strobe { impl ColorGrid for Strobe {

View file

@ -28,11 +28,11 @@ impl ColorGrid for UbootPrüfstandFade {
&Hsv::<f32, Turns<f32>>::new( &Hsv::<f32, Turns<f32>>::new(
Turns( 0.65 ), Turns( 0.65 ),
0.9, 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; let p = ( inputs.t.as_millis() as f32 / (32.0*inputs.cycle_len.as_millis() as f32)) % 1.0;
if p >= 0.7 { if p >= 0.7 {

View file

@ -32,22 +32,19 @@ impl SceneLibrary {
// 0 // 0
Box::new( Breathing{ inputs: inputs.clone() } ), Box::new( Breathing{ inputs: inputs.clone() } ),
// 1 - 4 // 1
Box::new( Strobe{ inputs: inputs.clone(), subdivision: 4 } ), Box::new( Strobe{ inputs: inputs.clone() } ),
Box::new( Strobe{ inputs: inputs.clone(), subdivision: 8 } ), // 2
Box::new( Strobe{ inputs: inputs.clone(), subdivision: 12 } ),
Box::new( Strobe{ inputs: inputs.clone(), subdivision: 16 } ),
// others
Box::new( WaveFade{ inputs: inputs.clone(), hue: 0.5 } ), Box::new( WaveFade{ inputs: inputs.clone(), hue: 0.5 } ),
// 3
Box::new( UbootPrüfstandFade{ inputs: inputs.clone() } ), Box::new( UbootPrüfstandFade{ inputs: inputs.clone() } ),
//4
Box::new( ArcticRain{ inputs: inputs.clone() } ), Box::new( ArcticRain{ inputs: inputs.clone() } ),
//5
Box::new( GastelFade{ inputs: inputs.clone() } ),
Box::new( Wheel{ inputs: inputs.clone() } ), Box::new( Wheel{ inputs: inputs.clone() } ),
//6
Box::new( Alternate{ inputs: inputs.clone() } ), Box::new( Alternate{ inputs: inputs.clone() } ),
//7
Box::new( PastelFade{ inputs: inputs.clone() } ), Box::new( PastelFade{ inputs: inputs.clone() } ),
], ],
current_scene: RwLock::new(0), current_scene: RwLock::new(0),

View file

@ -32,26 +32,19 @@ impl LightingSetup {
} }
pub fn update_outputs(&mut self, id: usize) { pub fn update_outputs(&mut self, id: usize) {
/* let fixture = &mut self.fixtures[id];
for fixture in self.fixtures.iter() {
if let Some(driver) = fixture.driver.as_ref() {
driver.send( &fixture.buffer );
}
}*/
let fixture = &self.fixtures[id]; if let Some(mut driver) = fixture.driver.as_mut() {
if let Some(driver) = fixture.driver.as_ref() {
driver.send( &fixture.buffer ); driver.send( &fixture.buffer );
//driver.sync();
} }
/* }
for fixture in self.fixtures.iter() {
if let Some(driver) = fixture.driver.as_ref() { 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(); driver.sync();
} }
} }
*/
}
pub fn draw_preview( pub fn draw_preview(
&self, &self,

View file

@ -6,6 +6,7 @@ use {
pub struct StripeDriver { pub struct StripeDriver {
socket: Arc<RwLock<std::net::UdpSocket>>, socket: Arc<RwLock<std::net::UdpSocket>>,
timeout: usize,
addr: String addr: String
} }
@ -13,34 +14,34 @@ impl StripeDriver {
pub fn new(addr: &str, socket: Arc<RwLock<std::net::UdpSocket>>) -> Self { pub fn new(addr: &str, socket: Arc<RwLock<std::net::UdpSocket>>) -> Self {
StripeDriver { StripeDriver {
addr: addr.into(), addr: addr.into(),
timeout: 0,
socket socket
} }
} }
} }
impl FixtureDriver for StripeDriver { impl FixtureDriver for StripeDriver {
fn send(&self, pixels: &Vec<Rgb<u8>>) { fn send(&mut self, pixels: &Vec<Rgb<u8>>) {
if self.timeout == 0 {
const STRIPE_LEN : usize = 72; const STRIPE_LEN : usize = 72;
let mut buf = [0 as u8; STRIPE_LEN*3]; let mut buf = [0 as u8; STRIPE_LEN*3];
for x in 0 .. STRIPE_LEN { for x in 0 .. STRIPE_LEN {
buf[x*3+0] = pixels[x].green()/2; buf[x*3+0] = pixels[x].green()/4;
buf[x*3+1] = pixels[x].red()/2; buf[x*3+1] = pixels[x].red()/4;
buf[x*3+2] = pixels[x].blue()/2; buf[x*3+2] = pixels[x].blue()/4;
} }
self.socket.write().unwrap().send_to(&buf, &self.addr); self.socket.write().unwrap().send_to(&buf, &self.addr);
self.timeout = 100;
} else {
self.timeout -= 1;
}
} }
fn sync(&self) { fn sync(&mut self) {
let mut rbuf = [0 as u8; 8]; self.timeout = 0;
match
self.socket.write().unwrap().recv(&mut rbuf) {
Ok(_) => {}
Err(_) => {
eprintln!("missing response from stripe");
}
}
} }
} }