Skip to content

Commit d8fc82b

Browse files
Add examples
1 parent 9b9fdb1 commit d8fc82b

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cortex-m = "0.5.8"
1818
cortex-m-rt = "0.6"
1919
panic-halt = "0.2"
2020
stm32f0xx-hal = {version = "0.11", features = ["stm32f030x4"]}
21+
smart-leds = {git = "https://github.com/smart-leds-rs/smart-leds"}
2122

2223
[profile.dev]
2324
debug = true

examples/blink.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_trait::{Color, SmartLedsWrite};
19+
20+
use cortex_m_rt::entry;
21+
22+
#[entry]
23+
fn main() -> ! {
24+
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
25+
let gpioa = p.GPIOA.split();
26+
27+
/* (Re-)configure PA7 as output */
28+
let mut ws_data_pin = gpioa.pa7.into_push_pull_output_hs();
29+
30+
// Constrain clocking registers
31+
let rcc = p.RCC.constrain();
32+
33+
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
34+
35+
let mut timer = Timer::tim1(p.TIM1, MegaHertz(3), clocks);
36+
37+
// Get delay provider
38+
let mut delay = Delay::new(cp.SYST, clocks);
39+
40+
let mut ws = Ws2812::new(timer, &mut ws_data_pin);
41+
let mut data: [Color; 3] = [Color::default(); 3];
42+
let empty: [Color; 3] = [Color::default(); 3];
43+
44+
data[0] = Color {
45+
r: 0,
46+
g: 0,
47+
b: 0x10,
48+
};
49+
data[1] = Color {
50+
r: 0,
51+
g: 0x10,
52+
b: 0,
53+
};
54+
data[2] = Color {
55+
r: 0x10,
56+
g: 0,
57+
b: 0,
58+
};
59+
60+
loop {
61+
ws.write(data.iter().cloned()).unwrap();
62+
delay.delay_ms(10 as u16);
63+
ws.write(empty.iter().cloned()).unwrap();
64+
delay.delay_ms(10 as u16);
65+
}
66+
}
67+
loop {
68+
continue;
69+
}
70+
}

examples/rainbow.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)