Skip to content

Commit

Permalink
examples: Add rainbow effect
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Jul 14, 2023
1 parent e235441 commit c637b08
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/rainbow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use navigator_rs::Navigator;
use std::thread::sleep;
use std::time::Duration;

fn color_from_sine(percentage: f32) -> [u8; 3] {
let pi = std::f32::consts::PI;
let red = (percentage * 2.0 * pi).sin() * 0.5 + 0.5;
let green = ((percentage + 0.33) * 2.0 * pi).sin() * 0.5 + 0.5;
let blue = ((percentage + 0.67) * 2.0 * pi).sin() * 0.5 + 0.5;
[
(red * 255.0) as u8,
(green * 255.0) as u8,
(blue * 255.0) as u8,
]
}

fn main() {
let mut nav = Navigator::new();
nav.init();

println!("Creating rainbow effect!");
loop {
let steps = 1000;
for i in 0..=steps {
let ratio = i as f32 / steps as f32;
nav.set_neopixel(&[color_from_sine(ratio)]);
sleep(Duration::from_millis(10));
}
}
}

0 comments on commit c637b08

Please sign in to comment.