use crate::patterns::Pattern; use crate::pixel_color::PixelColor; use crate::{impl_pattern_init_background, impl_pattern_last_iteration, Strip}; use serde::{Deserialize, Serialize}; /// # RingScanner /// /// display a ring on the strip fading for the previous ones #[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)] pub struct RingScanner { #[serde(default)] pub(crate) current_iteration: usize, pub(crate) max_iteration: Option, pub(crate) color: PixelColor, pub(crate) background_color: Option, pub(crate) ring_size: usize, } impl Pattern for RingScanner { type Strip = Strip; impl_pattern_init_background!(N); impl_pattern_last_iteration!(ColorWipe); } impl Iterator for RingScanner { type Item = Strip; fn next(&mut self) -> Option { if self.is_last_iteration() { return None; } let mut strip: Strip; if let Some(color) = self.background_color { strip = Strip::new(color); } else { strip = Strip::default(); } let nbr_iter = N / self.ring_size; let iter = self.current_iteration % nbr_iter; for j in 0..iter { let first_index = j * self.ring_size; let last_index = first_index + self.ring_size; let fade_gain = 2 * (iter - j); for i in first_index..last_index { strip[i] = Some(self.color / fade_gain as u8); } } self.current_iteration += 1; Some(strip) } }