diff --git a/src/conf.rs b/src/conf.rs index 4ddf686..9cff95c 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,101 +1,106 @@ use config::Config; -use serde::{Serialize,Deserialize}; -use crate::errors::{LJError,LJResult}; +use serde::{Serialize, Deserialize}; +use crate::errors::{LJError, LJResult}; use crate::transformer; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Conf { - pub laser_id: u8, - pub debug: bool, - pub redis_url: String, - pub dac: DacFamily, - #[serde(default)] - pub transformers: Vec + pub laser_id: u8, + pub debug: bool, + pub redis_url: String, + pub dac: DacFamily, + #[serde(default)] + pub transformers: Vec, } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum DacFamily { - #[serde(rename = "helios")] - Helios(HeliosConf), - #[serde(rename = "etherdream")] - Etherdream(EtherDreamConf), - #[serde(rename = "dummy")] - Dummy, + #[serde(rename = "helios")] + Helios(HeliosConf), + #[serde(rename = "etherdream")] + Etherdream(EtherDreamConf), + #[serde(rename = "dummy")] + Dummy, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct HeliosConf { - pub id: u8 + pub id: u8, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct EtherDreamConf { - pub ip: String + pub ip: String, } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum TransformConf { - #[serde(rename = "translate")] - Translate(transformer::Translate), - #[serde(rename = "replicate")] - Replicate(transformer::Replicate), - #[serde(rename = "rotate")] - Rotate(transformer::Rotate), - #[serde(rename = "flip_horizontal")] - FlipH(transformer::FlipHorizontal), - #[serde(rename = "flip_vertical")] - FlipV(transformer::FlipVertical), - #[serde(rename = "grid")] - Grid(transformer::Grid), - #[serde(rename = "homography")] - Homography(transformer::Homography), - #[serde(rename = "helios_to_etherdream")] - HeliosToEtherdream(transformer::HeliosToEtherdream), + #[serde(rename = "translate")] + Translate(transformer::Translate), + #[serde(rename = "replicate")] + Replicate(transformer::Replicate), + #[serde(rename = "rotate")] + Rotate(transformer::Rotate), + #[serde(rename = "flip_horizontal")] + FlipH(transformer::FlipHorizontal), + #[serde(rename = "flip_vertical")] + FlipV(transformer::FlipVertical), + #[serde(rename = "grid")] + Grid(transformer::Grid), + #[serde(rename = "homography")] + Homography(transformer::Homography), + #[serde(rename = "helios_to_etherdream")] + HeliosToEtherdream(transformer::HeliosToEtherdream), + #[serde(rename = "intensity")] + Intensity(transformer::Intensity), } impl Conf { - pub fn new(path: &str) -> LJResult { - let settings = Config::builder() - .add_source(config::File::with_name(path)) - .build()?; - - let conf : Conf = settings.try_deserialize().map_err(LJError::Config)?; - Ok(conf) - } + pub fn new(path: &str) -> LJResult { + let settings = Config::builder() + .add_source(config::File::with_name(path)) + .build()?; - pub fn get_transformers(&self) -> Vec> { - let mut v = vec![]; - for t in &self.transformers { - let t : Box = match t { - TransformConf::Translate(t) => Box::new(*t), - TransformConf::Replicate(r) => Box::new(*r), - TransformConf::Rotate(r) => Box::new(*r), - TransformConf::FlipH(r) => Box::new(*r), - TransformConf::FlipV(r) => Box::new(*r), - TransformConf::Grid(r) => Box::new(*r), - TransformConf::Homography(r) => Box::new(*r), - TransformConf::HeliosToEtherdream(r) => Box::new(*r), - }; - v.push(t); + let conf: Conf = settings.try_deserialize().map_err(LJError::Config)?; + Ok(conf) } - v - } - #[allow(dead_code)] - pub fn dump() { - let conf = Conf { - laser_id: 0, - debug: true, - redis_url: "redis://127.0.0.1:6379/".to_string(), - dac: DacFamily::Helios(HeliosConf { id: 0 }), - transformers: vec![ - TransformConf::Translate(transformer::Translate { x: 2000.0, - y: 2000.0 } ), - TransformConf::Replicate(transformer::Replicate::Until(48)) - ] - }; - let s = toml::to_string(&conf).unwrap(); - println!("{}", s); - } + pub fn get_transformers(&self) -> Vec> { + let mut v = vec![]; + for t in &self.transformers { + let t: Box = match t { + TransformConf::FlipH(r) => Box::new(*r), + TransformConf::FlipV(r) => Box::new(*r), + TransformConf::Grid(r) => Box::new(*r), + TransformConf::HeliosToEtherdream(r) => Box::new(*r), + TransformConf::Homography(r) => Box::new(*r), + TransformConf::Intensity(r) => Box::new(*r), + TransformConf::Replicate(r) => Box::new(*r), + TransformConf::Rotate(r) => Box::new(*r), + TransformConf::Translate(t) => Box::new(*t), + }; + v.push(t); + } + v + } + + #[allow(dead_code)] + pub fn dump() { + let conf = Conf { + laser_id: 0, + debug: true, + redis_url: "redis://127.0.0.1:6379/".to_string(), + dac: DacFamily::Helios(HeliosConf { id: 0 }), + transformers: vec![ + TransformConf::Translate(transformer::Translate { + x: 2000.0, + y: 2000.0, + }), + TransformConf::Replicate(transformer::Replicate::Until(48)), + ], + }; + let s = toml::to_string(&conf).unwrap(); + println!("{}", s); + } } diff --git a/src/point.rs b/src/point.rs index 273bfbc..21b5694 100644 --- a/src/point.rs +++ b/src/point.rs @@ -1,4 +1,5 @@ use ether_dream::protocol::DacPoint; +use std::ops::Mul; #[derive(Debug, Clone, Copy, Default, PartialEq)] @@ -14,6 +15,17 @@ pub struct Color { pub g: u8, pub b: u8, } +impl Mul for Color { + type Output = Self; + + fn mul(self, rhs: u8) -> Self { + Self{ + r: (255 * self.r as u16 / rhs as u16) as u8, + g: (255 * self.g as u16 / rhs as u16) as u8, + b: (255 * self.b as u16 / rhs as u16) as u8, + } + } +} impl From for u32 { fn from(value: Color) -> Self { diff --git a/src/transformer.rs b/src/transformer.rs index 2b9465f..ae66f8b 100644 --- a/src/transformer.rs +++ b/src/transformer.rs @@ -1,12 +1,12 @@ -//pub mod common; -mod translate; -mod replicate; -mod rotate; mod flip_horizontal; mod flip_vertical; mod grid; -mod homography; mod helios_to_etherdream; +mod homography; +mod intensity; +mod replicate; +mod rotate; +mod translate; use crate::point::Point; use crate::worldstate::WorldState; @@ -20,6 +20,7 @@ pub use flip_vertical::FlipVertical; pub use grid::Grid; pub use self::homography::Homography; pub use helios_to_etherdream::HeliosToEtherdream; +pub use intensity::Intensity; pub trait Transformers { fn apply( diff --git a/src/transformer/intensity.rs b/src/transformer/intensity.rs new file mode 100644 index 0000000..0aacc1c --- /dev/null +++ b/src/transformer/intensity.rs @@ -0,0 +1,28 @@ +use log::debug; +use crate::transformer::Transformers; +use crate::point::Point; +use crate::worldstate::WorldState; + +use serde::{Serialize, Deserialize}; + +/// Converts helios Geometry to Helios +#[allow(dead_code)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] +pub struct Intensity { +} + +impl Transformers for Intensity { + fn apply(&self, point_list: &[Point], ws: &WorldState) -> Vec { + // debug!("list helios {:?}", point_list); + let out = point_list.iter().map(|pt| { + Point { + x: pt.x, + y: pt.y, + color: pt.color * ws.intensity + } + }).collect(); + debug!("list intensity {:?}", out); + out + } +} +