added delta

This commit is contained in:
Andre Henriques 2023-03-15 22:35:16 +00:00
parent af2b8e55ae
commit c49d3380b4
2 changed files with 20 additions and 13 deletions

View File

@ -11,7 +11,6 @@ mod utils;
use animations::Animation; use animations::Animation;
use r2d2::PooledConnection; use r2d2::PooledConnection;
use utils::ApiResponse; use utils::ApiResponse;
use render::Lerp;
use rocket::{State, serde::json::serde_json::json}; use rocket::{State, serde::json::serde_json::json};
@ -114,7 +113,6 @@ fn ligth_controll(
if ani.is_ok() { if ani.is_ok() {
println!("Added animation"); println!("Added animation");
println!("aaa {}", 0.lerp(&255));
//TODO //TODO
render.add_animation(ani.ok().unwrap()); render.add_animation(ani.ok().unwrap());
//ani.ok().unwrap(). //ani.ok().unwrap().
@ -126,10 +124,6 @@ fn ligth_controll(
render.remove_animation(ani.ok().unwrap()); render.remove_animation(ani.ok().unwrap());
} }
println!("aaa");
println!("aaa {}", 0.lerp(&255));
render.render(); render.render();
} }

View File

@ -41,6 +41,8 @@ pub struct RenderAnimation {
pub struct Render { pub struct Render {
conn: DBPool, conn: DBPool,
num_leds: u32, num_leds: u32,
last_render: u64,
ws: Option<Ws2812Rpi>, ws: Option<Ws2812Rpi>,
last: Vec<RGB8>, last: Vec<RGB8>,
@ -65,6 +67,7 @@ impl Render {
let mut render = Render { let mut render = Render {
conn, conn,
num_leds: 0, num_leds: 0,
last_render: 0,
ws: None, ws: None,
animations: vec![], animations: vec![],
local_led_config: vec![], local_led_config: vec![],
@ -158,7 +161,6 @@ impl Render {
pub fn add_animation(&mut self, animation: Animation) { pub fn add_animation(&mut self, animation: Animation) {
println!("Added animation"); println!("Added animation");
println!("aaa {}", 0.lerp(&255));
let mut key_frames = Vec::new(); 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(); 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<String> = Vec::new(); let mut to_clean: Vec<String> = Vec::new();
let animations = self.animations.clone().iter_mut().map(|animation| { let animations = self.animations.clone().iter_mut().map(|animation| {
@ -287,7 +297,7 @@ impl Render {
} }
let lerp_data: Vec<RGB<u8>> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l)).collect(); let lerp_data: Vec<RGB<u8>> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l, delta as f64)).collect();
println!("d:{:?}\n l:{:?}", data, lerp_data); println!("d:{:?}\n l:{:?}", data, lerp_data);
@ -304,18 +314,21 @@ impl Render {
} }
pub trait Lerp { pub trait Lerp {
fn lerp(self, other: &Self) -> Self; fn lerp(self, other: &Self, diff: f64) -> Self;
} }
impl Lerp for u8 { impl Lerp for u8 {
fn lerp(self, other: &Self) -> Self { fn lerp(self, other: &Self, diff: f64) -> Self {
((self as f64) + (((*other as f64) - (self as f64)) * 0.1)) as u8
let delta = diff / 1000.0;
((self as f64) + (((*other as f64) - (self as f64)) * 0.5 * delta)) as u8
} }
} }
impl Lerp for RGB<u8> { impl Lerp for RGB<u8> {
fn lerp(self, other: &Self) -> Self { fn lerp(self, other: &Self, diff: f64) -> Self {
RGB::new(self.r.lerp(&other.r), self.g.lerp(&other.g), self.b.lerp(&other.b)) RGB::new(self.r.lerp(&other.r, diff), self.g.lerp(&other.g, diff), self.b.lerp(&other.b, diff))
} }
} }