AAdd flashing pattern

This commit is contained in:
Austen Adler 2021-08-15 14:18:18 -04:00
parent a00867ad03
commit ea30e00eba
5 changed files with 59 additions and 28 deletions

View File

@ -104,6 +104,17 @@ pub fn build_ramp(from_color: Rgb, to_color: Rgb, length: usize) -> Vec<Rgb> {
ret ret
} }
pub fn min_with_factor(at_least: u16, factor: u16) -> Result<u16, ()> {
Ok(at_least
.checked_sub(1)
.ok_or(())?
.div_euclid(factor)
.checked_add(1)
.ok_or(())?
.checked_mul(factor)
.ok_or(())?)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -4,12 +4,14 @@ use std::collections::vec_deque;
pub mod collide; pub mod collide;
pub mod fade; pub mod fade;
pub mod flashing;
pub mod moving_pixel; pub mod moving_pixel;
pub mod moving_rainbow; pub mod moving_rainbow;
pub mod orb; pub mod orb;
pub mod solid; pub mod solid;
pub use collide::Collide; pub use collide::Collide;
pub use fade::Fade; pub use fade::Fade;
pub use flashing::Flashing;
pub use moving_pixel::MovingPixel; pub use moving_pixel::MovingPixel;
pub use moving_rainbow::MovingRainbow; pub use moving_rainbow::MovingRainbow;
pub use orb::Orb; pub use orb::Orb;
@ -23,17 +25,19 @@ pub enum Parameters {
MovingRainbow(u8, bool), MovingRainbow(u8, bool),
Orb(Rgb, u8, u8), Orb(Rgb, u8, u8),
Solid((Rgb,)), Solid((Rgb,)),
Flashing(Vec<Rgb>, u8),
} }
impl Parameters { impl Parameters {
pub fn to_pattern(&self) -> Box<dyn Pattern + Send + Sync> { pub fn into_pattern(self) -> Box<dyn Pattern + Send + Sync> {
match self { match self {
Self::Collide(l, r, c) => Box::new(Collide::new(*l, *r, *c)), Self::Collide(l, r, c) => Box::new(Collide::new(l, r, c)),
Self::Fade((c,)) => Box::new(Fade::new(*c)), Self::Fade((c,)) => Box::new(Fade::new(c)),
Self::MovingPixel((c,)) => Box::new(MovingPixel::new(*c)), Self::MovingPixel((c,)) => Box::new(MovingPixel::new(c)),
Self::MovingRainbow(w, f) => Box::new(MovingRainbow::new(*w, *f)), Self::MovingRainbow(w, f) => Box::new(MovingRainbow::new(w, f)),
Self::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)), Self::Orb(c, x, y) => Box::new(Orb::new(c, x, y)),
Self::Solid((c,)) => Box::new(Solid::new(*c)), Self::Solid((c,)) => Box::new(Solid::new(c)),
Self::Flashing(cs, w) => Box::new(Flashing::new(cs, w)),
} }
} }
} }

View File

@ -1,13 +1,13 @@
use super::Pattern; use super::Pattern;
use crate::color::{Rgb, RAINBOW}; use crate::color::{self, Rgb, RAINBOW};
use std::collections::vec_deque; use std::collections::{vec_deque, VecDeque};
use std::collections::VecDeque;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::iter; use std::iter;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct MovingRainbow { pub struct MovingRainbow {
lights_buf: VecDeque<Rgb>, lights_buf: VecDeque<Rgb>,
skip: u8,
width: u8, width: u8,
forward: bool, forward: bool,
} }
@ -15,6 +15,7 @@ impl MovingRainbow {
pub fn new(width: u8, forward: bool) -> Self { pub fn new(width: u8, forward: bool) -> Self {
Self { Self {
lights_buf: VecDeque::new(), lights_buf: VecDeque::new(),
skip: 5,
width, width,
forward, forward,
} }
@ -37,22 +38,28 @@ impl Pattern for MovingRainbow {
return Err(()); return Err(());
} }
// The length of the buffer // RAINBOW.len() * width
// Always a factor of RAINBOW.len() * width
let length_factor = u16::try_from(RAINBOW.len()) let length_factor = u16::try_from(RAINBOW.len())
.or(Err(()))? .or(Err(()))?
.saturating_mul(u16::from(self.width)); .saturating_mul(u16::from(self.width));
let buf_length = num_lights // The length of the buffer
.checked_sub(1) // Always a factor of length_factor
.ok_or(())? let buf_length = color::min_with_factor(num_lights, length_factor)?;
.div_euclid(length_factor) // num_lights
.checked_add(1) // .checked_sub(1)
.ok_or(())? // .ok_or(())?
.saturating_mul(length_factor); // .div_euclid(length_factor)
// .checked_add(1)
// .ok_or(())?
// .saturating_mul(length_factor);
self.lights_buf = RAINBOW self.lights_buf = RAINBOW
.iter() .iter()
.flat_map(|&x| iter::repeat(x).take(self.width.into())) .flat_map(|&x| {
iter::repeat(x)
.take(self.width.into())
.chain(iter::repeat(color::BLACK).take(self.skip.into()))
})
.cycle() .cycle()
.take(buf_length.into()) .take(buf_length.into())
.collect(); .collect();

View File

@ -2,17 +2,18 @@ use crate::errors;
use crate::pattern; use crate::pattern;
use crate::strip; use crate::strip;
use actix_web::error::{JsonPayloadError, UrlencodedError}; use actix_web::{
use actix_web::web::JsonConfig; error::{JsonPayloadError, UrlencodedError},
use actix_web::{post, web, App, HttpServer, Responder, Result}; post, web,
web::JsonConfig,
App, HttpServer, Responder, Result,
};
use actix_web_static_files::ResourceFiles; use actix_web_static_files::ResourceFiles;
use std::io; use std::io;
use std::sync::{Arc, Mutex}; use std::sync::{mpsc::Sender, Arc, Mutex};
include!(concat!(env!("OUT_DIR"), "/generated.rs")); include!(concat!(env!("OUT_DIR"), "/generated.rs"));
use std::sync::mpsc::Sender;
struct AppState { struct AppState {
strip_tx: Arc<Mutex<Sender<strip::Message>>>, strip_tx: Arc<Mutex<Sender<strip::Message>>>,
} }
@ -26,9 +27,9 @@ async fn set_color_json(
data.strip_tx data.strip_tx
.lock() .lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to get a lock"))? .map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to get a lock"))?
.send(strip::Message::ChangePattern(params.0.to_pattern())) .send(strip::Message::ChangePattern(params.0.into_pattern()))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to send to channel"))?; .map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to send to channel"))?;
Ok(format!("{:?}", params)) Ok("Success")
} }
#[actix_web::main] #[actix_web::main]

View File

@ -24,6 +24,14 @@
{name: "center_width", type: "number", label: "Center Width", value: "1"}, {name: "center_width", type: "number", label: "Center Width", value: "1"},
{name: "backoff_width", type: "number", label: "Backoff Width", value: "0"}, {name: "backoff_width", type: "number", label: "Backoff Width", value: "0"},
]}, ]},
{name: "Flashing", text: "Flashing", formElements: [
{name: "color", type: "color", label: "Color1", value: "#000000"},
{name: "color", type: "color", label: "Color2", value: "#000000"},
{name: "color", type: "color", label: "Color3", value: "#000000"},
{name: "color", type: "color", label: "Color4", value: "#000000"},
{name: "color", type: "color", label: "Color5", value: "#000000"},
{name: "width", type: "number", label: "Width", value: "1"},
]},
]; ];
let selectedPattern = possiblePatterns[0]; let selectedPattern = possiblePatterns[0];