Make a simple twinkle.

This commit is contained in:
Tangent Wantwight 2019-12-21 17:51:35 -05:00
parent 8a1b23ba79
commit 82a9c5696a
1 changed files with 26 additions and 14 deletions

View File

@ -1,5 +1,6 @@
//! Golden background with twinkling lights //! Golden background with twinkling lights
use super::delay; use super::delay;
use core::convert::TryInto;
use house::Harrogate; use house::Harrogate;
use lights::rgb::Rgb; use lights::rgb::Rgb;
use lights::{HardwareRgb, Lights, murmurf}; use lights::{HardwareRgb, Lights, murmurf};
@ -16,23 +17,35 @@ const FULL_PORCH: usize = 150;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct Pixel { struct Pixel {
color: Rgb, color: Rgb,
delay: u16
} }
impl Pixel { impl Pixel {
const fn new() -> Pixel { const fn new() -> Pixel {
Pixel { Pixel {
color: GOLD color: GOLD,
delay: 0,
} }
} }
fn tick(&mut self, mut rng: u32) { fn tick(&mut self, rng: &mut u32) {
self.color = match murmurf(&mut rng) % 8 { if self.delay > 0 {
0..=1 => RED, self.delay -= 1;
2..=3 => GREEN, } else {
4..=5 => BLUE, self.color = match murmurf(rng) % 16 {
6 => PURPLE, 0..=1 => RED,
7 => WHITE, 2..=3 => GREEN,
_ => GOLD, 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<Pixel = HardwareRgb>) -> ! {
let mut rng = 1; let mut rng = 1;
let mut pixels = [Pixel::new(); FULL_PORCH]; let mut pixels = [Pixel::new(); FULL_PORCH];
for pixel in pixels.iter_mut() {
pixel.tick(murmurf(&mut rng));
}
loop { loop {
for pixel in pixels.iter_mut() {
pixel.tick(&mut rng);
}
Harrogate { Harrogate {
porch_back: (0..FULL_PORCH).map(|_| GOLD), porch_back: (0..FULL_PORCH).map(|_| GOLD),
@ -56,6 +68,6 @@ pub fn run(lights: &mut impl Lights<Pixel = HardwareRgb>) -> ! {
} }
.render_to(lights); .render_to(lights);
delay(300_000); delay(5_000_000 / 60);
} }
} }