feat: add grid

This commit is contained in:
alban 2023-07-09 22:16:01 +02:00
parent 69b619dbf5
commit e4580e0e53
7 changed files with 164 additions and 145 deletions

View File

@ -9,6 +9,7 @@ use crate::device::dummy::DummyDevice;
use crate::errors::LJResult;
use crate::point::Point;
use serde::Serialize;
use crate::redis_ctrl::Line;
/*
self.protocol_version,
@ -56,6 +57,7 @@ pub trait Device {
speed: u32,
) -> LJResult<()>;
fn stop(&mut self) -> LJResult<()>;
fn grid(&mut self) -> Vec<Point>;
}
pub fn device_factory(config: &Conf) -> LJResult<Box<dyn Device>> {

View File

@ -1,31 +0,0 @@
/*
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;
}

View File

@ -1,6 +1,6 @@
use crate::device::{Device, Status, PlaybackState};
use crate::errors::LJResult;
use crate::point::Point;
use crate::point::{Color, Point};
use log::debug;
pub struct DummyDevice {
@ -35,4 +35,9 @@ impl Device for DummyDevice {
fn stop(&mut self) -> LJResult<()> {
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 }}
)
}
}

View File

@ -13,8 +13,9 @@ use helios_dac::{
use crate::conf::HeliosConf;
use crate::device::{Device, Status, PlaybackState};
use crate::errors::{LJError, LJResult};
use crate::point::Point;
use crate::point::{Color, Point};
use chrono::Utc;
use crate::redis_ctrl::Line;
pub struct HeliosDevice {
pub conf: HeliosConf,
@ -22,7 +23,7 @@ pub struct HeliosDevice {
sent_points: u16,
state: PlaybackState,
lack: String,
last_traced_at : String
last_traced_at: String,
}
impl HeliosDevice {
@ -40,17 +41,16 @@ impl HeliosDevice {
sent_points: 0,
state: PlaybackState::PREPARE,
lack: "".to_string(),
last_traced_at: "1985-04-12T23:20:50.52Z".to_string()
last_traced_at: "1985-04-12T23:20:50.52Z".to_string(),
})
}
}
impl Device for HeliosDevice {
fn status(&self) -> Status {
let lack = self.lack.clone();
Status {
last_traced_at: self.last_traced_at.clone(),
last_traced_at: self.last_traced_at.clone(),
properties: vec!["foo".to_string()],
playback_state: self.state,
capacity: self.sent_points,
@ -78,4 +78,27 @@ impl Device for HeliosDevice {
self.dac.stop()?;
Ok(())
}
fn grid(&mut self) -> Vec<Point> {
let dim_min = 0 as f32;
let dim_mid = 2047 as f32;
let dim_max = 4095 as f32;
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 },
]
}
}

View File

@ -13,10 +13,10 @@ mod worldstate;
use device::device_factory;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use redis_ctrl::{RedisCtrl, Order};
use redis_ctrl::{RedisCtrl, Order, Line};
use conf::Conf;
use errors::LJResult;
use point::{Point,Color};
use point::{Point, Color};
use transformer::Transformers;
use log::{LevelFilter, info, /* warn, */ error};
use env_logger::Builder;
@ -51,8 +51,8 @@ fn run_all() -> LJResult<()> {
let mut rs = RedisCtrl::new(&config.redis_url, &config.laser_id)?;
let mut world_state = rs.init_world_state()?;
info!("WorldState: {:?}", world_state);
info!("WorldState: {:?}", world_state);
// Setup handler for interrupt Signals
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
@ -62,6 +62,7 @@ fn run_all() -> LJResult<()> {
// Setup Laser Device based on conf
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);
@ -78,8 +79,6 @@ fn run_all() -> LJResult<()> {
// 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)
@ -88,28 +87,29 @@ fn run_all() -> LJResult<()> {
// 7 : kpps change = reread redis key /kpps
// 8 : color balance change = reread redis keys /red /green /blue
match order {
Order::Draw | Order::Black => {
world_state.black = order == Order::Black;
Order::Draw | Order::Black | Order::Grid => {
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
&mut rs,
&world_state,
)?;
// For now, draw all the time
tracer.draw(frame, world_state.kpps)?;
},
Order::Intensity => {
world_state.intensity = rs.get_int("intensity")?
.try_into()?;
},
}
Order::Intensity => {
world_state.intensity = rs.get_int("intensity")?
.try_into()?;
}
Order::Edh => {
world_state.edh = rs.get_edh()?;
},
}
Order::Kpps => {
world_state.kpps = rs.get_int("kpps")?;
},
}
// Order::ClientKey => rs.client_key(),
// Order::ColorBalance => {},
@ -143,42 +143,52 @@ fn get_next_frame(
config: &Conf,
transformers: &[Box<dyn Transformers>],
rs: &mut RedisCtrl,
world_state : &WorldState
world_state: &WorldState,
) -> LJResult<Vec<Point>> {
let format_key = format!("{}{}",
world_state.client_key,
config.laser_id);
let line = rs.get_line(&format_key)?;
let mut line: Vec<Point> = line.into_iter()
.map(|tpl| tpl.into())
.collect();
for transformer in transformers {
line = transformer.apply(&line, world_state);
}
let format_key = format!("{}{}",
world_state.client_key,
config.laser_id);
info!("-> {}", world_state.black);
// LIMITER and BLACK
line = line.into_iter()
.map(| p | {
let color = if world_state.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);
Ok(line)
// Handle the grid case
let mut line : Vec<Point>;
if world_state.draw_grid {
line = world_state.grid.clone();
} else {
let redis_line = rs.get_line(&format_key)?;
line = redis_line.into_iter()
.map(|tpl| tpl.into())
.collect();
};
for transformer in transformers {
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);
Ok(line)
}

View File

@ -1,34 +1,43 @@
#[derive(Debug,Clone,Copy,Default,PartialEq)]
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
pub color: Color
pub x: f32,
pub y: f32,
pub color: Color,
}
#[derive(Debug,Clone,Copy,Default,PartialEq)]
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8
pub r: u8,
pub g: u8,
pub b: u8,
}
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<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 {
fn from(pt: Point) -> helios_dac::Point {
let x = pt.x.clamp(0.0, 4095.0) as u16;
let y = pt.y.clamp(0.0, 4095.0) as u16;
helios_dac::Point {
coordinate: (x, y).into(),
color: helios_dac::Color::new(pt.color.r, pt.color.g, pt.color.b),
intensity: 0xFF
fn from(pt: Point) -> helios_dac::Point {
let x = pt.x.clamp(0.0, 4095.0) as u16;
let y = pt.y.clamp(0.0, 4095.0) as u16;
helios_dac::Point {
coordinate: (x, y).into(),
color: helios_dac::Color::new(pt.color.r, pt.color.g, pt.color.b),
intensity: 0xFF,
}
}
}
}

View File

@ -1,52 +1,53 @@
use crate::point::{Point,Color};
use nalgebra::base::{Matrix3,Matrix1x3};
use crate::errors::{LJError,LJResult};
use crate::point::{Point, Color};
use nalgebra::base::{Matrix3, Matrix1x3};
use crate::errors::{LJError, LJResult};
use log::debug;
use crate::redis_ctrl::Line;
#[derive(Debug, Default)]
pub struct EDH {
pub matrix: Matrix3<f32>
pub matrix: Matrix3<f32>,
}
impl EDH {
pub fn new(vec: Vec<Vec<f32>>) -> LJResult<EDH> {
if vec.len() != 3 ||
vec[0].len() != 3 ||
vec[1].len() != 3 ||
vec[2].len() != 3 {
return Err(Box::new(LJError::BadEDH));
}
pub fn new(vec: Vec<Vec<f32>>) -> LJResult<EDH> {
if vec.len() != 3 ||
vec[0].len() != 3 ||
vec[1].len() != 3 ||
vec[2].len() != 3 {
return Err(Box::new(LJError::BadEDH));
}
// this is the matrix already transposed.
let matrix = Matrix3::new(vec[0][0], vec[1][0], vec[2][0],
vec[0][1], vec[1][1], vec[2][1],
vec[0][2], vec[1][2], vec[2][2]);
// this is the matrix already transposed.
let matrix = Matrix3::new(vec[0][0], vec[1][0], vec[2][0],
vec[0][1], vec[1][1], vec[2][1],
vec[0][2], vec[1][2], vec[2][2]);
Ok(EDH { matrix })
}
Ok(EDH { matrix })
}
pub fn apply(&self, point: &Point) -> Point {
let p = Matrix1x3::new(point.x, point.y, 1.0);
let p = p * self.matrix;
let new_p = Point { x: p[0] / p[2], y: p[1] / p[2], ..*point };
debug!("{:?} => {:?}", point, new_p);
new_p
}
pub fn apply(&self, point: &Point) -> Point {
let p = Matrix1x3::new(point.x, point.y, 1.0);
let p = p * self.matrix;
let new_p = Point { x: p[0] / p[2], y: p[1] / p[2], ..*point };
debug!("{:?} => {:?}", point, new_p);
new_p
}
}
#[derive(Debug, Default)]
pub struct WorldState {
pub edh: EDH,
pub resampler: Vec<f32>,
pub client_key: String, //u8,
pub client_key: String,
pub intensity: u8,
pub kpps: u32,
pub color: Color,
pub black: bool
pub color: Color,
pub draw_black: bool,
pub draw_grid: bool,
pub grid: Vec<Point>,
}
impl WorldState {
}
impl WorldState {}