Skip to content

Instantly share code, notes, and snippets.

@piedoom
Created June 21, 2020 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piedoom/515f0816b5a17dfd395352edf77a08fa to your computer and use it in GitHub Desktop.
Save piedoom/515f0816b5a17dfd395352edf77a08fa to your computer and use it in GitHub Desktop.
#![no_main]
#![no_std]
use stm32f1xx_hal as hal;
extern crate panic_semihosting;
use cortex_m_semihosting::hprintln;
use hal::{prelude::*, stm32, stm32::{Peripherals}, delay::Delay, timer::Timer};
use ws2812_timer_delay as ws2812;
use smart_leds::{RGB8, brightness, SmartLedsWrite};
use cortex_m_rt::{entry};
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (Peripherals::take(), cortex_m::Peripherals::take()) {
// Constrain clocking registers
let mut flash = p.FLASH.constrain();
let mut rcc = p.RCC.constrain();
let clocks = rcc.cfgr
.use_hse(8.mhz())
.sysclk(48.mhz())
.pclk1(24.mhz())
.freeze(&mut flash.acr);
let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
/* (Re-)configure PA7 as output */
let ws_data_pin =
gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
// Get delay provider
let mut delay = Delay::new(cp.SYST, clocks);
let timer = Timer::tim1(p.TIM1, &clocks, &mut rcc.apb2).start_count_down(3.mhz());
let mut ws = ws2812::Ws2812::new(timer, ws_data_pin);
const NUM_LEDS: usize = 50;
let mut data = [RGB8::default(); NUM_LEDS];
loop {
for j in 0..(256 * 5) {
for i in 0..NUM_LEDS {
data[i] = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8);
}
ws.write(brightness(data.iter().cloned(), 32)).unwrap();
delay.delay_ms(5u8);
}
}
}
loop {
continue;
}
}
/// Input a value 0 to 255 to get a color value
/// The colours are a transition r - g - b - back to r.
fn wheel(mut wheel_pos: u8) -> RGB8 {
wheel_pos = 255 - wheel_pos;
if wheel_pos < 85 {
return (255 - wheel_pos * 3, 0, wheel_pos * 3).into();
}
if wheel_pos < 170 {
wheel_pos -= 85;
return (0, wheel_pos * 3, 255 - wheel_pos * 3).into();
}
wheel_pos -= 170;
(wheel_pos * 3, 255 - wheel_pos * 3, 0).into()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment