From 221b18d0852b615a3ca3e962d67d1c6c85632383 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Thu, 12 Oct 2023 21:09:15 -0400 Subject: [PATCH] Halloween 2023 --- harrogate/Cargo.toml | 1 + harrogate/src/halloween_2023.rs | 96 +++++++++++++++++++++++++++++++++ harrogate/src/main.rs | 4 ++ 3 files changed, 101 insertions(+) create mode 100644 harrogate/src/halloween_2023.rs diff --git a/harrogate/Cargo.toml b/harrogate/Cargo.toml index 01807bd..2779039 100644 --- a/harrogate/Cargo.toml +++ b/harrogate/Cargo.toml @@ -25,6 +25,7 @@ july-4 = ["replace-default"] halloween-2020 = ["replace-default"] halloween-2021 = ["replace-default"] halloween-2022 = ["replace-default"] +halloween-2023 = ["replace-default"] new-years-eve-2020 = ["replace-default"] november-2020 = ["replace-default"] diff --git a/harrogate/src/halloween_2023.rs b/harrogate/src/halloween_2023.rs new file mode 100644 index 0000000..82984b5 --- /dev/null +++ b/harrogate/src/halloween_2023.rs @@ -0,0 +1,96 @@ +//! purple-and-orange with chaotically-varying background + +use super::delay; +use house::{Harrogate, PORCH_BACK_LEN, PORCH_FRONT_LEN}; +use lights::{ + murmurf, + rgb::{gray, Rgb}, + HardwareRgb, Lights, +}; + +const BLACK: Rgb = Rgb(0, 0, 0); +const BG_WAVE_1: Rgb = Rgb(200, 100, 0); +const BG_WAVE_2: Rgb = Rgb(0, 100, 100); +const ORANGE: Rgb = Rgb(255, 150, 0); +const PURPLE: Rgb = Rgb(100, 0, 128); + +// tenth of a second +const TIC: u32 = 4_800_000; + +enum Direction { + UP, + DOWN, +} + +struct Wave { + spot: u8, + target: u8, + direction: Direction, +} + +impl Wave { + fn tick(&mut self, rng: &mut u32) { + if self.spot == self.target { + // target caught, pick new one + self.target = (murmurf(rng) % 256) as u8; + self.direction = if self.target > self.spot { + Direction::UP + } else { + Direction::DOWN + }; + } else { + // chase target brightness + self.spot = match self.direction { + Direction::UP => self.spot + 1, + Direction::DOWN => self.spot - 1, + } + } + } + + fn rgb(&self, color: Rgb) -> Rgb { + color * gray(self.spot) + } +} + +#[allow(dead_code)] +#[inline(always)] +pub fn run(lights: &mut impl Lights) -> ! { + let mut rng = 0xB00u32; + let mut wave1 = Wave { + spot: 128, + target: 128, + direction: Direction::UP, + }; + let mut wave2 = Wave { + spot: 128, + target: 128, + direction: Direction::UP, + }; + + let mut back_buffer = [BLACK; PORCH_BACK_LEN]; + + let mut draw = |back_buffer: &[Rgb]| { + let front_pattern = [PURPLE, ORANGE].iter().cycle().cloned(); + Harrogate { + porch_front: front_pattern.take(PORCH_FRONT_LEN), + porch_back: back_buffer.iter().cloned(), + } + .render_to(lights); + }; + + loop { + // animate brightness variation + wave1.tick(&mut rng); + wave2.tick(&mut rng); + + // push into buffer + back_buffer.rotate_right(1); + back_buffer[0] = wave1.rgb(BG_WAVE_1) + wave2.rgb(BG_WAVE_2); + back_buffer[PORCH_BACK_LEN - 1].1 = 255; + + // render + draw(&back_buffer); + + delay(TIC); + } +} diff --git a/harrogate/src/main.rs b/harrogate/src/main.rs index 90f8dbe..a695ef4 100644 --- a/harrogate/src/main.rs +++ b/harrogate/src/main.rs @@ -16,6 +16,7 @@ mod july_4; mod halloween_2020; mod halloween_2021; mod halloween_2022; +mod halloween_2023; mod november_2020; mod new_years_eve_2020; mod door_light; @@ -57,6 +58,9 @@ fn main() -> ! { #[cfg(feature = "halloween-2022")] halloween_2022::run(&mut lights); + #[cfg(feature = "halloween-2023")] + halloween_2023::run(&mut lights); + #[cfg(feature = "new-years-eve-2020")] new_years_eve_2020::run(&mut lights);