add FixtureDriver trait and StripeDriver

This commit is contained in:
Michael Sippel 2024-04-26 02:36:34 +02:00
parent 37ce32712b
commit 215f694d88
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -63,13 +63,19 @@ impl ColorGrid for TestAnimation {
} }
} }
trait FixtureDriver {
fn send(&self, pixels: &Vec<Rgb<u8>>);
}
struct Fixture { struct Fixture {
resolution: Vector2<u32>, resolution: Vector2<u32>,
position: Vector2<f32>, position: Vector2<f32>,
rotation: f32, rotation: f32,
scale: f32, scale: f32,
brightness: f32, brightness: f32,
buffer: Vec< Rgb<u8> > buffer: Vec< Rgb<u8> >,
driver: Option<Box<dyn FixtureDriver>>
} }
impl Fixture { impl Fixture {
@ -80,7 +86,8 @@ impl Fixture {
rotation: 0.0, rotation: 0.0,
scale: 0.01, scale: 0.01,
brightness: 0.8, brightness: 0.8,
buffer: Vec::new() buffer: Vec::new(),
driver: None
}; };
fixture.buffer.resize( fixture.buffer.resize(
(fixture.resolution.x*fixture.resolution.y) as usize, (fixture.resolution.x*fixture.resolution.y) as usize,
@ -95,7 +102,8 @@ impl Fixture {
rotation: 0.0, rotation: 0.0,
scale: 0.03, scale: 0.03,
brightness: 0.8, brightness: 0.8,
buffer: Vec::new() buffer: Vec::new(),
driver: None
}; };
fixture.buffer.resize( fixture.buffer.resize(
(fixture.resolution.x*fixture.resolution.y) as usize, (fixture.resolution.x*fixture.resolution.y) as usize,
@ -104,6 +112,11 @@ impl Fixture {
fixture fixture
} }
pub fn with_driver(mut self, driver: Box<dyn FixtureDriver>) -> Self {
self.driver = Some(driver);
self
}
fn offset(mut self, offset: Vector2<f32>) -> Self { fn offset(mut self, offset: Vector2<f32>) -> Self {
self.position += offset; self.position += offset;
self 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( fn draw_preview(
&self, &self,
buffer: &mut softbuffer::Buffer<'_, Arc<winit::window::Window>, Arc<winit::window::Window>>, buffer: &mut softbuffer::Buffer<'_, Arc<winit::window::Window>, Arc<winit::window::Window>>,
@ -215,6 +236,44 @@ impl LightingSetup {
} }
} }
struct StripeDriver {
socket: Arc<RwLock<std::net::UdpSocket>>,
addr: String
}
impl StripeDriver {
fn new(addr: &str, socket: Arc<RwLock<std::net::UdpSocket>>) -> Self {
StripeDriver {
addr: addr.into(),
socket
}
}
}
impl FixtureDriver for StripeDriver {
fn send(&self, pixels: &Vec<Rgb<u8>>) {
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_std::main]
async fn main() { async fn main() {
let event_loop = EventLoop::new().unwrap(); 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 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( let mut lighting_setup = LightingSetup::new(
vec![ vec![
Fixture::new_matrix(), Fixture::new_matrix(),
Fixture::new_stripe().offset(Vector2::new(-0.5, 0.0)), // .with_driver( Box::new(MatrixTcpDriver::new("ip:port")) ),
Fixture::new_stripe().offset(Vector2::new(-0.4, 0.0)),
Fixture::new_stripe().offset(Vector2::new( 0.4, 0.0)), Fixture::new_stripe()
Fixture::new_stripe().offset(Vector2::new( 0.5, 0.0)) .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.t.write().unwrap() = tcur - tbegin;
lighting_setup.update_buffers(); lighting_setup.update_buffers();
lighting_setup.update_outputs();
match event { match event {
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => { Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {