From 4719dcc4305fa85f6b668dd856a6017e8ae9df37 Mon Sep 17 00:00:00 2001 From: alban Date: Thu, 20 Jul 2023 00:54:24 +0200 Subject: [PATCH] feat: more dispatch actions and cosmetic --- src/main.rs | 18 +++++-------- src/redis_ctrl.rs | 69 ++++++++++++++++++++++++++--------------------- src/worldstate.rs | 4 ++- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2624273..62067a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,19 +65,13 @@ fn run_all() -> LJResult<()> { let mut tracer = device_factory(&config)?; world_state.grid = tracer.grid(); - // can't work, but we can add + Debug to Device to make it work... - //dbg!(tracer); - // Setup geometry transformers on points lists let transformers = config.get_transformers(); // Dispatch based on redis requests while running.load(Ordering::SeqCst) { rs.set_status(tracer.status())?; - let order = rs.get_order(config.laser_id)?; - - match order { Order::Draw | Order::Black | Order::Grid => { // 0 : Draw Normal point list @@ -110,11 +104,14 @@ fn run_all() -> LJResult<()> { Order::ClientKey => { world_state.client_key = rs.get_client_key()?; } - // Order::ColorBalance => {}, + Order::ColorBalance => { + let (r, g, b) = rs.get_color_balance()?; + world_state.color_balance = Color { r, g, b }; + } + Order::Resampler => { + world_state.resampler = rs.get_resampler()?; + } _ => { - // 4 : Resampler Change (longs and shorts lsteps) - // 5 : Client Key Change = reread redis key /clientkey - // 8 : color balance change = reread redis keys /red /green /blue // 9 : poweroff LJ info!("Order: {:?}", order); } @@ -128,7 +125,6 @@ fn run_all() -> LJResult<()> { fn init_logging(config: &LJResult) { if let Ok(ref config) = config { - let level = if config.debug { LevelFilter::Debug } else { diff --git a/src/redis_ctrl.rs b/src/redis_ctrl.rs index 74ac5a0..9b4bb4a 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,EDH}; +use crate::worldstate::{WorldState, EDH}; // use log::info; #[repr(u8)] @@ -10,7 +10,6 @@ use crate::worldstate::{WorldState,EDH}; pub enum Order { Draw = 0, Edh, - //homography Black, Grid, Resampler, @@ -46,6 +45,7 @@ impl TryFrom for Order { } pub type Line = Vec<(f32, f32, u32)>; +pub type Resampler = Vec<(f32,f32)>; pub struct RedisCtrl { pub client: Client, @@ -84,11 +84,6 @@ impl RedisCtrl { Ok(val.try_into()?) } - /** - /lstt/lasernumber etherdream last_status.playback_state (0: idle 1: prepare 2: playing) - /cap/lasernumber number of empty points sent to fill etherdream buffer (up to 1799) - /lack/lasernumber "a": ACK "F": Full "I": invalid. 64 or 35 for no connection. - **/ pub fn set_status(&mut self, status: Status) -> LJResult<()> { let lstt_key = format!("/lstt/{}", self.laser_id); let cap_key = format!("/cap/{}", self.laser_id); @@ -99,34 +94,48 @@ impl RedisCtrl { Ok(()) } - pub fn init_world_state( &mut self) -> LJResult{ - Ok(WorldState { - client_key: self.get_client_key().unwrap(), - edh: self.get_edh().unwrap(), - kpps: self.get_int("kpps").unwrap().try_into().unwrap(), - intensity: self.get_int("intensity").unwrap().try_into().unwrap(), - ..WorldState::default() - }) + pub fn init_world_state(&mut self) -> LJResult { + Ok(WorldState { + client_key: self.get_client_key().unwrap(), + edh: self.get_edh().unwrap(), + kpps: self.get_int("kpps").unwrap().try_into().unwrap(), + intensity: self.get_int("intensity").unwrap().try_into().unwrap(), + ..WorldState::default() + }) } - pub fn get_edh( &mut self ) -> LJResult { - // Get new EDH - let edh_key = format!("/EDH/{}", self.laser_id); - let edh : String = self.connection.get(edh_key)?; - let edh : Vec> = from_str(&edh)?; - let edh = EDH::new(edh)?; - Ok(edh) + pub fn get_edh(&mut self) -> LJResult { + // Get new EDH + let edh_key = format!("/EDH/{}", self.laser_id); + let edh: String = self.connection.get(edh_key)?; + let edh: Vec> = from_str(&edh)?; + let edh = EDH::new(edh)?; + Ok(edh) } - pub fn get_client_key( &mut self ) -> LJResult { - let key : String = self.connection.get("/clientkey")?; - Ok(key) + pub fn get_client_key(&mut self) -> LJResult { + let key: String = self.connection.get("/clientkey")?; + Ok(key) } - pub fn get_int(&mut self, key: &str ) -> LJResult { - // Get new Int - let fmt = format!("/{key}/{}", self.laser_id); - let val : u32 = self.connection.get(fmt)?; - Ok(val) + pub fn get_color_balance(&mut self) -> LJResult<(u8, u8, u8)> { + Ok(( + self.connection.get("/red")?, + self.connection.get("/green")?, + self.connection.get("/blue")?, + )) + } + + pub fn get_resampler(&mut self ) -> LJResult { + let val: String = self.connection.get(format!("/resampler/{}", self.laser_id))?; + let resampler : Resampler = from_str(&val)?; + Ok(resampler) + } + + pub fn get_int(&mut self, key: &str) -> LJResult { + // Get new Int + let fmt = format!("/{key}/{}", self.laser_id); + let val: u32 = self.connection.get(fmt)?; + Ok(val) } } diff --git a/src/worldstate.rs b/src/worldstate.rs index e03edb1..4c1366c 100644 --- a/src/worldstate.rs +++ b/src/worldstate.rs @@ -2,6 +2,7 @@ use crate::point::{Point, Color}; use nalgebra::base::{Matrix3, Matrix1x3}; use crate::errors::{LJError, LJResult}; use log::debug; +use crate::redis_ctrl::Resampler; #[derive(Debug, Default)] pub struct EDH { @@ -39,7 +40,7 @@ impl EDH { #[derive(Debug, Default)] pub struct WorldState { pub edh: EDH, - pub resampler: Vec, + pub resampler: Resampler, pub client_key: String, pub intensity: u8, pub kpps: u32, @@ -47,6 +48,7 @@ pub struct WorldState { pub draw_black: bool, pub draw_grid: bool, pub grid: Vec, + pub color_balance: Color, } impl WorldState {}