From 20983a8c0827b7f779e168c8358a51bef54d7d64 Mon Sep 17 00:00:00 2001 From: Tobias Ollive Date: Wed, 2 Mar 2022 13:45:55 +0100 Subject: [PATCH] add show example and scanner pattern --- spectacle.yml | 37 +++++++++++++++++++++++- src/patterns.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/spectacle.yml b/spectacle.yml index b79a2ed..a168422 100644 --- a/spectacle.yml +++ b/spectacle.yml @@ -1 +1,36 @@ -let yaml = serde_yaml::to_string(&runner).unwrap(); \ No newline at end of file +- pattern: + type: Fade + current_iteration: 0 + nbr_iterations: 20 + begin_color: + red: 255 + green: 0 + blue: 0 + end_color: + red: 0 + green: 0 + blue: 255 + period: + secs: 0 + nanos: 200000000 +- pattern: + type: ColorWipe + current_iteration: 0 + color: + red: 0 + green: 255 + blue: 0 + infinite: false + period: + secs: 0 + nanos: 100000000 +- pattern: + type: Scanner + current_iteration: 0 + color: + red: 0 + green: 0 + blue: 255 + period: + secs: 0 + nanos: 100000000 \ No newline at end of file diff --git a/src/patterns.rs b/src/patterns.rs index 69750db..9f7de08 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -1,13 +1,32 @@ +use serde_derive::{Deserialize, Serialize}; +use std::cmp::{max, min, Ordering}; +use std::convert::Infallible; use std::fmt; +use std::ops::Div; -pub(crate) enum patterns { +#[derive(Serialize, Deserialize, Debug)] +#[serde(tag = "type")] +pub(crate) enum Patterns { Rainbow(Rainbow), Fade(Fade), ColorWipe(ColorWipe), Scanner(Scanner), } -#[derive(Clone, Copy, Debug, Default)] +impl Iterator for Patterns { + type Item = Strip; + + fn next(&mut self) -> Option { + match self { + Patterns::Rainbow(p) => p.next(), + Patterns::Fade(p) => p.next(), + Patterns::ColorWipe(p) => p.next(), + Patterns::Scanner(p) => p.next(), + } + } +} + +#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)] pub struct PixelColor { pub(crate) red: u8, pub(crate) green: u8, @@ -26,6 +45,18 @@ impl fmt::Display for PixelColor { } } +impl Div for PixelColor { + type Output = PixelColor; + + fn div(self, rhs: u8) -> Self::Output { + PixelColor { + red: self.red / rhs, + green: self.green / rhs, + blue: self.blue / rhs, + } + } +} + #[derive(Copy, Clone)] pub struct Strip { pub strip: [PixelColor; N], @@ -197,3 +228,43 @@ impl Iterator for Fade { Some(strip) } } + +////////////////////////////////////////////////////////////////////////// +/// scanner pattern //////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#[derive(Serialize, Deserialize, Debug)] +pub struct Scanner { + pub(crate) current_iteration: usize, + pub(crate) color: PixelColor, + pub(crate) background_color: Option, +} + +impl Iterator for Scanner { + type Item = Strip; + + fn next(&mut self) -> Option { + let mut strip = Strip::::default(); + if let Some(c) = self.background_color { + strip.fill(c); + } else { + strip.fill(PixelColor::default()); + } + + let current_led = self.current_iteration % N; + let min_led; + if current_led < 8 { + min_led = 0; + } else { + min_led = current_led - 8; + } + let mut c = self.color; + for i in min_led..current_led { + strip.strip[i] = c; + c = c / 2; + println!("{}", c); + } + self.current_iteration += 1; + Some(strip) + } +}