diff --git a/src/pattern.rs b/src/pattern.rs index 2a8185d..1ba6cd1 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -14,7 +14,7 @@ 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) -> bool; + fn step(&mut self, lights_buf: &mut Vec) -> Result; } // #[cfg(test)] diff --git a/src/pattern/collide.rs b/src/pattern/collide.rs index e20a7be..03135eb 100644 --- a/src/pattern/collide.rs +++ b/src/pattern/collide.rs @@ -33,7 +33,7 @@ impl Collide { } } impl Pattern for Collide { - fn step(&mut self, lights_buf: &mut Vec) -> bool { + fn step(&mut self, lights_buf: &mut Vec) -> Result { // TODO: Better range storage // Set the left and right color let colors = @@ -44,32 +44,38 @@ impl Pattern for Collide { }; // Turn off the previous LED - lights_buf[usize::from(self.previous_offset)] = color::BLACK; + *lights_buf + .get_mut(usize::from(self.previous_offset)) + .ok_or(())? = color::BLACK; if self.previous_offset != self .num_lights .saturating_sub(1) .saturating_sub(self.previous_offset) { - lights_buf[usize::from( - self.num_lights - .saturating_sub(1) - .saturating_sub(self.previous_offset), - )] = color::BLACK; + *lights_buf + .get_mut(usize::from( + self.num_lights + .saturating_sub(1) + .saturating_sub(self.previous_offset), + )) + .ok_or(())? = color::BLACK; } // Set the color of the current offset - lights_buf[usize::from(self.offset)] = colors.0; + *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[usize::from( - self.num_lights - .saturating_sub(1) - .saturating_sub(self.offset), - )] = colors.1; + *lights_buf + .get_mut(usize::from( + self.num_lights + .saturating_sub(1) + .saturating_sub(self.offset), + )) + .ok_or(())? = colors.1; } self.previous_offset = self.offset; @@ -88,7 +94,7 @@ impl Pattern for Collide { self.step = self.step.saturating_add(1).rem_euclid(self.step_max); - true + Ok(true) } fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { // Reset changing parameters diff --git a/src/pattern/fade.rs b/src/pattern/fade.rs index 6ff4a0f..87e1217 100644 --- a/src/pattern/fade.rs +++ b/src/pattern/fade.rs @@ -19,7 +19,7 @@ impl Fade { } } impl Pattern for Fade { - fn step(&mut self, lights_buf: &mut Vec) -> bool { + fn step(&mut self, lights_buf: &mut Vec) -> Result { if self.direction { if self.step == 254 { self.direction = !self.direction; @@ -32,7 +32,7 @@ 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()]; - true + Ok(true) } fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { diff --git a/src/pattern/moving_pixel.rs b/src/pattern/moving_pixel.rs index 9d1b7c3..03adb8c 100644 --- a/src/pattern/moving_pixel.rs +++ b/src/pattern/moving_pixel.rs @@ -17,14 +17,14 @@ impl MovingPixel { } } impl Pattern for MovingPixel { - fn step(&mut self, lights_buf: &mut Vec) -> bool { + fn step(&mut self, lights_buf: &mut Vec) -> Result { let len = self.num_lights; 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); - true + Ok(true) } fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { @@ -34,7 +34,7 @@ impl Pattern for MovingPixel { self.num_lights = num_lights; // Set the strip to black except for one pixel *lights_buf = vec![color::BLACK; num_lights.into()]; - lights_buf[0] = self.color; + *lights_buf.get_mut(0).ok_or(())? = self.color; Ok(()) } } diff --git a/src/pattern/moving_rainbow.rs b/src/pattern/moving_rainbow.rs index 88fa84c..27cfb70 100644 --- a/src/pattern/moving_rainbow.rs +++ b/src/pattern/moving_rainbow.rs @@ -9,9 +9,9 @@ impl MovingRainbow { } } impl Pattern for MovingRainbow { - fn step(&mut self, lights_buf: &mut Vec) -> bool { + fn step(&mut self, lights_buf: &mut Vec) -> Result { lights_buf.rotate_right(1); - true + Ok(true) } fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { diff --git a/src/pattern/solid.rs b/src/pattern/solid.rs index c707238..779cd38 100644 --- a/src/pattern/solid.rs +++ b/src/pattern/solid.rs @@ -15,10 +15,10 @@ impl Solid { } } impl Pattern for Solid { - fn step(&mut self, _lights_buf: &mut Vec) -> bool { + fn step(&mut self, _lights_buf: &mut Vec) -> Result { let ret = !self.has_run; self.has_run = true; - ret + Ok(ret) } fn init(&mut self, lights_buf: &mut Vec, num_lights: u16) -> Result<(), ()> { if num_lights < 1 { diff --git a/src/strip.rs b/src/strip.rs index 88a2c17..8e7f7e2 100644 --- a/src/strip.rs +++ b/src/strip.rs @@ -152,7 +152,7 @@ impl LEDStrip { } } - if self.pattern.step(&mut self.lights_buf) { + if let Ok(true) = self.pattern.step(&mut self.lights_buf) { self.write_buf(); }