finished lerp

This commit is contained in:
Andre Henriques 2023-03-15 22:02:56 +00:00
parent 8e615a31d1
commit fbfbd232a9

View File

@ -3,7 +3,7 @@ use ws281x_rpi::Ws2812Rpi;
use crate::{DBPool, animations::Animation}; use crate::{DBPool, animations::Animation};
use std::error::Error; use std::error::Error;
use smart_leds::{RGB8, SmartLedsWrite}; use smart_leds::{RGB8, SmartLedsWrite, RGB};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -277,17 +277,33 @@ impl Render {
if self.last.is_empty() || !self.last.clone().iter().eq(data.clone().iter()) { if self.last.is_empty() || !self.last.clone().iter().eq(data.clone().iter()) {
let lerp_data = data.iter().zip(self.last).map(|(d, l)| d - ((l - d) * 0.2)); let lerp_data: Vec<RGB<u8>> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l)).collect();
let err = self.ws.as_mut().unwrap().write(data.clone().into_iter()); let err = self.ws.as_mut().unwrap().write(lerp_data.clone().into_iter());
if err.is_err() || err.ok().is_none() { if err.is_err() || err.ok().is_none() {
println!("Failed to write data"); println!("Failed to write data");
} }
self.last = data; self.last = lerp_data;
} }
} }
} }
trait Lerp {
fn lerp(self, other: &Self) -> Self;
}
impl Lerp for u8 {
fn lerp(self, other: &Self) -> Self {
((self as f64) + (((*other as f64) - (self as f64)) * 0.2)) as u8
}
}
impl Lerp for RGB<u8> {
fn lerp(self, other: &Self) -> Self {
RGB::new(self.r.lerp(&other.r), self.g.lerp(&other.g), self.b.lerp(&other.b))
}
}