changed to render to f64

This commit is contained in:
Andre Henriques 2023-03-15 22:49:41 +00:00
parent c49d3380b4
commit 0fdba152d6

View File

@ -45,7 +45,7 @@ pub struct Render {
last_render: u64,
ws: Option<Ws2812Rpi>,
last: Vec<RGB8>,
last: Vec<RGB<f64>>,
animations: Vec<RenderAnimation>,
@ -201,10 +201,14 @@ impl Render {
self.animations = self.animations.clone().into_iter().filter(|ani| !ani.name.eq(&animation_name)).collect::<Vec<RenderAnimation>>();
}
pub fn proccess_settings (&mut self, data: &mut Vec<RGB8>, settings: &Vec<LigthSetting>) {
fn load_led_into_light (data: &mut Vec<RGB8>, color: RGB8, start: usize, end: usize) {
pub fn proccess_settings (&mut self, data: &mut Vec<RGB<f64>>, settings: &Vec<LigthSetting>) {
fn load_led_into_light (data: &mut Vec<RGB<f64>>, 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<RGB<f64>> = 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<RGB<u8>> = data.iter().zip(&self.last).map(|(d, l)| d.lerp(l, delta as f64)).collect();
let lerp_data: Vec<RGB<f64>> = 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<RGB<u8>> = 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<RGB<f64>>) -> Vec<RGB<u8>>{
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<u8> {
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<f64> {
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))
}
}