From 0fdba152d6402438cb893c115266e5d8b995a59b Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Wed, 15 Mar 2023 22:49:41 +0000 Subject: [PATCH] changed to render to f64 --- src/render.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/render.rs b/src/render.rs index 9c1e7d7..92dba7c 100644 --- a/src/render.rs +++ b/src/render.rs @@ -45,7 +45,7 @@ pub struct Render { last_render: u64, ws: Option, - last: Vec, + last: Vec>, animations: Vec, @@ -201,10 +201,14 @@ impl Render { self.animations = self.animations.clone().into_iter().filter(|ani| !ani.name.eq(&animation_name)).collect::>(); } - pub fn proccess_settings (&mut self, data: &mut Vec, settings: &Vec) { - fn load_led_into_light (data: &mut Vec, color: RGB8, start: usize, end: usize) { + pub fn proccess_settings (&mut self, data: &mut Vec>, settings: &Vec) { + fn load_led_into_light (data: &mut Vec>, color: RGB8, start: usize, end: usize) { for i in start..=end { - data[i] = color; + data[i] = RGB::new( + color.r as f64, + color.g as f64, + color.b as f64 + ); } } @@ -232,7 +236,7 @@ impl Render { if self.ws.is_none() { return; } - let mut data = vec![RGB8::default(); self.num_leds.try_into().unwrap()]; + let mut data: Vec> = vec![RGB::default(); self.num_leds.try_into().unwrap()]; let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs(); @@ -297,11 +301,13 @@ impl Render { } - let lerp_data: Vec> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l, delta as f64)).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); - let err = self.ws.as_mut().unwrap().write(lerp_data.clone().into_iter()); + let to_set_data: Vec> = rgbs_f64_to_u8(&lerp_data); + + let err = self.ws.as_mut().unwrap().write(to_set_data.into_iter()); if err.is_err() || err.ok().is_none() { println!("Failed to write data"); @@ -313,6 +319,16 @@ impl Render { } } +pub fn rgbs_f64_to_u8(data: &Vec>) -> Vec>{ + data.clone().iter().map(|a| { + RGB::new( + a.r as u8, + a.g as u8, + a.b as u8 + ) + }).collect() +} + pub trait Lerp { fn lerp(self, other: &Self, diff: f64) -> Self; } @@ -326,9 +342,21 @@ impl Lerp for u8 { } } +impl Lerp for f64 { + fn lerp(self, other: &Self, diff: f64) -> Self { + let delta = diff / 1000.0; + self + ((*other - self) * 0.5 * delta) + } +} + impl Lerp for RGB { 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)) } } +impl Lerp for RGB { + 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)) + } +}