From 2540faa0f531ea534350e4e9b7db931d5612c9ef Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 16 Aug 2021 01:45:33 +0100 Subject: [PATCH] Add skip distance to rainbow --- src/pattern.rs | 4 ++-- src/pattern/moving_rainbow.rs | 17 ++++++++++------- src/ui.rs | 11 ++++++++--- web/src/Form.svelte | 1 + 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/pattern.rs b/src/pattern.rs index 3daf65a..68f9201 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -22,7 +22,7 @@ pub enum Parameters { Collide(Rgb, Rgb, Rgb), Fade((Rgb,)), MovingPixel((Rgb,)), - MovingRainbow(u8, bool), + MovingRainbow(u8, bool, u8), Orb(Rgb, u8, u8), Solid((Rgb,)), Flashing(Vec, u8, u16), @@ -34,7 +34,7 @@ impl Parameters { 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) => Box::new(MovingRainbow::new(w, f)), + 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)), diff --git a/src/pattern/moving_rainbow.rs b/src/pattern/moving_rainbow.rs index 8e8c54f..0dd432c 100644 --- a/src/pattern/moving_rainbow.rs +++ b/src/pattern/moving_rainbow.rs @@ -12,10 +12,10 @@ pub struct MovingRainbow { forward: bool, } impl MovingRainbow { - pub fn new(width: u8, forward: bool) -> Self { + pub fn new(width: u8, forward: bool, skip: u8) -> Self { Self { lights_buf: VecDeque::new(), - skip: 5, + skip, width, forward, } @@ -38,15 +38,18 @@ impl Pattern for MovingRainbow { return Err(()); } - // RAINBOW.len() * width - let length_factor = u16::try_from(RAINBOW.len()) - .or(Err(()))? + // (width + skip) * RAINBOW.len() + let length_factor = u16::from(self.width) .checked_add(self.skip.into()) .ok_or(())? - .saturating_mul(u16::from(self.width)); + .saturating_mul(u16::try_from(RAINBOW.len()).or(Err(()))?); // The length of the buffer // Always a factor of length_factor - let buf_length = color::min_with_factor(num_lights, length_factor)?; + let buf_length = color::min_with_factor(num_lights, length_factor.into())?; + println!( + "Got buf length: {} with #lights {} and length factor {} ({})", + buf_length, num_lights, length_factor, (self.width+self.skip) as usize *RAINBOW.len() + ); // num_lights // .checked_sub(1) // .ok_or(())? diff --git a/src/ui.rs b/src/ui.rs index 76d07e4..d7924d1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -68,14 +68,19 @@ fn parse_cmd(strip_tx: &Sender, s: &str) -> Result<(), String> { let color = parse_color(c, c, c)?; change_pattern(strip_tx, Box::new(pattern::Solid::new(color))) }, - ["r"] =>change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(4, true))), + ["r"] => change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(4, true, 0))), ["r", w] => { let width = w.parse::().map_err(|_| String::from("Width could not be parsed"))?; - change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(width, true))) + change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(width, true, 0))) }, ["r", w, f] => { let width = w.parse::().map_err(|_| String::from("Width could not be parsed"))?; - change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(width, ["t", "T"].contains(f)))) + change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(width, ["t", "T"].contains(f), 0))) + }, + ["r", w, f, s] => { + let width = w.parse::().map_err(|_| String::from("Width could not be parsed"))?; + let shift = s.parse::().map_err(|_| String::from("Shift could not be parsed"))?; + change_pattern(strip_tx, Box::new(pattern::MovingRainbow::new(width, ["t", "T"].contains(f), shift))) }, ["b", r1, g1, b1, r2, g2, b2, r3, g3, b3] => { let left = parse_color(r1, g1, b1)?; diff --git a/web/src/Form.svelte b/web/src/Form.svelte index 27793d3..681a0c1 100644 --- a/web/src/Form.svelte +++ b/web/src/Form.svelte @@ -20,6 +20,7 @@ {name: "MovingRainbow", text: "MovingRainbow", formElements: [ {name: "width", type: "number", label: "Width", value: "4"}, {name: "forward", type: "checkbox", label: "More Forward?", value: ""}, + {name: "skip", type: "number", label: "# to skip", value: ""}, ]}, {name: "Orb", text: "Orb", formElements: [ {name: "color", type: "color", label: "Color", value: "#000000"},