Lerppppppp
Squashed commit of the following: commit11c776c2a9Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:23:08 2023 +0000 update to have diff abs commit92fb1478b7Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:21:40 2023 +0000 speed up commit5418a139f2Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:20:09 2023 +0000 fix diff commitf96fc9b48dAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:18:34 2023 +0000 update check commit788b705c87Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:16:50 2023 +0000 update check commit29efa6be83Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:14:41 2023 +0000 change values to u128 commit100f782aeaAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:10:03 2023 +0000 addjust for diff values commitf31bc45947Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:07:51 2023 +0000 account for 0 diff commitb31c967ca0Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:05:17 2023 +0000 update lerp functionn commit81930f35ebAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:03:51 2023 +0000 update lerp functionn commitcb9eb57a2cAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 23:00:06 2023 +0000 update diff target commit0bce36f41dAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:58:46 2023 +0000 update diff target commitbe35ecb310Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:57:28 2023 +0000 fix render commit0a72146362Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:55:56 2023 +0000 update render commit16ea28557eAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:52:45 2023 +0000 update delta diff commit540af20174Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:51:15 2023 +0000 update delta diff commit0fdba152d6Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:49:41 2023 +0000 changed to render to f64 commitc49d3380b4Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:35:16 2023 +0000 added delta commitaf2b8e55aeAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:28:55 2023 +0000 init last if empty commit02f261a85aAuthor: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:23:05 2023 +0000 added more logs to test commitb6d0cca501Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:21:53 2023 +0000 added more logs to test commit81b4de3690Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:19:48 2023 +0000 added more logs to test commit8c4c736555Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:16:14 2023 +0000 added more logs commitd82df637e0Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:14:02 2023 +0000 added logs commitfbfbd232a9Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Wed Mar 15 22:02:56 2023 +0000 finished lerp commit8e615a31d1Author: Andre Henriques <andr3h3nriqu3s@gmail.com> Date: Tue Mar 14 23:25:15 2023 +0000 started working
This commit is contained in:
		
							parent
							
								
									40a109637f
								
							
						
					
					
						commit
						5d98aa866b
					
				@ -301,7 +301,7 @@ pub fn get_animation(name: &str, db: &State<DBPool>) -> Result<Option<Animation>
 | 
				
			|||||||
    struct AnimationId {
 | 
					    struct AnimationId {
 | 
				
			||||||
        animation: Animation,
 | 
					        animation: Animation,
 | 
				
			||||||
        id: u8
 | 
					        id: u8
 | 
				
			||||||
    };
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let next = stmt.query_map([name], |row| {
 | 
					    let next = stmt.query_map([name], |row| {
 | 
				
			||||||
        Ok(AnimationId {
 | 
					        Ok(AnimationId {
 | 
				
			||||||
@ -370,7 +370,7 @@ pub fn get_animation(name: &str, db: &State<DBPool>) -> Result<Option<Animation>
 | 
				
			|||||||
 * 
 | 
					 * 
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#[get("/delete/<name>")]
 | 
					#[get("/delete/<name>")]
 | 
				
			||||||
pub async fn delete(name: &str, start_sender: &State<ASender<Animation>>, db: &State<DBPool>) -> String {
 | 
					pub async fn delete(name: &str, db: &State<DBPool>) -> String {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let animation = remove_animation(name, db);
 | 
					    let animation = remove_animation(name, db);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -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)]
 | 
				
			||||||
@ -23,7 +23,7 @@ pub struct LigthSetting {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[derive(Clone)]
 | 
					#[derive(Clone)]
 | 
				
			||||||
pub struct KeyFrame {
 | 
					pub struct KeyFrame {
 | 
				
			||||||
    duration: u64,
 | 
					    duration: u128,
 | 
				
			||||||
    settings: Vec<LigthSetting>
 | 
					    settings: Vec<LigthSetting>
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -34,16 +34,18 @@ pub struct RenderAnimation {
 | 
				
			|||||||
    name: String,
 | 
					    name: String,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    current_frame: usize,
 | 
					    current_frame: usize,
 | 
				
			||||||
    start_time: u64,
 | 
					    start_time: u128,
 | 
				
			||||||
    reset: bool,
 | 
					    reset: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Render {
 | 
					pub struct Render {
 | 
				
			||||||
    conn: DBPool,
 | 
					    conn: DBPool,
 | 
				
			||||||
    num_leds: u32,
 | 
					    num_leds: u32,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    last_render: u128,
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    ws: Option<Ws2812Rpi>,
 | 
					    ws: Option<Ws2812Rpi>,
 | 
				
			||||||
    last: Vec<RGB8>,
 | 
					    last: Vec<RGB<f64>>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    animations: Vec<RenderAnimation>,
 | 
					    animations: Vec<RenderAnimation>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -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![],
 | 
				
			||||||
@ -173,7 +176,7 @@ impl Render {
 | 
				
			|||||||
                });
 | 
					                });
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            key_frames.push(KeyFrame { duration: frame.get_duration().into(), settings: light_settings })
 | 
					            key_frames.push(KeyFrame { duration: (frame.get_duration() as u128) * (1000 as u128), settings: light_settings })
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        let new_animation = RenderAnimation {
 | 
					        let new_animation = RenderAnimation {
 | 
				
			||||||
@ -198,10 +201,14 @@ impl Render {
 | 
				
			|||||||
        self.animations = self.animations.clone().into_iter().filter(|ani| !ani.name.eq(&animation_name)).collect::<Vec<RenderAnimation>>();
 | 
					        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>) {
 | 
					    pub fn proccess_settings (&mut self, data: &mut Vec<RGB<f64>>, settings: &Vec<LigthSetting>) {
 | 
				
			||||||
        fn load_led_into_light (data: &mut Vec<RGB8>, color: RGB8, start: usize, end: usize) {
 | 
					        fn load_led_into_light (data: &mut Vec<RGB<f64>>, color: RGB8, start: usize, end: usize) {
 | 
				
			||||||
            for i in start..=end {
 | 
					            for i in start..=end {
 | 
				
			||||||
                data[i] = color;
 | 
					                data[i] = RGB::new(
 | 
				
			||||||
 | 
					                    color.r as f64, 
 | 
				
			||||||
 | 
					                    color.g as f64, 
 | 
				
			||||||
 | 
					                    color.b as f64
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -227,9 +234,17 @@ impl Render {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if self.ws.is_none() { return; }
 | 
					        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();
 | 
					        let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_millis();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        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();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -276,15 +291,66 @@ impl Render {
 | 
				
			|||||||
        to_clean.iter().for_each(|i| self.remove_animation(i.to_string()));
 | 
					        to_clean.iter().for_each(|i| self.remove_animation(i.to_string()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        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 err = self.ws.as_mut().unwrap().write(data.clone().into_iter());
 | 
					
 | 
				
			||||||
 | 
					            if self.last.len() == 0 {
 | 
				
			||||||
 | 
					                self.last = data.clone();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            let lerp_data: Vec<RGB<f64>> = data.iter().zip(&self.last).map(|(d, l)| l.lerp(d, delta as f64)).collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //println!("d:{:?}\n l:{:?}", data, lerp_data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            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() {
 | 
					            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;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Lerp for f64 {
 | 
				
			||||||
 | 
					    fn lerp(self, other: &Self, diff: f64) -> Self {
 | 
				
			||||||
 | 
					        let delta = diff / 1000.0;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        let change = (*other - self) * 0.8 * delta;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let lerped = self + change;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if *other != self && diff != 0.0 {
 | 
				
			||||||
 | 
					            println!("c: {} target:{} lerp: {} move: {}", self, other, lerped, change);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (*other - self).abs() < 2.0 && diff != 0.0 {
 | 
				
			||||||
 | 
					            return *other;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        lerped
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,4 @@
 | 
				
			|||||||
use rocket::{serde::{Deserialize, Serialize}, Responder, http::{ContentType, Header}};
 | 
					use rocket::{serde::{Deserialize, Serialize}, Responder};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Deserialize, Serialize)]
 | 
					#[derive(Deserialize, Serialize)]
 | 
				
			||||||
#[serde(crate="rocket::serde")]
 | 
					#[serde(crate="rocket::serde")]
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user