|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +#[allow(unused)] |
| 5 | +use panic_halt; |
| 6 | + |
| 7 | +use stm32f0xx_hal as hal; |
| 8 | +use ws2812_timer_delay as ws2812; |
| 9 | + |
| 10 | +use crate::hal::delay::Delay; |
| 11 | +use crate::hal::prelude::*; |
| 12 | +use crate::hal::stm32; |
| 13 | +use crate::hal::time::*; |
| 14 | +use crate::hal::timers::*; |
| 15 | +use crate::ws2812::Ws2812; |
| 16 | +use cortex_m::peripheral::Peripherals; |
| 17 | + |
| 18 | +use smart_leds::brightness; |
| 19 | +use smart_leds_trait::{Color, SmartLedsWrite}; |
| 20 | + |
| 21 | +use cortex_m_rt::entry; |
| 22 | + |
| 23 | +#[entry] |
| 24 | +fn main() -> ! { |
| 25 | + if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { |
| 26 | + let gpioa = p.GPIOA.split(); |
| 27 | + |
| 28 | + /* (Re-)configure PA7 as output */ |
| 29 | + let mut ws_data_pin = gpioa.pa7.into_push_pull_output(); |
| 30 | + |
| 31 | + // Constrain clocking registers |
| 32 | + let rcc = p.RCC.constrain(); |
| 33 | + |
| 34 | + let clocks = rcc.cfgr.sysclk(48.mhz()).freeze(); |
| 35 | + |
| 36 | + let mut timer = Timer::tim1(p.TIM1, MegaHertz(3), clocks); |
| 37 | + |
| 38 | + // Get delay provider |
| 39 | + let mut delay = Delay::new(cp.SYST, clocks); |
| 40 | + |
| 41 | + let mut ws = Ws2812::new(timer, &mut ws_data_pin); |
| 42 | + |
| 43 | + const NUM_LEDS: usize = 10; |
| 44 | + let mut data = [Color::default(); NUM_LEDS]; |
| 45 | + |
| 46 | + loop { |
| 47 | + for j in 0..(256 * 5) { |
| 48 | + for i in 0..NUM_LEDS { |
| 49 | + data[i] = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8); |
| 50 | + } |
| 51 | + ws.write(brightness(data.iter().cloned(), 32)).unwrap(); |
| 52 | + delay.delay_ms(5u8); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + loop { |
| 57 | + continue; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Input a value 0 to 255 to get a color value |
| 62 | +/// The colours are a transition r - g - b - back to r. |
| 63 | +fn wheel(mut wheel_pos: u8) -> Color { |
| 64 | + wheel_pos = 255 - wheel_pos; |
| 65 | + if wheel_pos < 85 { |
| 66 | + return (255 - wheel_pos * 3, 0, wheel_pos * 3).into(); |
| 67 | + } |
| 68 | + if wheel_pos < 170 { |
| 69 | + wheel_pos -= 85; |
| 70 | + return (0, wheel_pos * 3, 255 - wheel_pos * 3).into(); |
| 71 | + } |
| 72 | + wheel_pos -= 170; |
| 73 | + (wheel_pos * 3, 255 - wheel_pos * 3, 0).into() |
| 74 | +} |
0 commit comments