Compare commits

...

3 Commits

Author SHA1 Message Date
c18de96e10 refatoring errors 2023-06-04 11:50:05 +02:00
c25d977598 change a panic\! for an unreachable\! 2023-06-04 11:32:11 +02:00
b4766a3301 start to implement proper custom errors 2023-06-04 11:28:43 +02:00
5 changed files with 115 additions and 68 deletions

View File

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

36
src/errors.rs Normal file
View File

@ -0,0 +1,36 @@
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,2 +1,3 @@
pub mod conf; pub mod conf;
pub mod redis_ctrl; pub mod redis_ctrl;
pub mod errors;

View File

@ -1,9 +1,10 @@
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::{
@ -15,43 +16,41 @@ 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;
use conf::{load_config, Conf}; const CENTER : (u16,u16) = (2000, 2000);
use redis_ctrl::{Order, RedisCtrl};
const CENTER: (u16, u16) = (2000, 2000);
pub fn main() { pub fn main() {
let config = match load_config("Settings") { match run_all() {
Ok(c) => c, Ok(()) => {},
Err(err) => { Err(err) => {
panic!("Unable to load config file: {:?}", err) println!("Error: {}", err);
} }
};
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(config: Conf, mut rs: RedisCtrl) -> Result<(), Box<dyn std::error::Error>> { 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)
}
fn run_dac(
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()?;
@ -64,14 +63,16 @@ fn run_dac(config: Conf, mut rs: RedisCtrl) -> Result<(), Box<dyn std::error::Er
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(0)?; let order = rs.get_order(config.laser_id)?;
if order != Order::Draw { if order != Order::Draw {
println!("{:?}", order); println!("{:?}", order);
} }
let frame = get_next_frame(1000, &mut rs)?; let frame = get_next_frame(&config, 1000,
&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)?;
} }
@ -80,8 +81,14 @@ fn run_dac(config: Conf, mut rs: RedisCtrl) -> Result<(), Box<dyn std::error::Er
Ok(()) Ok(())
} }
fn get_next_frame(speed: u32, rs: &mut RedisCtrl) -> Result<Frame, Box<dyn std::error::Error>> { fn get_next_frame(
let line = rs.get("/pl/0/0")?; config: &Conf,
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![];
@ -91,18 +98,20 @@ fn get_next_frame(speed: u32, rs: &mut RedisCtrl) -> Result<Frame, Box<dyn std::
} }
} }
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; let x = CENTER.0 + *x as u16 * 2;
let y = CENTER.1 + *y as u16; let y = CENTER.1 + *y as u16 * 2;
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,5 +1,6 @@
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)]
@ -21,22 +22,22 @@ impl TryFrom<u8> for Order {
fn try_from(value: u8) -> Result<Self, Self::Error> { fn try_from(value: u8) -> Result<Self, Self::Error> {
use Order::*; use Order::*;
if value > 8 { if value > 8 {
return Err("order out of range".to_string()); return Err("order out of range".to_string());
} }
Ok(match value { Ok(match value {
0 => Draw, 0 => Draw,
1 => Edh, 1 => Edh,
2 => Black, 2 => Black,
3 => Grid, 3 => Grid,
4 => Resampler, 4 => Resampler,
5 => ClientKey, 5 => ClientKey,
6 => Intensity, 6 => Intensity,
7 => Kpps, 7 => Kpps,
8 => ColorBalance, 8 => ColorBalance,
_ => panic!("Can't be there"), _ => unreachable!()
}) })
} }
} }
@ -49,8 +50,10 @@ 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/")
let connection = client.get_connection()?; .map_err(| err | LJError::RedisConnect(err))?;
let connection = client.get_connection()
.map_err(| err | LJError::RedisConnect(err))?;
Ok(RedisCtrl { client, connection }) Ok(RedisCtrl { client, connection })
} }