aw-lights/common/src/pattern.rs
Austen Adler 97d523a149 Use strum
2023-05-28 17:43:07 -04:00

248 lines
7.6 KiB
Rust

use crate::color::Rgb;
use serde::{Deserialize, Serialize};
pub mod collide;
pub mod fade;
pub mod flashing;
pub mod moving_pixel;
pub mod moving_rainbow;
pub mod orb;
pub mod slide;
pub mod solid;
pub mod visualizer;
pub use collide::{Collide, CollideParams};
pub use fade::{Fade, FadeParams};
pub use flashing::{Flashing, FlashingParams};
pub use moving_pixel::{MovingPixel, MovingPixelParams};
pub use moving_rainbow::{MovingRainbow, MovingRainbowParams};
pub use orb::{Orb, OrbParams};
pub use slide::{Slide, SlideParams};
pub use solid::{Solid, SolidParams};
pub use visualizer::{Visualizer, VisualizerParams};
pub type ColorIterator<'a> = Box<dyn Iterator<Item = &'a Rgb> + 'a>;
pub type PatternResult<T> = Result<T, PatternError>;
#[derive(Debug)]
pub enum PatternError {
ArithmeticError,
Index,
LightCount,
CommandNotFound(String),
IoError(std::io::Error),
InternalError,
}
impl From<std::io::Error> for PatternError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}
pub trait FormRender {
fn render(&self) -> String;
}
pub trait InputRender {
fn render(&self, name: &str, multi_index: Option<usize>) -> String;
}
impl InputRender for bool {
fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!(
r#"<label for="{name}">{name}</label><input type="checkbox" name="{name}" {}{}/>"#,
if *self { " checked" } else { "" },
if let Some(i) = multi_index {
format!(r#" name="{name}-{i}" rust-form-multi="{name}""#)
} else {
format!(r#" name="{name}""#)
},
name = name
)
}
}
impl InputRender for Rgb {
fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!(
r#"<label for="{name}">{name}</label><input type="color" value="{}" name="{name}" {}/>"#,
self.to_hex_str(),
if let Some(i) = multi_index {
format!(r#" name="{name}-{i}" rust-form-multi="{name}""#)
} else {
format!(r#" name="{name}""#)
},
name = name
)
}
}
impl InputRender for Vec<Rgb> {
fn render(&self, name: &str, _multi_index: Option<usize>) -> String {
self.iter()
// .chain(iter::once(&Rgb::default()))
.enumerate()
.fold(String::new(), |acc, (i, c)| {
acc + &c.render(name, Some(i)) + "\n"
})
}
}
impl InputRender for u8 {
fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!(
r#"<label for="{name}">{name}</label><input type="number" max="255" min="0" step="1" value="{}" name="{name}" {}/>"#,
self,
if let Some(i) = multi_index {
format!(r#" name="{name}-{i}" rust-form-multi="{name}""#)
} else {
format!(r#" name="{name}""#)
},
name = name
)
}
}
impl InputRender for u16 {
fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!(
r#"<label for="{name}">{name}</label><input type="number" max="65535" min="0" step="1" value="{}" name="{name}" {}/>"#,
self,
if let Some(i) = multi_index {
format!(r#" name="{name}-{i}" rust-form-multi="{name}""#)
} else {
format!(r#" name="{name}""#)
},
name = name
)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, strum::Display)]
pub enum Parameters {
Collide(CollideParams),
Slide(SlideParams),
Fade(FadeParams),
MovingPixel(MovingPixelParams),
MovingRainbow(MovingRainbowParams),
Orb(OrbParams),
Solid(SolidParams),
Visualizer(VisualizerParams),
Flashing(FlashingParams),
}
impl Default for Parameters {
fn default() -> Self {
Self::Visualizer(VisualizerParams::default())
}
}
impl FormRender for Parameters {
fn render(&self) -> String {
match self {
Self::Collide(ref p) => p.render(),
Self::Slide(ref p) => p.render(),
Self::Fade(ref p) => p.render(),
Self::MovingPixel(ref p) => p.render(),
Self::MovingRainbow(ref p) => p.render(),
Self::Orb(ref p) => p.render(),
Self::Solid(ref p) => p.render(),
Self::Visualizer(ref p) => p.render(),
Self::Flashing(ref p) => p.render(),
}
}
}
impl Parameters {
pub fn default_with_name(name: &str) -> Option<Self> {
match name {
"Collide" => Some(Self::Collide(CollideParams::default())),
"Slide" => Some(Self::Slide(SlideParams::default())),
"Fade" => Some(Self::Fade(FadeParams::default())),
"MovingPixel" => Some(Self::MovingPixel(MovingPixelParams::default())),
"MovingRainbow" => Some(Self::MovingRainbow(MovingRainbowParams::default())),
"Orb" => Some(Self::Orb(OrbParams::default())),
"Solid" => Some(Self::Solid(SolidParams::default())),
"Visualizer" => Some(Self::Visualizer(VisualizerParams::default())),
"Flashing" => Some(Self::Flashing(FlashingParams::default())),
_ => None,
}
}
pub const fn get_names() -> &'static [&'static str] {
&[
"Solid",
"Visualizer",
"Collide",
"Slide",
"Fade",
"MovingPixel",
"MovingRainbow",
"Orb",
"Flashing",
]
}
pub fn to_pattern(&self) -> Box<dyn Pattern + Send + Sync> {
match self {
Self::Collide(ref p) => Box::new(Collide::new(p)),
Self::Slide(ref p) => Box::new(Slide::new(p)),
Self::Fade(ref p) => Box::new(Fade::new(p)),
Self::MovingPixel(ref p) => Box::new(MovingPixel::new(p)),
Self::MovingRainbow(ref p) => Box::new(MovingRainbow::new(p)),
Self::Orb(ref p) => Box::new(Orb::new(p)),
Self::Solid(ref p) => Box::new(Solid::new(p)),
Self::Visualizer(ref p) => Box::new(Visualizer::new(p)),
Self::Flashing(ref p) => Box::new(Flashing::new(p)),
}
}
}
pub trait Pattern: std::fmt::Debug + Send + Sync {
fn init(&mut self, num_lights: u16) -> PatternResult<()>;
fn step(&mut self) -> PatternResult<bool>;
fn get_strip(&self) -> ColorIterator;
fn cleanup(&mut self) -> PatternResult<()> {
Ok(())
}
}
// #[cfg(test)]
// mod tests {
// use super::*;
// const NUM_LIGHTS: u16 = 10;
// fn test_strip() -> Vec<Rgb> {
// 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() {}
// }