From 86d5ea05224a2dbfe2c22fb0d476b2c2feb3a2e4 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sat, 3 Jun 2023 15:21:36 +0200 Subject: [PATCH] move redis stuff to its own file --- Cargo.toml | 1 + src/main.rs | 50 ++++++++++++++++++++++++----------------------- src/redis_ctrl.rs | 32 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 src/redis_ctrl.rs diff --git a/Cargo.toml b/Cargo.toml index 31004fa..435bde2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ctrlc = "3.4.0" helios-dac = { version = "0.1", default-features = false, features = ["native"] } redis = "0.23.0" ron = "0.8.0" diff --git a/src/main.rs b/src/main.rs index 09c5336..3e49000 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,55 +3,57 @@ /// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md /// +mod redis_ctrl; + use helios_dac::{Frame, Point, DeviceStatus, // Coordinate, Color}; +use helios_dac::NativeHeliosDacController; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; -use redis::{ //RedisResult, - Client, - Connection, - Commands -}; - -use ron::de::from_str; - -type Line = Vec<(f32,f32,u32)>; - +use redis_ctrl::RedisCtrl; const CENTER : (u16,u16) = (2000, 2000); + pub fn main() -> Result<(), Box> { - use helios_dac::NativeHeliosDacController; - - let client = Client::open("redis://127.0.0.1/")?; - let mut con = client.get_connection()?; + 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 mut rs = RedisCtrl::new()?; + let controller = NativeHeliosDacController::new()?; let devices = controller.list_devices()?; let device = devices.into_iter().next().unwrap(); let mut device = device.open()?; - for _ in 0..100 { - let frame = get_next_frame(1000, &mut con)?; - + while running.load(Ordering::SeqCst) { + let frame = get_next_frame(1000, &mut rs)?; + while let Ok(DeviceStatus::NotReady) = device.status() { } device.write_frame(frame)?; } - + + println!("Exiting, stoping device."); device.stop()?; Ok(()) } fn get_next_frame( speed: u32, - con: &mut Connection + rs: &mut RedisCtrl ) -> Result> { - let val : String = con.get("/pl/0/0")?; - let line : Line = from_str(&val)?; + + let line = rs.get("/pl/0/0")?; let line : Vec = line.iter() .map(tuple_to_point) .collect(); @@ -69,9 +71,9 @@ fn get_next_frame( 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; let y = CENTER.1 + *y as u16; diff --git a/src/redis_ctrl.rs b/src/redis_ctrl.rs new file mode 100644 index 0000000..bd8cb9b --- /dev/null +++ b/src/redis_ctrl.rs @@ -0,0 +1,32 @@ +use redis::{ + Client, + Connection, + Commands +}; +use ron::de::from_str; + +pub type Line = Vec<(f32,f32,u32)>; + +pub struct RedisCtrl { + pub client: Client, + pub connection: Connection +} + +impl RedisCtrl { + + pub fn new() -> Result> { + let client = Client::open("redis://127.0.0.1/")?; + let connection = client.get_connection()?; + Ok(RedisCtrl { client, connection }) + } + + pub fn get( + &mut self, + key: &str + ) -> Result> { + let val : String = self.connection.get(key)?; + let line : Line = from_str(&val)?; + Ok(line) + } +} +