From f307fea63c6cc5274a23dfeab33fe64c64cf05e4 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Thu, 29 Jun 2023 23:24:56 +0200 Subject: [PATCH] fix code + dummy dac --- copyme.settings.toml | 3 +++ src/conf.rs | 2 ++ src/device.rs | 9 +++++--- src/device/dummy.rs | 36 ++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 6 ++--- src/point.rs | 4 ++-- src/redis_ctrl.rs | 15 ++++++------- src/transformer.rs | 7 +++++- src/transformer/flip_horizontal.rs | 4 +++- src/transformer/flip_vertical.rs | 4 +++- src/transformer/grid.rs | 4 +++- src/transformer/replicate.rs | 4 +++- src/transformer/rotate.rs | 4 +++- src/transformer/translate.rs | 3 ++- src/worldstate.rs | 8 ++++--- 16 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 src/device/dummy.rs diff --git a/copyme.settings.toml b/copyme.settings.toml index f440bc0..5f9eaff 100644 --- a/copyme.settings.toml +++ b/copyme.settings.toml @@ -15,6 +15,9 @@ redis_url = "redis://127.0.0.1:6379/" [dac.helios] id = 0 +# For dummy dac: +# [dac.dummy] + # For Etherdream. IP of the DAC # [dac.etherdream] # url = "192.168.1.68" diff --git a/src/conf.rs b/src/conf.rs index 01827c3..1cc1b3e 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -19,6 +19,8 @@ pub enum DacFamily { Helios(HeliosConf), #[serde(rename = "etherdream")] Etherdream(EtherDreamConf), + #[serde(rename = "dummy")] + Dummy, } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/src/device.rs b/src/device.rs index 0947bf2..c97276c 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,9 +1,11 @@ mod helios; +mod dummy; use std::fmt; use crate::conf::{Conf, DacFamily /*EtherDreamConf, HeliosConf*/}; use crate::device::helios::HeliosDevice; +use crate::device::dummy::DummyDevice; use crate::errors::LJResult; use crate::point::Point; use serde::Serialize; @@ -57,9 +59,10 @@ pub trait Device { } pub fn device_factory(config: &Conf) -> LJResult> { - let device = match &config.dac { - DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?), - DacFamily::Etherdream(_conf) => todo!(), + let device : Box = match &config.dac { + DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?), + DacFamily::Etherdream(_conf) => todo!(), + DacFamily::Dummy => Box::new(DummyDevice::new()?) }; Ok(device) } diff --git a/src/device/dummy.rs b/src/device/dummy.rs new file mode 100644 index 0000000..8a9ed9b --- /dev/null +++ b/src/device/dummy.rs @@ -0,0 +1,36 @@ +use crate::device::{Device, Status, PlaybackState}; +use crate::errors::{LJError, LJResult}; +use crate::point::Point; + +pub struct DummyDevice { + state: PlaybackState +} + +impl DummyDevice { + pub fn new() -> LJResult { + Ok(Self { state: PlaybackState::IDLE }) + } +} + +impl Device for DummyDevice { + fn status(&self) -> Status { + Status { + last_traced_at: "never".to_string(), + properties: vec!["foo".to_string()], + playback_state: self.state, + capacity: 0, + lack: "lack".to_string() + } + } + + fn draw(&mut self, + line: Vec, + speed: u32, + ) -> LJResult<()> { + Ok(()) + } + + fn stop(&mut self) -> LJResult<()> { + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index b183c49..9048f40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,4 @@ pub mod errors; pub mod device; pub mod point; pub mod transformer; +pub mod worldstate; diff --git a/src/main.rs b/src/main.rs index 874ae54..6a1103b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use redis_ctrl::{RedisCtrl, Order}; use conf::Conf; use errors::LJResult; -use point::{Point, Color}; +use point::Point; use transformer::Transformers; use log::{LevelFilter, info, /* warn, */ error}; use env_logger::Builder; @@ -50,7 +50,7 @@ fn run_all() -> LJResult<()> { // Setup Redis Service let mut rs = RedisCtrl::new(&config.redis_url, &config.laser_id)?; - let mut world_state = rs.init_world_state(); + let mut world_state = rs.init_world_state()?; // Setup handler for interrupt Signals let running = Arc::new(AtomicBool::new(true)); @@ -100,7 +100,7 @@ fn run_all() -> LJResult<()> { tracer.draw(frame, 2_000)?; } Order::Edh => { - let world_state.edh = rs.get_edh(), + world_state.edh = rs.get_edh()?; } // Order::ClientKey => rs.client_key(), diff --git a/src/point.rs b/src/point.rs index 1d43bfe..44fa9b4 100644 --- a/src/point.rs +++ b/src/point.rs @@ -1,11 +1,11 @@ -#[derive(Debug,Clone,Copy)] +#[derive(Debug,Clone,Copy,Default)] pub struct Point { pub x: f32, pub y: f32, pub color: Color } -#[derive(Debug,Clone,Copy)] +#[derive(Debug,Clone,Copy,Default)] pub struct Color { r: u8, g: u8, diff --git a/src/redis_ctrl.rs b/src/redis_ctrl.rs index cbffc7c..b78af9e 100644 --- a/src/redis_ctrl.rs +++ b/src/redis_ctrl.rs @@ -2,7 +2,7 @@ use redis::{Client, Commands, Connection}; use ron::de::from_str; use crate::device::Status; use crate::errors::{LJError, LJResult}; -use crate::worldstate::{WorldState}; +use crate::worldstate::{WorldState,EDH}; #[repr(u8)] #[derive(Debug, PartialEq)] @@ -99,14 +99,13 @@ impl RedisCtrl { } pub fn init_world_state( &mut self) -> LJResult{ - WorldState + Ok(WorldState::default()) } - pub fn get_edh( &mut self ) -> LJResult<()> { - - // Get new EDH - let edh = self.get("/EDH/1"); - EDH( edh ) - + pub fn get_edh( &mut self ) -> LJResult { + // Get new EDH + let edh : String = self.connection.get("/EDH/0")?; + let edh : Vec> = from_str(&edh)?; + Ok(EDH { matrix: edh }) } } diff --git a/src/transformer.rs b/src/transformer.rs index 19d4445..6132658 100644 --- a/src/transformer.rs +++ b/src/transformer.rs @@ -7,6 +7,7 @@ mod flip_vertical; mod grid; use crate::point::Point; +use crate::worldstate::WorldState; // re-export transformers to be abe to use it directly from transformer:: pub use translate::Translate; @@ -17,5 +18,9 @@ pub use flip_vertical::FlipVertical; pub use grid::Grid; pub trait Transformers { - fn apply(&self, point_list: &[Point]) -> Vec; + fn apply( + &self, + point_list: &[Point], + world_state: &WorldState + ) -> Vec; } diff --git a/src/transformer/flip_horizontal.rs b/src/transformer/flip_horizontal.rs index d156739..ed9cec3 100644 --- a/src/transformer/flip_horizontal.rs +++ b/src/transformer/flip_horizontal.rs @@ -1,5 +1,7 @@ use crate::transformer::Transformers; use crate::point::Point; +use crate::worldstate::WorldState; + use serde::{Serialize,Deserialize}; /// Flip Horizontal @@ -10,7 +12,7 @@ pub struct FlipHorizontal { } impl Transformers for FlipHorizontal { - fn apply(&self, point_list: &[Point]) -> Vec { + fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec { point_list.iter() .map(| pt | { let dx = pt.x - self.x; diff --git a/src/transformer/flip_vertical.rs b/src/transformer/flip_vertical.rs index fc741c7..19529e8 100644 --- a/src/transformer/flip_vertical.rs +++ b/src/transformer/flip_vertical.rs @@ -1,5 +1,7 @@ use crate::transformer::Transformers; use crate::point::Point; +use crate::worldstate::WorldState; + use serde::{Serialize,Deserialize}; /// Flip Vertical @@ -10,7 +12,7 @@ pub struct FlipVertical { } impl Transformers for FlipVertical { - fn apply(&self, point_list: &[Point]) -> Vec { + fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec { point_list.iter() .map(| pt | { let dy = pt.y - self.y; diff --git a/src/transformer/grid.rs b/src/transformer/grid.rs index 25258ff..7a9d37b 100644 --- a/src/transformer/grid.rs +++ b/src/transformer/grid.rs @@ -1,5 +1,7 @@ use crate::transformer::Transformers; use crate::point::Point; +use crate::worldstate::WorldState; + use serde::{Serialize,Deserialize}; /// Translate @@ -44,7 +46,7 @@ fn square_box(size: f32, color: u32) -> Vec<(f32, f32, u32)> { } impl Transformers for Grid { - fn apply(&self, _point_list: &[Point]) -> Vec { + fn apply(&self, _point_list: &[Point], _ws: &WorldState) -> Vec { let mut sq1 = square_box(1000.0, 255 << 8); let mut line = square_box(2000.0, 255); line.append(&mut sq1); diff --git a/src/transformer/replicate.rs b/src/transformer/replicate.rs index 99258ae..a5e5ab2 100644 --- a/src/transformer/replicate.rs +++ b/src/transformer/replicate.rs @@ -1,5 +1,7 @@ use crate::transformer::Transformers; use crate::point::Point; +use crate::worldstate::WorldState; + use serde::{Serialize,Deserialize}; /// Replicate @@ -12,7 +14,7 @@ pub enum Replicate { } impl Transformers for Replicate { - fn apply(&self, point_list: &[Point]) -> Vec { + fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec { let mut point_list2 = vec![]; match self { Replicate::Until(n) => { diff --git a/src/transformer/rotate.rs b/src/transformer/rotate.rs index 60fc109..0dac920 100644 --- a/src/transformer/rotate.rs +++ b/src/transformer/rotate.rs @@ -1,5 +1,7 @@ use crate::transformer::Transformers; use crate::point::Point; +use crate::worldstate::WorldState; + use serde::{Serialize,Deserialize}; //use std::f32::consts::PI; @@ -14,7 +16,7 @@ pub struct Rotate { } impl Transformers for Rotate { - fn apply(&self, point_list: &[Point]) -> Vec { + fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec { point_list.iter() .map(| pt | { let dx = pt.x - self.cx; diff --git a/src/transformer/translate.rs b/src/transformer/translate.rs index c3f4fec..a0262a1 100644 --- a/src/transformer/translate.rs +++ b/src/transformer/translate.rs @@ -1,5 +1,6 @@ use crate::transformer::Transformers; use crate::point::Point; +use crate::worldstate::WorldState; use serde::{Serialize,Deserialize}; /// Translate @@ -11,7 +12,7 @@ pub struct Translate { } impl Transformers for Translate { - fn apply(&self, point_list: &[Point]) -> Vec { + fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec { point_list.iter() .map(| pt | { Point { x: pt.x + self.x, diff --git a/src/worldstate.rs b/src/worldstate.rs index 097b19b..41eeef7 100644 --- a/src/worldstate.rs +++ b/src/worldstate.rs @@ -1,14 +1,16 @@ +use crate::point::Color; - +#[derive(Debug, Default)] pub struct EDH { - pub matrix: Matrix3 + pub matrix: Vec> //Matrix3 } -#[derive(Debug, Default )] + impl EDH { } +#[derive(Debug, Default)] pub struct WorldState { pub edh: EDH, pub resampler: Vec,