From c49d3380b49e13d6be5c832679077b98b66c9ac7 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Wed, 15 Mar 2023 22:35:16 +0000 Subject: [PATCH] added delta --- src/main.rs | 6 ------ src/render.rs | 27 ++++++++++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 44e235d..904c5d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,6 @@ mod utils; use animations::Animation; use r2d2::PooledConnection; use utils::ApiResponse; -use render::Lerp; use rocket::{State, serde::json::serde_json::json}; @@ -114,7 +113,6 @@ fn ligth_controll( if ani.is_ok() { println!("Added animation"); - println!("aaa {}", 0.lerp(&255)); //TODO render.add_animation(ani.ok().unwrap()); //ani.ok().unwrap(). @@ -126,10 +124,6 @@ fn ligth_controll( render.remove_animation(ani.ok().unwrap()); } - println!("aaa"); - - println!("aaa {}", 0.lerp(&255)); - render.render(); } diff --git a/src/render.rs b/src/render.rs index e86f1b0..9c1e7d7 100644 --- a/src/render.rs +++ b/src/render.rs @@ -41,6 +41,8 @@ pub struct RenderAnimation { pub struct Render { conn: DBPool, num_leds: u32, + + last_render: u64, ws: Option, last: Vec, @@ -65,6 +67,7 @@ impl Render { let mut render = Render { conn, num_leds: 0, + last_render: 0, ws: None, animations: vec![], local_led_config: vec![], @@ -158,7 +161,6 @@ impl Render { pub fn add_animation(&mut self, animation: Animation) { println!("Added animation"); - println!("aaa {}", 0.lerp(&255)); let mut key_frames = Vec::new(); @@ -234,6 +236,14 @@ impl Render { let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs(); + if self.last_render == 0 { + self.last_render = time; + } + + let delta = time - self.last_render; + + self.last_render = time; + let mut to_clean: Vec = Vec::new(); let animations = self.animations.clone().iter_mut().map(|animation| { @@ -287,7 +297,7 @@ impl Render { } - let lerp_data: Vec> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l)).collect(); + let lerp_data: Vec> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l, delta as f64)).collect(); println!("d:{:?}\n l:{:?}", data, lerp_data); @@ -304,18 +314,21 @@ impl Render { } pub trait Lerp { - fn lerp(self, other: &Self) -> Self; + fn lerp(self, other: &Self, diff: f64) -> Self; } impl Lerp for u8 { - fn lerp(self, other: &Self) -> Self { - ((self as f64) + (((*other as f64) - (self as f64)) * 0.1)) as u8 + fn lerp(self, other: &Self, diff: f64) -> Self { + + let delta = diff / 1000.0; + + ((self as f64) + (((*other as f64) - (self as f64)) * 0.5 * delta)) as u8 } } impl Lerp for RGB { - fn lerp(self, other: &Self) -> Self { - RGB::new(self.r.lerp(&other.r), self.g.lerp(&other.g), self.b.lerp(&other.b)) + fn lerp(self, other: &Self, diff: f64) -> Self { + RGB::new(self.r.lerp(&other.r, diff), self.g.lerp(&other.g, diff), self.b.lerp(&other.b, diff)) } }