Make led 2 fade through spectrum
This commit is contained in:
parent
4b2c303f13
commit
24294efe1d
|
@ -8,5 +8,6 @@ readme = "README.md"
|
|||
[dependencies]
|
||||
cortex-m = "0.6.4"
|
||||
cortex-m-rt = "0.6.13"
|
||||
nb = "1"
|
||||
panic-halt = "0.2.0"
|
||||
stm32f0xx-hal = { version = "0.18.0", features = ["rt", "stm32f072"] }
|
||||
|
|
58
src/main.rs
58
src/main.rs
|
@ -20,9 +20,12 @@
|
|||
#![no_main]
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use nb::block;
|
||||
use panic_halt as _;
|
||||
use stm32f0xx_hal::pac::Peripherals;
|
||||
use stm32f0xx_hal::prelude::*;
|
||||
use stm32f0xx_hal::pwm::tim3;
|
||||
use stm32f0xx_hal::timers::Timer;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
|
@ -33,24 +36,61 @@ fn main() -> ! {
|
|||
let gpioa = p.GPIOA.split(&mut rcc);
|
||||
let gpiob = p.GPIOB.split(&mut rcc);
|
||||
|
||||
let (mut led1_b, mut led1_g, mut led1_r, mut led2_g, mut led2_r, mut led2_b) = cortex_m::interrupt::free(|cs| (
|
||||
let (mut led1_b, mut led1_g, mut led1_r) = cortex_m::interrupt::free(|cs| (
|
||||
gpioa.pa0.into_push_pull_output(cs),
|
||||
gpioa.pa1.into_push_pull_output(cs),
|
||||
gpioa.pa2.into_push_pull_output(cs),
|
||||
gpiob.pb0.into_push_pull_output(cs),
|
||||
gpiob.pb1.into_push_pull_output(cs),
|
||||
gpiob.pb5.into_push_pull_output(cs),
|
||||
));
|
||||
|
||||
led1_r.set_high().ok(); // 0
|
||||
led1_g.set_high().ok(); // 0
|
||||
led1_b.set_low().ok(); // 255
|
||||
// ^ blue
|
||||
led2_r.set_high().ok(); // 0
|
||||
led2_g.set_low().ok(); // 255
|
||||
led2_b.set_low().ok(); // 255
|
||||
// ^ cyan
|
||||
|
||||
let led2_channels = cortex_m::interrupt::free(|cs| (
|
||||
gpiob.pb4.into_alternate_af1(cs),
|
||||
gpiob.pb5.into_alternate_af1(cs),
|
||||
gpiob.pb0.into_alternate_af1(cs),
|
||||
gpiob.pb1.into_alternate_af1(cs),
|
||||
));
|
||||
|
||||
let (_, mut led2_b, mut led2_g, mut led2_r) = tim3(p.TIM3, led2_channels, &mut rcc, 20u32.khz());
|
||||
let led2_r_max = led2_r.get_max_duty();
|
||||
let led2_g_max = led2_g.get_max_duty();
|
||||
let led2_b_max = led2_b.get_max_duty();
|
||||
|
||||
led2_r.set_duty(led2_r_max); // 0
|
||||
led2_g.set_duty(led2_g_max); // 0
|
||||
led2_b.set_duty(led2_b_max); // 0
|
||||
// ^ violet
|
||||
|
||||
led2_r.enable();
|
||||
led2_g.enable();
|
||||
led2_b.enable();
|
||||
|
||||
let mut timer = Timer::tim1(p.TIM1, 100.hz(), &mut rcc);
|
||||
|
||||
let mut hue = 0;
|
||||
loop {
|
||||
cortex_m::asm::nop();
|
||||
let h_i = hue / 60;
|
||||
let f = hue as f32 / 60.0 - h_i as f32;
|
||||
let q = 1.0 - f;
|
||||
|
||||
let (r, g, b) = match h_i {
|
||||
0 => (1.0, f, 0.0),
|
||||
1 => ( q, 1.0, 0.0),
|
||||
2 => (0.0, 1.0, f),
|
||||
3 => (0.0, q, 1.0),
|
||||
4 => ( f, 0.0, 1.0),
|
||||
5 => (1.0, 0.0, q),
|
||||
_ => (0.0, 0.0, 0.0),
|
||||
};
|
||||
led2_r.set_duty(led2_r_max - (led2_r_max as f32 * r) as u16);
|
||||
led2_g.set_duty(led2_g_max - (led2_g_max as f32 * g) as u16);
|
||||
led2_b.set_duty(led2_b_max - (led2_b_max as f32 * b) as u16);
|
||||
|
||||
hue = (hue + 1) % 360;
|
||||
|
||||
block!(timer.wait()).ok();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue