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 {
fn send(&self, pixels: &Vec<Rgb<u8>>);
fn sync(&self);
fn send(&mut self, pixels: &Vec<Rgb<u8>>);
fn sync(&mut self);
}
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 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,19 +111,43 @@ 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 {
// 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);
}

View file

@ -18,13 +18,13 @@ impl ColorGrid for PastelFade {
fn get(&self, p: &Vector2<f32>) -> Rgb<f32> {
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::<f32, Turns<f32>>::new(
Turns( i ),
0.5,
(4.0*i) %1.0
1.0
)
)
}

View file

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

View file

@ -28,11 +28,11 @@ impl ColorGrid for UbootPrüfstandFade {
&Hsv::<f32, Turns<f32>>::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 {

View file

@ -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),

View file

@ -32,26 +32,19 @@ 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() {
}
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(
&self,

View file

@ -6,6 +6,7 @@ use {
pub struct StripeDriver {
socket: Arc<RwLock<std::net::UdpSocket>>,
timeout: usize,
addr: String
}
@ -13,34 +14,34 @@ impl StripeDriver {
pub fn new(addr: &str, socket: Arc<RwLock<std::net::UdpSocket>>) -> Self {
StripeDriver {
addr: addr.into(),
timeout: 0,
socket
}
}
}
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;
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;
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);
}
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");
}
}
self.timeout = 100;
} else {
self.timeout -= 1;
}
}
fn sync(&mut self) {
self.timeout = 0;
}
}