Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ws2812_spi example #363

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- `ws2812_spi` example [#363]
- `AsRef/AsMut<Rx/Tx` for Serial [#355]
- `spi::Transactional` [#356]
- `IoPin` for `Output<OpenDrain>` and `Output<PushPull>> <-> Input<Floating>>` [#356]
Expand Down Expand Up @@ -78,6 +79,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#355]: https://github.com/stm32-rs/stm32f4xx-hal/pull/355
[#356]: https://github.com/stm32-rs/stm32f4xx-hal/pull/356
[#357]: https://github.com/stm32-rs/stm32f4xx-hal/pull/357
[#363]: https://github.com/stm32-rs/stm32f4xx-hal/pull/363

### Changed

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ st7789 = "0.6.1"
panic-rtt-core = "0.2.1"
display-interface-spi = "0.4"
ist7920 = "0.1.0"
smart-leds = "0.3.0"
ws2812-spi = { version = "0.4.0", features = [] }

[features]
device-selected = []
Expand Down
2 changes: 1 addition & 1 deletion examples/analog-stopwatch-with-spi-ssd1306.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn main() -> ! {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
stm32f4xx_hal::time::KiloHertz(2000).into(),
2000.khz(),
clocks,
);

Expand Down
63 changes: 63 additions & 0 deletions examples/ws2812_spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use panic_halt as _;
use stm32f4xx_hal as hal;

use cortex_m_rt::entry;
use hal::{gpio::NoPin, pac, prelude::*, spi::Spi};
use smart_leds::{brightness, hsv::RGB8, SmartLedsWrite};
use ws2812_spi as ws2812;

#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().expect("cannot take peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

// Configure APB bus clock to 56MHz, cause ws2812b requires 3.5Mbps SPI
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(56.mhz()).freeze();

let mut delay = hal::delay::Delay::new(cp.SYST, &clocks);
let gpioa = dp.GPIOA.split();
let gpioc = dp.GPIOC.split();

let spi = Spi::new(
dp.SPI1,
(gpioa.pa5, NoPin, gpioa.pa7),
ws2812::MODE,
3500.khz(),
clocks,
);

let mut ws = ws2812::Ws2812::new(spi);

const NUM_LEDS: usize = 8;
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);
}
}
}

/// 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()
}
10 changes: 5 additions & 5 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ macro_rules! spi {
spi: $SPI,
pins: (SCK, MISO, MOSI),
mode: Mode,
freq: Hertz,
freq: impl Into<Hertz>,
clocks: Clocks,
) -> Spi<$SPI, (SCK, MISO, MOSI), TransferModeNormal> {
Self::new(spi, pins, mode, freq, clocks)
Expand Down Expand Up @@ -450,7 +450,7 @@ where
spi: SPI,
mut pins: (SCK, MISO, MOSI),
mode: Mode,
freq: Hertz,
freq: impl Into<Hertz>,
clocks: Clocks,
) -> Self {
unsafe {
Expand All @@ -469,7 +469,7 @@ where
pins,
transfer_mode: TransferModeNormal,
}
.pre_init(mode, freq, SPI::get_frequency(&clocks))
.pre_init(mode, freq.into(), SPI::get_frequency(&clocks))
.init()
}

Expand All @@ -492,7 +492,7 @@ where
spi: SPI,
mut pins: (SCK, MISO, MOSI),
mode: Mode,
freq: Hertz,
freq: impl Into<Hertz>,
clocks: Clocks,
) -> Self {
unsafe {
Expand All @@ -511,7 +511,7 @@ where
pins,
transfer_mode: TransferModeBidi,
}
.pre_init(mode, freq, SPI::get_frequency(&clocks))
.pre_init(mode, freq.into(), SPI::get_frequency(&clocks))
.init()
}

Expand Down