add fillUnstable pattern

This commit is contained in:
Tobias Ollive 2022-03-09 18:09:26 +01:00
parent 086e194a1d
commit 4451917154
2 changed files with 48 additions and 2 deletions

View File

@ -11,6 +11,7 @@ pub(crate) enum Patterns<const N: usize> {
ColorWipe(ColorWipe<N>),
Scanner(Scanner<N>),
FillRandom(FillRandom<N>),
FillUnstable(FillUnstable<N>)
}
impl<const N: usize> Iterator for Patterns<N> {
@ -23,6 +24,7 @@ impl<const N: usize> Iterator for Patterns<N> {
Patterns::ColorWipe(p) => p.next(),
Patterns::Scanner(p) => p.next(),
Patterns::FillRandom(p) => p.next(),
Patterns::FillUnstable(p) => p.next(),
}
}
}
@ -311,7 +313,7 @@ impl<const N: usize> Iterator for Scanner<N> {
/// # FillRandom
///
/// fill strip with color then apply random variation
/// fill strip with color then apply random variation to each pixel
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
pub struct FillRandom<const N: usize> {
pub(crate) color: PixelColor,
@ -356,3 +358,48 @@ impl<const N: usize> Iterator for FillRandom<N> {
Some(strip)
}
}
/// # FillUnstable
///
/// fill strip with color then apply same random variation to all pixel
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
pub struct FillUnstable<const N: usize> {
pub(crate) color: PixelColor,
pub(crate) stability: usize,
pub(crate) max_iteration: Option<usize>,
}
impl<const N: usize> Iterator for FillUnstable<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(iteration) = self.max_iteration {
if iteration == 0 {
return None;
} else {
self.max_iteration = Some(iteration - 1);
}
}
let mut strip = Strip::<N>::default();
let mut rng = rand::thread_rng();
let red_delta = rng.gen_range(0..self.stability) as u8;
let green_delta = rng.gen_range(0..self.stability) as u8;
let blue_delta = rng.gen_range(0..self.stability) as u8;
let operation = rng.gen_bool(0.5);
let red;
let green;
let blue;
if operation {
red = self.color.red.saturating_add(red_delta);
green = self.color.green.saturating_add(green_delta);
blue = self.color.blue.saturating_add(blue_delta);
} else {
red = self.color.red.saturating_sub(red_delta);
green = self.color.green.saturating_sub(green_delta);
blue = self.color.blue.saturating_sub(blue_delta);
}
strip.fill(PixelColor { red, green, blue });
Some(strip)
}
}

View File

@ -21,7 +21,6 @@ pub async fn runner_loop<const N: usize>(runner: DeviceSequence<N>, tx: Sender<S
for mut context in runner {
let mut strip = context.pattern.next().unwrap();
let delay = context.period;
println!("{:?}", delay);
while tx.send(strip).is_ok() {
if let Some(value) = context.pattern.next() {
strip = value;