Lerppppppp

Squashed commit of the following:

commit 11c776c2a9
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:23:08 2023 +0000

    update to have diff abs

commit 92fb1478b7
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:21:40 2023 +0000

    speed up

commit 5418a139f2
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:20:09 2023 +0000

    fix diff

commit f96fc9b48d
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:18:34 2023 +0000

    update check

commit 788b705c87
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:16:50 2023 +0000

    update check

commit 29efa6be83
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:14:41 2023 +0000

    change values to u128

commit 100f782aea
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:10:03 2023 +0000

    addjust for diff values

commit f31bc45947
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:07:51 2023 +0000

    account for 0 diff

commit b31c967ca0
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:05:17 2023 +0000

    update lerp functionn

commit 81930f35eb
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:03:51 2023 +0000

    update lerp functionn

commit cb9eb57a2c
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 23:00:06 2023 +0000

    update diff target

commit 0bce36f41d
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:58:46 2023 +0000

    update diff target

commit be35ecb310
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:57:28 2023 +0000

    fix render

commit 0a72146362
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:55:56 2023 +0000

    update render

commit 16ea28557e
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:52:45 2023 +0000

    update delta diff

commit 540af20174
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:51:15 2023 +0000

    update delta diff

commit 0fdba152d6
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:49:41 2023 +0000

    changed to render to f64

commit c49d3380b4
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:35:16 2023 +0000

    added delta

commit af2b8e55ae
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:28:55 2023 +0000

    init last if empty

commit 02f261a85a
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:23:05 2023 +0000

    added more logs to test

commit b6d0cca501
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:21:53 2023 +0000

    added more logs to test

commit 81b4de3690
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:19:48 2023 +0000

    added more logs to test

commit 8c4c736555
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:16:14 2023 +0000

    added more logs

commit d82df637e0
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:14:02 2023 +0000

    added logs

commit fbfbd232a9
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Wed Mar 15 22:02:56 2023 +0000

    finished lerp

commit 8e615a31d1
Author: Andre Henriques <andr3h3nriqu3s@gmail.com>
Date:   Tue Mar 14 23:25:15 2023 +0000

    started working
This commit is contained in:
Andre Henriques 2023-03-15 23:27:37 +00:00
parent 40a109637f
commit 5d98aa866b
3 changed files with 81 additions and 15 deletions

View File

@ -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);

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)]
@ -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))
}
}

View File

@ -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")]