From 9844475fa1ec04226273a1c79b60b7a5ae0bdb19 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Mon, 5 Jun 2023 11:00:19 +0200 Subject: [PATCH] list of transformers --- src/main.rs | 28 +++++++++++++++------------- src/modifier.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9a33a20..72e62a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use redis_ctrl::{RedisCtrl,Order}; use conf::Conf; use errors::{LJError,LJResult}; use point::Point; -use modifier::{Transformers,Translate}; +use modifier::{Transformers,Translate,Replicate}; const CENTER : (f32,f32) = (2000.0, 2000.0); @@ -48,14 +48,19 @@ fn run_all() -> LJResult<()> { })?; let mut device = get_helios_device()?; - + + let transformers : Vec> = vec![ + Box::new(Translate::new(CENTER.0, CENTER.1)), + Box::new(Replicate::Until(48)) + ]; + while running.load(Ordering::SeqCst) { let order = rs.get_order(config.laser_id)?; if order != Order::Draw { println!("{:?}", order); } - let frame = get_next_frame(&config, 1000, + let frame = get_next_frame(&config, 1000, &transformers, &mut rs, order == Order::Black)?; while let Ok(DeviceStatus::NotReady) = device.status() { @@ -81,23 +86,20 @@ fn get_helios_device() -> LJResult { fn get_next_frame( config: &Conf, speed: u32, + transformers: &[Box], rs: &mut RedisCtrl, _black: bool ) -> LJResult { + let line = rs.get(&format!("/pl/{}/0", config.laser_id))?; - let line: Vec = line.into_iter().map(| tpl | tpl.into()).collect(); - let line = Translate::new(CENTER.0, CENTER.1).apply(&line); - - let mut line2 = vec![]; - while line2.len() < 48 { - for p in &line { - line2.push(*p); - } + let mut line: Vec = line.into_iter().map(| tpl | tpl.into()).collect(); + for transformer in transformers { + line = transformer.apply(&line); } - println!("{:?}", line2); + println!("{:?}", line); - let line2 : Vec = line2.into_iter().map(| p | p.into()).collect(); + let line2 : Vec = line.into_iter().map(| p | p.into()).collect(); Ok(Frame::new(speed, line2)) } diff --git a/src/modifier.rs b/src/modifier.rs index 56395ed..6d0c2c2 100644 --- a/src/modifier.rs +++ b/src/modifier.rs @@ -4,6 +4,8 @@ pub trait Transformers { fn apply(&self, point_list: &Vec) -> Vec; } +/// Translate + #[derive(Debug,Clone,Copy)] pub struct Translate { x: f32, @@ -27,3 +29,31 @@ impl Transformers for Translate { }).collect() } } + +/// Replicate + +#[derive(Debug,Clone,Copy)] +pub enum Replicate { + Until(usize), + Times(usize) +} + +impl Transformers for Replicate { + fn apply(&self, point_list: &Vec) -> Vec { + let mut point_list2 = vec![]; + match self { + Replicate::Until(n) => { + while point_list2.len() < *n { + point_list2.append(&mut point_list.clone()); + } + }, + Replicate::Times(n) => { + for _ in 0..*n { + point_list2.append(&mut point_list.clone()); + } + } + } + point_list2 + } +} +