more errors refactoring

This commit is contained in:
Marc Planard 2023-06-04 12:02:41 +02:00
parent c18de96e10
commit 49830795e6
2 changed files with 21 additions and 22 deletions

View File

@ -5,7 +5,8 @@ use redis::RedisError;
#[derive(Debug)]
pub enum LJError {
ConfigFileMissing,
RedisConnect(RedisError)
RedisConnect(RedisError),
HeliosDeviceMissing
}
impl fmt::Display for LJError {
@ -19,6 +20,9 @@ impl fmt::Display for LJError {
},
RedisConnect(err) => {
write!(f, "unable to connect to redis server: {err}")
},
HeliosDeviceMissing => {
write!(f, "helios device not found")
}
}
}
@ -29,8 +33,8 @@ impl Error for LJError {
use LJError::*;
match self {
ConfigFileMissing => None,
RedisConnect(err) => Some(err)
RedisConnect(err) => Some(err),
_ => None
}
}
}

View File

@ -6,8 +6,9 @@ mod redis_ctrl;
mod conf;
mod errors;
use helios_dac::NativeHeliosDacController;
use helios_dac::{
NativeHeliosDacController,
NativeHeliosDac,
// Coordinate,
Color,
DeviceStatus,
@ -36,31 +37,15 @@ fn run_all() -> Result<(), Box<dyn std::error::Error>> {
return Err(Box::new(LJError::ConfigFileMissing));
};
let config = load_config(&filename)?;
let rs = RedisCtrl::new()?;
let mut 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 r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
})?;
let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?;
let Some(device) = devices.into_iter().next() else {
println!("Unable to find an helios device");
return Ok(());
};
let mut device = device.open()?;
let mut device = get_helios_device()?;
while running.load(Ordering::SeqCst) {
let order = rs.get_order(config.laser_id)?;
@ -81,6 +66,16 @@ fn run_dac(
Ok(())
}
fn get_helios_device() -> Result<NativeHeliosDac, Box<dyn std::error::Error>> {
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)
}
fn get_next_frame(
config: &Conf,
speed: u32,