From c637b08e265e8ac498e2218bd97059a584ceeda6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 14 Jul 2023 16:05:29 -0300 Subject: [PATCH] examples: Add rainbow effect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/rainbow.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/rainbow.rs diff --git a/examples/rainbow.rs b/examples/rainbow.rs new file mode 100644 index 0000000000..086b2d2dc2 --- /dev/null +++ b/examples/rainbow.rs @@ -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)); + } + } +}