fix: format using rustfmt

This commit is contained in:
alban 2023-06-03 18:54:41 +02:00
parent 4ce35dfd0d
commit ae14cb600d
4 changed files with 113 additions and 140 deletions

View File

@ -1,12 +1,10 @@
use config::Config; use config::Config;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub enum DacFamily { pub enum DacFamily {
Helios, Helios,
Etherdream Etherdream,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -16,17 +14,15 @@ pub struct Conf {
redis_url: String, redis_url: String,
dac_family: DacFamily, dac_family: DacFamily,
dac_id: Option<u8>, dac_id: Option<u8>,
dac_url : Option<String> dac_url: Option<String>,
} }
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 let conf = settings.try_deserialize::<Conf>()?;
.try_deserialize::<Conf>()?;
Ok(conf) Ok(conf)
} }

View File

@ -1,28 +1,27 @@
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;
use helios_dac::{Frame,
Point,
DeviceStatus,
// Coordinate,
Color};
use helios_dac::NativeHeliosDacController; use helios_dac::NativeHeliosDacController;
use helios_dac::{
// Coordinate,
Color,
DeviceStatus,
Frame,
Point,
};
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 conf::{load_config, Conf};
use redis_ctrl::{Order, RedisCtrl};
const CENTER: (u16, u16) = (2000, 2000); const CENTER: (u16, u16) = (2000, 2000);
pub fn main() { pub fn main() {
let config = match load_config("Settings") { let config = match load_config("Settings") {
Ok(c) => c, Ok(c) => c,
Err(err) => { Err(err) => {
@ -38,23 +37,21 @@ pub fn main() {
}; };
match run_dac(config, rs) { match run_dac(config, rs) {
Ok(()) => {}, Ok(()) => {}
Err(err) => { Err(err) => {
panic!("Error: {:?}", 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"); })
.expect("Error setting Ctrl-C handler");
let controller = NativeHeliosDacController::new()?; let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?; let devices = controller.list_devices()?;
@ -74,8 +71,7 @@ fn run_dac(
let frame = get_next_frame(1000, &mut rs)?; 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)?; device.write_frame(frame)?;
} }
@ -84,15 +80,9 @@ 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>> {
speed: u32,
rs: &mut RedisCtrl
) -> Result<Frame, Box<dyn std::error::Error>> {
let line = rs.get("/pl/0/0")?; let line = rs.get("/pl/0/0")?;
let line : Vec<Point> = line.iter() let line: Vec<Point> = line.iter().map(tuple_to_point).collect();
.map(tuple_to_point)
.collect();
let mut line2 = vec![]; let mut line2 = vec![];
while line2.len() < 48 { while line2.len() < 48 {
@ -123,6 +113,6 @@ fn tuple_to_point(tpl: &(f32,f32,u32)) -> Point {
Point { Point {
coordinate: (x, y).into(), coordinate: (x, y).into(),
color: Color::new(r, g, b), color: Color::new(r, g, b),
intensity: 0xFF intensity: 0xFF,
} }
} }

View File

@ -1,8 +1,4 @@
use redis::{ use redis::{Client, Commands, Connection};
Client,
Connection,
Commands
};
use ron::de::from_str; use ron::de::from_str;
#[repr(u8)] #[repr(u8)]
@ -16,7 +12,7 @@ pub enum Order {
ClientKey, ClientKey,
Intensity, Intensity,
Kpps, Kpps,
ColorBalance ColorBalance,
} }
impl TryFrom<u8> for Order { impl TryFrom<u8> for Order {
@ -39,7 +35,7 @@ impl TryFrom<u8> for Order {
6 => Intensity, 6 => Intensity,
7 => Kpps, 7 => Kpps,
8 => ColorBalance, 8 => ColorBalance,
_ => panic!("Can't be there") _ => panic!("Can't be there"),
}) })
} }
} }
@ -48,30 +44,23 @@ pub type Line = Vec<(f32,f32,u32)>;
pub struct RedisCtrl { pub struct RedisCtrl {
pub client: Client, pub client: Client,
pub connection: Connection pub connection: Connection,
} }
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()?; let connection = client.get_connection()?;
Ok(RedisCtrl { client, connection }) Ok(RedisCtrl { client, connection })
} }
pub fn get( pub fn get(&mut self, key: &str) -> Result<Line, Box<dyn std::error::Error>> {
&mut self,
key: &str
) -> Result<Line, Box<dyn std::error::Error>> {
let val: String = self.connection.get(key)?; let val: String = self.connection.get(key)?;
let line: Line = from_str(&val)?; let line: Line = from_str(&val)?;
Ok(line) Ok(line)
} }
pub fn get_order( pub fn get_order(&mut self, id: u8) -> Result<Order, Box<dyn std::error::Error>> {
&mut self,
id: u8
) -> Result<Order, Box<dyn std::error::Error>> {
let path = format!("/order/{id}"); let path = format!("/order/{id}");
let val: u8 = self.connection.get(path.clone())?; let val: u8 = self.connection.get(path.clone())?;
@ -81,6 +70,4 @@ impl RedisCtrl {
Ok(val.try_into()?) Ok(val.try_into()?)
} }
} }