From fa2f55c170fa11a5ebb395c59ea1ac0b2e1e353a Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Fri, 6 Aug 2021 20:32:16 -0400 Subject: [PATCH] Pass slice instead of mutable vec reference --- src/main.rs | 2 +- src/pattern.rs | 7 +++--- src/pattern/collide.rs | 26 +++++++++++++++------ src/pattern/fade.rs | 13 +++++++---- src/pattern/moving_pixel.rs | 18 +++++++++++---- src/pattern/moving_rainbow.rs | 17 +++++++++----- src/pattern/solid.rs | 12 +++++++--- src/strip.rs | 43 +++++++++++++++-------------------- 8 files changed, 84 insertions(+), 54 deletions(-) diff --git a/src/main.rs b/src/main.rs index d591792..f953262 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ clippy::integer_arithmetic, clippy::integer_division, // Expect prints a message when there is a critical error, so this is valid - // clippy::expect_used, + clippy::expect_used, clippy::unwrap_used, )] // See https://rust-lang.github.io/rust-clippy/master/index.html for more lints diff --git a/src/pattern.rs b/src/pattern.rs index 1ba6cd1..a31fe23 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -5,7 +5,6 @@ pub mod fade; pub mod moving_pixel; pub mod moving_rainbow; pub mod solid; - pub use collide::Collide; pub use fade::Fade; pub use moving_pixel::MovingPixel; @@ -13,8 +12,10 @@ pub use moving_rainbow::MovingRainbow; pub use solid::Solid; pub trait Pattern: std::fmt::Debug { - fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()>; - fn step(&mut self, lights_buf: &mut Vec) -> Result; + fn init(&mut self, num_lights: u16) -> Result<(), ()>; + fn step(&mut self) -> Result; + fn get_strip(&self) -> &[RGB]; + // fn get_strip_mut(&mut self) -> &[RGB]; } // #[cfg(test)] diff --git a/src/pattern/collide.rs b/src/pattern/collide.rs index 03135eb..fa28002 100644 --- a/src/pattern/collide.rs +++ b/src/pattern/collide.rs @@ -14,6 +14,7 @@ pub struct Collide { offset: u16, previous_offset: u16, increase_offset: bool, + lights_buf: Vec, } impl Collide { pub const fn new(left_color: RGB, right_color: RGB, conjoined_color: RGB) -> Self { @@ -29,11 +30,12 @@ impl Collide { previous_offset: 0, offset: 0, increase_offset: true, + lights_buf: vec![], } } } impl Pattern for Collide { - fn step(&mut self, lights_buf: &mut Vec) -> Result { + fn step(&mut self) -> Result { // TODO: Better range storage // Set the left and right color let colors = @@ -44,7 +46,8 @@ impl Pattern for Collide { }; // Turn off the previous LED - *lights_buf + *self + .lights_buf .get_mut(usize::from(self.previous_offset)) .ok_or(())? = color::BLACK; if self.previous_offset @@ -53,7 +56,8 @@ impl Pattern for Collide { .saturating_sub(1) .saturating_sub(self.previous_offset) { - *lights_buf + *self + .lights_buf .get_mut(usize::from( self.num_lights .saturating_sub(1) @@ -62,14 +66,18 @@ impl Pattern for Collide { .ok_or(())? = color::BLACK; } // Set the color of the current offset - *lights_buf.get_mut(usize::from(self.offset)).ok_or(())? = colors.0; + *self + .lights_buf + .get_mut(usize::from(self.offset)) + .ok_or(())? = colors.0; if self.offset != self .num_lights .saturating_sub(1) .saturating_sub(self.offset) { - *lights_buf + *self + .lights_buf .get_mut(usize::from( self.num_lights .saturating_sub(1) @@ -96,7 +104,7 @@ impl Pattern for Collide { Ok(true) } - fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { + fn init(&mut self, num_lights: u16) -> Result<(), ()> { // Reset changing parameters self.step = 0; self.offset = 0; @@ -106,7 +114,7 @@ impl Pattern for Collide { if self.num_lights < 3 { return Err(()); } - *lights_buf = vec![color::BLACK; self.num_lights.into()]; + self.lights_buf = vec![color::BLACK; self.num_lights.into()]; if self.num_lights.rem_euclid(2) == 0 { self.conjoined_bounds = ( self.num_lights @@ -138,4 +146,8 @@ impl Pattern for Collide { .saturating_mul(4); Ok(()) } + + fn get_strip(&self) -> &[RGB] { + &self.lights_buf + } } diff --git a/src/pattern/fade.rs b/src/pattern/fade.rs index 87e1217..4626eef 100644 --- a/src/pattern/fade.rs +++ b/src/pattern/fade.rs @@ -7,6 +7,7 @@ pub struct Fade { step: u8, direction: bool, num_lights: u16, + lights_buf: Vec, } impl Fade { pub const fn new(color: RGB) -> Self { @@ -15,11 +16,12 @@ impl Fade { step: 0, direction: true, num_lights: 1, + lights_buf: vec![], } } } impl Pattern for Fade { - fn step(&mut self, lights_buf: &mut Vec) -> Result { + fn step(&mut self) -> Result { if self.direction { if self.step == 254 { self.direction = !self.direction; @@ -31,17 +33,20 @@ impl Pattern for Fade { } self.step = self.step.saturating_sub(1); } - *lights_buf = vec![RGB(self.step, self.step, self.step); self.num_lights.into()]; + self.lights_buf = vec![RGB(self.step, self.step, self.step); self.num_lights.into()]; Ok(true) } - fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { + fn init(&mut self, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { return Err(()); } self.step = 0; self.direction = true; self.num_lights = num_lights; - *lights_buf = vec![color::BLACK; self.num_lights.into()]; + self.lights_buf = vec![color::BLACK; self.num_lights.into()]; Ok(()) } + fn get_strip(&self) -> &[RGB] { + &self.lights_buf + } } diff --git a/src/pattern/moving_pixel.rs b/src/pattern/moving_pixel.rs index 03adb8c..53d5ee3 100644 --- a/src/pattern/moving_pixel.rs +++ b/src/pattern/moving_pixel.rs @@ -6,35 +6,43 @@ pub struct MovingPixel { color: RGB, num_lights: u16, step: u16, + lights_buf: Vec, } + impl MovingPixel { pub const fn new(color: RGB) -> Self { Self { color, step: 0, + // TODO: Better initialization num_lights: 1, + lights_buf: vec![], } } } + impl Pattern for MovingPixel { - fn step(&mut self, lights_buf: &mut Vec) -> Result { + fn step(&mut self) -> Result { let len = self.num_lights; - lights_buf.swap( + self.lights_buf.swap( self.step.rem_euclid(len).into(), self.step.saturating_add(1).saturating_mul(3).into(), ); self.step = self.step.wrapping_add(1); Ok(true) } - fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { + fn init(&mut self, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { return Err(()); } self.step = 0; self.num_lights = num_lights; // Set the strip to black except for one pixel - *lights_buf = vec![color::BLACK; num_lights.into()]; - *lights_buf.get_mut(0).ok_or(())? = self.color; + self.lights_buf = 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 + } } diff --git a/src/pattern/moving_rainbow.rs b/src/pattern/moving_rainbow.rs index 27cfb70..b6264d4 100644 --- a/src/pattern/moving_rainbow.rs +++ b/src/pattern/moving_rainbow.rs @@ -2,22 +2,24 @@ use super::Pattern; use crate::color::{RAINBOW, RGB}; #[derive(Clone, Debug)] -pub struct MovingRainbow {} +pub struct MovingRainbow { + lights_buf: Vec, +} impl MovingRainbow { pub const fn new() -> Self { - Self {} + Self { lights_buf: vec![] } } } impl Pattern for MovingRainbow { - fn step(&mut self, lights_buf: &mut Vec) -> Result { - lights_buf.rotate_right(1); + fn step(&mut self) -> Result { + self.lights_buf.rotate_right(1); Ok(true) } - fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { + fn init(&mut self, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { return Err(()); } - *lights_buf = RAINBOW + self.lights_buf = RAINBOW .iter() .cycle() .take( @@ -32,4 +34,7 @@ impl Pattern for MovingRainbow { .collect::>(); Ok(()) } + fn get_strip(&self) -> &[RGB] { + &self.lights_buf + } } diff --git a/src/pattern/solid.rs b/src/pattern/solid.rs index 779cd38..4b54403 100644 --- a/src/pattern/solid.rs +++ b/src/pattern/solid.rs @@ -5,27 +5,33 @@ use crate::color::RGB; pub struct Solid { color: RGB, has_run: bool, + lights_buf: Vec, } impl Solid { pub const fn new(color: RGB) -> Self { Self { color, has_run: false, + lights_buf: vec![], } } } impl Pattern for Solid { - fn step(&mut self, _lights_buf: &mut Vec) -> Result { + fn step(&mut self) -> Result { let ret = !self.has_run; self.has_run = true; Ok(ret) } - fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { + fn init(&mut self, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { return Err(()); } self.has_run = false; - *lights_buf = vec![self.color; num_lights.into()]; + self.lights_buf = vec![self.color; num_lights.into()]; Ok(()) } + + fn get_strip(&self) -> &[RGB] { + &self.lights_buf + } } diff --git a/src/strip.rs b/src/strip.rs index 8e7f7e2..f8afdb2 100644 --- a/src/strip.rs +++ b/src/strip.rs @@ -1,4 +1,4 @@ -use crate::color::{self, RGB}; +use crate::color; use crate::pattern::{self, Pattern}; use std::cmp; use std::ops::Add; @@ -40,7 +40,6 @@ pub struct LEDStrip { pub adapter: Box, pub config: Config, pub pattern: Box, - pub lights_buf: Vec, } impl LEDStrip { @@ -50,36 +49,37 @@ impl LEDStrip { ); let pattern = Box::new(pattern::Solid::new(color::BLACK)); let num_lights = config.num_lights; - let lights_buf = vec![color::BLACK; num_lights.into()]; let mut ret = Self { adapter, pattern, - lights_buf, config, }; ret.set_num_lights(num_lights); ret } - fn write_buf(&mut self) { + fn write_buf_from_pattern(&mut self) { + let global_brightness_max = self.config.global_brightness_max; let data = vec![color::BLACK] .iter() .cycle() .take(self.config.shift_lights.into()) .chain( - self.lights_buf - .as_slice() + self.pattern + .get_strip() + // .as_slice() .iter() .take(self.config.num_lights.into()), ) .map(|c| c.to_tuple()) .map(|(r, g, b)| { ( - cmp::min(r, self.config.global_brightness_max), - cmp::min(g, self.config.global_brightness_max), - cmp::min(b, self.config.global_brightness_max), + cmp::min(r, global_brightness_max), + cmp::min(g, global_brightness_max), + cmp::min(b, global_brightness_max), ) }) + // TODO: Do not re-collect as u8s .collect::>(); self.adapter .write_rgb(data.as_slice()) @@ -94,7 +94,7 @@ impl LEDStrip { ); return; } - if self.pattern.init(&mut self.lights_buf, num_lights).is_ok() { + if self.pattern.init(num_lights).is_ok() { self.config.num_lights = num_lights; } } @@ -108,10 +108,7 @@ impl LEDStrip { match message { Message::ClearLights => { let mut pat = Box::new(pattern::Solid::new(color::BLACK)); - if pat - .init(&mut self.lights_buf, self.config.num_lights) - .is_ok() - { + if pat.init(self.config.num_lights).is_ok() { self.pattern = pat; } else { println!("Clearing light strip: {:?}", pat); @@ -119,10 +116,7 @@ impl LEDStrip { } Message::ChangePattern(pat) => { let mut pat = pat; - if pat - .init(&mut self.lights_buf, self.config.num_lights) - .is_ok() - { + if pat.init(self.config.num_lights).is_ok() { self.pattern = pat; } else { println!("Error with pattern: {:?}", pat); @@ -140,10 +134,7 @@ impl LEDStrip { Message::Quit => { exit = true; let mut pat = pattern::Solid::new(color::BLACK); - if pat - .init(&mut self.lights_buf, self.config.num_lights) - .is_ok() - { + if pat.init(self.config.num_lights).is_ok() { self.pattern = Box::new(pat); } else { println!("Could not construct clear pattern"); @@ -152,8 +143,10 @@ impl LEDStrip { } } - if let Ok(true) = self.pattern.step(&mut self.lights_buf) { - self.write_buf(); + // TODO: Handle error properly + let result = self.pattern.step(); + if let Ok(true) = result { + self.write_buf_from_pattern(); } if exit {