Return VecDeque Iter instead of slice in case VecDeque is used
This commit is contained in:
parent
bf07bf9a3c
commit
e2952cb614
@ -25,7 +25,7 @@ impl FromStr for Rgb {
|
|||||||
type Err = ParseIntError;
|
type Err = ParseIntError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
if let [r, g, b] = s.split(" ").collect::<Vec<&str>>().as_slice() {
|
if let [r, g, b] = s.split(' ').collect::<Vec<&str>>().as_slice() {
|
||||||
return Ok(Self(r.parse::<u8>()?, g.parse::<u8>()?, b.parse::<u8>()?));
|
return Ok(Self(r.parse::<u8>()?, g.parse::<u8>()?, b.parse::<u8>()?));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::color::Rgb;
|
use crate::color::Rgb;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::collections::vec_deque;
|
||||||
|
|
||||||
pub mod collide;
|
pub mod collide;
|
||||||
pub mod fade;
|
pub mod fade;
|
||||||
@ -15,7 +16,7 @@ pub use orb::Orb;
|
|||||||
pub use solid::Solid;
|
pub use solid::Solid;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub enum PatternParameters {
|
pub enum Parameters {
|
||||||
Collide(Rgb, Rgb, Rgb),
|
Collide(Rgb, Rgb, Rgb),
|
||||||
Fade((Rgb,)),
|
Fade((Rgb,)),
|
||||||
MovingPixel((Rgb,)),
|
MovingPixel((Rgb,)),
|
||||||
@ -24,15 +25,15 @@ pub enum PatternParameters {
|
|||||||
Solid((Rgb,)),
|
Solid((Rgb,)),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PatternParameters {
|
impl Parameters {
|
||||||
pub fn to_pattern(&self) -> Box<dyn Pattern + Send + Sync> {
|
pub fn to_pattern(&self) -> Box<dyn Pattern + Send + Sync> {
|
||||||
match self {
|
match self {
|
||||||
PatternParameters::Collide(l, r, c) => Box::new(Collide::new(*l, *r, *c)),
|
Self::Collide(l, r, c) => Box::new(Collide::new(*l, *r, *c)),
|
||||||
PatternParameters::Fade((c,)) => Box::new(Fade::new(*c)),
|
Self::Fade((c,)) => Box::new(Fade::new(*c)),
|
||||||
PatternParameters::MovingPixel((c,)) => Box::new(MovingPixel::new(*c)),
|
Self::MovingPixel((c,)) => Box::new(MovingPixel::new(*c)),
|
||||||
PatternParameters::MovingRainbow(w, f) => Box::new(MovingRainbow::new(*w, *f)),
|
Self::MovingRainbow(w, f) => Box::new(MovingRainbow::new(*w, *f)),
|
||||||
PatternParameters::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)),
|
Self::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)),
|
||||||
PatternParameters::Solid((c,)) => Box::new(Solid::new(*c)),
|
Self::Solid((c,)) => Box::new(Solid::new(*c)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,7 +41,7 @@ impl PatternParameters {
|
|||||||
pub trait Pattern: std::fmt::Debug + Send + Sync {
|
pub trait Pattern: std::fmt::Debug + Send + Sync {
|
||||||
fn init(&mut self, num_lights: u16) -> Result<(), ()>;
|
fn init(&mut self, num_lights: u16) -> Result<(), ()>;
|
||||||
fn step(&mut self) -> Result<bool, ()>;
|
fn step(&mut self) -> Result<bool, ()>;
|
||||||
fn get_strip(&self) -> &[Rgb];
|
fn get_strip(&self) -> vec_deque::Iter<Rgb>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[cfg(test)]
|
// #[cfg(test)]
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use super::Pattern;
|
use super::Pattern;
|
||||||
use crate::color::{self, Rgb};
|
use crate::color::{self, Rgb};
|
||||||
|
use std::collections::vec_deque;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Collide {
|
pub struct Collide {
|
||||||
@ -14,10 +16,10 @@ pub struct Collide {
|
|||||||
offset: u16,
|
offset: u16,
|
||||||
previous_offset: u16,
|
previous_offset: u16,
|
||||||
increase_offset: bool,
|
increase_offset: bool,
|
||||||
lights_buf: Vec<Rgb>,
|
lights_buf: VecDeque<Rgb>,
|
||||||
}
|
}
|
||||||
impl Collide {
|
impl Collide {
|
||||||
pub const fn new(left_color: Rgb, right_color: Rgb, conjoined_color: Rgb) -> Self {
|
pub fn new(left_color: Rgb, right_color: Rgb, conjoined_color: Rgb) -> Self {
|
||||||
Self {
|
Self {
|
||||||
num_lights: 0,
|
num_lights: 0,
|
||||||
left_color,
|
left_color,
|
||||||
@ -30,7 +32,7 @@ impl Collide {
|
|||||||
previous_offset: 0,
|
previous_offset: 0,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
increase_offset: true,
|
increase_offset: true,
|
||||||
lights_buf: vec![],
|
lights_buf: VecDeque::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,7 +116,7 @@ impl Pattern for Collide {
|
|||||||
if self.num_lights < 3 {
|
if self.num_lights < 3 {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
self.lights_buf = vec![color::BLACK; self.num_lights.into()];
|
self.lights_buf = VecDeque::from(vec![color::BLACK; self.num_lights.into()]);
|
||||||
if self.num_lights.rem_euclid(2) == 0 {
|
if self.num_lights.rem_euclid(2) == 0 {
|
||||||
self.conjoined_bounds = (
|
self.conjoined_bounds = (
|
||||||
self.num_lights
|
self.num_lights
|
||||||
@ -147,7 +149,7 @@ impl Pattern for Collide {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_strip(&self) -> &[Rgb] {
|
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
|
||||||
&self.lights_buf
|
self.lights_buf.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use super::Pattern;
|
use super::Pattern;
|
||||||
use crate::color::{self, Rgb};
|
use crate::color::{self, Rgb};
|
||||||
|
use std::collections::vec_deque;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Fade {
|
pub struct Fade {
|
||||||
@ -7,16 +9,16 @@ pub struct Fade {
|
|||||||
step: u8,
|
step: u8,
|
||||||
direction: bool,
|
direction: bool,
|
||||||
num_lights: u16,
|
num_lights: u16,
|
||||||
lights_buf: Vec<Rgb>,
|
lights_buf: VecDeque<Rgb>,
|
||||||
}
|
}
|
||||||
impl Fade {
|
impl Fade {
|
||||||
pub const fn new(color: Rgb) -> Self {
|
pub fn new(color: Rgb) -> Self {
|
||||||
Self {
|
Self {
|
||||||
color,
|
color,
|
||||||
step: 0,
|
step: 0,
|
||||||
direction: true,
|
direction: true,
|
||||||
num_lights: 1,
|
num_lights: 1,
|
||||||
lights_buf: vec![],
|
lights_buf: VecDeque::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,7 +35,10 @@ impl Pattern for Fade {
|
|||||||
}
|
}
|
||||||
self.step = self.step.saturating_sub(1);
|
self.step = self.step.saturating_sub(1);
|
||||||
}
|
}
|
||||||
self.lights_buf = vec![Rgb(self.step, self.step, self.step); self.num_lights.into()];
|
self.lights_buf = VecDeque::from(vec![
|
||||||
|
Rgb(self.step, self.step, self.step);
|
||||||
|
self.num_lights.into()
|
||||||
|
]);
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
fn init(&mut self, num_lights: u16) -> Result<(), ()> {
|
fn init(&mut self, num_lights: u16) -> Result<(), ()> {
|
||||||
@ -43,10 +48,10 @@ impl Pattern for Fade {
|
|||||||
self.step = 0;
|
self.step = 0;
|
||||||
self.direction = true;
|
self.direction = true;
|
||||||
self.num_lights = num_lights;
|
self.num_lights = num_lights;
|
||||||
self.lights_buf = vec![color::BLACK; self.num_lights.into()];
|
self.lights_buf = VecDeque::from(vec![color::BLACK; self.num_lights.into()]);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn get_strip(&self) -> &[Rgb] {
|
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
|
||||||
&self.lights_buf
|
self.lights_buf.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,24 @@
|
|||||||
use super::Pattern;
|
use super::Pattern;
|
||||||
use crate::color::{self, Rgb};
|
use crate::color::{self, Rgb};
|
||||||
|
use std::collections::vec_deque;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct MovingPixel {
|
pub struct MovingPixel {
|
||||||
color: Rgb,
|
color: Rgb,
|
||||||
num_lights: u16,
|
num_lights: u16,
|
||||||
step: u16,
|
step: u16,
|
||||||
lights_buf: Vec<Rgb>,
|
lights_buf: VecDeque<Rgb>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MovingPixel {
|
impl MovingPixel {
|
||||||
pub const fn new(color: Rgb) -> Self {
|
pub fn new(color: Rgb) -> Self {
|
||||||
Self {
|
Self {
|
||||||
color,
|
color,
|
||||||
step: 0,
|
step: 0,
|
||||||
// TODO: Better initialization
|
// TODO: Better initialization
|
||||||
num_lights: 1,
|
num_lights: 1,
|
||||||
lights_buf: vec![],
|
lights_buf: VecDeque::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,11 +40,11 @@ impl Pattern for MovingPixel {
|
|||||||
self.step = 0;
|
self.step = 0;
|
||||||
self.num_lights = num_lights;
|
self.num_lights = num_lights;
|
||||||
// Set the strip to black except for one pixel
|
// Set the strip to black except for one pixel
|
||||||
self.lights_buf = vec![color::BLACK; num_lights.into()];
|
self.lights_buf = VecDeque::from(vec![color::BLACK; num_lights.into()]);
|
||||||
*self.lights_buf.get_mut(0).ok_or(())? = self.color;
|
*self.lights_buf.get_mut(0).ok_or(())? = self.color;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn get_strip(&self) -> &[Rgb] {
|
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
|
||||||
&self.lights_buf
|
self.lights_buf.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
use super::Pattern;
|
use super::Pattern;
|
||||||
use crate::color::{Rgb, RAINBOW};
|
use crate::color::{Rgb, RAINBOW};
|
||||||
|
use std::collections::vec_deque;
|
||||||
|
use std::collections::VecDeque;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
//use std::collections::VecDeque;
|
|
||||||
//TODO Use a VecDeque
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct MovingRainbow {
|
pub struct MovingRainbow {
|
||||||
lights_buf: Vec<Rgb>,
|
lights_buf: VecDeque<Rgb>,
|
||||||
width: u8,
|
width: u8,
|
||||||
forward: bool,
|
forward: bool,
|
||||||
}
|
}
|
||||||
impl MovingRainbow {
|
impl MovingRainbow {
|
||||||
pub const fn new(width: u8, forward: bool) -> Self {
|
pub fn new(width: u8, forward: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
lights_buf: vec![],
|
lights_buf: VecDeque::new(),
|
||||||
width,
|
width,
|
||||||
forward,
|
forward,
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ impl Pattern for MovingRainbow {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn get_strip(&self) -> &[Rgb] {
|
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
|
||||||
&self.lights_buf
|
self.lights_buf.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use super::Pattern;
|
use super::Pattern;
|
||||||
use crate::color::{self, Rgb};
|
use crate::color::{self, Rgb};
|
||||||
use std::iter;
|
use std::collections::vec_deque;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Orb {
|
pub struct Orb {
|
||||||
@ -25,7 +26,7 @@ pub struct Orb {
|
|||||||
direction: bool,
|
direction: bool,
|
||||||
}
|
}
|
||||||
impl Orb {
|
impl Orb {
|
||||||
pub const fn new(color: Rgb, center_width: u8, backoff_width: u8) -> Self {
|
pub fn new(color: Rgb, center_width: u8, backoff_width: u8) -> Self {
|
||||||
Self {
|
Self {
|
||||||
lights_buf: VecDeque::new(),
|
lights_buf: VecDeque::new(),
|
||||||
color,
|
color,
|
||||||
@ -89,7 +90,7 @@ impl Pattern for Orb {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn get_strip(&self) -> &[Rgb] {
|
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
|
||||||
&self.lights_buf
|
self.lights_buf.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
use super::Pattern;
|
use super::Pattern;
|
||||||
use crate::color::Rgb;
|
use crate::color::Rgb;
|
||||||
|
use std::collections::vec_deque;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Solid {
|
pub struct Solid {
|
||||||
color: Rgb,
|
color: Rgb,
|
||||||
has_run: bool,
|
has_run: bool,
|
||||||
lights_buf: Vec<Rgb>,
|
lights_buf: VecDeque<Rgb>,
|
||||||
}
|
}
|
||||||
impl Solid {
|
impl Solid {
|
||||||
pub const fn new(color: Rgb) -> Self {
|
pub fn new(color: Rgb) -> Self {
|
||||||
Self {
|
Self {
|
||||||
color,
|
color,
|
||||||
has_run: false,
|
has_run: false,
|
||||||
lights_buf: vec![],
|
lights_buf: VecDeque::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -27,11 +29,11 @@ impl Pattern for Solid {
|
|||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
self.has_run = false;
|
self.has_run = false;
|
||||||
self.lights_buf = vec![self.color; num_lights.into()];
|
self.lights_buf = VecDeque::from(vec![self.color; num_lights.into()]);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_strip(&self) -> &[Rgb] {
|
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
|
||||||
&self.lights_buf
|
self.lights_buf.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,6 @@ impl LedStrip {
|
|||||||
self.pattern
|
self.pattern
|
||||||
.get_strip()
|
.get_strip()
|
||||||
// .as_slice()
|
// .as_slice()
|
||||||
.iter()
|
|
||||||
.take(self.config.num_lights.into()),
|
.take(self.config.num_lights.into()),
|
||||||
)
|
)
|
||||||
.map(|c| c.to_tuple())
|
.map(|c| c.to_tuple())
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
use crate::pattern;
|
use crate::pattern;
|
||||||
use crate::strip;
|
use crate::strip;
|
||||||
use actix_web::error::{ErrorBadRequest, JsonPayloadError};
|
use actix_web::error::JsonPayloadError;
|
||||||
use actix_web::web::JsonConfig;
|
use actix_web::web::JsonConfig;
|
||||||
use actix_web::{post, web, App, HttpServer, Responder, Result};
|
use actix_web::{post, web, App, HttpServer, Responder, Result};
|
||||||
use actix_web_static_files;
|
use actix_web_static_files;
|
||||||
use pattern::PatternParameters;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
@ -19,7 +18,7 @@ struct AppState {
|
|||||||
#[post("/setcolor")]
|
#[post("/setcolor")]
|
||||||
async fn set_color(
|
async fn set_color(
|
||||||
data: web::Data<AppState>,
|
data: web::Data<AppState>,
|
||||||
params: web::Json<PatternParameters>,
|
params: web::Json<pattern::Parameters>,
|
||||||
) -> Result<impl Responder> {
|
) -> Result<impl Responder> {
|
||||||
println!("Got params: {:?}", params);
|
println!("Got params: {:?}", params);
|
||||||
data.strip_tx
|
data.strip_tx
|
||||||
|
Loading…
Reference in New Issue
Block a user