Compare commits
	
		
			No commits in common. "c18de96e10199311b6dde8396adbd5e86b675dee" and "312a48ff5b9346872eddace40da72121d507cab2" have entirely different histories.
		
	
	
		
			c18de96e10
			...
			312a48ff5b
		
	
		
@ -21,6 +21,8 @@ pub fn load_config(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
 | 
			
		||||
        .add_source(config::File::with_name(path))
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    let conf : Conf = settings.try_deserialize()?;
 | 
			
		||||
    let conf = settings.try_deserialize::<Conf>()?;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    Ok(conf)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,3 +1,2 @@
 | 
			
		||||
pub mod conf;
 | 
			
		||||
pub mod redis_ctrl;
 | 
			
		||||
pub mod errors;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										77
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								src/main.rs
									
									
									
									
									
								
							@ -1,10 +1,9 @@
 | 
			
		||||
mod conf;
 | 
			
		||||
///
 | 
			
		||||
/// Configure udev:
 | 
			
		||||
/// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md
 | 
			
		||||
///
 | 
			
		||||
mod redis_ctrl;
 | 
			
		||||
mod conf;
 | 
			
		||||
mod errors;
 | 
			
		||||
 | 
			
		||||
use helios_dac::NativeHeliosDacController;
 | 
			
		||||
use helios_dac::{
 | 
			
		||||
@ -16,41 +15,43 @@ use helios_dac::{
 | 
			
		||||
};
 | 
			
		||||
use std::sync::atomic::{AtomicBool, Ordering};
 | 
			
		||||
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() {
 | 
			
		||||
    match run_all() {
 | 
			
		||||
	Ok(()) => {},
 | 
			
		||||
    let config = match load_config("Settings") {
 | 
			
		||||
        Ok(c) => c,
 | 
			
		||||
        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(
 | 
			
		||||
    config: Conf,
 | 
			
		||||
    mut rs: RedisCtrl
 | 
			
		||||
) -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
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);
 | 
			
		||||
    })?;
 | 
			
		||||
    })
 | 
			
		||||
    .expect("Error setting Ctrl-C handler");
 | 
			
		||||
 | 
			
		||||
    let controller = NativeHeliosDacController::new()?;
 | 
			
		||||
    let devices = controller.list_devices()?;
 | 
			
		||||
@ -63,16 +64,14 @@ fn run_dac(
 | 
			
		||||
    let mut device = device.open()?;
 | 
			
		||||
 | 
			
		||||
    while running.load(Ordering::SeqCst) {
 | 
			
		||||
	let order = rs.get_order(config.laser_id)?;
 | 
			
		||||
        let order = rs.get_order(0)?;
 | 
			
		||||
        if order != Order::Draw {
 | 
			
		||||
            println!("{:?}", order);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
	let frame = get_next_frame(&config, 1000,
 | 
			
		||||
				   &mut rs, order == Order::Black)?;
 | 
			
		||||
        let frame = get_next_frame(1000, &mut rs)?;
 | 
			
		||||
 | 
			
		||||
	while let Ok(DeviceStatus::NotReady) = device.status() {
 | 
			
		||||
	}
 | 
			
		||||
        while let Ok(DeviceStatus::NotReady) = device.status() {}
 | 
			
		||||
        device.write_frame(frame)?;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -81,14 +80,8 @@ fn run_dac(
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn get_next_frame(
 | 
			
		||||
    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))?;
 | 
			
		||||
fn get_next_frame(speed: u32, rs: &mut RedisCtrl) -> Result<Frame, Box<dyn std::error::Error>> {
 | 
			
		||||
    let line = rs.get("/pl/0/0")?;
 | 
			
		||||
    let line: Vec<Point> = line.iter().map(tuple_to_point).collect();
 | 
			
		||||
 | 
			
		||||
    let mut line2 = vec![];
 | 
			
		||||
@ -98,20 +91,18 @@ fn get_next_frame(
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!("{:?}", line2);
 | 
			
		||||
 | 
			
		||||
    Ok(Frame::new(speed, line2))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn tuple_to_point(tpl: &(f32, f32, u32)) -> Point {
 | 
			
		||||
    let (x, y, col) = tpl;
 | 
			
		||||
 | 
			
		||||
    let r = (col >> 16) as u8 ;
 | 
			
		||||
    let g = ((col >> 8) & 255) as u8 ;
 | 
			
		||||
    let b = (col & 255) as u8 ;
 | 
			
		||||
    let r = (col >> 16) as u8;
 | 
			
		||||
    let g = ((col >> 8) & 255) as u8;
 | 
			
		||||
    let b = (col & 255) as u8;
 | 
			
		||||
 | 
			
		||||
    let x = CENTER.0 + *x as u16 * 2;
 | 
			
		||||
    let y = CENTER.1 + *y as u16 * 2;
 | 
			
		||||
    let x = CENTER.0 + *x as u16;
 | 
			
		||||
    let y = CENTER.1 + *y as u16;
 | 
			
		||||
 | 
			
		||||
    if x >= 4096 || y >= 4096 {
 | 
			
		||||
        println!("WARN: coordinate out of range: {} {}", x, y);
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,5 @@
 | 
			
		||||
use redis::{Client, Commands, Connection};
 | 
			
		||||
use ron::de::from_str;
 | 
			
		||||
use crate::errors::LJError;
 | 
			
		||||
 | 
			
		||||
#[repr(u8)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
@ -36,7 +35,7 @@ impl TryFrom<u8> for Order {
 | 
			
		||||
            6 => Intensity,
 | 
			
		||||
            7 => Kpps,
 | 
			
		||||
            8 => ColorBalance,
 | 
			
		||||
	    _ => unreachable!()
 | 
			
		||||
            _ => panic!("Can't be there"),
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -50,10 +49,8 @@ pub struct RedisCtrl {
 | 
			
		||||
 | 
			
		||||
impl RedisCtrl {
 | 
			
		||||
    pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
 | 
			
		||||
        let client = Client::open("redis://127.0.0.1/")
 | 
			
		||||
	    .map_err(| err | LJError::RedisConnect(err))?;
 | 
			
		||||
        let connection = client.get_connection()
 | 
			
		||||
	    .map_err(| err | LJError::RedisConnect(err))?;
 | 
			
		||||
        let client = Client::open("redis://127.0.0.1/")?;
 | 
			
		||||
        let connection = client.get_connection()?;
 | 
			
		||||
        Ok(RedisCtrl { client, connection })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user