-
Notifications
You must be signed in to change notification settings - Fork 200
/
neopixel_rainbow.rs
69 lines (60 loc) · 1.97 KB
/
neopixel_rainbow.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#![no_std]
#![no_main]
// Neopixel Rainbow
// This only functions when the --release version is compiled. Using the debug
// version leads to slow pulse durations which results in a straight white LED
// output.
//
// // Needs to be compiled with --release for the timing to be correct
use bsp::hal;
use feather_m4 as bsp;
#[cfg(not(feature = "use_semihosting"))]
use panic_halt as _;
#[cfg(feature = "use_semihosting")]
use panic_semihosting as _;
use bsp::entry;
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::ehal::delay::DelayNs;
use hal::pac::{CorePeripherals, Peripherals};
use hal::time::Hertz;
use hal::timer::*;
use hal::timer_traits::InterruptDrivenTimer;
use smart_leds::{
hsv::{hsv2rgb, Hsv},
SmartLedsWrite,
};
use ws2812_timer_delay::Ws2812;
#[entry]
fn main() -> ! {
let mut peripherals = Peripherals::take().unwrap();
let core = CorePeripherals::take().unwrap();
let mut clocks = GenericClockController::with_external_32kosc(
peripherals.gclk,
&mut peripherals.mclk,
&mut peripherals.osc32kctrl,
&mut peripherals.oscctrl,
&mut peripherals.nvmctrl,
);
let pins = bsp::Pins::new(peripherals.port);
let mut delay = Delay::new(core.SYST, &mut clocks);
let gclk0 = clocks.gclk0();
let timer_clock = clocks.tc2_tc3(&gclk0).unwrap();
let mut timer = TimerCounter::tc3_(&timer_clock, peripherals.tc3, &mut peripherals.mclk);
timer.start(Hertz::MHz(3).into_duration());
let neopixel_pin = pins.neopixel.into_push_pull_output();
let mut neopixel = Ws2812::new(timer, neopixel_pin);
// Loop through all of the available hue values (colors) to make a
// rainbow effect from the onboard neopixel
loop {
for j in 0..255u8 {
let colors = [hsv2rgb(Hsv {
hue: j,
sat: 255,
val: 2,
})];
neopixel.write(colors.iter().cloned()).unwrap();
delay.delay_ms(5);
}
}
}