From 215f694d88ba7ec318eca7a528b12ac7d1f65a05 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 26 Apr 2024 02:36:34 +0200 Subject: [PATCH] add FixtureDriver trait and StripeDriver --- src/main.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c358e9..4fdc283 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,13 +63,19 @@ impl ColorGrid for TestAnimation { } } +trait FixtureDriver { + fn send(&self, pixels: &Vec>); +} + struct Fixture { resolution: Vector2, position: Vector2, rotation: f32, scale: f32, brightness: f32, - buffer: Vec< Rgb > + buffer: Vec< Rgb >, + + driver: Option> } impl Fixture { @@ -80,7 +86,8 @@ impl Fixture { rotation: 0.0, scale: 0.01, brightness: 0.8, - buffer: Vec::new() + buffer: Vec::new(), + driver: None }; fixture.buffer.resize( (fixture.resolution.x*fixture.resolution.y) as usize, @@ -95,7 +102,8 @@ impl Fixture { rotation: 0.0, scale: 0.03, brightness: 0.8, - buffer: Vec::new() + buffer: Vec::new(), + driver: None }; fixture.buffer.resize( (fixture.resolution.x*fixture.resolution.y) as usize, @@ -104,6 +112,11 @@ impl Fixture { fixture } + pub fn with_driver(mut self, driver: Box) -> Self { + self.driver = Some(driver); + self + } + fn offset(mut self, offset: Vector2) -> Self { self.position += offset; self @@ -157,6 +170,14 @@ impl LightingSetup { } } + fn update_outputs(&mut self) { + for fixture in self.fixtures.iter() { + if let Some(driver) = fixture.driver.as_ref() { + driver.send( &fixture.buffer ); + } + } + } + fn draw_preview( &self, buffer: &mut softbuffer::Buffer<'_, Arc, Arc>, @@ -215,6 +236,44 @@ impl LightingSetup { } } +struct StripeDriver { + socket: Arc>, + addr: String +} + +impl StripeDriver { + fn new(addr: &str, socket: Arc>) -> Self { + StripeDriver { + addr: addr.into(), + socket + } + } +} + +impl FixtureDriver for StripeDriver { + fn send(&self, pixels: &Vec>) { + 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(); + buf[x*3+1] = pixels[x].red(); + buf[x*3+2] = pixels[x].blue(); + } + + self.socket.write().unwrap().send_to(&buf, &self.addr); + + let mut rbuf = [0 as u8; 8]; + match + self.socket.write().unwrap().recv(&mut rbuf) { + Ok(_) => {} + Err(_) => { + eprintln!("missing response from stripe"); + } + } + } +} + #[async_std::main] async fn main() { let event_loop = EventLoop::new().unwrap(); @@ -224,13 +283,30 @@ 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))); + let mut lighting_setup = LightingSetup::new( vec![ Fixture::new_matrix(), - Fixture::new_stripe().offset(Vector2::new(-0.5, 0.0)), - Fixture::new_stripe().offset(Vector2::new(-0.4, 0.0)), - Fixture::new_stripe().offset(Vector2::new( 0.4, 0.0)), - Fixture::new_stripe().offset(Vector2::new( 0.5, 0.0)) +// .with_driver( Box::new(MatrixTcpDriver::new("ip:port")) ), + + Fixture::new_stripe() + .with_driver( Box::new(StripeDriver::new("192.168.0.111:4210", socket.clone())) ) + .offset(Vector2::new(-0.5, 0.0)), + + 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()))) + .offset(Vector2::new(0.5, 0.0)) ] ); @@ -243,6 +319,7 @@ async fn main() { *lighting_setup.t.write().unwrap() = tcur - tbegin; lighting_setup.update_buffers(); + lighting_setup.update_outputs(); match event { Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {