Compare commits

...

4 Commits

8 changed files with 810 additions and 697 deletions

View File

@ -1,4 +1,3 @@
use std::io::_print;
/** /**
# Populate Redis Example # Populate Redis Example
@ -7,28 +6,30 @@ use std::io::_print;
$ cargo run --example populate_redis $ cargo run --example populate_redis
**/ **/
use redis::{ use redis::{
//RedisResult, //RedisResult,
Client, Client,
Commands, Commands,
Connection, Connection,
}; };
fn do_something() -> redis::RedisResult<()> { fn do_something() -> redis::RedisResult<()> {
let client = Client::open("redis://127.0.0.1/")?; let client = Client::open("redis://127.0.0.1/")?;
let mut con: Connection = client.get_connection()?; let mut con: Connection = client.get_connection()?;
let _ = con.set("/clientkey", "/pl/0/")?; let _ = con.set("/clientkey", "/pl/0/")?;
let _ = con.set("/EDH/0", "[[1.0, 0.0, 0.0],\n [ 0.0, 1.0, 0.0],\n [ 0.0, 0.0, 1.0]]")?; let _ = con.set(
let _ = con.set("/kpps/0", "5000")?; "/EDH/0",
let _ = con.set("/intensity/0", "255")?; "[[1.0, 0.0, 0.0],\n [ 0.0, 1.0, 0.0],\n [ 0.0, 0.0, 1.0]]",
let _ = con.set("/pl/0/0", "[(1000, 2000, 0), (1000, 1000, 65535), (2000, 1000, 65535), (2000, 2000, 65535), (1000, 2000, 65535)]")?; )?;
Ok(()) let _ = con.set("/kpps/0", "5000")?;
let _ = con.set("/intensity/0", "255")?;
let _ = con.set("/pl/0/0", "[(1000, 2000, 0), (1000, 1000, 65535), (2000, 1000, 65535), (2000, 2000, 65535), (1000, 2000, 65535)]")?;
Ok(())
} }
fn main() { fn main() {
match do_something() { match do_something() {
Err(err) => println!("Something wrong occured: {:?}", err), Err(err) => println!("Something wrong occured: {:?}", err),
Ok(..) => println!("Successfully inserted content in Redis") Ok(..) => println!("Successfully inserted content in Redis"),
} }
} }

View File

@ -1,42 +1,40 @@
/// ///
/// $ cargo run --example simple_client /// $ cargo run --example simple_client
/// ///
use redis::{ use redis::{
//RedisResult, //RedisResult,
Client, Client,
Commands, Commands,
Connection, Connection,
}; };
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::time::Instant; const SCALE: f32 = 20.0;
fn do_something() -> redis::RedisResult<()> { fn do_something() -> redis::RedisResult<()> {
let client = Client::open("redis://127.0.0.1/")?; let client = Client::open("redis://127.0.0.1/")?;
let mut con: Connection = client.get_connection()?; let mut con: Connection = client.get_connection()?;
let start = Instant::now(); let start = Instant::now();
loop { loop {
let elapsed = start.elapsed(); let elapsed = start.elapsed();
let time = 60.0 * elapsed.as_millis() as f32 / 1000.0; let time = 60.0 * elapsed.as_millis() as f32 / 5_000.0;
let mut v : Vec<(f32,f32,u32)> = vec![]; let mut v: Vec<(f32, f32, u32)> = vec![];
for i in 0..128 {
let a = (time + i as f32) / 128.0 * std::f32::consts::PI * 2.0;
let r = 1200.0 + (a*5.0).cos() * (500.0 * (time/5.0).cos());
let x = a.cos() * r; for i in 0..128 {
let y = a.sin() * r; let a = (time + i as f32) / 128.0 * std::f32::consts::PI * 2.0;
let col = if i % 8 < 4 { let r = 24.0 * SCALE + (a * 5.0).cos() * (10.0 * SCALE * (time / 5.0).cos());
0x000000ff
} else { let x = a.cos() * r;
0x00ff0000 let y = a.sin() * r;
}; let col = if i % 8 < 4 { 0x000000ff } else { 0x00ff0000 };
v.push((x,y,col)); v.push((x, y, col));
} }
// println!("{:?}", v); // println!("{:?}", v);
let _ = con.set("/pl/0/0", format!("{:?}", v))?; let _ = con.set("/pl/0/0", format!("{:?}", v))?;
sleep(Duration::from_millis(100));
} }
// Ok(()) // Ok(())
} }

View File

@ -1,15 +1,15 @@
mod helios;
mod dummy; mod dummy;
mod etherdream; mod etherdream;
mod helios;
use std::fmt;
use crate::conf::{Conf, DacFamily /*EtherDreamConf, HeliosConf*/}; use crate::conf::{Conf, DacFamily /*EtherDreamConf, HeliosConf*/};
use crate::device::helios::HeliosDevice;
use crate::device::dummy::DummyDevice; use crate::device::dummy::DummyDevice;
use crate::device::etherdream::EtherdreamDevice;
use crate::device::helios::HeliosDevice;
use crate::errors::LJResult; use crate::errors::LJResult;
use crate::point::Point; use crate::point::Point;
use serde::Serialize; use serde::Serialize;
use crate::device::etherdream::EtherdreamDevice; use std::fmt;
/* /*
self.protocol_version, self.protocol_version,
@ -26,24 +26,24 @@ self.point_count
#[repr(u8)] #[repr(u8)]
#[derive(Debug, PartialEq, Serialize, Copy, Clone)] #[derive(Debug, PartialEq, Serialize, Copy, Clone)]
pub enum PlaybackState { pub enum PlaybackState {
IDLE = 0, IDLE = 0,
PREPARE = 1, PREPARE = 1,
PLAYING = 2 PLAYING = 2,
} }
impl fmt::Display for PlaybackState { impl fmt::Display for PlaybackState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self) write!(f, "{:?}", self)
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Status { pub struct Status {
pub last_traced_at: String, pub last_traced_at: String,
pub properties: Vec<String>, pub properties: Vec<String>,
pub playback_state: PlaybackState, pub playback_state: PlaybackState,
pub capacity: usize, pub capacity: usize,
pub lack: String, pub lack: String,
} }
// /lstt/lasernumber etherdream last_status.playback_state (0: idle 1: prepare 2: playing) // /lstt/lasernumber etherdream last_status.playback_state (0: idle 1: prepare 2: playing)
@ -51,21 +51,17 @@ pub struct Status {
// /lack/lasernumber "a": ACK "F": Full "I": invalid. 64 or 35 for no connection. // /lack/lasernumber "a": ACK "F": Full "I": invalid. 64 or 35 for no connection.
pub trait Device { pub trait Device {
fn status(&mut self) -> Status; fn status(&mut self) -> Status;
fn draw( fn draw(&mut self, frame: Vec<Point>, speed: u32) -> LJResult<()>;
&mut self, fn stop(&mut self) -> LJResult<()>;
frame: Vec<Point>, fn grid(&mut self) -> Vec<Point>;
speed: u32,
) -> LJResult<()>;
fn stop(&mut self) -> LJResult<()>;
fn grid(&mut self) -> Vec<Point>;
} }
pub fn device_factory(config: &Conf) -> LJResult<Box<dyn Device>> { pub fn device_factory(config: &Conf) -> LJResult<Box<dyn Device>> {
let device: Box<dyn Device> = match &config.dac { let device: Box<dyn Device> = match &config.dac {
DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?), DacFamily::Helios(conf) => Box::new(HeliosDevice::new(conf)?),
DacFamily::Etherdream( conf) => Box::new( EtherdreamDevice::new(conf)?), DacFamily::Etherdream(conf) => Box::new(EtherdreamDevice::new(conf)?),
DacFamily::Dummy => Box::new(DummyDevice::new()?) DacFamily::Dummy => Box::new(DummyDevice::new()?),
}; };
Ok(device) Ok(device)
} }

View File

@ -1,308 +1,369 @@
#[warn(unused_imports)] #[warn(unused_imports)]
use log::{ debug, info, warn}; use log::{debug, info, warn};
use chrono::{DateTime, Utc};
use ether_dream::dac::stream::{connect, CommunicationError};
use ether_dream::dac::{Playback, Stream};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::thread::sleep; use std::thread::sleep;
use ether_dream::dac::stream::{CommunicationError, connect};
use ether_dream::dac::{Playback, Stream};
use chrono::{DateTime, Utc};
use std::time; use std::time;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use crate::conf::EtherDreamConf; use crate::conf::EtherDreamConf;
use crate::device::{Device, Status, PlaybackState}; use crate::device::{Device, PlaybackState, Status};
use crate::errors::{LJError, LJResult}; use crate::errors::{LJError, LJResult};
use crate::point::{Color, Point}; use crate::point::{Color, Point};
use ether_dream::protocol::{DacBroadcast, DacResponse}; use ether_dream::protocol::{DacBroadcast, DacResponse};
#[warn(dead_code)] #[warn(dead_code)]
pub struct EtherdreamDevice { pub struct EtherdreamDevice {
pub conf: EtherDreamConf, conf: EtherDreamConf,
dac: DacBroadcast, dac: DacBroadcast,
stream: Stream, stream: Stream,
// "a": ACK "F": Full "I": invalid. 64 or 35 for no connection. // "a": ACK "F": Full "I": invalid. 64 or 35 for no connection.
// /// The previous command was accepted. // /// The previous command was accepted.
// pub const ACK: u8 = 0x61; // pub const ACK: u8 = 0x61;
// /// The write command could not be performed because there was not enough buffer space when it // /// The write command could not be performed because there was not enough buffer space when it
// /// was received. // /// was received.
// pub const NAK_FULL: u8 = 0x46; // pub const NAK_FULL: u8 = 0x46;
// /// The command contained an invalid `command` byte or parameters. // /// The command contained an invalid `command` byte or parameters.
// pub const NAK_INVALID: u8 = 0x49; // pub const NAK_INVALID: u8 = 0x49;
// /// An emergency-stop condition still exists. // /// An emergency-stop condition still exists.
// pub const NAK_STOP_CONDITION: u8 = 0x21; // pub const NAK_STOP_CONDITION: u8 = 0x21;
// } // }
dac_response: u8, dac_response: u8,
chunk_size: usize,
cool_down: Duration, // in milisecs
} }
impl EtherdreamDevice { impl EtherdreamDevice {
pub fn new(conf: &EtherDreamConf) -> LJResult<Self> { pub fn new(conf: &EtherDreamConf) -> LJResult<Self> {
let (dac, _source_address, stream) = EtherdreamDevice::connect(conf)?; let (dac, _source_address, stream) = EtherdreamDevice::connect(conf)?;
Ok(Self {
conf: (*conf).clone(),
dac,
stream,
dac_response: DacResponse::ACK,
})
}
fn connect(conf: &EtherDreamConf) -> LJResult<(DacBroadcast, SocketAddr, Stream)> {
let ip = &conf.ip;
let dac_broadcast = ether_dream::recv_dac_broadcasts()?;
dac_broadcast.set_timeout(Some(time::Duration::new(10, 0)))?;
info!("Attempting to get DAC broadcast...");
let broadcast = dac_broadcast
.take(3)
.filter_map(|result| {
match result {
Err(err) => {
warn!( "Failed to find a valid DAC via broadcast. Error: {:?}", err);
info!( "Retrying...");
None
}
Ok((dac, source_addr)) => {
info!("Valid broadcast, source_addr: {}", source_addr);
if source_addr.is_ipv6() {
warn!("Broadcast ignored: ipv6 address.");
return None;
}
let src_ip = source_addr.ip().to_string();
if &src_ip != ip {
warn!("Broadcast ignored: expected {ip}, got: {src_ip} ");
return None;
}
Some(Ok((dac, source_addr)))
}
}
})
.next()
.expect("Failed to receive broadcast.");
match broadcast {
Err(err) => {
Err(Box::new(LJError::EtherdreamConnectError(err)))
}
Ok((dac, source_addr)) => {
info!("Trying to open TCP stream...");
let stream = EtherdreamDevice::get_tcp_stream(&dac, &source_addr)?;
info!("Finished configuring DAC and TCP stream.");
Ok((dac, source_addr, stream))
}
}
}
fn get_tcp_stream(dac: &DacBroadcast, source_address: &SocketAddr) -> LJResult<Stream> { let chunk_size = dac.buffer_capacity / 10;
let mut stream = connect(dac, source_address.ip())?; Ok(Self {
match stream conf: (*conf).clone(),
dac,
stream,
dac_response: DacResponse::ACK,
chunk_size: chunk_size.into(),
cool_down: Duration::from_millis(20),
})
}
fn connect(conf: &EtherDreamConf) -> LJResult<(DacBroadcast, SocketAddr, Stream)> {
let ip = &conf.ip;
let dac_broadcast = ether_dream::recv_dac_broadcasts()?;
dac_broadcast.set_timeout(Some(time::Duration::new(10, 0)))?;
info!("Attempting to get DAC broadcast...");
let broadcast = dac_broadcast
.take(3)
.filter_map(|result| match result {
Err(err) => {
warn!("Failed to find a valid DAC via broadcast. Error: {:?}", err);
info!("Retrying...");
None
}
Ok((dac, source_addr)) => {
info!("Valid broadcast, source_addr: {}", source_addr);
if source_addr.is_ipv6() {
warn!("Broadcast ignored: ipv6 address.");
return None;
}
let src_ip = source_addr.ip().to_string();
if &src_ip != ip {
warn!("Broadcast ignored: expected {ip}, got: {src_ip} ");
return None;
}
Some(Ok((dac, source_addr)))
}
})
.next()
.expect("Failed to receive broadcast.");
match broadcast {
Err(err) => Err(Box::new(LJError::EtherdreamConnectError(err))),
Ok((dac, source_addr)) => {
info!("Trying to open TCP stream...");
let stream = EtherdreamDevice::get_tcp_stream(&dac, &source_addr)?;
info!("Finished configuring DAC and TCP stream.");
Ok((dac, source_addr, stream))
}
}
}
fn get_tcp_stream(dac: &DacBroadcast, source_address: &SocketAddr) -> LJResult<Stream> {
let mut stream = connect(dac, source_address.ip())?;
match stream
.queue_commands() .queue_commands()
.prepare_stream() .prepare_stream()
.submit() { .submit() {
Err(err) => warn!("err occurred when submitting PREPARE_STREAM command and listening for response: {}",err), Err(err) => warn!("err occurred when submitting PREPARE_STREAM command and listening for response: {}",err),
Ok(_) => info!("Prepared Stream.") Ok(_) => info!("Prepared Stream.")
} }
// If we want to create an animation (in our case a moving sine wave) we need a frame rate. // If we want to create an animation (in our case a moving sine wave) we need a frame rate.
let frames_per_second = 60.0; let frames_per_second = 60.0;
// Lets use the DAC at an eighth the maximum scan rate. // Lets use the DAC at an eighth the maximum scan rate.
let points_per_second = stream.dac().max_point_rate / 32; let points_per_second = stream.dac().max_point_rate / 32;
// Determine the number of points per frame given our target frame and point rates. // Determine the number of points per frame given our target frame and point rates.
let points_per_frame = (points_per_second as f32 / frames_per_second) as u16; let points_per_frame = (points_per_second as f32 / frames_per_second) as u16;
let mut sine_wave = SineWave { let mut sine_wave = SineWave {
point: 0, point: 0,
points_per_frame, points_per_frame,
frames_per_second, frames_per_second,
}; };
match stream match stream
.queue_commands() .queue_commands()
.data(sine_wave.by_ref().take(400)) .data(sine_wave.by_ref().take(400))
// .data(begin_list.into_iter().take(400 as usize)) // .data(begin_list.into_iter().take(400 as usize))
.begin(0, points_per_second) .begin(0, points_per_second)
.submit() { .submit()
Err(err) => warn!("err occurred when submitting first data: {}",err), {
Ok(_) => info!("Sent first data to Etherdream.") Err(err) => warn!("err occurred when submitting first data: {}", err),
} Ok(_) => info!("Sent first data to Etherdream."),
}
Ok(stream) Ok(stream)
} }
fn points_capacity(&self) -> usize { fn points_capacity(&self) -> usize {
/*** /***
Determine the number of points needed to fill the DAC. Determine the number of points needed to fill the DAC.
***/ ***/
// Fixme thread 'main' panicked at 'attempt to subtract with overflow', src/device/etherdream.rs:144:24 // Fixme thread 'main' panicked at 'attempt to subtract with overflow', src/device/etherdream.rs:144:24
let n_points = self.dac.buffer_capacity as usize - self.stream.dac().dac.status.buffer_fullness as usize - 1; let n_points = self.dac.buffer_capacity as i64
n_points - self.stream.dac().dac.status.buffer_fullness as i64
} - 1;
n_points.max(0) as usize
}
fn ping(&mut self) -> LJResult<()> { fn ping(&mut self) -> LJResult<()> {
Ok(self.stream.queue_commands().ping().submit()?)
Ok(self.stream.queue_commands().ping().submit()?) }
}
} }
impl Device for EtherdreamDevice { impl Device for EtherdreamDevice {
fn status(&mut self) -> Status { fn status(&mut self) -> Status {
let playback_state = match self.stream.dac().dac.status.playback { let playback_state = match self.stream.dac().dac.status.playback {
Playback::Idle => PlaybackState::IDLE, Playback::Idle => PlaybackState::IDLE,
Playback::Prepared => PlaybackState::PREPARE, Playback::Prepared => PlaybackState::PREPARE,
Playback::Playing => PlaybackState::PLAYING, Playback::Playing => PlaybackState::PLAYING,
}; };
let now = SystemTime::now(); let now = SystemTime::now();
let now: DateTime<Utc> = now.into(); let now: DateTime<Utc> = now.into();
let now = now.to_rfc3339(); let now = now.to_rfc3339();
Status { Status {
last_traced_at: now, last_traced_at: now,
properties: vec!["foo".to_string()], properties: vec!["foo".to_string()],
playback_state, playback_state,
capacity: self.points_capacity(), capacity: self.points_capacity(),
lack: self.dac_response.to_string(), lack: self.dac_response.to_string(),
} }
// debug!("Dac Status: {:?} ", status ); // debug!("Dac Status: {:?} ", status );
// debug!("Etherdream Dac {:?} ", self.dac ); // debug!("Etherdream Dac {:?} ", self.dac );
// debug!("Stream dac{:?}", self.stream.dac()); // debug!("Stream dac{:?}", self.stream.dac());
// status // status
} }
fn draw(&mut self, fn draw(&mut self, line: Vec<Point>, _speed: u32) -> LJResult<()> {
line: Vec<Point>, let points_iter = line.into_iter();
_speed: u32, for chunk in points_iter.as_slice().chunks(self.chunk_size) {
) -> LJResult<()> { debug!("New chunk length: {:?}", chunk.len());
let chunk_size = 64; debug!("Point Cap: {}", self.points_capacity());
let points_iter = line.into_iter(); debug!("drawing");
for chunk in points_iter.as_slice().chunks(chunk_size){ let mut cool_down = self.cool_down;
debug!("New chunk length: {:?}", chunk.len()); let mut i = 0;
loop { while self.points_capacity() < chunk.len() {
let capacity = self.points_capacity(); debug!(
if chunk.len() > capacity { "Buffer full, waiting... (Point Cap: {}), cool_down: {:?}",
debug!("Sleep, capacity : {:?}", capacity); self.points_capacity(),
// Sleep for 1/100th of a sec self.cool_down
sleep(Duration::new( 0, 10000000)); );
self.ping()?; sleep(cool_down);
} else { self.ping()?;
break; if i > 0 {
} cool_down *= 2;
} }
debug!("drawing"); i += 1;
match self.stream }
.queue_commands() if i > 1 {
.data( info!(
chunk.iter() "Cool_down adjusted from {:?} to {:?}",
.map(|point| (*point).into()) self.cool_down, cool_down
.take(chunk_size) );
) self.cool_down = cool_down;
.submit() { }
Err(err) => {
// We should account for
// 'Broken pipe (os error 32)'
// Connection reset by peer (os error 104)
self.dac_response = match err {
CommunicationError::Io(err) => {
warn!("IO ERROR while drawing: '{}'",err);
DacResponse::ACK
}
CommunicationError::Protocol(err) => {
warn!("Protocol ERROR while drawing: '{}'",err);
DacResponse::ACK
}
CommunicationError::Response(err) => {
warn!("Response ERROR while drawing: '{}'",err);
err.response.response
}
};
}
Ok(_) => {
self.dac_response = DacResponse::ACK;
// debug!("Draw is ok");
}
};
}
Ok(())
}
fn stop(&mut self) -> LJResult<()> { match self
info!("Stopping Etherdream device..."); .stream
match self.stream .queue_commands()
.queue_commands() .data(
.stop() chunk
.submit() .iter()
{ .map(|point| (*point).into())
Err(err) => { .take(self.chunk_size),
warn!("Failed to stop EtherDream device with error {:?}", err); )
Err(Box::new(err)) .submit()
} {
Ok(_) => { Err(err) => {
info!("Sucessfully closed EtherDream device."); // We should account for
Ok(()) // 'Broken pipe (os error 32)'
} // Connection reset by peer (os error 104)
} self.dac_response = match err {
} CommunicationError::Io(err) => {
warn!("IO ERROR while drawing: '{}'", err);
DacResponse::ACK
}
CommunicationError::Protocol(err) => {
warn!("Protocol ERROR while drawing: '{}'", err);
DacResponse::ACK
}
CommunicationError::Response(err) => {
warn!("Response ERROR while drawing: '{}'", err);
err.response.response
}
};
}
Ok(_) => {
self.dac_response = DacResponse::ACK;
}
}
}
Ok(())
}
fn grid(&mut self) -> Vec<Point> { fn stop(&mut self) -> LJResult<()> {
let dim_mid = 16000.0; info!("Stopping Etherdream device...");
let dim_max = 32000.0; match self.stream.queue_commands().stop().submit() {
let col_min = Color { r: 0, g: 0, b: 0 }; Err(err) => {
let col_max = Color { r: 255, g: 255, b: 255 }; warn!("Failed to stop EtherDream device with error {:?}", err);
Err(Box::new(err))
}
Ok(_) => {
info!("Sucessfully closed EtherDream device.");
Ok(())
}
}
}
vec![ fn grid(&mut self) -> Vec<Point> {
Point { x: -dim_max, y: dim_max, color: col_min }, let dim_mid = 16000.0;
Point { x: -dim_max, y: dim_max, color: col_max }, let dim_max = 32000.0;
Point { x: dim_max, y: dim_max, color: col_max }, let col_min = Color { r: 0, g: 0, b: 0 };
Point { x: dim_max, y: -dim_max, color: col_max }, let col_max = Color {
Point { x: -dim_max, y: -dim_max, color: col_max }, r: 255,
Point { x: -dim_max, y: -dim_mid, color: col_min }, g: 255,
Point { x: -dim_mid, y: dim_mid, color: col_min }, b: 255,
Point { x: -dim_mid, y: dim_mid, color: col_max }, };
Point { x: dim_mid, y: dim_mid, color: col_max },
Point { x: dim_mid, y: -dim_mid, color: col_max }, vec![
Point { x: -dim_mid, y: -dim_mid, color: col_max }, Point {
Point { x: -dim_mid, y: -dim_mid, color: col_min }, x: -dim_max,
] y: dim_max,
} color: col_min,
},
Point {
x: -dim_max,
y: dim_max,
color: col_max,
},
Point {
x: dim_max,
y: dim_max,
color: col_max,
},
Point {
x: dim_max,
y: -dim_max,
color: col_max,
},
Point {
x: -dim_max,
y: -dim_max,
color: col_max,
},
Point {
x: -dim_max,
y: -dim_mid,
color: col_min,
},
Point {
x: -dim_mid,
y: dim_mid,
color: col_min,
},
Point {
x: -dim_mid,
y: dim_mid,
color: col_max,
},
Point {
x: dim_mid,
y: dim_mid,
color: col_max,
},
Point {
x: dim_mid,
y: -dim_mid,
color: col_max,
},
Point {
x: -dim_mid,
y: -dim_mid,
color: col_max,
},
Point {
x: -dim_mid,
y: -dim_mid,
color: col_min,
},
]
}
} }
// An iterator that endlessly generates a sine wave of DAC points. // An iterator that endlessly generates a sine wave of DAC points.
// //
// The sine wave oscillates at a rate of once per second. // The sine wave oscillates at a rate of once per second.
struct SineWave { struct SineWave {
point: u32, point: u32,
points_per_frame: u16, points_per_frame: u16,
frames_per_second: f32, frames_per_second: f32,
} }
impl Iterator for SineWave { impl Iterator for SineWave {
type Item = ether_dream::protocol::DacPoint; type Item = ether_dream::protocol::DacPoint;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let coloured_points_per_frame = self.points_per_frame - 1; let coloured_points_per_frame = self.points_per_frame - 1;
let i = (self.point % self.points_per_frame as u32) as u16; let i = (self.point % self.points_per_frame as u32) as u16;
let hz = 1.0; let hz = 1.0;
let fract = i as f32 / coloured_points_per_frame as f32; let fract = i as f32 / coloured_points_per_frame as f32;
let phase = (self.point as f32 / coloured_points_per_frame as f32) / self.frames_per_second; let phase = (self.point as f32 / coloured_points_per_frame as f32) / self.frames_per_second;
let amp = (hz * (fract + phase) * 2.0 * std::f32::consts::PI).sin(); let amp = (hz * (fract + phase) * 2.0 * std::f32::consts::PI).sin();
let (r, g, b) = match i { let (r, g, b) = match i {
i if i == coloured_points_per_frame || i < 13 => (0, 0, 0), i if i == coloured_points_per_frame || i < 13 => (0, 0, 0),
_ => (u16::MAX, u16::MAX, u16::MAX), _ => (u16::MAX, u16::MAX, u16::MAX),
}; };
let x_min = i16::MIN; let x_min = i16::MIN;
let x_max = i16::MAX; let x_max = i16::MAX;
let x = (x_min as f32 + fract * (x_max as f32 - x_min as f32)) as i16; let x = (x_min as f32 + fract * (x_max as f32 - x_min as f32)) as i16;
let y = (amp * x_max as f32) as i16; let y = (amp * x_max as f32) as i16;
let control = 0; let control = 0;
let (u1, u2) = (0, 0); let (u1, u2) = (0, 0);
let p = ether_dream::protocol::DacPoint { let p = ether_dream::protocol::DacPoint {
control, control,
x, x,
y, y,
i, i,
r, r,
g, g,
b, b,
u1, u1,
u2, u2,
}; };
// debug!("{:?}",p); // debug!("{:?}",p);
self.point += 1; self.point += 1;
Some(p) Some(p)
} }
} }

View File

@ -1,108 +1,158 @@
use std::time::SystemTime; use crate::conf::HeliosConf;
use crate::device::{Device, PlaybackState, Status};
use crate::errors::{LJError, LJResult};
use crate::point::{Color, Point};
use chrono::{DateTime, Utc};
use helios_dac::{
// Coordinate,
// Color,
DeviceStatus,
Frame,
// Point as HeliosPoint,
};
/// ///
/// 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
/// ///
use helios_dac::{NativeHeliosDac, NativeHeliosDacController}; use helios_dac::{NativeHeliosDac, NativeHeliosDacController};
use helios_dac::{ use std::time::SystemTime;
// Coordinate,
// Color,
DeviceStatus,
Frame,
// Point as HeliosPoint,
};
use crate::conf::HeliosConf;
use crate::device::{Device, Status, PlaybackState};
use crate::errors::{LJError, LJResult};
use crate::point::{Color, Point};
use chrono::{DateTime, Utc};
#[allow(dead_code)]
pub struct HeliosDevice { pub struct HeliosDevice {
pub conf: HeliosConf, pub conf: HeliosConf,
dac: NativeHeliosDac, dac: NativeHeliosDac,
sent_points: u16, sent_points: u16,
state: PlaybackState, state: PlaybackState,
lack: String, lack: String,
last_traced_at: String, last_traced_at: String,
} }
impl HeliosDevice { impl HeliosDevice {
pub fn new(conf: &HeliosConf) -> LJResult<Self> { pub fn new(conf: &HeliosConf) -> LJResult<Self> {
let id = conf.id; let id = conf.id;
let controller = NativeHeliosDacController::new()?; let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?; let devices = controller.list_devices()?;
let Some(device) = devices.into_iter().nth(id as usize) else { let Some(device) = devices.into_iter().nth(id as usize) else {
return Err(Box::new(LJError::HeliosDeviceMissing)); return Err(Box::new(LJError::HeliosDeviceMissing));
}; };
let dac = device.open()?; let dac = device.open()?;
let now = SystemTime::now(); let now = SystemTime::now();
let now: DateTime<Utc> = now.into(); let now: DateTime<Utc> = now.into();
let last_traced_at = now.to_rfc3339(); let last_traced_at = now.to_rfc3339();
Ok(Self { Ok(Self {
conf: (*conf).clone(), conf: (*conf).clone(),
dac, dac,
sent_points: 0, sent_points: 0,
state: PlaybackState::PREPARE, state: PlaybackState::PREPARE,
lack: "".to_string(), lack: "".to_string(),
last_traced_at, last_traced_at,
}) })
} }
} }
impl Device for HeliosDevice { impl Device for HeliosDevice {
fn status(&mut self) -> Status { fn status(&mut self) -> Status {
let lack = self.lack.clone(); let lack = self.lack.clone();
Status { Status {
last_traced_at: self.last_traced_at.clone(), last_traced_at: self.last_traced_at.clone(),
properties: vec!["foo".to_string()], properties: vec!["foo".to_string()],
playback_state: self.state, playback_state: self.state,
capacity: self.sent_points as usize, capacity: self.sent_points as usize,
lack, lack,
} }
} }
fn draw(&mut self, fn draw(&mut self, line: Vec<Point>, speed: u32) -> LJResult<()> {
line: Vec<Point>, self.state = PlaybackState::IDLE;
speed: u32, while let Ok(DeviceStatus::NotReady) = self.dac.status() {}
) -> LJResult<()> { self.state = PlaybackState::PLAYING;
self.state = PlaybackState::IDLE;
while let Ok(DeviceStatus::NotReady) = self.dac.status() {}
self.state = PlaybackState::PLAYING;
let points: Vec<helios_dac::Point> = line.into_iter().map(|p| p.into()).collect(); let points: Vec<helios_dac::Point> = line.into_iter().map(|p| p.into()).collect();
let frame = Frame::new(speed, points.clone()); let frame = Frame::new(speed, points.clone());
self.dac.write_frame(frame.clone())?; self.dac.write_frame(frame.clone())?;
self.sent_points = points.len() as u16; self.sent_points = points.len() as u16;
self.last_traced_at = Utc::now().to_rfc3339(); self.last_traced_at = Utc::now().to_rfc3339();
Ok(()) Ok(())
} }
fn stop(&mut self) -> LJResult<()> { fn stop(&mut self) -> LJResult<()> {
self.dac.stop()?; self.dac.stop()?;
Ok(()) Ok(())
} }
fn grid(&mut self) -> Vec<Point> { fn grid(&mut self) -> Vec<Point> {
let dim_min = 0 as f32; let dim_min = 0 as f32;
let dim_mid = 2047.0; let dim_mid = 2047.0;
let dim_max = 4095.0; let dim_max = 4095.0;
let col_min = Color { r: 0, g: 0, b: 0 }; let col_min = Color { r: 0, g: 0, b: 0 };
let col_max = Color { r: 255, g: 255, b: 255 }; let col_max = Color {
r: 255,
g: 255,
b: 255,
};
vec![ vec![
Point { x: dim_min, y: dim_max, color: col_min }, Point {
Point { x: dim_min, y: dim_max, color: col_max }, x: dim_min,
Point { x: dim_max, y: dim_max, color: col_max }, y: dim_max,
Point { x: dim_max, y: dim_min, color: col_max }, color: col_min,
Point { x: dim_min, y: dim_min, color: col_max }, },
Point { x: dim_min, y: dim_min, color: col_min }, Point {
Point { x: dim_min, y: dim_mid, color: col_min }, x: dim_min,
Point { x: dim_min, y: dim_mid, color: col_max }, y: dim_max,
Point { x: dim_mid, y: dim_mid, color: col_max }, color: col_max,
Point { x: dim_mid, y: dim_min, color: col_max }, },
Point { x: dim_min, y: dim_min, color: col_max }, Point {
Point { x: dim_min, y: dim_min, color: col_min }, x: dim_max,
] y: dim_max,
} color: col_max,
},
Point {
x: dim_max,
y: dim_min,
color: col_max,
},
Point {
x: dim_min,
y: dim_min,
color: col_max,
},
Point {
x: dim_min,
y: dim_min,
color: col_min,
},
Point {
x: dim_min,
y: dim_mid,
color: col_min,
},
Point {
x: dim_min,
y: dim_mid,
color: col_max,
},
Point {
x: dim_mid,
y: dim_mid,
color: col_max,
},
Point {
x: dim_mid,
y: dim_min,
color: col_max,
},
Point {
x: dim_min,
y: dim_min,
color: col_max,
},
Point {
x: dim_min,
y: dim_min,
color: col_min,
},
]
}
} }

View File

@ -1,200 +1,204 @@
mod conf;
mod device;
mod errors;
mod framerate;
mod point;
/// ///
/// 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;
mod point;
mod transformer; mod transformer;
mod device;
mod worldstate; mod worldstate;
mod framerate;
use conf::Conf;
use device::device_factory; use device::device_factory;
use env_logger::Builder;
use errors::LJResult;
use framerate::Framerate;
use log::{/* warn, */ error, info, LevelFilter};
use point::{Color, Point};
use redis_ctrl::{Order, RedisCtrl};
use std::io::Read;
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::Conf;
use errors::LJResult;
use point::{Point, Color};
use transformer::Transformers; use transformer::Transformers;
use log::{LevelFilter, info, /* warn, */ error};
use env_logger::Builder;
use worldstate::WorldState; use worldstate::WorldState;
use framerate::Framerate;
const DEFAULT_CONF_FILE: &str = "settings.toml"; const DEFAULT_CONF_FILE: &str = "settings.toml";
pub fn main() { pub fn main() {
match run_all() { match run_all() {
Ok(()) => {} Ok(()) => {}
Err(err) => { Err(err) => {
error!("Error: {}", err); error!("Error: {}", err);
} }
} }
} }
fn run_all() -> LJResult<()> { fn run_all() -> LJResult<()> {
// Setup handler for interrupt Signals // Setup handler for interrupt Signals
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);
})?; })?;
// Setup configuration file and set up logs // Setup configuration file and set up logs
let filename = std::env::args().nth(1).unwrap_or_else(|| { let filename = std::env::args()
DEFAULT_CONF_FILE.to_string() .nth(1)
}); .unwrap_or_else(|| DEFAULT_CONF_FILE.to_string());
let config = Conf::new(&filename); // read conf from stdin?
init_logging(&config); let config = if filename == "-" {
let config = config?; println!("Awaiting JSON config on standard input...");
info!("*** Starting up ***"); let mut buff = String::new();
loop {
let mut buff2: Vec<u8> = vec![0; 4096];
let Ok(n) = std::io::stdin().read(&mut buff2) else {
panic!("read failled");
};
let s = std::str::from_utf8(&buff2[0..n]).unwrap();
buff.push_str(&s);
info!("{:?}", config); if n == 0 || buff2[n - 1] == 0 {
let config = toml::from_str(&buff[0..buff.len() - 1])?;
break Ok(config);
}
}
} else {
Conf::new(&filename)
};
init_logging(&config);
let config = config?;
// Setup Redis Service info!("*** Starting up ***");
let mut rs = RedisCtrl::new(&config.redis_url, &config.laser_id)?;
let mut world_state = rs.init_world_state().unwrap(); info!("{:?}", config);
info!("WorldState: {:?}", world_state);
// Setup Redis Service
let mut rs = RedisCtrl::new(&config.redis_url, &config.laser_id)?;
// Setup Laser Device based on conf let mut world_state = rs.init_world_state().unwrap();
let mut tracer = device_factory(&config)?; info!("WorldState: {:?}", world_state);
world_state.grid = tracer.grid();
// Setup geometry transformers on points lists // Setup Laser Device based on conf
let transformers = config.get_transformers(); let mut tracer = device_factory(&config)?;
world_state.grid = tracer.grid();
// Setup framerate limiter // Setup geometry transformers on points lists
let mut framerate_handler = Framerate::new()?; let transformers = config.get_transformers();
// Dispatch based on redis requests // Setup framerate limiter
while running.load(Ordering::SeqCst) { let mut framerate_handler = Framerate::new()?;
rs.set_status(tracer.status())?;
framerate_handler.handle_time()?;
let order = rs.get_order(config.laser_id)?;
match order {
Order::Draw | Order::Black | Order::Grid => {
// 0 : Draw Normal point list
// 2 : Draw BLACK point list
// 3 : Draw GRID point list
world_state.draw_black = order == Order::Black;
world_state.draw_grid = order == Order::Grid;
let frame = get_next_frame(
&config,
&transformers,
&mut rs,
&world_state,
)?;
// For now, draw all the time
tracer.draw(frame, world_state.kpps)?;
}
Order::Intensity => {
// 6 : Max Intensity Change = reread redis key /intensity
world_state.intensity = rs.get_int("intensity")?
.try_into()?;
}
Order::Edh => {
// 1 : Get the new EDH = reread redis key /EDH/lasernumber
world_state.edh = rs.get_edh()?;
}
Order::Kpps => {
// 7 : kpps change = reread redis key /kpps
world_state.kpps = rs.get_int("kpps")?;
}
Order::ClientKey => {
world_state.client_key = rs.get_client_key()?;
}
Order::ColorBalance => {
let (r, g, b) = rs.get_color_balance()?;
world_state.color_balance = Color { r, g, b };
}
Order::Resampler => {
world_state.resampler = rs.get_resampler()?;
}
_ => {
// 9 : poweroff LJ
info!("Order: {:?}", order);
}
}
}
info!("Exiting, stoping device."); // Dispatch based on redis requests
tracer.stop()?; while running.load(Ordering::SeqCst) {
Ok(()) rs.set_status(tracer.status())?;
framerate_handler.handle_time()?;
let order = rs.get_order(config.laser_id)?;
match order {
Order::Draw | Order::Black | Order::Grid => {
// 0 : Draw Normal point list
// 2 : Draw BLACK point list
// 3 : Draw GRID point list
world_state.draw_black = order == Order::Black;
world_state.draw_grid = order == Order::Grid;
let frame = get_next_frame(&config, &transformers, &mut rs, &world_state)?;
// For now, draw all the time
tracer.draw(frame, world_state.kpps)?;
}
Order::Intensity => {
// 6 : Max Intensity Change = reread redis key /intensity
world_state.intensity = rs.get_int("intensity")?.try_into()?;
}
Order::Edh => {
// 1 : Get the new EDH = reread redis key /EDH/lasernumber
world_state.edh = rs.get_edh()?;
}
Order::Kpps => {
// 7 : kpps change = reread redis key /kpps
world_state.kpps = rs.get_int("kpps")?;
}
Order::ClientKey => {
world_state.client_key = rs.get_client_key()?;
}
Order::ColorBalance => {
let (r, g, b) = rs.get_color_balance()?;
world_state.color_balance = Color { r, g, b };
}
Order::Resampler => {
world_state.resampler = rs.get_resampler()?;
}
Order::PowerOff => {
info!("PowerOff command recieved.");
break;
}
}
}
info!("Exiting, stoping device.");
tracer.stop()?;
Ok(())
} }
fn init_logging(config: &LJResult<Conf>) { fn init_logging(config: &LJResult<Conf>) {
if let Ok(ref config) = config { if let Ok(ref config) = config {
let level = if config.debug { let level = if config.debug {
LevelFilter::Debug LevelFilter::Debug
} else { } else {
LevelFilter::Info LevelFilter::Info
}; };
let mut builder = Builder::from_default_env(); let mut builder = Builder::from_default_env();
builder builder.filter(None, level).init();
.filter(None, level) info!("Debug mode enabled from configuration file");
.init(); return;
info!("Debug mode enabled from configuration file"); }
return; info!("Logging level inherited from env");
} env_logger::init();
info!("Logging level inherited from env");
env_logger::init();
} }
fn get_next_frame( fn get_next_frame(
config: &Conf, config: &Conf,
transformers: &[Box<dyn Transformers>], transformers: &[Box<dyn Transformers>],
rs: &mut RedisCtrl, rs: &mut RedisCtrl,
world_state: &WorldState, world_state: &WorldState,
) -> LJResult<Vec<Point>> { ) -> LJResult<Vec<Point>> {
let format_key = format!("{}{}", let format_key = format!("{}{}", world_state.client_key, config.laser_id);
world_state.client_key,
config.laser_id);
// Handle the grid case // Handle the grid case
let mut line: Vec<Point> = if world_state.draw_grid { let mut line: Vec<Point> = if world_state.draw_grid {
world_state.grid.clone() world_state.grid.clone()
} else { } else {
let redis_line = rs.get_line(&format_key)?; let redis_line = rs.get_line(&format_key)?;
redis_line.into_iter() redis_line.into_iter().map(|tpl| tpl.into()).collect()
.map(|tpl| tpl.into()) };
.collect()
};
for transformer in transformers { for transformer in transformers {
line = transformer.apply(&line, world_state); line = transformer.apply(&line, world_state);
} }
// info!("Draw Black -> {}", world_state.draw_black); // info!("Draw Black -> {}", world_state.draw_black);
// info!("Draw Grid -> {}", world_state.draw_grid); // info!("Draw Grid -> {}", world_state.draw_grid);
// LIMITER and BLACK // LIMITER and BLACK
line = line.into_iter() line = line
.map(|p| { .into_iter()
let color = if world_state.draw_black { .map(|p| {
Color { r: 0, g: 0, b: 0 } let color = if world_state.draw_black {
} else { Color { r: 0, g: 0, b: 0 }
Color { } else {
r: p.color.r.min(world_state.intensity), Color {
g: p.color.g.min(world_state.intensity), r: p.color.r.min(world_state.intensity),
b: p.color.b.min(world_state.intensity), g: p.color.g.min(world_state.intensity),
} b: p.color.b.min(world_state.intensity),
}; }
Point { };
color, Point { color, ..p }
..p })
} .collect();
})
.collect();
//info!("Line: {:?}", line); //info!("Line: {:?}", line);
Ok(line) Ok(line)
} }

View File

@ -1,143 +1,147 @@
use redis::{Client, Commands, Connection};
use ron::de::from_str;
use crate::device::Status; use crate::device::Status;
use crate::errors::{LJError, LJResult}; use crate::errors::{LJError, LJResult};
use crate::worldstate::{WorldState, EDH}; use crate::worldstate::{WorldState, EDH};
use redis::{Client, Commands, Connection};
use ron::de::from_str;
// use log::info; // use log::info;
#[repr(u8)] #[repr(u8)]
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Order { pub enum Order {
Draw = 0, Draw = 0,
Edh, Edh,
Black, Black,
Grid, Grid,
Resampler, Resampler,
ClientKey, ClientKey,
Intensity, Intensity,
Kpps, Kpps,
ColorBalance, ColorBalance,
PowerOff PowerOff,
} }
impl TryFrom<u8> for Order { impl TryFrom<u8> for Order {
type Error = String; type Error = String;
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 > 9 {
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,
9 => PowerOff, 9 => PowerOff,
_ => unreachable!() _ => unreachable!(),
}) })
} }
} }
pub type Line = Vec<(f32, f32, u32)>; pub type Line = Vec<(f32, f32, u32)>;
pub type Resampler = Vec<Vec<(f32,f32)>>; pub type Resampler = Vec<Vec<(f32, f32)>>;
pub struct RedisCtrl { pub struct RedisCtrl {
pub client: Client, //pub client: Client,
pub connection: Connection, pub connection: Connection,
laser_id: u8, laser_id: u8,
} }
impl RedisCtrl { impl RedisCtrl {
pub fn new(url: &str, laser_id: &u8) -> LJResult<Self> { pub fn new(url: &str, laser_id: &u8) -> LJResult<Self> {
let client = Client::open(url) let client = Client::open(url).map_err(LJError::RedisConnect)?;
.map_err(LJError::RedisConnect)?; let connection = client.get_connection().map_err(LJError::RedisConnect)?;
let connection = client.get_connection() Ok(RedisCtrl {
.map_err(LJError::RedisConnect)?; //client,
Ok(RedisCtrl { client, connection, laser_id: *laser_id }) connection,
} laser_id: *laser_id,
})
}
pub fn get_line(&mut self, key: &str) -> LJResult<Line> { pub fn get_line(&mut self, key: &str) -> LJResult<Line> {
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 set(&mut self, key: String, value: String) -> LJResult<()> { pub fn set(&mut self, key: String, value: String) -> LJResult<()> {
self.connection.set(key, value)?; self.connection.set(key, value)?;
Ok(()) Ok(())
} }
pub fn get_order(&mut self, id: u8) -> LJResult<Order> { pub fn get_order(&mut self, id: u8) -> LJResult<Order> {
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())?;
if val == 1 || val >= 4 { if val == 1 || val >= 4 {
self.connection.set(path, 0)?; self.connection.set(path, 0)?;
} }
Ok(val.try_into()?) Ok(val.try_into()?)
} }
pub fn set_status(&mut self, status: Status) -> LJResult<()> { pub fn set_status(&mut self, status: Status) -> LJResult<()> {
let lstt_key = format!("/lstt/{}", self.laser_id); let lstt_key = format!("/lstt/{}", self.laser_id);
let cap_key = format!("/cap/{}", self.laser_id); let cap_key = format!("/cap/{}", self.laser_id);
let lack_key = format!("/lack/{}", self.laser_id); let lack_key = format!("/lack/{}", self.laser_id);
self.set(lstt_key, status.playback_state.to_string())?; self.set(lstt_key, status.playback_state.to_string())?;
self.set(cap_key, status.capacity.to_string())?; self.set(cap_key, status.capacity.to_string())?;
self.set(lack_key, status.lack.to_string())?; self.set(lack_key, status.lack.to_string())?;
Ok(()) Ok(())
} }
pub fn init_world_state(&mut self) -> LJResult<WorldState> { pub fn init_world_state(&mut self) -> LJResult<WorldState> {
Ok(WorldState { Ok(WorldState {
client_key: self.get_client_key()?, client_key: self.get_client_key()?,
edh: self.get_edh()?, edh: self.get_edh()?,
kpps: self.get_int("kpps")?.try_into()?, kpps: self.get_int("kpps")?.try_into()?,
intensity: self.get_int("intensity")?.try_into()?, intensity: self.get_int("intensity")?.try_into()?,
..WorldState::default() ..WorldState::default()
}) })
} }
pub fn get_edh(&mut self) -> LJResult<EDH> { pub fn get_edh(&mut self) -> LJResult<EDH> {
// Get new EDH // Get new EDH
let edh_key = format!("/EDH/{}", self.laser_id); let edh_key = format!("/EDH/{}", self.laser_id);
let edh: String = self.connection.get(edh_key)?; let edh: String = self.connection.get(edh_key)?;
let edh: Vec<Vec<f32>> = from_str(&edh)?; let edh: Vec<Vec<f32>> = from_str(&edh)?;
let edh = EDH::new(edh)?; let edh = EDH::new(edh)?;
Ok(edh) Ok(edh)
} }
pub fn get_client_key(&mut self) -> LJResult<String> { pub fn get_client_key(&mut self) -> LJResult<String> {
let key: String = self.connection.get("/clientkey")?; let key: String = self.connection.get("/clientkey")?;
Ok(key) Ok(key)
} }
pub fn get_color_balance(&mut self) -> LJResult<(u8, u8, u8)> { pub fn get_color_balance(&mut self) -> LJResult<(u8, u8, u8)> {
Ok(( Ok((
self.connection.get("/red")?, self.connection.get("/red")?,
self.connection.get("/green")?, self.connection.get("/green")?,
self.connection.get("/blue")?, self.connection.get("/blue")?,
)) ))
} }
pub fn get_resampler(&mut self ) -> LJResult<Resampler> { pub fn get_resampler(&mut self) -> LJResult<Resampler> {
let val: String = self.connection.get(format!("/resampler/{}", self.laser_id))?; let val: String = self
let resampler : Resampler = from_str(&val)?; .connection
Ok(resampler) .get(format!("/resampler/{}", self.laser_id))?;
} let resampler: Resampler = from_str(&val)?;
Ok(resampler)
}
pub fn get_int(&mut self, key: &str) -> LJResult<u32> { pub fn get_int(&mut self, key: &str) -> LJResult<u32> {
// Get new Int // Get new Int
let fmt = format!("/{key}/{}", self.laser_id); let fmt = format!("/{key}/{}", self.laser_id);
let val: u32 = self.connection.get(fmt)?; let val: u32 = self.connection.get(fmt)?;
Ok(val) Ok(val)
} }
} }

View File

@ -1,28 +1,27 @@
use log::debug;
use crate::transformer::Transformers;
use crate::point::Point; use crate::point::Point;
use crate::transformer::Transformers;
use crate::worldstate::WorldState; use crate::worldstate::WorldState;
use log::debug;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
/// Converts helios Geometry to Helios /// Converts helios Geometry to Helios
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Intensity { pub struct Intensity {}
}
impl Transformers for Intensity { impl Transformers for Intensity {
fn apply(&self, point_list: &[Point], ws: &WorldState) -> Vec<Point> { fn apply(&self, point_list: &[Point], ws: &WorldState) -> Vec<Point> {
// debug!("list helios {:?}", point_list); // debug!("list helios {:?}", point_list);
let out = point_list.iter().map(|pt| { let out = point_list
Point { .iter()
x: pt.x, .map(|pt| Point {
y: pt.y, x: pt.x,
color: pt.color * ws.intensity y: pt.y,
} color: pt.color * ws.intensity,
}).collect(); })
debug!("list intensity {:?}", out); .collect();
out //debug!("list intensity {:?}", out);
} out
}
} }