Skip to content

Commit

Permalink
vinscant: better idle animation
Browse files Browse the repository at this point in the history
  • Loading branch information
draxaris1010 committed Sep 12, 2024
1 parent 9b6440b commit ee6c949
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
7 changes: 5 additions & 2 deletions vinscant/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use mfrc522::{
};
use palette;

use lib::wifi;
use lib::{ping_pong::PingPong, wifi};

#[toml_cfg::toml_config]
pub struct Config {
Expand Down Expand Up @@ -117,14 +117,17 @@ 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::<RGB8, LedPixelColorGrb24>::new(channel, led_pin).unwrap();

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]);
Expand Down
91 changes: 91 additions & 0 deletions vinscant/src/ping_pong.rs
Original file line number Diff line number Diff line change
@@ -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<Srgb<u8>>,
reverse: bool,
end: usize,
count: usize,
randomize: bool,
}

impl PingPong {
pub fn new(count: usize, data: Vec<Srgb<u8>>) -> Self {
let mut buffer = vec![Srgb::<u8>::new(0, 0, 0); count - 1];
buffer.extend(data);
buffer.extend(vec![Srgb::<u8>::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<Srgb<u8>>) -> 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<u8>) {
let mut buffer = vec![Srgb::<u8>::new(0, 0, 0); self.count];
buffer.extend(vec![colour; self.count]);
buffer.extend(vec![Srgb::<u8>::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<u8> =
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<Vec<Srgb<u8>>> {
let out = self
.buffer
.iter()
.skip(self.position)
.take(self.count)
.copied()
.collect::<Vec<Srgb<u8>>>();

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)
}
}

0 comments on commit ee6c949

Please sign in to comment.