Add skip distance to rainbow

This commit is contained in:
Your Name 2021-08-16 01:45:33 +01:00
parent fc3a85140f
commit 2540faa0f5
4 changed files with 21 additions and 12 deletions

View File

@ -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<Rgb>, 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)),

View File

@ -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(())?

View File

@ -68,14 +68,19 @@ fn parse_cmd(strip_tx: &Sender<strip::Message>, 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::<u8>().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::<u8>().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::<u8>().map_err(|_| String::from("Width could not be parsed"))?;
let shift = s.parse::<u8>().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)?;

View File

@ -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"},