117 lines
2.7 KiB
Rust
117 lines
2.7 KiB
Rust
///
|
|
/// Configure udev:
|
|
/// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md
|
|
///
|
|
mod redis_ctrl;
|
|
mod conf;
|
|
mod errors;
|
|
mod point;
|
|
mod transformer;
|
|
mod device;
|
|
|
|
use device::device_factory;
|
|
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::Arc;
|
|
use redis_ctrl::{RedisCtrl, Order};
|
|
use conf::Conf;
|
|
use errors::{LJError, LJResult};
|
|
use point::Point;
|
|
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);
|
|
|
|
pub fn main() {
|
|
match run_all() {
|
|
Ok(()) => {}
|
|
Err(err) => {
|
|
error!("Error: {}", err);
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
});
|
|
let config = Conf::new(&filename);
|
|
init_logging(&config);
|
|
let config = config?;
|
|
info!("*** Starting up ***");
|
|
|
|
// 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(conf)?;
|
|
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 : Vec<Point>, 1000);
|
|
}
|
|
|
|
info!("Exiting, stoping device.");
|
|
device.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;
|
|
}
|
|
}
|
|
info!("Logging level inherited from env");
|
|
env_logger::init();
|
|
}
|
|
|
|
fn get_next_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);
|
|
}
|
|
|
|
info!("Line: {:?}", line);
|
|
Ok(line)
|
|
}
|