From 5dc0438dd8fcd2df921aedb74cbdbb8a3d4bcfef Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sun, 15 Aug 2021 14:53:11 -0400 Subject: [PATCH] Add flashing pattern --- src/pattern/flashing.rs | 53 +++++++++++++++++++++ web/src/Colors.svelte | 22 +++++++++ web/src/Form.svelte | 102 ++++++++++++++++++++++------------------ 3 files changed, 132 insertions(+), 45 deletions(-) create mode 100644 src/pattern/flashing.rs create mode 100644 web/src/Colors.svelte diff --git a/src/pattern/flashing.rs b/src/pattern/flashing.rs new file mode 100644 index 0000000..592bfa5 --- /dev/null +++ b/src/pattern/flashing.rs @@ -0,0 +1,53 @@ +use super::Pattern; +use crate::color::{self, Rgb}; +use std::{ + collections::{vec_deque, VecDeque}, + convert::TryFrom, + iter, +}; + +#[derive(Clone, Debug)] +pub struct Flashing { + lights_buf: VecDeque, + width: u8, + colors: Vec, +} + +impl Flashing { + pub fn new(colors: Vec, width: u8) -> Self { + Self { + colors, + width, + lights_buf: VecDeque::new(), + } + } +} + +impl Pattern for Flashing { + fn step(&mut self) -> Result { + self.lights_buf.rotate_right(self.width.into()); + Ok(true) + } + fn init(&mut self, num_lights: u16) -> Result<(), ()> { + if self.lights_buf.len() == 0 { + return Err(()); + } + let length_factor = u16::try_from(self.lights_buf.len()) + .or(Err(()))? + .saturating_mul(u16::from(self.width)); + + let buf_length = color::min_with_factor(num_lights, length_factor)?; + + self.lights_buf = self + .colors + .iter() + .flat_map(|&x| iter::repeat(x).take(self.width.into())) + .cycle() + .take(buf_length.into()) + .collect(); + Ok(()) + } + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() + } +} diff --git a/web/src/Colors.svelte b/web/src/Colors.svelte new file mode 100644 index 0000000..94fc272 --- /dev/null +++ b/web/src/Colors.svelte @@ -0,0 +1,22 @@ + +{#each value as value} + +{/each} + + diff --git a/web/src/Form.svelte b/web/src/Form.svelte index 7580906..a0a182c 100644 --- a/web/src/Form.svelte +++ b/web/src/Form.svelte @@ -1,49 +1,53 @@ -
- + {#each possiblePatterns as p} + {/each} {#each selectedPattern.formElements as fe} - fe.value = e.target.value} /> + {#if fe.type === "colors"} + + {:else} + fe.value = e.target.value} /> + {/if} {/each} - +