Compare commits
No commits in common. "master" and "feature/worldstate" have entirely different histories.
master
...
feature/wo
@ -10,7 +10,6 @@ chrono = "0.4.26"
|
|||||||
config = "0.13.3"
|
config = "0.13.3"
|
||||||
ctrlc = "3.4.0"
|
ctrlc = "3.4.0"
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
ether-dream = "0.2.5"
|
|
||||||
helios-dac = { version = "0.1", default-features = false, features = ["native"] }
|
helios-dac = { version = "0.1", default-features = false, features = ["native"] }
|
||||||
|
|
||||||
log = "0.4.18"
|
log = "0.4.18"
|
||||||
|
16
README.md
16
README.md
@ -1,16 +0,0 @@
|
|||||||
# LJ rust
|
|
||||||
|
|
||||||
## Crashcourse
|
|
||||||
|
|
||||||
```shell
|
|
||||||
|
|
||||||
# Copy and edit the settings file
|
|
||||||
$ cp copyme.settings.toml settings.toml
|
|
||||||
|
|
||||||
# Populate the redis database
|
|
||||||
$ cargo run --example populate_redis
|
|
||||||
|
|
||||||
# Run
|
|
||||||
$ cargo run --release
|
|
||||||
|
|
||||||
```
|
|
@ -1,4 +1,4 @@
|
|||||||
# file: settings.toml
|
# file: Settings.toml
|
||||||
# Rename me !
|
# Rename me !
|
||||||
|
|
||||||
# The main key of your laser in LJ
|
# The main key of your laser in LJ
|
||||||
@ -12,26 +12,20 @@ redis_url = "redis://127.0.0.1:6379/"
|
|||||||
|
|
||||||
# Either Helios or Etherdream
|
# Either Helios or Etherdream
|
||||||
# For Helios. USB Device Id of the DAC
|
# For Helios. USB Device Id of the DAC
|
||||||
#[dac.helios]
|
[dac.helios]
|
||||||
#id = 0
|
id = 0
|
||||||
|
|
||||||
# For dummy dac:
|
# For dummy dac:
|
||||||
[dac.dummy]
|
# [dac.dummy]
|
||||||
|
|
||||||
# For Etherdream. IP of the DAC
|
# For Etherdream. IP of the DAC
|
||||||
# [dac.etherdream]
|
# [dac.etherdream]
|
||||||
# ip = "192.168.1.68"
|
# url = "192.168.1.68"
|
||||||
|
|
||||||
[[transformers]]
|
[[transformers]]
|
||||||
[transformers.translate]
|
[transformers.translate]
|
||||||
x = 2000
|
x = 2000
|
||||||
y = 2000
|
y = 2000
|
||||||
|
|
||||||
[[transformers]]
|
[[transformers]]
|
||||||
[transformers.replicate]
|
[transformers.replicate]
|
||||||
Until = 48
|
Until = 48
|
||||||
|
|
||||||
# Never remove this : this is mandatory
|
|
||||||
[[transformers]]
|
|
||||||
[transformers.intensity]
|
|
||||||
|
|
||||||
|
@ -1,151 +0,0 @@
|
|||||||
extern crate ether_dream;
|
|
||||||
|
|
||||||
use ether_dream::dac;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
println!("Listening for an Ether Dream DAC...");
|
|
||||||
|
|
||||||
let (dac_broadcast, source_addr) = ether_dream::recv_dac_broadcasts()
|
|
||||||
.expect("failed to bind to UDP socket")
|
|
||||||
.filter_map(Result::ok)
|
|
||||||
.next()
|
|
||||||
.unwrap();
|
|
||||||
let mac_address = dac::MacAddress(dac_broadcast.mac_address);
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"Discovered DAC \"{}\" at \"{}\"! Connecting...",
|
|
||||||
mac_address, source_addr
|
|
||||||
);
|
|
||||||
|
|
||||||
// Establish the TCP connection.
|
|
||||||
let mut stream = dac::stream::connect(&dac_broadcast, source_addr.ip().clone()).unwrap();
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
// Lets use the DAC at an eighth the maximum scan rate.
|
|
||||||
let points_per_second = stream.dac().max_point_rate / 32;
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"Preparing for playback:\n\tframe_hz: {}\n\tpoint_hz: {}\n\tpoints_per_frame: {}\n",
|
|
||||||
frames_per_second, points_per_second, points_per_frame
|
|
||||||
);
|
|
||||||
|
|
||||||
// Prepare the DAC's playback engine and await the repsonse.
|
|
||||||
stream
|
|
||||||
.queue_commands()
|
|
||||||
.prepare_stream()
|
|
||||||
.submit()
|
|
||||||
.err()
|
|
||||||
.map(|err| {
|
|
||||||
eprintln!(
|
|
||||||
"err occurred when submitting PREPARE_STREAM \
|
|
||||||
command and listening for response: {}",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
println!("Beginning playback!");
|
|
||||||
|
|
||||||
// The sine wave used to generate points.
|
|
||||||
let mut sine_wave = SineWave {
|
|
||||||
point: 0,
|
|
||||||
points_per_frame,
|
|
||||||
frames_per_second,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Queue the initial frame and tell the DAC to begin producing output.
|
|
||||||
let n_points = points_to_generate(stream.dac());
|
|
||||||
stream
|
|
||||||
.queue_commands()
|
|
||||||
.data(sine_wave.by_ref().take(n_points))
|
|
||||||
.begin(0, points_per_second)
|
|
||||||
.submit()
|
|
||||||
.err()
|
|
||||||
.map(|err| {
|
|
||||||
eprintln!(
|
|
||||||
"err occurred when submitting initial DATA and BEGIN \
|
|
||||||
commands and listening for response: {}",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
|
||||||
eprintln!("Stream dac{:?}", stream.dac());
|
|
||||||
|
|
||||||
// Loop and continue to send points forever.
|
|
||||||
loop {
|
|
||||||
// Determine how many points the DAC can currently receive.
|
|
||||||
let n_points = points_to_generate(stream.dac());
|
|
||||||
if let Err(err) = stream
|
|
||||||
.queue_commands()
|
|
||||||
.data(sine_wave.by_ref().take(n_points))
|
|
||||||
.submit()
|
|
||||||
{
|
|
||||||
eprintln!(
|
|
||||||
"err occurred when submitting DATA command and listening \
|
|
||||||
for response: {}",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tell the DAC to stop producing output and return to idle. Wait for the response.
|
|
||||||
//
|
|
||||||
// Note that the DAC is commanded to stop on `Drop` if this is not called and any errors
|
|
||||||
// produced are ignored.
|
|
||||||
stream
|
|
||||||
.queue_commands()
|
|
||||||
.stop()
|
|
||||||
.submit()
|
|
||||||
.expect("err occurred when submitting STOP command and listening for response");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine the number of points needed to fill the DAC.
|
|
||||||
fn points_to_generate(dac: ðer_dream::dac::Dac) -> usize {
|
|
||||||
dac.buffer_capacity as usize - 1 - dac.status.buffer_fullness as usize
|
|
||||||
}
|
|
||||||
|
|
||||||
// An iterator that endlessly generates a sine wave of DAC points.
|
|
||||||
//
|
|
||||||
// The sine wave oscillates at a rate of once per second.
|
|
||||||
struct SineWave {
|
|
||||||
point: u32,
|
|
||||||
points_per_frame: u16,
|
|
||||||
frames_per_second: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for SineWave {
|
|
||||||
type Item = ether_dream::protocol::DacPoint;
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
let coloured_points_per_frame = self.points_per_frame - 1;
|
|
||||||
let i = (self.point % self.points_per_frame as u32) as u16;
|
|
||||||
let hz = 1.0;
|
|
||||||
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 amp = (hz * (fract + phase) * 2.0 * std::f32::consts::PI).sin();
|
|
||||||
let (r, g, b) = match i {
|
|
||||||
i if i == coloured_points_per_frame || i < 13 => (0, 0, 0),
|
|
||||||
_ => (std::u16::MAX, std::u16::MAX, std::u16::MAX),
|
|
||||||
};
|
|
||||||
let x_min = std::i16::MIN;
|
|
||||||
let x_max = std::i8::MAX 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 control = 0;
|
|
||||||
let (u1, u2) = (0, 0);
|
|
||||||
let p = ether_dream::protocol::DacPoint {
|
|
||||||
control,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
i,
|
|
||||||
r,
|
|
||||||
g,
|
|
||||||
b,
|
|
||||||
u1,
|
|
||||||
u2,
|
|
||||||
};
|
|
||||||
self.point += 1;
|
|
||||||
Some(p)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
use std::io::_print;
|
|
||||||
/**
|
|
||||||
|
|
||||||
# Populate Redis Example
|
|
||||||
|
|
||||||
**This script simulates the redis content provided by the LJ Python / web tool**
|
|
||||||
|
|
||||||
$ cargo run --example populate_redis
|
|
||||||
**/
|
|
||||||
|
|
||||||
use redis::{
|
|
||||||
//RedisResult,
|
|
||||||
Client,
|
|
||||||
Commands,
|
|
||||||
Connection,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn do_something() -> redis::RedisResult<()> {
|
|
||||||
let client = Client::open("redis://127.0.0.1/")?;
|
|
||||||
let mut con: Connection = client.get_connection()?;
|
|
||||||
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("/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() {
|
|
||||||
match do_something() {
|
|
||||||
Err(err) => println!("Something wrong occured: {:?}", err),
|
|
||||||
Ok(..) => println!("Successfully inserted content in Redis")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
///
|
|
||||||
/// $ cargo run --example simple_client
|
|
||||||
///
|
|
||||||
|
|
||||||
use redis::{
|
|
||||||
//RedisResult,
|
|
||||||
Client,
|
|
||||||
Commands,
|
|
||||||
Connection,
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
fn do_something() -> redis::RedisResult<()> {
|
|
||||||
let client = Client::open("redis://127.0.0.1/")?;
|
|
||||||
let mut con: Connection = client.get_connection()?;
|
|
||||||
let start = Instant::now();
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let elapsed = start.elapsed();
|
|
||||||
let time = 60.0 * elapsed.as_millis() as f32 / 1000.0;
|
|
||||||
|
|
||||||
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;
|
|
||||||
let y = a.sin() * r;
|
|
||||||
let col = if i % 8 < 4 {
|
|
||||||
0x000000ff
|
|
||||||
} else {
|
|
||||||
0x00ff0000
|
|
||||||
};
|
|
||||||
v.push((x, y, col));
|
|
||||||
}
|
|
||||||
// println!("{:?}", v);
|
|
||||||
let _ = con.set("/pl/0/0", format!("{:?}", v))?;
|
|
||||||
}
|
|
||||||
// Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
_ = do_something();
|
|
||||||
}
|
|
144
src/conf.rs
144
src/conf.rs
@ -1,106 +1,96 @@
|
|||||||
use config::Config;
|
use config::Config;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize,Deserialize};
|
||||||
use crate::errors::{LJError, LJResult};
|
use crate::errors::{LJError,LJResult};
|
||||||
use crate::transformer;
|
use crate::transformer;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct Conf {
|
pub struct Conf {
|
||||||
pub laser_id: u8,
|
pub laser_id: u8,
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
pub redis_url: String,
|
pub redis_url: String,
|
||||||
pub dac: DacFamily,
|
pub dac: DacFamily,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub transformers: Vec<TransformConf>,
|
pub transformers: Vec<TransformConf>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub enum DacFamily {
|
pub enum DacFamily {
|
||||||
#[serde(rename = "helios")]
|
#[serde(rename = "helios")]
|
||||||
Helios(HeliosConf),
|
Helios(HeliosConf),
|
||||||
#[serde(rename = "etherdream")]
|
#[serde(rename = "etherdream")]
|
||||||
Etherdream(EtherDreamConf),
|
Etherdream(EtherDreamConf),
|
||||||
#[serde(rename = "dummy")]
|
#[serde(rename = "dummy")]
|
||||||
Dummy,
|
Dummy,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct HeliosConf {
|
pub struct HeliosConf {
|
||||||
pub id: u8,
|
pub id: u8
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct EtherDreamConf {
|
pub struct EtherDreamConf {
|
||||||
pub ip: String,
|
pub url: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub enum TransformConf {
|
pub enum TransformConf {
|
||||||
#[serde(rename = "translate")]
|
#[serde(rename = "translate")]
|
||||||
Translate(transformer::Translate),
|
Translate(transformer::Translate),
|
||||||
#[serde(rename = "replicate")]
|
#[serde(rename = "replicate")]
|
||||||
Replicate(transformer::Replicate),
|
Replicate(transformer::Replicate),
|
||||||
#[serde(rename = "rotate")]
|
#[serde(rename = "rotate")]
|
||||||
Rotate(transformer::Rotate),
|
Rotate(transformer::Rotate),
|
||||||
#[serde(rename = "flip_horizontal")]
|
#[serde(rename = "flip_horizontal")]
|
||||||
FlipH(transformer::FlipHorizontal),
|
FlipH(transformer::FlipHorizontal),
|
||||||
#[serde(rename = "flip_vertical")]
|
#[serde(rename = "flip_vertical")]
|
||||||
FlipV(transformer::FlipVertical),
|
FlipV(transformer::FlipVertical),
|
||||||
#[serde(rename = "grid")]
|
#[serde(rename = "grid")]
|
||||||
Grid(transformer::Grid),
|
Grid(transformer::Grid),
|
||||||
#[serde(rename = "homography")]
|
#[serde(rename = "homography")]
|
||||||
Homography(transformer::Homography),
|
Homography(transformer::Homography)
|
||||||
#[serde(rename = "helios_to_etherdream")]
|
|
||||||
HeliosToEtherdream(transformer::HeliosToEtherdream),
|
|
||||||
#[serde(rename = "intensity")]
|
|
||||||
Intensity(transformer::Intensity),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Conf {
|
impl Conf {
|
||||||
pub fn new(path: &str) -> LJResult<Conf> {
|
pub fn new(path: &str) -> LJResult<Conf> {
|
||||||
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 : Conf = settings.try_deserialize().map_err(LJError::Config)?;
|
||||||
|
Ok(conf)
|
||||||
|
}
|
||||||
|
|
||||||
let conf: Conf = settings.try_deserialize().map_err(LJError::Config)?;
|
pub fn get_transformers(&self) -> Vec<Box<dyn transformer::Transformers>> {
|
||||||
Ok(conf)
|
let mut v = vec![];
|
||||||
|
for t in &self.transformers {
|
||||||
|
let t : Box<dyn transformer::Transformers> = match t {
|
||||||
|
TransformConf::Translate(t) => Box::new(*t),
|
||||||
|
TransformConf::Replicate(r) => Box::new(*r),
|
||||||
|
TransformConf::Rotate(r) => Box::new(*r),
|
||||||
|
TransformConf::FlipH(r) => Box::new(*r),
|
||||||
|
TransformConf::FlipV(r) => Box::new(*r),
|
||||||
|
TransformConf::Grid(r) => Box::new(*r),
|
||||||
|
TransformConf::Homography(r) => Box::new(*r),
|
||||||
|
};
|
||||||
|
v.push(t);
|
||||||
}
|
}
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_transformers(&self) -> Vec<Box<dyn transformer::Transformers>> {
|
#[allow(dead_code)]
|
||||||
let mut v = vec![];
|
pub fn dump() {
|
||||||
for t in &self.transformers {
|
let conf = Conf { laser_id: 0,
|
||||||
let t: Box<dyn transformer::Transformers> = match t {
|
debug: true,
|
||||||
TransformConf::FlipH(r) => Box::new(*r),
|
redis_url: "redis://127.0.0.1:6379/".to_string(),
|
||||||
TransformConf::FlipV(r) => Box::new(*r),
|
dac: DacFamily::Helios(HeliosConf { id: 0 }),
|
||||||
TransformConf::Grid(r) => Box::new(*r),
|
transformers: vec![
|
||||||
TransformConf::HeliosToEtherdream(r) => Box::new(*r),
|
TransformConf::Translate(transformer::Translate { x: 2000.0, y: 2000.0 } ),
|
||||||
TransformConf::Homography(r) => Box::new(*r),
|
TransformConf::Replicate(transformer::Replicate::Until(48))
|
||||||
TransformConf::Intensity(r) => Box::new(*r),
|
]
|
||||||
TransformConf::Replicate(r) => Box::new(*r),
|
};
|
||||||
TransformConf::Rotate(r) => Box::new(*r),
|
let s = toml::to_string(&conf).unwrap();
|
||||||
TransformConf::Translate(t) => Box::new(*t),
|
println!("{}", s);
|
||||||
};
|
}
|
||||||
v.push(t);
|
|
||||||
}
|
|
||||||
v
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn dump() {
|
|
||||||
let conf = Conf {
|
|
||||||
laser_id: 0,
|
|
||||||
debug: true,
|
|
||||||
redis_url: "redis://127.0.0.1:6379/".to_string(),
|
|
||||||
dac: DacFamily::Helios(HeliosConf { id: 0 }),
|
|
||||||
transformers: vec![
|
|
||||||
TransformConf::Translate(transformer::Translate {
|
|
||||||
x: 2000.0,
|
|
||||||
y: 2000.0,
|
|
||||||
}),
|
|
||||||
TransformConf::Replicate(transformer::Replicate::Until(48)),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
let s = toml::to_string(&conf).unwrap();
|
|
||||||
println!("{}", s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
mod helios;
|
mod helios;
|
||||||
mod dummy;
|
mod dummy;
|
||||||
mod etherdream;
|
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use crate::conf::{Conf, DacFamily /*EtherDreamConf, HeliosConf*/};
|
use crate::conf::{Conf, DacFamily /*EtherDreamConf, HeliosConf*/};
|
||||||
@ -9,7 +9,6 @@ use crate::device::dummy::DummyDevice;
|
|||||||
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;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
self.protocol_version,
|
self.protocol_version,
|
||||||
@ -27,14 +26,13 @@ self.point_count
|
|||||||
#[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,
|
||||||
PLAYING = 2
|
PLAYING,
|
||||||
}
|
}
|
||||||
|
|
||||||
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)]
|
||||||
@ -42,8 +40,8 @@ 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: u16,
|
||||||
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 +49,20 @@ 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(&self) -> Status;
|
||||||
fn draw(
|
fn draw(
|
||||||
&mut self,
|
&mut self,
|
||||||
frame: Vec<Point>,
|
frame: Vec<Point>,
|
||||||
speed: u32,
|
speed: u32,
|
||||||
) -> LJResult<()>;
|
) -> LJResult<()>;
|
||||||
fn stop(&mut self) -> 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) => todo!(),
|
||||||
DacFamily::Dummy => Box::new(DummyDevice::new()?)
|
DacFamily::Dummy => Box::new(DummyDevice::new()?)
|
||||||
};
|
};
|
||||||
Ok(device)
|
Ok(device)
|
||||||
}
|
}
|
||||||
|
31
src/device/common.rs
Normal file
31
src/device/common.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
self.protocol_version,
|
||||||
|
self.le_state,
|
||||||
|
self.playback_state,
|
||||||
|
self.source,
|
||||||
|
self.le_flags,
|
||||||
|
self.playback_flags,
|
||||||
|
self.source_flags,
|
||||||
|
self.fullness,
|
||||||
|
self.point_rate,
|
||||||
|
self.point_count
|
||||||
|
*/
|
||||||
|
|
||||||
|
pub struct Status {
|
||||||
|
pub active: bool,
|
||||||
|
pub last_traced_at: String,
|
||||||
|
pub properties: Vec<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Device {
|
||||||
|
/**
|
||||||
|
fn intersect(&self, orig : &Vec3, dir : &Vec3) -> Option<Float>;
|
||||||
|
fn get_surface(&self, v : &Vec3) -> Vec3;
|
||||||
|
fn get_normal(&self, v : &Vec3) -> Vec3;
|
||||||
|
fn get_material(&self) -> &dyn Material;
|
||||||
|
**/
|
||||||
|
|
||||||
|
fn status( &self ) -> Status;
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
use crate::device::{Device, Status, PlaybackState};
|
use crate::device::{Device, Status, PlaybackState};
|
||||||
use crate::errors::LJResult;
|
use crate::errors::LJResult;
|
||||||
use crate::point::{Color, Point};
|
use crate::point::Point;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
pub struct DummyDevice {
|
pub struct DummyDevice {
|
||||||
@ -14,7 +14,7 @@ impl DummyDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Device for DummyDevice {
|
impl Device for DummyDevice {
|
||||||
fn status(&mut self) -> Status {
|
fn status(&self) -> Status {
|
||||||
Status {
|
Status {
|
||||||
last_traced_at: "never".to_string(),
|
last_traced_at: "never".to_string(),
|
||||||
properties: vec!["foo".to_string()],
|
properties: vec!["foo".to_string()],
|
||||||
@ -35,9 +35,4 @@ impl Device for DummyDevice {
|
|||||||
fn stop(&mut self) -> LJResult<()> {
|
fn stop(&mut self) -> LJResult<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn grid(&mut self) -> Vec<Point> {
|
|
||||||
vec!(
|
|
||||||
Point{ x: 0 as f32, y: 0 as f32, color:Color{ r: 0, g: 0, b: 0 }}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,308 +0,0 @@
|
|||||||
#[warn(unused_imports)]
|
|
||||||
use log::{ debug, info, warn};
|
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
|
||||||
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::{Duration, SystemTime};
|
|
||||||
|
|
||||||
use crate::conf::EtherDreamConf;
|
|
||||||
use crate::device::{Device, Status, PlaybackState};
|
|
||||||
use crate::errors::{LJError, LJResult};
|
|
||||||
use crate::point::{Color, Point};
|
|
||||||
use ether_dream::protocol::{DacBroadcast, DacResponse};
|
|
||||||
|
|
||||||
|
|
||||||
#[warn(dead_code)]
|
|
||||||
pub struct EtherdreamDevice {
|
|
||||||
pub conf: EtherDreamConf,
|
|
||||||
dac: DacBroadcast,
|
|
||||||
stream: Stream,
|
|
||||||
|
|
||||||
// "a": ACK "F": Full "I": invalid. 64 or 35 for no connection.
|
|
||||||
// /// The previous command was accepted.
|
|
||||||
// pub const ACK: u8 = 0x61;
|
|
||||||
// /// The write command could not be performed because there was not enough buffer space when it
|
|
||||||
// /// was received.
|
|
||||||
// pub const NAK_FULL: u8 = 0x46;
|
|
||||||
// /// The command contained an invalid `command` byte or parameters.
|
|
||||||
// pub const NAK_INVALID: u8 = 0x49;
|
|
||||||
// /// An emergency-stop condition still exists.
|
|
||||||
// pub const NAK_STOP_CONDITION: u8 = 0x21;
|
|
||||||
// }
|
|
||||||
dac_response: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EtherdreamDevice {
|
|
||||||
pub fn new(conf: &EtherDreamConf) -> LJResult<Self> {
|
|
||||||
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 mut stream = connect(dac, source_address.ip())?;
|
|
||||||
match stream
|
|
||||||
.queue_commands()
|
|
||||||
.prepare_stream()
|
|
||||||
.submit() {
|
|
||||||
Err(err) => warn!("err occurred when submitting PREPARE_STREAM command and listening for response: {}",err),
|
|
||||||
Ok(_) => info!("Prepared Stream.")
|
|
||||||
}
|
|
||||||
// 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;
|
|
||||||
// Lets use the DAC at an eighth the maximum scan rate.
|
|
||||||
let points_per_second = stream.dac().max_point_rate / 32;
|
|
||||||
// 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 mut sine_wave = SineWave {
|
|
||||||
point: 0,
|
|
||||||
points_per_frame,
|
|
||||||
frames_per_second,
|
|
||||||
};
|
|
||||||
|
|
||||||
match stream
|
|
||||||
.queue_commands()
|
|
||||||
.data(sine_wave.by_ref().take(400))
|
|
||||||
// .data(begin_list.into_iter().take(400 as usize))
|
|
||||||
.begin(0, points_per_second)
|
|
||||||
.submit() {
|
|
||||||
Err(err) => warn!("err occurred when submitting first data: {}",err),
|
|
||||||
Ok(_) => info!("Sent first data to Etherdream.")
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(stream)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn points_capacity(&self) -> usize {
|
|
||||||
/***
|
|
||||||
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
|
|
||||||
let n_points = self.dac.buffer_capacity as usize - self.stream.dac().dac.status.buffer_fullness as usize - 1;
|
|
||||||
n_points
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ping(&mut self) -> LJResult<()> {
|
|
||||||
|
|
||||||
Ok(self.stream.queue_commands().ping().submit()?)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Device for EtherdreamDevice {
|
|
||||||
fn status(&mut self) -> Status {
|
|
||||||
let playback_state = match self.stream.dac().dac.status.playback {
|
|
||||||
Playback::Idle => PlaybackState::IDLE,
|
|
||||||
Playback::Prepared => PlaybackState::PREPARE,
|
|
||||||
Playback::Playing => PlaybackState::PLAYING,
|
|
||||||
};
|
|
||||||
let now = SystemTime::now();
|
|
||||||
let now: DateTime<Utc> = now.into();
|
|
||||||
let now = now.to_rfc3339();
|
|
||||||
|
|
||||||
Status {
|
|
||||||
last_traced_at: now,
|
|
||||||
properties: vec!["foo".to_string()],
|
|
||||||
playback_state,
|
|
||||||
capacity: self.points_capacity(),
|
|
||||||
lack: self.dac_response.to_string(),
|
|
||||||
}
|
|
||||||
// debug!("Dac Status: {:?} ", status );
|
|
||||||
// debug!("Etherdream Dac {:?} ", self.dac );
|
|
||||||
// debug!("Stream dac{:?}", self.stream.dac());
|
|
||||||
// status
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(&mut self,
|
|
||||||
line: Vec<Point>,
|
|
||||||
_speed: u32,
|
|
||||||
) -> LJResult<()> {
|
|
||||||
let chunk_size = 64;
|
|
||||||
let points_iter = line.into_iter();
|
|
||||||
for chunk in points_iter.as_slice().chunks(chunk_size){
|
|
||||||
debug!("New chunk length: {:?}", chunk.len());
|
|
||||||
loop {
|
|
||||||
let capacity = self.points_capacity();
|
|
||||||
if chunk.len() > capacity {
|
|
||||||
debug!("Sleep, capacity : {:?}", capacity);
|
|
||||||
// Sleep for 1/100th of a sec
|
|
||||||
sleep(Duration::new( 0, 10000000));
|
|
||||||
self.ping()?;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
debug!("drawing");
|
|
||||||
match self.stream
|
|
||||||
.queue_commands()
|
|
||||||
.data(
|
|
||||||
chunk.iter()
|
|
||||||
.map(|point| (*point).into())
|
|
||||||
.take(chunk_size)
|
|
||||||
)
|
|
||||||
.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<()> {
|
|
||||||
info!("Stopping Etherdream device...");
|
|
||||||
match self.stream
|
|
||||||
.queue_commands()
|
|
||||||
.stop()
|
|
||||||
.submit()
|
|
||||||
{
|
|
||||||
Err(err) => {
|
|
||||||
warn!("Failed to stop EtherDream device with error {:?}", err);
|
|
||||||
Err(Box::new(err))
|
|
||||||
}
|
|
||||||
Ok(_) => {
|
|
||||||
info!("Sucessfully closed EtherDream device.");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn grid(&mut self) -> Vec<Point> {
|
|
||||||
let dim_mid = 16000.0;
|
|
||||||
let dim_max = 32000.0;
|
|
||||||
let col_min = Color { r: 0, g: 0, b: 0 };
|
|
||||||
let col_max = Color { r: 255, g: 255, b: 255 };
|
|
||||||
|
|
||||||
vec![
|
|
||||||
Point { 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.
|
|
||||||
//
|
|
||||||
// The sine wave oscillates at a rate of once per second.
|
|
||||||
struct SineWave {
|
|
||||||
point: u32,
|
|
||||||
points_per_frame: u16,
|
|
||||||
frames_per_second: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for SineWave {
|
|
||||||
type Item = ether_dream::protocol::DacPoint;
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
let coloured_points_per_frame = self.points_per_frame - 1;
|
|
||||||
let i = (self.point % self.points_per_frame as u32) as u16;
|
|
||||||
let hz = 1.0;
|
|
||||||
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 amp = (hz * (fract + phase) * 2.0 * std::f32::consts::PI).sin();
|
|
||||||
let (r, g, b) = match i {
|
|
||||||
i if i == coloured_points_per_frame || i < 13 => (0, 0, 0),
|
|
||||||
_ => (u16::MAX, u16::MAX, u16::MAX),
|
|
||||||
};
|
|
||||||
let x_min = i16::MIN;
|
|
||||||
let x_max = i16::MAX;
|
|
||||||
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 control = 0;
|
|
||||||
let (u1, u2) = (0, 0);
|
|
||||||
let p = ether_dream::protocol::DacPoint {
|
|
||||||
control,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
i,
|
|
||||||
r,
|
|
||||||
g,
|
|
||||||
b,
|
|
||||||
u1,
|
|
||||||
u2,
|
|
||||||
};
|
|
||||||
// debug!("{:?}",p);
|
|
||||||
self.point += 1;
|
|
||||||
Some(p)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,3 @@
|
|||||||
use std::time::SystemTime;
|
|
||||||
///
|
///
|
||||||
/// 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
|
||||||
@ -14,8 +13,8 @@ use helios_dac::{
|
|||||||
use crate::conf::HeliosConf;
|
use crate::conf::HeliosConf;
|
||||||
use crate::device::{Device, Status, PlaybackState};
|
use crate::device::{Device, Status, PlaybackState};
|
||||||
use crate::errors::{LJError, LJResult};
|
use crate::errors::{LJError, LJResult};
|
||||||
use crate::point::{Color, Point};
|
use crate::point::Point;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::Utc;
|
||||||
|
|
||||||
pub struct HeliosDevice {
|
pub struct HeliosDevice {
|
||||||
pub conf: HeliosConf,
|
pub conf: HeliosConf,
|
||||||
@ -23,7 +22,7 @@ pub struct HeliosDevice {
|
|||||||
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 {
|
||||||
@ -35,29 +34,26 @@ impl HeliosDevice {
|
|||||||
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: DateTime<Utc> = now.into();
|
|
||||||
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: "1985-04-12T23:20:50.52Z".to_string()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device for HeliosDevice {
|
impl Device for HeliosDevice {
|
||||||
fn status(&mut self) -> Status {
|
fn status(&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,
|
||||||
lack,
|
lack,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,27 +78,4 @@ impl Device for HeliosDevice {
|
|||||||
self.dac.stop()?;
|
self.dac.stop()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn grid(&mut self) -> Vec<Point> {
|
|
||||||
let dim_min = 0 as f32;
|
|
||||||
let dim_mid = 2047.0;
|
|
||||||
let dim_max = 4095.0;
|
|
||||||
let col_min = Color { r: 0, g: 0, b: 0 };
|
|
||||||
let col_max = Color { r: 255, g: 255, b: 255 };
|
|
||||||
|
|
||||||
vec![
|
|
||||||
Point { x: dim_min, y: dim_max, color: col_min },
|
|
||||||
Point { x: dim_min, y: dim_max, color: col_max },
|
|
||||||
Point { 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 },
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::{fmt, io};
|
use std::fmt;
|
||||||
use redis::RedisError;
|
use redis::RedisError;
|
||||||
use config::ConfigError;
|
use config::ConfigError;
|
||||||
|
|
||||||
@ -7,44 +7,41 @@ pub type LJResult<T> = Result<T, Box<dyn std::error::Error>>;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LJError {
|
pub enum LJError {
|
||||||
Config(ConfigError),
|
Config(ConfigError),
|
||||||
RedisConnect(RedisError),
|
RedisConnect(RedisError),
|
||||||
HeliosDeviceMissing,
|
HeliosDeviceMissing,
|
||||||
BadEDH,
|
BadEDH
|
||||||
EtherdreamConnectError(io::Error),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for LJError {
|
impl fmt::Display for LJError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
use LJError::*;
|
use LJError::*;
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Config(err) => {
|
||||||
|
write!(f, "unable to load config file: {err}")
|
||||||
|
},
|
||||||
|
|
||||||
match self {
|
RedisConnect(err) => {
|
||||||
Config(err) => {
|
write!(f, "unable to connect to redis server: {err}")
|
||||||
write!(f, "unable to load config file: {err}")
|
},
|
||||||
}
|
HeliosDeviceMissing => {
|
||||||
RedisConnect(err) => {
|
write!(f, "helios device not found")
|
||||||
write!(f, "unable to connect to redis server: {err}")
|
},
|
||||||
}
|
BadEDH => {
|
||||||
HeliosDeviceMissing => {
|
write!(f, "EDH matrix is not a 3x3 matrix")
|
||||||
write!(f, "helios device not found")
|
}
|
||||||
}
|
|
||||||
BadEDH => {
|
|
||||||
write!(f, "EDH matrix is not a 3x3 matrix")
|
|
||||||
}
|
|
||||||
EtherdreamConnectError(err) => {
|
|
||||||
write!(f, "Failed to retrieve Etherdream device: {err}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for LJError {
|
impl Error for LJError {
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
use LJError::*;
|
use LJError::*;
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
RedisConnect(err) => Some(err),
|
RedisConnect(err) => Some(err),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
use log::{debug, warn};
|
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
use crate::errors::LJResult;
|
|
||||||
use std::{thread};
|
|
||||||
|
|
||||||
/// Converts helios Geometry to Helios
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct Framerate {
|
|
||||||
prev_trace_time: Instant,
|
|
||||||
fps: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Framerate {
|
|
||||||
pub fn new() -> LJResult<Self> {
|
|
||||||
Ok(Framerate {
|
|
||||||
prev_trace_time: Instant::now(),
|
|
||||||
fps: 20,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
pub fn handle_time(&mut self) -> LJResult<()> {
|
|
||||||
let frame_time = 1000000000 / self.fps as u128;
|
|
||||||
let now = Instant::now();
|
|
||||||
// How long since last loop ?
|
|
||||||
let nanotime_spent = self.prev_trace_time.elapsed().as_nanos();
|
|
||||||
// Diw it go too fast? If so : sleep a bit
|
|
||||||
if frame_time > nanotime_spent {
|
|
||||||
let nanotime_towait = frame_time - nanotime_spent;
|
|
||||||
let dur = Duration::new(0, (nanotime_towait as f32 * 0.9) as u32);
|
|
||||||
// debug!("{:?} - {:?} : {:?}", nanotime_towait, self.prev_trace_time, now );
|
|
||||||
thread::sleep(dur);
|
|
||||||
debug!("Framerate OK");
|
|
||||||
} else {
|
|
||||||
warn!("Frame longer than expected {:?} > {:?}", nanotime_spent, frame_time, );
|
|
||||||
}
|
|
||||||
|
|
||||||
self.prev_trace_time = now;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,4 +5,3 @@ pub mod device;
|
|||||||
pub mod point;
|
pub mod point;
|
||||||
pub mod transformer;
|
pub mod transformer;
|
||||||
pub mod worldstate;
|
pub mod worldstate;
|
||||||
pub mod framerate;
|
|
||||||
|
142
src/main.rs
142
src/main.rs
@ -9,7 +9,6 @@ mod point;
|
|||||||
mod transformer;
|
mod transformer;
|
||||||
mod device;
|
mod device;
|
||||||
mod worldstate;
|
mod worldstate;
|
||||||
mod framerate;
|
|
||||||
|
|
||||||
use device::device_factory;
|
use device::device_factory;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
@ -17,12 +16,11 @@ use std::sync::Arc;
|
|||||||
use redis_ctrl::{RedisCtrl, Order};
|
use redis_ctrl::{RedisCtrl, Order};
|
||||||
use conf::Conf;
|
use conf::Conf;
|
||||||
use errors::LJResult;
|
use errors::LJResult;
|
||||||
use point::{Point, Color};
|
use point::Point;
|
||||||
use transformer::Transformers;
|
use transformer::Transformers;
|
||||||
use log::{LevelFilter, info, /* warn, */ error};
|
use log::{LevelFilter, info, /* warn, */ error};
|
||||||
use env_logger::Builder;
|
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";
|
||||||
|
|
||||||
@ -37,13 +35,6 @@ pub fn main() {
|
|||||||
|
|
||||||
|
|
||||||
fn run_all() -> LJResult<()> {
|
fn run_all() -> LJResult<()> {
|
||||||
// Setup handler for interrupt Signals
|
|
||||||
let running = Arc::new(AtomicBool::new(true));
|
|
||||||
let r = running.clone();
|
|
||||||
ctrlc::set_handler(move || {
|
|
||||||
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().nth(1).unwrap_or_else(|| {
|
||||||
DEFAULT_CONF_FILE.to_string()
|
DEFAULT_CONF_FILE.to_string()
|
||||||
@ -59,66 +50,62 @@ fn run_all() -> LJResult<()> {
|
|||||||
// Setup Redis Service
|
// Setup Redis Service
|
||||||
let mut rs = RedisCtrl::new(&config.redis_url, &config.laser_id)?;
|
let mut rs = RedisCtrl::new(&config.redis_url, &config.laser_id)?;
|
||||||
|
|
||||||
let mut world_state = rs.init_world_state().unwrap();
|
let mut world_state = rs.init_world_state()?;
|
||||||
info!("WorldState: {:?}", world_state);
|
|
||||||
|
|
||||||
|
// Setup handler for interrupt Signals
|
||||||
|
let running = Arc::new(AtomicBool::new(true));
|
||||||
|
let r = running.clone();
|
||||||
|
ctrlc::set_handler(move || {
|
||||||
|
r.store(false, Ordering::SeqCst);
|
||||||
|
})?;
|
||||||
|
|
||||||
// Setup Laser Device based on conf
|
// Setup Laser Device based on conf
|
||||||
let mut tracer = device_factory(&config)?;
|
let mut tracer = device_factory(&config)?;
|
||||||
world_state.grid = tracer.grid();
|
|
||||||
|
// can't work, but we can add + Debug to Device to make it work...
|
||||||
|
//dbg!(tracer);
|
||||||
|
|
||||||
// Setup geometry transformers on points lists
|
// Setup geometry transformers on points lists
|
||||||
let transformers = config.get_transformers();
|
let transformers = config.get_transformers();
|
||||||
|
|
||||||
// Setup framerate limiter
|
|
||||||
let mut framerate_handler = Framerate::new()?;
|
|
||||||
|
|
||||||
// Dispatch based on redis requests
|
// Dispatch based on redis requests
|
||||||
while running.load(Ordering::SeqCst) {
|
while running.load(Ordering::SeqCst) {
|
||||||
rs.set_status(tracer.status())?;
|
rs.set_status(tracer.status())?;
|
||||||
framerate_handler.handle_time()?;
|
|
||||||
let order = rs.get_order(config.laser_id)?;
|
let order = rs.get_order(config.laser_id)?;
|
||||||
|
// 0 : Draw Normal point list
|
||||||
|
// 2 : Draw BLACK point list
|
||||||
|
// 3 : Draw GRID point list
|
||||||
|
|
||||||
|
// /worldstate.rs
|
||||||
|
// /edh.rs
|
||||||
|
|
||||||
|
// 1 : Get the new EDH = reread redis key /EDH/lasernumber
|
||||||
|
// 4 : Resampler Change (longs and shorts lsteps)
|
||||||
|
// 5 : Client Key Change = reread redis key /clientkey
|
||||||
|
// 6 : Max Intensity Change = reread redis key /intensity
|
||||||
|
// 7 : kpps change = reread redis key /kpps
|
||||||
|
// 8 : color balance change = reread redis keys /red /green /blue
|
||||||
|
|
||||||
match order {
|
match order {
|
||||||
Order::Draw | Order::Black | Order::Grid => {
|
Order::Draw => {
|
||||||
// 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(
|
let frame = get_next_frame(
|
||||||
&config,
|
&config,
|
||||||
&transformers,
|
&transformers,
|
||||||
&mut rs,
|
&mut rs,
|
||||||
&world_state,
|
// order == Order::Black,
|
||||||
|
&world_state
|
||||||
)?;
|
)?;
|
||||||
// For now, draw all the time
|
// For now, draw all the time
|
||||||
tracer.draw(frame, world_state.kpps)?;
|
tracer.draw(frame, 2_000)?;
|
||||||
}
|
|
||||||
Order::Intensity => {
|
|
||||||
// 6 : Max Intensity Change = reread redis key /intensity
|
|
||||||
world_state.intensity = rs.get_int("intensity")?
|
|
||||||
.try_into()?;
|
|
||||||
}
|
}
|
||||||
Order::Edh => {
|
Order::Edh => {
|
||||||
// 1 : Get the new EDH = reread redis key /EDH/lasernumber
|
|
||||||
world_state.edh = rs.get_edh()?;
|
world_state.edh = rs.get_edh()?;
|
||||||
}
|
}
|
||||||
Order::Kpps => {
|
|
||||||
// 7 : kpps change = reread redis key /kpps
|
// Order::ClientKey => rs.client_key(),
|
||||||
world_state.kpps = rs.get_int("kpps")?;
|
// Order::ColorBalance => {},
|
||||||
}
|
|
||||||
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!("Order: {:?}", order);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,17 +118,14 @@ fn run_all() -> LJResult<()> {
|
|||||||
|
|
||||||
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 {
|
if config.debug {
|
||||||
LevelFilter::Debug
|
let mut builder = Builder::from_default_env();
|
||||||
} else {
|
builder
|
||||||
LevelFilter::Info
|
.filter(None, LevelFilter::Info)
|
||||||
};
|
.init();
|
||||||
let mut builder = Builder::from_default_env();
|
info!("Debug mode enabled from configuration file");
|
||||||
builder
|
return;
|
||||||
.filter(None, level)
|
}
|
||||||
.init();
|
|
||||||
info!("Debug mode enabled from configuration file");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
info!("Logging level inherited from env");
|
info!("Logging level inherited from env");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -151,49 +135,17 @@ 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 line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
|
||||||
world_state.client_key,
|
let mut line: Vec<Point> = line.into_iter()
|
||||||
config.laser_id);
|
.map(|tpl| tpl.into())
|
||||||
|
.collect();
|
||||||
// Handle the grid case
|
|
||||||
|
|
||||||
let mut line: Vec<Point> = if world_state.draw_grid {
|
|
||||||
world_state.grid.clone()
|
|
||||||
} else {
|
|
||||||
let redis_line = rs.get_line(&format_key)?;
|
|
||||||
redis_line.into_iter()
|
|
||||||
.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 Grid -> {}", world_state.draw_grid);
|
|
||||||
|
|
||||||
// LIMITER and BLACK
|
|
||||||
line = line.into_iter()
|
|
||||||
.map(|p| {
|
|
||||||
let color = if world_state.draw_black {
|
|
||||||
Color { r: 0, g: 0, b: 0 }
|
|
||||||
} else {
|
|
||||||
Color {
|
|
||||||
r: p.color.r.min(world_state.intensity),
|
|
||||||
g: p.color.g.min(world_state.intensity),
|
|
||||||
b: p.color.b.min(world_state.intensity),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Point {
|
|
||||||
color,
|
|
||||||
..p
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
//info!("Line: {:?}", line);
|
//info!("Line: {:?}", line);
|
||||||
Ok(line)
|
Ok(line)
|
||||||
}
|
}
|
||||||
|
91
src/point.rs
91
src/point.rs
@ -1,81 +1,34 @@
|
|||||||
use ether_dream::protocol::DacPoint;
|
#[derive(Debug,Clone,Copy,Default,PartialEq)]
|
||||||
use std::ops::Mul;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
|
||||||
pub struct Point {
|
pub struct Point {
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
pub y: f32,
|
pub y: f32,
|
||||||
pub color: Color,
|
pub color: Color
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
#[derive(Debug,Clone,Copy,Default,PartialEq)]
|
||||||
pub struct Color {
|
pub struct Color {
|
||||||
pub r: u8,
|
r: u8,
|
||||||
pub g: u8,
|
g: u8,
|
||||||
pub b: u8,
|
b: u8
|
||||||
}
|
}
|
||||||
impl Mul<u8> for Color {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn mul(self, rhs: u8) -> Self {
|
impl From<(f32,f32,u32)> for Point {
|
||||||
Self{
|
fn from((x, y, color) : (f32, f32, u32)) -> Point {
|
||||||
r: (255 * self.r as u16 / rhs as u16) as u8,
|
let r = (color >> 16) as u8 ;
|
||||||
g: (255 * self.g as u16 / rhs as u16) as u8,
|
let g = ((color >> 8) & 255) as u8 ;
|
||||||
b: (255 * self.b as u16 / rhs as u16) as u8,
|
let b = (color & 255) as u8 ;
|
||||||
}
|
Point { x, y, color: Color { r, g, b } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Color> for u32 {
|
|
||||||
fn from(value: Color) -> Self {
|
|
||||||
let r = value.r as u32;
|
|
||||||
let g = value.g as u32;
|
|
||||||
let b = (value.b) as u32;
|
|
||||||
(r << 16) + (g << 8) + b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<(f32, f32, u32)> for Point {
|
|
||||||
fn from((x, y, color): (f32, f32, u32)) -> Point {
|
|
||||||
let r = (color >> 16) as u8;
|
|
||||||
let g = ((color >> 8) & 255) as u8;
|
|
||||||
let b = (color & 255) as u8;
|
|
||||||
Point { x, y, color: Color { r, g, b } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Point> for helios_dac::Point {
|
impl From<Point> for helios_dac::Point {
|
||||||
fn from(pt: Point) -> helios_dac::Point {
|
fn from(pt: Point) -> helios_dac::Point {
|
||||||
let x = pt.x.clamp(0.0, 4095.0) as u16;
|
let x = pt.x.clamp(0.0, 4095.0) as u16;
|
||||||
let y = pt.y.clamp(0.0, 4095.0) as u16;
|
let y = pt.y.clamp(0.0, 4095.0) as u16;
|
||||||
helios_dac::Point {
|
helios_dac::Point {
|
||||||
coordinate: (x, y).into(),
|
coordinate: (x, y).into(),
|
||||||
color: helios_dac::Color::new(pt.color.r, pt.color.g, pt.color.b),
|
color: helios_dac::Color::new(pt.color.r, pt.color.g, pt.color.b),
|
||||||
intensity: 0xFF,
|
intensity: 0xFF
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Point> for DacPoint {
|
|
||||||
fn from(pt: Point) -> DacPoint {
|
|
||||||
let control = 0;
|
|
||||||
let (u1, u2) = (0, 0);
|
|
||||||
let i = 0;
|
|
||||||
let x = pt.x.clamp(-32000.0, 32000.0);
|
|
||||||
let y = pt.y.clamp(-32000.0, 32000.0);
|
|
||||||
let pt = DacPoint {
|
|
||||||
control,
|
|
||||||
x: x as i16,
|
|
||||||
y: y as i16,
|
|
||||||
i,
|
|
||||||
r: (pt.color.r as u16) * 255,
|
|
||||||
g: (pt.color.g as u16) * 255,
|
|
||||||
b: (pt.color.b as u16) * 255,
|
|
||||||
u1,
|
|
||||||
u2,
|
|
||||||
};
|
|
||||||
// debug!("point {:?}", pt);
|
|
||||||
pt
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,15 @@ use redis::{Client, Commands, Connection};
|
|||||||
use ron::de::from_str;
|
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 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,
|
||||||
|
//homography
|
||||||
Black,
|
Black,
|
||||||
Grid,
|
Grid,
|
||||||
Resampler,
|
Resampler,
|
||||||
@ -17,7 +18,6 @@ pub enum Order {
|
|||||||
Intensity,
|
Intensity,
|
||||||
Kpps,
|
Kpps,
|
||||||
ColorBalance,
|
ColorBalance,
|
||||||
PowerOff
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u8> for Order {
|
impl TryFrom<u8> for Order {
|
||||||
@ -40,14 +40,12 @@ impl TryFrom<u8> for Order {
|
|||||||
6 => Intensity,
|
6 => Intensity,
|
||||||
7 => Kpps,
|
7 => Kpps,
|
||||||
8 => ColorBalance,
|
8 => ColorBalance,
|
||||||
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 struct RedisCtrl {
|
pub struct RedisCtrl {
|
||||||
pub client: Client,
|
pub client: Client,
|
||||||
@ -64,7 +62,7 @@ impl RedisCtrl {
|
|||||||
Ok(RedisCtrl { client, connection, laser_id: *laser_id })
|
Ok(RedisCtrl { client, connection, laser_id: *laser_id })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_line(&mut self, key: &str) -> LJResult<Line> {
|
pub fn get(&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)
|
||||||
@ -86,6 +84,11 @@ impl RedisCtrl {
|
|||||||
Ok(val.try_into()?)
|
Ok(val.try_into()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
/lstt/lasernumber etherdream last_status.playback_state (0: idle 1: prepare 2: playing)
|
||||||
|
/cap/lasernumber number of empty points sent to fill etherdream buffer (up to 1799)
|
||||||
|
/lack/lasernumber "a": ACK "F": Full "I": invalid. 64 or 35 for no connection.
|
||||||
|
**/
|
||||||
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);
|
||||||
@ -96,48 +99,22 @@ impl RedisCtrl {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_world_state(&mut self) -> LJResult<WorldState> {
|
pub fn init_world_state( &mut self) -> LJResult<WorldState>{
|
||||||
Ok(WorldState {
|
let edh = self.get_edh()?;
|
||||||
client_key: self.get_client_key()?,
|
info!("EDH: {:?}", edh);
|
||||||
edh: self.get_edh()?,
|
|
||||||
kpps: self.get_int("kpps")?.try_into()?,
|
Ok(WorldState {
|
||||||
intensity: self.get_int("intensity")?.try_into()?,
|
edh,
|
||||||
..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> {
|
|
||||||
let key: String = self.connection.get("/clientkey")?;
|
|
||||||
Ok(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_color_balance(&mut self) -> LJResult<(u8, u8, u8)> {
|
|
||||||
Ok((
|
|
||||||
self.connection.get("/red")?,
|
|
||||||
self.connection.get("/green")?,
|
|
||||||
self.connection.get("/blue")?,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_resampler(&mut self ) -> LJResult<Resampler> {
|
|
||||||
let val: String = self.connection.get(format!("/resampler/{}", self.laser_id))?;
|
|
||||||
let resampler : Resampler = from_str(&val)?;
|
|
||||||
Ok(resampler)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_int(&mut self, key: &str) -> LJResult<u32> {
|
|
||||||
// Get new Int
|
|
||||||
let fmt = format!("/{key}/{}", self.laser_id);
|
|
||||||
let val: u32 = self.connection.get(fmt)?;
|
|
||||||
Ok(val)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
|
//pub mod common;
|
||||||
|
mod translate;
|
||||||
|
mod replicate;
|
||||||
|
mod rotate;
|
||||||
mod flip_horizontal;
|
mod flip_horizontal;
|
||||||
mod flip_vertical;
|
mod flip_vertical;
|
||||||
mod grid;
|
mod grid;
|
||||||
mod helios_to_etherdream;
|
|
||||||
mod homography;
|
mod homography;
|
||||||
mod intensity;
|
|
||||||
mod replicate;
|
|
||||||
mod rotate;
|
|
||||||
mod translate;
|
|
||||||
|
|
||||||
use crate::point::Point;
|
use crate::point::Point;
|
||||||
use crate::worldstate::WorldState;
|
use crate::worldstate::WorldState;
|
||||||
@ -19,8 +18,6 @@ pub use flip_horizontal::FlipHorizontal;
|
|||||||
pub use flip_vertical::FlipVertical;
|
pub use flip_vertical::FlipVertical;
|
||||||
pub use grid::Grid;
|
pub use grid::Grid;
|
||||||
pub use self::homography::Homography;
|
pub use self::homography::Homography;
|
||||||
pub use helios_to_etherdream::HeliosToEtherdream;
|
|
||||||
pub use intensity::Intensity;
|
|
||||||
|
|
||||||
pub trait Transformers {
|
pub trait Transformers {
|
||||||
fn apply(
|
fn apply(
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
use crate::transformer::Transformers;
|
|
||||||
use crate::point::Point;
|
|
||||||
use crate::worldstate::WorldState;
|
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
/// Converts helios Geometry to Helios
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
|
||||||
pub struct HeliosToEtherdream {
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Transformers for HeliosToEtherdream {
|
|
||||||
fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec<Point> {
|
|
||||||
// debug!("list helios {:?}", point_list);
|
|
||||||
let out = point_list.iter().map(|pt| {
|
|
||||||
Point {
|
|
||||||
x: 8.0 * (pt.x - 2047.0),
|
|
||||||
y: 8.0 * (pt.y - 2047.0),
|
|
||||||
..*pt
|
|
||||||
}
|
|
||||||
}).collect();
|
|
||||||
// debug!("list etherdream {:?}", out);
|
|
||||||
out
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
|||||||
use log::debug;
|
|
||||||
use crate::transformer::Transformers;
|
|
||||||
use crate::point::Point;
|
|
||||||
use crate::worldstate::WorldState;
|
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
/// Converts helios Geometry to Helios
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
|
||||||
pub struct Intensity {
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Transformers for Intensity {
|
|
||||||
fn apply(&self, point_list: &[Point], ws: &WorldState) -> Vec<Point> {
|
|
||||||
// debug!("list helios {:?}", point_list);
|
|
||||||
let out = point_list.iter().map(|pt| {
|
|
||||||
Point {
|
|
||||||
x: pt.x,
|
|
||||||
y: pt.y,
|
|
||||||
color: pt.color * ws.intensity
|
|
||||||
}
|
|
||||||
}).collect();
|
|
||||||
debug!("list intensity {:?}", out);
|
|
||||||
out
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,54 +1,51 @@
|
|||||||
use crate::point::{Point, Color};
|
use crate::point::{Point,Color};
|
||||||
use nalgebra::base::{Matrix3, Matrix1x3};
|
use nalgebra::base::{Matrix3,Matrix1x3};
|
||||||
use crate::errors::{LJError, LJResult};
|
use crate::errors::{LJError,LJResult};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use crate::redis_ctrl::Resampler;
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct EDH {
|
pub struct EDH {
|
||||||
pub matrix: Matrix3<f32>,
|
pub matrix: Matrix3<f32>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EDH {
|
impl EDH {
|
||||||
pub fn new(vec: Vec<Vec<f32>>) -> LJResult<EDH> {
|
pub fn new(vec: Vec<Vec<f32>>) -> LJResult<EDH> {
|
||||||
if vec.len() != 3 ||
|
if vec.len() != 3 ||
|
||||||
vec[0].len() != 3 ||
|
vec[0].len() != 3 ||
|
||||||
vec[1].len() != 3 ||
|
vec[1].len() != 3 ||
|
||||||
vec[2].len() != 3 {
|
vec[2].len() != 3 {
|
||||||
return Err(Box::new(LJError::BadEDH));
|
return Err(Box::new(LJError::BadEDH));
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is the matrix already transposed.
|
// this is the matrix already transposed.
|
||||||
let matrix = Matrix3::new(vec[0][0], vec[1][0], vec[2][0],
|
let matrix = Matrix3::new(vec[0][0], vec[1][0], vec[2][0],
|
||||||
vec[0][1], vec[1][1], vec[2][1],
|
vec[0][1], vec[1][1], vec[2][1],
|
||||||
vec[0][2], vec[1][2], vec[2][2]);
|
vec[0][2], vec[1][2], vec[2][2]);
|
||||||
|
|
||||||
Ok(EDH { matrix })
|
Ok(EDH { matrix })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply(&self, point: &Point) -> Point {
|
pub fn apply(&self, point: &Point) -> Point {
|
||||||
let p = Matrix1x3::new(point.x, point.y, 1.0);
|
let p = Matrix1x3::new(point.x, point.y, 1.0);
|
||||||
let p = p * self.matrix;
|
let p = p * self.matrix;
|
||||||
let new_p = Point { x: p[0] / p[2], y: p[1] / p[2], ..*point };
|
let new_p = Point { x: p[0] / p[2], y: p[1] / p[2], ..*point };
|
||||||
|
|
||||||
debug!("{:?} => {:?}", point, new_p);
|
debug!("{:?} => {:?}", point, new_p);
|
||||||
|
|
||||||
new_p
|
new_p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct WorldState {
|
pub struct WorldState {
|
||||||
pub edh: EDH,
|
pub edh: EDH,
|
||||||
pub resampler: Resampler,
|
pub resampler: Vec<f32>,
|
||||||
pub client_key: String,
|
pub client_key: u8,
|
||||||
pub intensity: u8,
|
pub intensity: u8,
|
||||||
pub kpps: u32,
|
pub kpps: u32,
|
||||||
pub color: Color,
|
pub color: Color
|
||||||
pub draw_black: bool,
|
|
||||||
pub draw_grid: bool,
|
|
||||||
pub grid: Vec<Point>,
|
|
||||||
pub color_balance: Color,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WorldState {}
|
impl WorldState {
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -12,4 +12,4 @@ redis_url = "redis://127.0.0.1:6379/"
|
|||||||
|
|
||||||
# For Etherdream. IP of the DAC
|
# For Etherdream. IP of the DAC
|
||||||
[dac.etherdream]
|
[dac.etherdream]
|
||||||
ip = "192.168.1.68"
|
url = "192.168.1.68"
|
||||||
|
@ -28,7 +28,7 @@ fn it_finds_struct_fields() {
|
|||||||
fn it_finds_etherdream_fields() {
|
fn it_finds_etherdream_fields() {
|
||||||
let config = Conf::new("tests/settings/valid_etherdream").unwrap();
|
let config = Conf::new("tests/settings/valid_etherdream").unwrap();
|
||||||
assert!(match config.dac {
|
assert!(match config.dac {
|
||||||
DacFamily::Etherdream(EtherDreamConf { ip }) if ip == "192.168.1.68" => true,
|
DacFamily::Etherdream(EtherDreamConf { url }) if url == "192.168.1.68" => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user