diff --git a/vinscant/src/main.rs b/vinscant/src/main.rs index 956cc38..0ac76bf 100644 --- a/vinscant/src/main.rs +++ b/vinscant/src/main.rs @@ -22,7 +22,7 @@ use mfrc522::{ }; use palette; -use lib::wifi; +use lib::{ping_pong::PingPong, wifi}; #[toml_cfg::toml_config] pub struct Config { @@ -117,6 +117,9 @@ fn main() { let scan_interface = SpiInterface::new(scan_spi_device); let mut scanner = Mfrc522::new(scan_interface).init().unwrap(); + // esp32s2 + //let led_pin = pins.gpio?; + // esp32 let led_pin = pins.gpio5; let channel = peripherals.rmt.channel0; let mut led_strip = LedPixelEsp32Rmt::::new(channel, led_pin).unwrap(); @@ -124,7 +127,7 @@ fn main() { let mut status_notifier = StatusNotifier { led_strip, leds: 8, - idle_effect: Box::new(Wipe::new(8, vec![Srgb::new(0x00, 0x00, 0x00), Srgb::new(0xff, 0x7f, 0x00)], true)), + idle_effect: Box::new(PingPong::new(8, vec![Srgb::new(0xff, 0x7f, 0x00)])), }; let mut last_uid = hex::encode([0_u8]); diff --git a/vinscant/src/ping_pong.rs b/vinscant/src/ping_pong.rs new file mode 100644 index 0000000..cadd8ad --- /dev/null +++ b/vinscant/src/ping_pong.rs @@ -0,0 +1,91 @@ +use smart_led_effects::strip::EffectIterator; +use palette::{FromColor, Hsv, Srgb}; +use rand::Rng; + +#[derive(Debug)] +pub struct PingPong { + position: usize, + buffer: Vec>, + reverse: bool, + end: usize, + count: usize, + randomize: bool, +} + +impl PingPong { + pub fn new(count: usize, data: Vec>) -> Self { + let mut buffer = vec![Srgb::::new(0, 0, 0); count - 1]; + buffer.extend(data); + buffer.extend(vec![Srgb::::new(0, 0, 0); count - 1]); + + let end = buffer.len() - count; + + PingPong { + position: 0, + buffer, + reverse: false, + end, + count, + randomize: false, + } + } + + pub fn colour_ping_pong(count: usize, colour: Option>) -> Self { + let mut s = PingPong::new(count, vec![Srgb::new(0, 0, 0); count]); + match colour { + Some(colour) => s.fill_ping_pong(colour), + None => s.randomize_colour_ping_pong(), + } + s + } + + fn fill_ping_pong(&mut self, colour: Srgb) { + let mut buffer = vec![Srgb::::new(0, 0, 0); self.count]; + buffer.extend(vec![colour; self.count]); + buffer.extend(vec![Srgb::::new(0, 0, 0); self.count]); + self.buffer = buffer; + } + + fn randomize_colour_ping_pong(&mut self) { + let mut rng = rand::thread_rng(); + let colour: Srgb = + Srgb::from_color(Hsv::new(rng.gen_range(0.0..360.0), 1.0, 1.0)).into_format(); + self.fill_ping_pong(colour); + self.randomize = true; + } +} + +impl EffectIterator for PingPong { + fn name(&self) -> &'static str { + "PingPong" + } + + fn next(&mut self) -> Option>> { + let out = self + .buffer + .iter() + .skip(self.position) + .take(self.count) + .copied() + .collect::>>(); + + if self.reverse { + self.position -= 1; + if self.position == 0 { + self.reverse = false; + if self.randomize { + self.randomize_colour_ping_pong(); + } + } + } else { + self.position += 1; + if self.position >= self.end { + self.reverse = true; + if self.randomize { + self.randomize_colour_ping_pong(); + } + } + } + Some(out) + } +}