Compare commits

...

1 Commits

Author SHA1 Message Date
alban 259fdeb7b0 feat: Add device interface and factory
wip

fix borrowing issue and make everything compile

wip
2023-06-07 12:20:42 +02:00
5 changed files with 197 additions and 98 deletions

View File

@ -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
}

62
src/device/helios.rs Normal file
View File

@ -0,0 +1,62 @@
///
/// Configure udev:
/// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md
///
use helios_dac::{NativeHeliosDac, NativeHeliosDacController};
use helios_dac::{
// Coordinate,
Color,
DeviceStatus,
Frame,
Point as HeliosPoint,
};
use crate::conf::HeliosConf;
use crate::device::{Device, Status};
use crate::errors::{LJError, LJResult};
use crate::point::Point;
pub struct HeliosDevice {
pub conf: HeliosConf,
dac: NativeHeliosDac,
}
impl HeliosDevice {
pub fn new(conf: &HeliosConf) -> LJResult<Self> {
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 draw(&self,
line: Vec<Point>,
speed: u32,
) -> LJResult<()> {
while let Ok(DeviceStatus::NotReady) = self.dac.status() {}
let points: Vec<helios_dac::Point> = line.into_iter().map(|p| p.into()).collect();
let frame = Frame::new(speed, points);
Ok(())
}
fn stop(&mut self) -> LJResult<()> {
self.dac.stop()?;
Ok(())
}
}

45
src/device/mod.rs Normal file
View File

@ -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<String>
}
pub trait Device {
fn status( &self ) -> Status;
fn draw(
&self,
frame: Vec<Point>,
speed: u32,
) -> LJResult<()> ;
fn stop(&mut self) -> LJResult<()>;
}
pub fn device_factory(config: &Conf) -> LJResult<Box<dyn Device>> {
let device = match &config.dac {
DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?),
DacFamily::Etherdream(_conf) => todo!(),
};
Ok(device)
}

View File

@ -1,3 +1,5 @@
pub mod conf;
pub mod redis_ctrl;
pub mod errors;
pub mod device;
pub mod point;

View File

@ -7,122 +7,112 @@ mod conf;
mod errors;
mod point;
mod transformer;
mod device;
use device::device_factory;
use helios_dac::{
self,
NativeHeliosDacController,
NativeHeliosDac,
DeviceStatus,
Frame,
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order};
use redis_ctrl::{RedisCtrl, Order};
use conf::Conf;
use errors::{LJError,LJResult};
use errors::{LJError, LJResult};
use point::Point;
use transformer::{Transformers,Translate,Replicate};
use log::{LevelFilter,info,/* warn, */ error};
use transformer::{Transformers, Translate, Replicate};
use log::{LevelFilter, info, /* warn, */ error};
use env_logger::Builder;
const DEFAULT_CONF_FILE : &str = "settings.toml";
const CENTER : (f32,f32) = (2000.0, 2000.0);
const DEFAULT_CONF_FILE: &str = "settings.toml";
const CENTER: (f32, f32) = (2000.0, 2000.0);
pub fn main() {
match run_all() {
Ok(()) => {},
Err(err) => {
error!("Error: {}", err);
pub fn main() {
match run_all() {
Ok(()) => {}
Err(err) => {
error!("Error: {}", err);
}
}
}
}
fn run_all() -> LJResult<()> {
let filename = std::env::args().nth(1).unwrap_or_else(|| {
DEFAULT_CONF_FILE.to_string()
});
let config = Conf::new(&filename);
init_logging(&config);
let config = config?;
info!("*** Starting up ***");
let mut rs = RedisCtrl::new(&config.redis_url)?;
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 configuration file and set up logs
let filename = std::env::args().nth(1).unwrap_or_else(|| {
DEFAULT_CONF_FILE.to_string()
});
let config = Conf::new(&filename);
init_logging(&config);
let config = config?;
info!("*** Starting up ***");
let transformers : Vec<Box<dyn Transformers>> = vec![
Box::new(Translate::new(CENTER.0, CENTER.1)),
Box::new(Replicate::Until(48))
];
while running.load(Ordering::SeqCst) {
let order = rs.get_order(config.laser_id)?;
if order != Order::Draw {
info!("Order: {:?}", order);
// 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);
})?;
// Setup Laser Device based on conf
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
let transformers: Vec<Box<dyn Transformers>> = vec![
Box::new(Translate::new(CENTER.0, CENTER.1)),
Box::new(Replicate::Until(48)),
];
// 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, &transformers,
&mut rs, order == Order::Black)?;
// For now, draw all the time
tracer.draw(frame, 1000)?;
}
let frame = get_next_frame(&config, 1000, &transformers,
&mut rs, order == Order::Black)?;
while let Ok(DeviceStatus::NotReady) = device.status() {
}
device.write_frame(frame)?;
}
info!("Exiting, stoping device.");
device.stop()?;
Ok(())
info!("Exiting, stoping device.");
tracer.stop()?;
Ok(())
}
fn init_logging(config: &LJResult<Conf>) {
if let Ok(ref config) = config {
if config.debug {
let mut builder = Builder::from_default_env();
builder
.filter(None, LevelFilter::Info)
.init();
info!("Debug mode enabled from configuration file");
return;
if let Ok(ref config) = config {
if config.debug {
let mut builder = Builder::from_default_env();
builder
.filter(None, LevelFilter::Info)
.init();
info!("Debug mode enabled from configuration file");
return;
}
}
}
info!("Logging level inherited from env");
env_logger::init();
}
fn get_helios_device() -> LJResult<NativeHeliosDac> {
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)
info!("Logging level inherited from env");
env_logger::init();
}
fn get_next_frame(
config: &Conf,
speed: u32,
transformers: &[Box<dyn Transformers>],
rs: &mut RedisCtrl,
_black: bool
) -> LJResult<Frame> {
config: &Conf,
transformers: &[Box<dyn Transformers>],
rs: &mut RedisCtrl,
_black: bool,
) -> LJResult<Vec<Point>> {
let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let mut line: Vec<Point> = line.into_iter().map(|tpl| tpl.into()).collect();
for transformer in transformers {
line = transformer.apply(&line);
}
let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let mut line: Vec<Point> = line.into_iter().map(| tpl | tpl.into()).collect();
for transformer in transformers {
line = transformer.apply(&line);
}
info!("Line: {:?}", line);
let line2 : Vec<helios_dac::Point> = line.into_iter().map(| p | p.into()).collect();
Ok(Frame::new(speed, line2))
info!("Line: {:?}", line);
Ok(line)
}