rust_lum/src/patterns/fade_random.rs
2022-03-12 20:45:27 +01:00

61 lines
1.6 KiB
Rust

use crate::patterns::fade::Fade;
use crate::Strip;
use serde::{Serialize, Deserialize};
use crate::patterns::Pattern;
/// # Blink
///
/// fade from one color to an other then go back several times
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
pub struct FadeRandom<const N: usize> {
#[serde(default)]
pub(crate) current_iteration: usize,
pub(crate) max_iteration: Option<usize>,
pub(crate) stability: usize,
fade: Fade<N>,
}
impl<const N: usize> Pattern for FadeRandom<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
None
}
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => { false }
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
}
impl<const N: usize> Iterator for FadeRandom<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
let strip = self.fade.next();
match strip {
None => { None }
Some(mut fade) => {
let rng = rand::thread_rng();
for i in 0..N {
match fade[i] {
None => { }
Some(pixel) => {
fade[i] = Some(pixel.random_delta(self.stability, &mut rng.clone()))
}
}
}
Some(fade)
}
}
}
}