use crate::color::Rgb; use serde::{Deserialize, Serialize}; use std::collections::vec_deque; pub mod collide; pub mod fade; pub mod flashing; pub mod moving_pixel; pub mod moving_rainbow; pub mod orb; pub mod solid; pub use collide::Collide; pub use fade::Fade; pub use flashing::Flashing; pub use moving_pixel::MovingPixel; pub use moving_rainbow::MovingRainbow; pub use orb::Orb; pub use solid::Solid; #[derive(Serialize, Deserialize, Debug)] pub enum Parameters { Collide(Rgb, Rgb, Rgb), Fade((Rgb,)), MovingPixel((Rgb,)), MovingRainbow(u8, bool, u8), Orb(Rgb, u8, u8), Solid((Rgb,)), Flashing(Vec, u8, u16), } impl Parameters { pub fn into_pattern(self) -> Box { match self { Self::Collide(l, r, c) => Box::new(Collide::new(l, r, c)), Self::Fade((c,)) => Box::new(Fade::new(c)), Self::MovingPixel((c,)) => Box::new(MovingPixel::new(c)), Self::MovingRainbow(w, f, s) => Box::new(MovingRainbow::new(w, f, s)), Self::Orb(c, x, y) => Box::new(Orb::new(c, x, y)), Self::Solid((c,)) => Box::new(Solid::new(c)), Self::Flashing(cs, w, r) => Box::new(Flashing::new(cs, w, r)), } } } pub trait Pattern: std::fmt::Debug + Send + Sync { fn init(&mut self, num_lights: u16) -> Result<(), ()>; fn step(&mut self) -> Result; fn get_strip(&self) -> vec_deque::Iter; } // #[cfg(test)] // mod tests { // use super::*; // const NUM_LIGHTS: u16 = 10; // fn test_strip() -> Vec { // vec![color::BLACK; NUM_LIGHTS.into()] // } // #[test] // fn moving_pixel() { // let color = Rgb(123, 152, 89); // let mut pat = MovingPixel::new(color.clone()); // let mut strip = test_strip(); // assert!(pat.init(&mut strip, NUM_LIGHTS).is_ok()); // // One is my color // assert_eq!(strip.iter().filter(|c| **c == color).count(), 1); // // The rest are off // assert_eq!( // strip.iter().filter(|c| **c == color::BLACK).count(), // (NUM_LIGHTS - 1).into() // ); // pat.step(&mut strip); // // One is my color // assert_eq!(strip.iter().filter(|c| **c == color).count(), 1); // // The rest are off // assert_eq!( // strip.iter().filter(|c| **c == color::BLACK).count(), // (NUM_LIGHTS - 1).into() // ); // } // #[test] // fn solid() {} // #[test] // fn moving_rainbow() {} // #[test] // fn fade() {} // #[test] // fn collide() {} // }