From 82a9c5696afb9909f9f7f3e7dac102968635d583 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 21 Dec 2019 17:51:35 -0500 Subject: [PATCH] Make a simple twinkle. --- harrogate/src/december_21_2019.rs | 40 ++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/harrogate/src/december_21_2019.rs b/harrogate/src/december_21_2019.rs index 29dbd64..6c2c2b5 100644 --- a/harrogate/src/december_21_2019.rs +++ b/harrogate/src/december_21_2019.rs @@ -1,5 +1,6 @@ //! Golden background with twinkling lights use super::delay; +use core::convert::TryInto; use house::Harrogate; use lights::rgb::Rgb; use lights::{HardwareRgb, Lights, murmurf}; @@ -16,23 +17,35 @@ const FULL_PORCH: usize = 150; #[derive(Copy, Clone)] struct Pixel { color: Rgb, + delay: u16 } impl Pixel { const fn new() -> Pixel { Pixel { - color: GOLD + color: GOLD, + delay: 0, } } - fn tick(&mut self, mut rng: u32) { - self.color = match murmurf(&mut rng) % 8 { - 0..=1 => RED, - 2..=3 => GREEN, - 4..=5 => BLUE, - 6 => PURPLE, - 7 => WHITE, - _ => GOLD, + fn tick(&mut self, rng: &mut u32) { + if self.delay > 0 { + self.delay -= 1; + } else { + self.color = match murmurf(rng) % 16 { + 0..=1 => RED, + 2..=3 => GREEN, + 4..=5 => BLUE, + 6 => PURPLE, + 7 => WHITE, + _ => GOLD, + }; + + self.delay = if self.color == GOLD { + (murmurf(rng) % 600 + 300).try_into().unwrap_or(1000) + } else { + (murmurf(rng) % 100 + 50).try_into().unwrap_or(0) + } } } } @@ -44,11 +57,10 @@ pub fn run(lights: &mut impl Lights) -> ! { let mut rng = 1; let mut pixels = [Pixel::new(); FULL_PORCH]; - for pixel in pixels.iter_mut() { - pixel.tick(murmurf(&mut rng)); - } - loop { + for pixel in pixels.iter_mut() { + pixel.tick(&mut rng); + } Harrogate { porch_back: (0..FULL_PORCH).map(|_| GOLD), @@ -56,6 +68,6 @@ pub fn run(lights: &mut impl Lights) -> ! { } .render_to(lights); - delay(300_000); + delay(5_000_000 / 60); } }