Implement lightning flashes

This commit is contained in:
Tangent Wantwight 2020-10-07 20:24:24 -04:00
parent fbc4eb1e86
commit ce5b674920

View file

@ -1,27 +1,57 @@
//! orange-and-purple with "lightning" flashes //! orange-and-purple with "lightning" flashes
use super::delay; use super::delay;
use house::{Harrogate, PORCH_BACK_LEN, PORCH_FRONT_LEN}; use house::{Harrogate, PORCH_BACK_LEN, PORCH_FRONT_LEN};
use lights::{rgb::Rgb, HardwareRgb, Lights}; use lights::{murmurf, rgb::Rgb, HardwareRgb, Lights};
const BLACK: Rgb = Rgb(0, 0, 0); const BLACK: Rgb = Rgb(0, 0, 0);
const LIGHTNING: Rgb = Rgb(255, 255, 220);
const ORANGE: Rgb = Rgb(255, 150, 0); const ORANGE: Rgb = Rgb(255, 150, 0);
const PURPLE: Rgb = Rgb(100, 0, 128); const PURPLE: Rgb = Rgb(100, 0, 128);
// tenth of a second
const TIC: u32 = 4_800_000;
#[allow(dead_code)] #[allow(dead_code)]
#[inline(always)] #[inline(always)]
pub fn run(lights: &mut impl Lights<Pixel = HardwareRgb>) -> ! { pub fn run(lights: &mut impl Lights<Pixel = HardwareRgb>) -> ! {
let front_pattern = [PURPLE, ORANGE].iter().cycle().cloned(); let mut rng = 0xB00u32;
let mut back_buffer = [BLACK; PORCH_BACK_LEN];
let back_buffer = [BLACK; PORCH_BACK_LEN];
loop {
let mut draw = |back_buffer: &[Rgb]| {
let front_pattern = [PURPLE, ORANGE].iter().cycle().cloned();
Harrogate { Harrogate {
porch_front: front_pattern.clone().take(PORCH_FRONT_LEN), porch_front: front_pattern.take(PORCH_FRONT_LEN),
porch_back: back_buffer.iter().cloned() porch_back: back_buffer.iter().cloned(),
} }
.render_to(lights); .render_to(lights);
};
delay(12_000_000); loop {
// clear
back_buffer.iter_mut().for_each(|pix| *pix = BLACK);
draw(&back_buffer);
// delay 4-20 seconds
delay(TIC * (murmurf(&mut rng) % 160 + 40));
// flash 1
flash(&mut rng, &mut back_buffer);
draw(&back_buffer);
delay(TIC);
// flash 2
flash(&mut rng, &mut back_buffer);
draw(&back_buffer);
delay(TIC * 2);
} }
} }
fn flash(rng: &mut u32, back_buffer: &mut [Rgb]) {
let start = murmurf(rng) as usize % (PORCH_BACK_LEN - 20);
let len = murmurf(rng) as usize % 20 + murmurf(rng) as usize % 20 + 10;
let end = (start + len).min(PORCH_BACK_LEN);
back_buffer.as_mut()[start..end]
.iter_mut()
.for_each(|pix| *pix = LIGHTNING);
}