Return VecDeque Iter instead of slice in case VecDeque is used

This commit is contained in:
Austen Adler 2021-08-12 21:06:22 -04:00
parent bf07bf9a3c
commit e2952cb614
10 changed files with 61 additions and 50 deletions

View File

@ -25,7 +25,7 @@ impl FromStr for Rgb {
type Err = ParseIntError;
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>()?));
}

View File

@ -1,5 +1,6 @@
use crate::color::Rgb;
use serde::Deserialize;
use std::collections::vec_deque;
pub mod collide;
pub mod fade;
@ -15,7 +16,7 @@ pub use orb::Orb;
pub use solid::Solid;
#[derive(Deserialize, Debug)]
pub enum PatternParameters {
pub enum Parameters {
Collide(Rgb, Rgb, Rgb),
Fade((Rgb,)),
MovingPixel((Rgb,)),
@ -24,15 +25,15 @@ pub enum PatternParameters {
Solid((Rgb,)),
}
impl PatternParameters {
impl Parameters {
pub fn to_pattern(&self) -> Box<dyn Pattern + Send + Sync> {
match self {
PatternParameters::Collide(l, r, c) => Box::new(Collide::new(*l, *r, *c)),
PatternParameters::Fade((c,)) => Box::new(Fade::new(*c)),
PatternParameters::MovingPixel((c,)) => Box::new(MovingPixel::new(*c)),
PatternParameters::MovingRainbow(w, f) => Box::new(MovingRainbow::new(*w, *f)),
PatternParameters::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)),
PatternParameters::Solid((c,)) => Box::new(Solid::new(*c)),
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::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)),
Self::Solid((c,)) => Box::new(Solid::new(*c)),
}
}
}
@ -40,7 +41,7 @@ impl PatternParameters {
pub trait Pattern: std::fmt::Debug + Send + Sync {
fn init(&mut self, num_lights: u16) -> Result<(), ()>;
fn step(&mut self) -> Result<bool, ()>;
fn get_strip(&self) -> &[Rgb];
fn get_strip(&self) -> vec_deque::Iter<Rgb>;
}
// #[cfg(test)]

View File

@ -1,5 +1,7 @@
use super::Pattern;
use crate::color::{self, Rgb};
use std::collections::vec_deque;
use std::collections::VecDeque;
#[derive(Clone, Debug)]
pub struct Collide {
@ -14,10 +16,10 @@ pub struct Collide {
offset: u16,
previous_offset: u16,
increase_offset: bool,
lights_buf: Vec<Rgb>,
lights_buf: VecDeque<Rgb>,
}
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 {
num_lights: 0,
left_color,
@ -30,7 +32,7 @@ impl Collide {
previous_offset: 0,
offset: 0,
increase_offset: true,
lights_buf: vec![],
lights_buf: VecDeque::new(),
}
}
}
@ -114,7 +116,7 @@ impl Pattern for Collide {
if self.num_lights < 3 {
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 {
self.conjoined_bounds = (
self.num_lights
@ -147,7 +149,7 @@ impl Pattern for Collide {
Ok(())
}
fn get_strip(&self) -> &[Rgb] {
&self.lights_buf
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
}
}

View File

@ -1,5 +1,7 @@
use super::Pattern;
use crate::color::{self, Rgb};
use std::collections::vec_deque;
use std::collections::VecDeque;
#[derive(Clone, Debug)]
pub struct Fade {
@ -7,16 +9,16 @@ pub struct Fade {
step: u8,
direction: bool,
num_lights: u16,
lights_buf: Vec<Rgb>,
lights_buf: VecDeque<Rgb>,
}
impl Fade {
pub const fn new(color: Rgb) -> Self {
pub fn new(color: Rgb) -> Self {
Self {
color,
step: 0,
direction: true,
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.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)
}
fn init(&mut self, num_lights: u16) -> Result<(), ()> {
@ -43,10 +48,10 @@ impl Pattern for Fade {
self.step = 0;
self.direction = true;
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(())
}
fn get_strip(&self) -> &[Rgb] {
&self.lights_buf
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
}
}

View File

@ -1,22 +1,24 @@
use super::Pattern;
use crate::color::{self, Rgb};
use std::collections::vec_deque;
use std::collections::VecDeque;
#[derive(Clone, Debug)]
pub struct MovingPixel {
color: Rgb,
num_lights: u16,
step: u16,
lights_buf: Vec<Rgb>,
lights_buf: VecDeque<Rgb>,
}
impl MovingPixel {
pub const fn new(color: Rgb) -> Self {
pub fn new(color: Rgb) -> Self {
Self {
color,
step: 0,
// TODO: Better initialization
num_lights: 1,
lights_buf: vec![],
lights_buf: VecDeque::new(),
}
}
}
@ -38,11 +40,11 @@ impl Pattern for MovingPixel {
self.step = 0;
self.num_lights = num_lights;
// 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;
Ok(())
}
fn get_strip(&self) -> &[Rgb] {
&self.lights_buf
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
}
}

View File

@ -1,20 +1,20 @@
use super::Pattern;
use crate::color::{Rgb, RAINBOW};
use std::collections::vec_deque;
use std::collections::VecDeque;
use std::convert::TryFrom;
use std::iter;
//use std::collections::VecDeque;
//TODO Use a VecDeque
#[derive(Clone, Debug)]
pub struct MovingRainbow {
lights_buf: Vec<Rgb>,
lights_buf: VecDeque<Rgb>,
width: u8,
forward: bool,
}
impl MovingRainbow {
pub const fn new(width: u8, forward: bool) -> Self {
pub fn new(width: u8, forward: bool) -> Self {
Self {
lights_buf: vec![],
lights_buf: VecDeque::new(),
width,
forward,
}
@ -57,7 +57,7 @@ impl Pattern for MovingRainbow {
Ok(())
}
fn get_strip(&self) -> &[Rgb] {
&self.lights_buf
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
}
}

View File

@ -1,7 +1,8 @@
use super::Pattern;
use crate::color::{self, Rgb};
use std::iter;
use std::collections::vec_deque;
use std::collections::VecDeque;
use std::iter;
#[derive(Clone, Debug)]
pub struct Orb {
@ -25,7 +26,7 @@ pub struct Orb {
direction: bool,
}
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 {
lights_buf: VecDeque::new(),
color,
@ -89,7 +90,7 @@ impl Pattern for Orb {
Ok(())
}
fn get_strip(&self) -> &[Rgb] {
&self.lights_buf
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
}
}

View File

@ -1,18 +1,20 @@
use super::Pattern;
use crate::color::Rgb;
use std::collections::vec_deque;
use std::collections::VecDeque;
#[derive(Clone, Debug)]
pub struct Solid {
color: Rgb,
has_run: bool,
lights_buf: Vec<Rgb>,
lights_buf: VecDeque<Rgb>,
}
impl Solid {
pub const fn new(color: Rgb) -> Self {
pub fn new(color: Rgb) -> Self {
Self {
color,
has_run: false,
lights_buf: vec![],
lights_buf: VecDeque::new(),
}
}
}
@ -27,11 +29,11 @@ impl Pattern for Solid {
return Err(());
}
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(())
}
fn get_strip(&self) -> &[Rgb] {
&self.lights_buf
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
}
}

View File

@ -70,7 +70,6 @@ impl LedStrip {
self.pattern
.get_strip()
// .as_slice()
.iter()
.take(self.config.num_lights.into()),
)
.map(|c| c.to_tuple())

View File

@ -1,10 +1,9 @@
use crate::pattern;
use crate::strip;
use actix_web::error::{ErrorBadRequest, JsonPayloadError};
use actix_web::error::JsonPayloadError;
use actix_web::web::JsonConfig;
use actix_web::{post, web, App, HttpServer, Responder, Result};
use actix_web_static_files;
use pattern::PatternParameters;
use std::io;
use std::sync::{Arc, Mutex};
@ -19,7 +18,7 @@ struct AppState {
#[post("/setcolor")]
async fn set_color(
data: web::Data<AppState>,
params: web::Json<PatternParameters>,
params: web::Json<pattern::Parameters>,
) -> Result<impl Responder> {
println!("Got params: {:?}", params);
data.strip_tx