diff --git a/Cargo.toml b/Cargo.toml index ccbd534..18f311d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ log = "0.4.18" redis = "0.23.0" ron = "0.8.0" serde = { version = "1.0.163", features = ["derive"] } +toml = "0.7.4" diff --git a/copyme.Settings.toml b/copyme.Settings.toml index d0055a3..f440bc0 100644 --- a/copyme.Settings.toml +++ b/copyme.Settings.toml @@ -18,3 +18,11 @@ id = 0 # For Etherdream. IP of the DAC # [dac.etherdream] # url = "192.168.1.68" + +[[transformers]] +[transformers.translate] +x = 2000 +y = 2000 +[[transformers]] +[transformers.replicate] +Until = 48 diff --git a/src/conf.rs b/src/conf.rs index 4d64fb2..781bcaa 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,16 +1,19 @@ use config::Config; use serde::{Serialize,Deserialize}; use crate::errors::LJResult; +use crate::transformer; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Conf { pub laser_id: u8, pub debug: bool, pub redis_url: String, - pub dac: DacFamily + pub dac: DacFamily, + #[serde(default)] + pub transformers: Vec } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub enum DacFamily { #[serde(rename = "helios")] Helios(HeliosConf), @@ -18,16 +21,25 @@ pub enum DacFamily { Etherdream(EtherDreamConf), } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct HeliosConf { pub id: u8 } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct EtherDreamConf { pub url: String } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum TransformConf { + #[serde(rename = "translate")] + Translate(transformer::Translate), + #[serde(rename = "replicate")] + Replicate(transformer::Replicate) +} + + impl Conf { pub fn new(path: &str) -> LJResult { let settings = Config::builder() @@ -37,4 +49,30 @@ impl Conf { let conf : Conf = settings.try_deserialize()?; Ok(conf) } + + 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.clone()), + TransformConf::Replicate(r) => Box::new(r.clone()) + }; + v.push(t); + } + v + } + + 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::new(2000.0,2000.0)), + TransformConf::Replicate(transformer::Replicate::Until(48)) + ] + }; + let s = toml::to_string(&conf).unwrap(); + println!("{}", s); + } } diff --git a/src/device.rs b/src/device.rs deleted file mode 100644 index e245e5e..0000000 --- a/src/device.rs +++ /dev/null @@ -1,15 +0,0 @@ -mod common; -mod helios; - -use crate::conf::{Conf, DacFamily}; //, EtherDreamConf, HeliosConf}; -use crate::device::common::Device; -use crate::device::helios::HeliosDevice; - -pub fn device_factory(config: Conf) -> Box { - let device = match config.dac { - DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)), - DacFamily::Etherdream(_conf) => todo!(), - }; - - device -} diff --git a/src/device/helios.rs b/src/device/helios.rs index b87a8f8..e1b8efc 100644 --- a/src/device/helios.rs +++ b/src/device/helios.rs @@ -2,37 +2,59 @@ /// Configure udev: /// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md /// -/* -use helios_dac::NativeHeliosDacController; +use helios_dac::{NativeHeliosDac, NativeHeliosDacController}; use helios_dac::{ - // Coordinate, - Color, - DeviceStatus, - Frame, - Point, + // Coordinate, + Color, + DeviceStatus, + Frame, + Point as HeliosPoint, }; -*/ use crate::conf::HeliosConf; -use crate::device::common::{Device, Status}; +use crate::device::{Device, Status}; +use crate::errors::{LJError, LJResult}; +use crate::point::Point; pub struct HeliosDevice { - pub conf: HeliosConf, + pub conf: HeliosConf, + dac: NativeHeliosDac, } impl HeliosDevice { - - pub fn new ( conf: HeliosConf) -> Self{ - Self{ conf } - } - + pub fn new(conf: &HeliosConf) -> LJResult { + let id = conf.id; + let controller = NativeHeliosDacController::new()?; + let devices = controller.list_devices()?; + let Some(device) = devices.into_iter().nth(id as usize) else { + return Err(Box::new(LJError::HeliosDeviceMissing)); + }; + let dac = device.open()?; + Ok(Self { conf: (*conf).clone(), dac }) + } } impl Device for HeliosDevice { - fn status(&self) -> Status { - return Status { - active: true, - last_traced_at: "now".to_string(), - properties: vec!["foo".to_string()], - }; - } + fn status(&self) -> Status { + return Status { + active: true, + last_traced_at: "now".to_string(), + properties: vec!["foo".to_string()], + }; + } + + fn draw(&self, + line: Vec, + speed: u32, + ) -> LJResult<()> { + while let Ok(DeviceStatus::NotReady) = self.dac.status() {} + + let points: Vec = line.into_iter().map(|p| p.into()).collect(); + let frame = Frame::new(speed, points); + Ok(()) + } + + fn stop(&mut self) -> LJResult<()> { + self.dac.stop()?; + Ok(()) + } } diff --git a/src/device/mod.rs b/src/device/mod.rs new file mode 100644 index 0000000..70a3916 --- /dev/null +++ b/src/device/mod.rs @@ -0,0 +1,45 @@ +use crate::point::Point; + +mod helios; + +use crate::conf::{Conf, DacFamily, EtherDreamConf, HeliosConf}; +use crate::device::helios::HeliosDevice; +use crate::errors::LJResult; + +/* +self.protocol_version, +self.le_state, +self.playback_state, +self.source, +self.le_flags, +self.playback_flags, +self.source_flags, +self.fullness, +self.point_rate, +self.point_count +*/ + + +pub struct Status { + pub active: bool, + pub last_traced_at: String, + pub properties: Vec +} + +pub trait Device { + fn status( &self ) -> Status; + fn draw( + &self, + frame: Vec, + speed: u32, + ) -> LJResult<()> ; + fn stop(&mut self) -> LJResult<()>; +} + +pub fn device_factory(config: &Conf) -> LJResult> { + let device = match &config.dac { + DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?), + DacFamily::Etherdream(_conf) => todo!(), + }; + Ok(device) +} diff --git a/src/lib.rs b/src/lib.rs index 41e05b8..ad51a61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ -pub mod conf; +/* pub mod redis_ctrl; +pub mod conf; pub mod errors; pub mod device; +pub mod point; +*/ diff --git a/src/main.rs b/src/main.rs index c9c77b4..7debd47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,29 +9,18 @@ mod point; mod transformer; mod device; -use helios_dac::{ - self, - NativeHeliosDacController, - NativeHeliosDac, - DeviceStatus, - Frame, -}; -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc -}; +use device::device_factory; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; use redis_ctrl::{RedisCtrl, Order}; +use conf::Conf; +use errors::LJResult; +use point::Point; +use transformer::{Transformers, Translate, Replicate}; use log::{LevelFilter, info, /* warn, */ error}; use env_logger::Builder; -use conf::Conf; -use errors::{LJError, LJResult}; -use point::Point; -use device::device_factory; -use transformer::{Transformers,Translate,Replicate}; - const DEFAULT_CONF_FILE: &str = "settings.toml"; -const CENTER: (f32, f32) = (2000.0, 2000.0); pub fn main() { match run_all() { @@ -43,6 +32,7 @@ pub fn main() { } fn run_all() -> LJResult<()> { + // Setup configuration file and set up logs let filename = std::env::args().nth(1).unwrap_or_else(|| { DEFAULT_CONF_FILE.to_string() }); @@ -52,37 +42,43 @@ fn run_all() -> LJResult<()> { let config = config?; info!("*** Starting up ***"); + info!("{:?}", config); + + // Setup Redis Service let mut rs = RedisCtrl::new(&config.redis_url)?; + // Setup handler for interrupt Signals let running = Arc::new(AtomicBool::new(true)); let r = running.clone(); ctrlc::set_handler(move || { r.store(false, Ordering::SeqCst); })?; - let mut device = get_helios_device()?; + // Setup Laser Device based on conf + let mut tracer = device_factory(&config)?; - let transformers: Vec> = vec![ - Box::new(Translate::new(CENTER.0, CENTER.1)), - Box::new(Replicate::Until(48)), - ]; + // 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) { - let order = rs.get_order(config.laser_id)?; if order != Order::Draw { info!("Order: {:?}", order); } - let frame = get_next_frame(&config, 1000, &transformers, + let frame = get_next_frame(&config, &transformers, &mut rs, order == Order::Black)?; - while let Ok(DeviceStatus::NotReady) = device.status() {} - device.write_frame(frame)?; + // For now, draw all the time + tracer.draw(frame, 1000)?; } info!("Exiting, stoping device."); - device.stop()?; + tracer.stop()?; Ok(()) } @@ -101,23 +97,12 @@ fn init_logging(config: &LJResult) { env_logger::init(); } -fn get_helios_device() -> LJResult { - let controller = NativeHeliosDacController::new()?; - let devices = controller.list_devices()?; - let Some(device) = devices.into_iter().next() else { - return Err(Box::new(LJError::HeliosDeviceMissing)); - }; - let device = device.open()?; - Ok(device) -} - fn get_next_frame( config: &Conf, - speed: u32, transformers: &[Box], rs: &mut RedisCtrl, _black: bool, -) -> LJResult { +) -> LJResult> { let line = rs.get(&format!("/pl/{}/0", config.laser_id))?; let mut line: Vec = line.into_iter().map(|tpl| tpl.into()).collect(); for transformer in transformers { @@ -125,7 +110,5 @@ fn get_next_frame( } info!("Line: {:?}", line); - - let line2: Vec = line.into_iter().map(|p| p.into()).collect(); - Ok(Frame::new(speed, line2)) + Ok(line) } diff --git a/src/transformer/replicate.rs b/src/transformer/replicate.rs index bb56fec..b8a1d71 100644 --- a/src/transformer/replicate.rs +++ b/src/transformer/replicate.rs @@ -1,10 +1,11 @@ use crate::Transformers; use crate::point::Point; +use serde::{Serialize,Deserialize}; /// Replicate #[allow(dead_code)] -#[derive(Debug,Clone,Copy)] +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] pub enum Replicate { Until(usize), Times(usize) diff --git a/src/transformer/translate.rs b/src/transformer/translate.rs index 6b0c029..2e94e2e 100644 --- a/src/transformer/translate.rs +++ b/src/transformer/translate.rs @@ -1,8 +1,10 @@ use crate::Transformers; use crate::point::Point; +use serde::{Serialize,Deserialize}; + /// Translate -#[derive(Debug,Clone,Copy)] +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] pub struct Translate { x: f32, y: f32