lights/src/render.rs
Andre Henriques 5d98aa866b 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
2023-03-15 23:27:37 +00:00

357 lines
10 KiB
Rust

use ws281x_rpi::Ws2812Rpi;
use crate::{DBPool, animations::Animation};
use std::error::Error;
use smart_leds::{RGB8, SmartLedsWrite, RGB};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone, Debug)]
struct LocalLedConfiguration {
tag: Option<String>,
start: usize,
end: usize
}
#[derive(Clone)]
pub struct LigthSetting {
start: Option<usize>,
end: Option<usize>,
tags: Option<String>,
color: RGB8,
}
#[derive(Clone)]
pub struct KeyFrame {
duration: u128,
settings: Vec<LigthSetting>
}
#[derive(Clone)]
pub struct RenderAnimation {
key_frames: Vec<KeyFrame>,
priority: u32,
name: String,
current_frame: usize,
start_time: u128,
reset: bool,
}
pub struct Render {
conn: DBPool,
num_leds: u32,
last_render: u128,
ws: Option<Ws2812Rpi>,
last: Vec<RGB<f64>>,
animations: Vec<RenderAnimation>,
local_led_config: Vec<LocalLedConfiguration>,
}
fn try_into_usize (input: Option<u32>) -> Option<usize> {
if input.is_none() {return None;}
let t: Result<usize, _> = input.unwrap().try_into();
if t.is_err() {return None;}
return t.ok();
}
impl Render {
pub fn new(conn: DBPool) -> Result<Render, Box<dyn Error>> {
let mut render = Render {
conn,
num_leds: 0,
last_render: 0,
ws: None,
animations: vec![],
local_led_config: vec![],
last: vec![],
};
render.load_config()?;
return Ok(render);
}
pub fn load_config(&mut self) -> Result<(), Box<dyn Error>> {
println!("Load config");
// GPIO Pin 10 is SPI
// Other modes and PINs are available depending on the Raspberry Pi revision
// Additional OS configuration might be needed for any mode.
// Check https://github.com/jgarff/rpi_ws281x for more information.
const PIN: i32 = 18;
let conn = self.conn.get()?;
let stmt = conn.prepare("SELECT ledcount, tags from configuration;");
if stmt.is_err() || stmt.as_ref().ok().is_none() {
Err(format!("Something went wrong while reading the database {:?}", stmt.as_ref().err()))?;
}
let mut stmt = stmt?;
struct Data {
ledcount: u32,
tag: String
}
let version_iter = stmt.query_map([], |row| {
Ok(Data {
ledcount: row.get(0)?,
tag: row.get(1)?
})
});
if version_iter.is_err() || version_iter.as_ref().ok().is_none() {
Err(format!("Something went wrong while reading the database {:?}", version_iter.as_ref().err()))?;
}
let version_iter = version_iter.ok().unwrap();
let mut current_pos = 0;
let mut local_led_config = Vec::new();
for data in version_iter {
let data = data?;
let tag = if data.tag.is_empty() { None } else { Some(data.tag) };
let start = current_pos;
let end = current_pos + data.ledcount;
current_pos = end;
let start: Result<usize, _> = start.try_into();
let end: Result<usize, _> = end.try_into();
if start.is_err() || start.ok().is_none() || end.is_err() || end.ok().is_none() {
println!("Skiping loading configuration");
continue;
}
let start = start.ok().unwrap();
let end = end.ok().unwrap();
local_led_config.push(LocalLedConfiguration {
tag,
start,
end
});
}
self.num_leds = current_pos;
println!("Loaded: {} leds, with config: {:?}", self.num_leds, local_led_config);
self.local_led_config = local_led_config;
self.ws = Some(Ws2812Rpi::new(current_pos as i32, PIN).unwrap());
println!("Here");
Ok(())
}
pub fn add_animation(&mut self, animation: Animation) {
println!("Added animation");
let mut key_frames = Vec::new();
for frame in animation.get_frames().iter() {
let mut light_settings = Vec::new();
for setting in frame.get_settings() {
light_settings.push(LigthSetting {
tags: setting.get_tags(),
start: try_into_usize(setting.get_start()),
end: try_into_usize(setting.get_end()),
color: RGB8::new(setting.get_r(), setting.get_g(), setting.get_b()),
});
}
key_frames.push(KeyFrame { duration: (frame.get_duration() as u128) * (1000 as u128), settings: light_settings })
}
let new_animation = RenderAnimation {
key_frames,
priority: animation.get_priority(),
name: animation.get_name(),
current_frame: 0,
start_time: 0,
reset: animation.get_repeat(),
};
self.animations.push(new_animation);
self.animations.sort_by(|a,b| a.priority.partial_cmp(&b.priority).unwrap())
}
pub fn blank(&mut self) {
self.animations = Vec::new();
}
pub fn remove_animation(&mut self, animation_name: String) {
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<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] = RGB::new(
color.r as f64,
color.g as f64,
color.b as f64
);
}
}
for setting in settings {
if setting.tags.is_some() {
let tags = setting.tags.as_ref().unwrap().to_string();
let tags = tags.split(",");
let tags_fn = |t: String| tags.clone().into_iter().any(|tag| tag.eq(&t));
for config in self.local_led_config.clone() {
if config.tag.is_some() && tags_fn(config.tag.unwrap()) {
load_led_into_light(data, setting.color, config.start, config.end);
}
}
}
if setting.start.is_some() && setting.end.is_some() {
load_led_into_light(data, setting.color, setting.start.unwrap(), setting.end.unwrap());
}
}
}
pub fn render(&mut self) {
if self.ws.is_none() { return; }
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_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 animations = self.animations.clone().iter_mut().map(|animation| {
let mut animation = animation.to_owned();
let mut start_time = animation.start_time;
if start_time == 0 {
animation.start_time = time;
animation.current_frame = 0;
self.proccess_settings(&mut data, &animation.key_frames[0].settings);
return animation;
}
while start_time + animation.key_frames[animation.current_frame].duration < time {
start_time += animation.key_frames[animation.current_frame].duration;
animation.start_time = start_time;
if animation.current_frame + 1 >= animation.key_frames.len() {
if animation.reset {
animation.start_time = time;
animation.current_frame = 0;
self.proccess_settings(&mut data, &animation.key_frames[0].settings);
} else {
to_clean.push(animation.name.to_string());
}
return animation;
}
animation.current_frame += 1;
}
let cf = animation.current_frame;
self.proccess_settings(&mut data, &animation.key_frames[cf].settings);
return animation;
}).collect();
self.animations = animations;
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.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() {
println!("Failed to write 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))
}
}