list of transformers

This commit is contained in:
Marc Planard 2023-06-05 11:00:19 +02:00
parent de26c1c793
commit 9844475fa1
2 changed files with 45 additions and 13 deletions

View File

@ -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<Box<dyn Transformers>> = 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<NativeHeliosDac> {
fn get_next_frame(
config: &Conf,
speed: u32,
transformers: &[Box<dyn Transformers>],
rs: &mut RedisCtrl,
_black: bool
) -> LJResult<Frame> {
let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let line: Vec<Point> = 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<Point> = line.into_iter().map(| tpl | tpl.into()).collect();
for transformer in transformers {
line = transformer.apply(&line);
}
println!("{:?}", line2);
println!("{:?}", line);
let line2 : Vec<helios_dac::Point> = line2.into_iter().map(| p | p.into()).collect();
let line2 : Vec<helios_dac::Point> = line.into_iter().map(| p | p.into()).collect();
Ok(Frame::new(speed, line2))
}

View File

@ -4,6 +4,8 @@ pub trait Transformers {
fn apply(&self, point_list: &Vec<Point>) -> Vec<Point>;
}
/// 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<Point>) -> Vec<Point> {
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
}
}