Compare commits

..

No commits in common. "c18de96e10199311b6dde8396adbd5e86b675dee" and "312a48ff5b9346872eddace40da72121d507cab2" have entirely different histories.

5 changed files with 70 additions and 117 deletions

View File

@ -21,6 +21,8 @@ pub fn load_config(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
.add_source(config::File::with_name(path)) .add_source(config::File::with_name(path))
.build()?; .build()?;
let conf : Conf = settings.try_deserialize()?; let conf = settings.try_deserialize::<Conf>()?;
Ok(conf) Ok(conf)
} }

View File

@ -1,36 +0,0 @@
use std::error::Error;
use std::fmt;
use redis::RedisError;
#[derive(Debug)]
pub enum LJError {
ConfigFileMissing,
RedisConnect(RedisError)
}
impl fmt::Display for LJError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use LJError::*;
match self {
ConfigFileMissing => {
write!(f, "first argument must be a TOML config file \
(see copyme.Settings.toml)")
},
RedisConnect(err) => {
write!(f, "unable to connect to redis server: {err}")
}
}
}
}
impl Error for LJError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
use LJError::*;
match self {
ConfigFileMissing => None,
RedisConnect(err) => Some(err)
}
}
}

View File

@ -1,3 +1,2 @@
pub mod conf; pub mod conf;
pub mod redis_ctrl; pub mod redis_ctrl;
pub mod errors;

View File

@ -1,10 +1,9 @@
mod conf;
/// ///
/// Configure udev: /// Configure udev:
/// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md /// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md
/// ///
mod redis_ctrl; mod redis_ctrl;
mod conf;
mod errors;
use helios_dac::NativeHeliosDacController; use helios_dac::NativeHeliosDacController;
use helios_dac::{ use helios_dac::{
@ -16,41 +15,43 @@ use helios_dac::{
}; };
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order};
use conf::{load_config,Conf};
use errors::LJError;
const CENTER : (u16,u16) = (2000, 2000); use conf::{load_config, Conf};
use redis_ctrl::{Order, RedisCtrl};
const CENTER: (u16, u16) = (2000, 2000);
pub fn main() { pub fn main() {
match run_all() { let config = match load_config("Settings") {
Ok(()) => {}, Ok(c) => c,
Err(err) => { Err(err) => {
println!("Error: {}", err); panic!("Unable to load config file: {:?}", err)
} }
}
}
fn run_all() -> Result<(), Box<dyn std::error::Error>> {
let Some(filename) = std::env::args().nth(1) else {
return Err(Box::new(LJError::ConfigFileMissing));
}; };
let config = load_config(&filename)?;
let rs = RedisCtrl::new()?;
run_dac(config, rs) let rs = match RedisCtrl::new() {
Ok(rs) => rs,
Err(err) => {
panic!("Unable to connect to redis: {:?}", err);
}
};
match run_dac(config, rs) {
Ok(()) => {}
Err(err) => {
panic!("Error: {:?}", err)
}
}
} }
fn run_dac( fn run_dac(config: Conf, mut rs: RedisCtrl) -> Result<(), Box<dyn std::error::Error>> {
config: Conf,
mut rs: RedisCtrl
) -> Result<(), Box<dyn std::error::Error>> {
let running = Arc::new(AtomicBool::new(true)); let running = Arc::new(AtomicBool::new(true));
let r = running.clone(); let r = running.clone();
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst); r.store(false, Ordering::SeqCst);
})?; })
.expect("Error setting Ctrl-C handler");
let controller = NativeHeliosDacController::new()?; let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?; let devices = controller.list_devices()?;
@ -63,16 +64,14 @@ fn run_dac(
let mut device = device.open()?; let mut device = device.open()?;
while running.load(Ordering::SeqCst) { while running.load(Ordering::SeqCst) {
let order = rs.get_order(config.laser_id)?; let order = rs.get_order(0)?;
if order != Order::Draw { if order != Order::Draw {
println!("{:?}", order); println!("{:?}", order);
} }
let frame = get_next_frame(&config, 1000, let frame = get_next_frame(1000, &mut rs)?;
&mut rs, order == Order::Black)?;
while let Ok(DeviceStatus::NotReady) = device.status() { while let Ok(DeviceStatus::NotReady) = device.status() {}
}
device.write_frame(frame)?; device.write_frame(frame)?;
} }
@ -81,14 +80,8 @@ fn run_dac(
Ok(()) Ok(())
} }
fn get_next_frame( fn get_next_frame(speed: u32, rs: &mut RedisCtrl) -> Result<Frame, Box<dyn std::error::Error>> {
config: &Conf, let line = rs.get("/pl/0/0")?;
speed: u32,
rs: &mut RedisCtrl,
_black: bool
) -> Result<Frame, Box<dyn std::error::Error>> {
let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let line: Vec<Point> = line.iter().map(tuple_to_point).collect(); let line: Vec<Point> = line.iter().map(tuple_to_point).collect();
let mut line2 = vec![]; let mut line2 = vec![];
@ -98,20 +91,18 @@ fn get_next_frame(
} }
} }
println!("{:?}", line2);
Ok(Frame::new(speed, line2)) Ok(Frame::new(speed, line2))
} }
fn tuple_to_point(tpl: &(f32, f32, u32)) -> Point { fn tuple_to_point(tpl: &(f32, f32, u32)) -> Point {
let (x, y, col) = tpl; let (x, y, col) = tpl;
let r = (col >> 16) as u8 ; let r = (col >> 16) as u8;
let g = ((col >> 8) & 255) as u8 ; let g = ((col >> 8) & 255) as u8;
let b = (col & 255) as u8 ; let b = (col & 255) as u8;
let x = CENTER.0 + *x as u16 * 2; let x = CENTER.0 + *x as u16;
let y = CENTER.1 + *y as u16 * 2; let y = CENTER.1 + *y as u16;
if x >= 4096 || y >= 4096 { if x >= 4096 || y >= 4096 {
println!("WARN: coordinate out of range: {} {}", x, y); println!("WARN: coordinate out of range: {} {}", x, y);

View File

@ -1,6 +1,5 @@
use redis::{Client, Commands, Connection}; use redis::{Client, Commands, Connection};
use ron::de::from_str; use ron::de::from_str;
use crate::errors::LJError;
#[repr(u8)] #[repr(u8)]
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -36,7 +35,7 @@ impl TryFrom<u8> for Order {
6 => Intensity, 6 => Intensity,
7 => Kpps, 7 => Kpps,
8 => ColorBalance, 8 => ColorBalance,
_ => unreachable!() _ => panic!("Can't be there"),
}) })
} }
} }
@ -50,10 +49,8 @@ pub struct RedisCtrl {
impl RedisCtrl { impl RedisCtrl {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> { pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let client = Client::open("redis://127.0.0.1/") let client = Client::open("redis://127.0.0.1/")?;
.map_err(| err | LJError::RedisConnect(err))?; let connection = client.get_connection()?;
let connection = client.get_connection()
.map_err(| err | LJError::RedisConnect(err))?;
Ok(RedisCtrl { client, connection }) Ok(RedisCtrl { client, connection })
} }