Skip to content

Commit 43ae47e

Browse files
committed
ws2812 prerendered
1 parent e1a1fd1 commit 43ae47e

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

examples/ws2812_spi.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,40 @@ use panic_halt as _;
66
use stm32f4xx_hal as hal;
77

88
use cortex_m_rt::entry;
9-
use hal::{gpio::NoPin, pac, prelude::*};
9+
use hal::{gpio::NoPin, pac, prelude::*, spi::Error};
1010
use smart_leds::{brightness, hsv::RGB8, SmartLedsWrite};
1111
use ws2812_spi as ws2812;
1212

1313
#[entry]
1414
fn main() -> ! {
1515
let dp = pac::Peripherals::take().expect("cannot take peripherals");
1616

17-
// Configure APB bus clock to 56MHz, cause ws2812b requires 3.5Mbps SPI
17+
// Configure APB bus clock to 48 MHz, cause ws2812b requires 3 Mbps SPI
1818
let rcc = dp.RCC.constrain();
19-
let clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(56.MHz()).freeze();
19+
let clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(48.MHz()).freeze();
2020

2121
let mut delay = dp.TIM1.delay_us(&clocks);
2222
let gpioa = dp.GPIOA.split();
2323

2424
let spi = dp.SPI1.spi(
2525
(gpioa.pa5, NoPin, gpioa.pa7),
2626
ws2812::MODE,
27-
3500.kHz(),
27+
3000.kHz(),
2828
&clocks,
2929
);
3030

31-
let mut ws = ws2812::Ws2812::new(spi);
32-
33-
const NUM_LEDS: usize = 8;
34-
let mut data = [RGB8::default(); NUM_LEDS];
31+
const NUM_LEDS: usize = 20;
32+
let mut buffer = [0; NUM_LEDS * 12 + 20];
33+
let mut ws = ws2812::prerendered::Ws2812::new(spi, buffer.as_mut_slice());
3534

3635
// Wait before start write for syncronization
3736
delay.delay(200.micros());
3837

3938
loop {
4039
for j in 0..(256 * 5) {
41-
for (i, b) in data.iter_mut().enumerate() {
42-
*b = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8);
43-
}
44-
ws.write(brightness(data.iter().cloned(), 32)).unwrap();
40+
let data = (0..NUM_LEDS)
41+
.map(|i| wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8));
42+
ws.write(brightness(data, 32)).unwrap();
4543
delay.delay(10.millis());
4644
}
4745
}

0 commit comments

Comments
 (0)