55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use crate::patterns::fade::Fade;
|
|
use crate::patterns::Pattern;
|
|
use crate::Strip;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// # Blink
|
|
///
|
|
/// fade from one color to an other then go back several times
|
|
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
|
|
pub struct FadeUnstable<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 FadeUnstable<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 FadeUnstable<N> {
|
|
type Item = Strip<N>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
let strip = self.fade.next();
|
|
match strip {
|
|
None => None,
|
|
Some(fade) => {
|
|
let mut rng = rand::thread_rng();
|
|
let pixel = fade[0].unwrap().random_delta(self.stability, &mut rng);
|
|
|
|
Some(Strip::new(pixel))
|
|
}
|
|
}
|
|
}
|
|
}
|