From e68cc8bcbcd8ec1e59505e4ca4425c495d13fd05 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Wed, 7 Jun 2023 11:46:30 +0200 Subject: [PATCH] fix borrowing issue and make everything compile --- src/conf.rs | 8 ++++---- src/device/helios.rs | 17 +++++++++++------ src/device/mod.rs | 16 ++++++++-------- src/main.rs | 10 ++++++---- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index 4d64fb2..a92dcff 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -2,7 +2,7 @@ use config::Config; use serde::{Serialize,Deserialize}; use crate::errors::LJResult; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Conf { pub laser_id: u8, pub debug: bool, @@ -10,7 +10,7 @@ pub struct Conf { pub dac: DacFamily } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub enum DacFamily { #[serde(rename = "helios")] Helios(HeliosConf), @@ -18,12 +18,12 @@ 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 } diff --git a/src/device/helios.rs b/src/device/helios.rs index c30b1af..4057b82 100644 --- a/src/device/helios.rs +++ b/src/device/helios.rs @@ -27,16 +27,16 @@ impl HeliosDevice { /// /// ` some example ` /// - pub fn new(conf: HeliosConf) -> LJResult { + pub fn new(conf: &HeliosConf) -> LJResult { let id = conf.id; let controller = NativeHeliosDacController::new()?; let devices = controller.list_devices()?; - let Some( dac) = devices.iter().nth(id as usize) else { + let Some(device) = devices.into_iter().nth(id as usize) else { return Err(Box::new(LJError::HeliosDeviceMissing)); }; - dac.open()?; + let dac = device.open()?; Ok(Self { - conf, + conf: (*conf).clone(), dac, }) } @@ -64,11 +64,16 @@ impl Device for HeliosDevice { fn draw(&self, line: Vec, speed: u32, - ) -> LJResult { + ) -> 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(true) + Ok(()) + } + + fn stop(&mut self) -> LJResult<()> { + self.dac.stop()?; + Ok(()) } } diff --git a/src/device/mod.rs b/src/device/mod.rs index 9710f5c..70a3916 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -27,19 +27,19 @@ pub struct Status { } pub trait Device { - fn status( &self ) -> Status; fn draw( &self, frame: Vec, speed: u32, - ) -> LJResult ; + ) -> LJResult<()> ; + fn stop(&mut self) -> LJResult<()>; } -pub fn device_factory(config: Conf) -> Box { - let device = match config.dac { - DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf).unwrap()), - DacFamily::Etherdream(conf) => todo!(), +pub fn device_factory(config: &Conf) -> LJResult> { + let device = match &config.dac { + DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?), + DacFamily::Etherdream(_conf) => todo!(), }; - device -} \ No newline at end of file + Ok(device) +} diff --git a/src/main.rs b/src/main.rs index 8a0c594..ea02ccf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,8 +56,10 @@ fn run_all() -> LJResult<()> { })?; // Setup Laser Device based on conf - let mut tracer = device_factory(conf)?; - dbg!(tracer); + let mut tracer = device_factory(&config)?; + + // can't work, but we can add + Debug to Device to make it work... + //dbg!(tracer); // Setup geometry transformers on points lists // @todo use the config @@ -76,11 +78,11 @@ fn run_all() -> LJResult<()> { &mut rs, order == Order::Black)?; // For now, draw all the time - tracer.draw(frame : Vec, 1000); + tracer.draw(frame, 1000)?; } info!("Exiting, stoping device."); - device.stop()?; + tracer.stop()?; Ok(()) }