diff options
| author | huntc <[email protected]> | 2022-01-25 18:06:42 +1100 |
|---|---|---|
| committer | huntc <[email protected]> | 2022-01-28 11:20:04 +1100 |
| commit | 47aeab152fff59e64d8244475dfbec338e6f98e5 (patch) | |
| tree | 0bc694c01f575d9a26071416f803494b6988dbb7 /examples/nrf/src/bin/pwm_sequence.rs | |
| parent | d76cd5ceaf5140c48ef97180beae156c0c0e07c8 (diff) | |
PWM WS2812B example and per sequence config
Demonstrates how to set the colour of a WS2812B to blue using PWM, and the use of multiple sequences along with their own config. This required an API change.
Diffstat (limited to 'examples/nrf/src/bin/pwm_sequence.rs')
| -rw-r--r-- | examples/nrf/src/bin/pwm_sequence.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 56c865d1c..2dcbc7475 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs | |||
| @@ -8,7 +8,7 @@ use defmt::*; | |||
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::time::{Duration, Timer}; | 9 | use embassy::time::{Duration, Timer}; |
| 10 | use embassy_nrf::gpio::NoPin; | 10 | use embassy_nrf::gpio::NoPin; |
| 11 | use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceMode, SequencePwm}; | 11 | use embassy_nrf::pwm::{Config, Prescaler, SequenceMode, SequencePwm}; |
| 12 | use embassy_nrf::Peripherals; | 12 | use embassy_nrf::Peripherals; |
| 13 | 13 | ||
| 14 | #[embassy::main] | 14 | #[embassy::main] |
| @@ -16,26 +16,39 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 16 | let seq_values_1: [u16; 5] = [1000, 250, 100, 50, 0]; | 16 | let seq_values_1: [u16; 5] = [1000, 250, 100, 50, 0]; |
| 17 | let seq_values_2: [u16; 5] = [0, 50, 100, 250, 1000]; | 17 | let seq_values_2: [u16; 5] = [0, 50, 100, 250, 1000]; |
| 18 | 18 | ||
| 19 | let mut config = SequenceConfig::default(); | 19 | let mut config = Config::default(); |
| 20 | config.prescaler = Prescaler::Div128; | 20 | config.prescaler = Prescaler::Div128; |
| 21 | // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us | 21 | // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us |
| 22 | // but say we want to hold the value for 5000ms | 22 | // but say we want to hold the value for 5000ms |
| 23 | // so we want to repeat our value as many times as necessary until 5000ms passes | 23 | // so we want to repeat our value as many times as necessary until 5000ms passes |
| 24 | // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember) | 24 | // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember) |
| 25 | config.refresh = 624; | 25 | let mut seq_config = Config::default(); |
| 26 | seq_config.refresh = 624; | ||
| 26 | // thus our sequence takes 5 * 5000ms or 25 seconds | 27 | // thus our sequence takes 5 * 5000ms or 25 seconds |
| 27 | 28 | ||
| 28 | let mut pwm = unwrap!(SequencePwm::new( | 29 | let mut pwm = unwrap!(SequencePwm::new( |
| 29 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, | 30 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, |
| 30 | )); | 31 | )); |
| 31 | let _ = pwm.start(&seq_values_1, SequenceMode::Infinite); | 32 | let _ = pwm.start( |
| 33 | &seq_values_1, | ||
| 34 | seq_config, | ||
| 35 | None, | ||
| 36 | None, | ||
| 37 | SeqSequenceMode::Infinite, | ||
| 38 | ); | ||
| 32 | 39 | ||
| 33 | info!("pwm started!"); | 40 | info!("pwm started!"); |
| 34 | 41 | ||
| 35 | Timer::after(Duration::from_millis(20000)).await; | 42 | Timer::after(Duration::from_millis(20000)).await; |
| 36 | info!("pwm starting with another sequence!"); | 43 | info!("pwm starting with another sequence!"); |
| 37 | 44 | ||
| 38 | let _ = pwm.start(&seq_values_2, SequenceMode::Infinite); | 45 | let _ = pwm.start( |
| 46 | &seq_values_2, | ||
| 47 | seq_config, | ||
| 48 | None, | ||
| 49 | None, | ||
| 50 | SequenceMode::Infinite, | ||
| 51 | ); | ||
| 39 | 52 | ||
| 40 | // we can abort a sequence if we need to before its complete with pwm.stop() | 53 | // we can abort a sequence if we need to before its complete with pwm.stop() |
| 41 | // or stop is also implicitly called when the pwm peripheral is dropped | 54 | // or stop is also implicitly called when the pwm peripheral is dropped |
