aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf/src/bin/pwm_double_sequence.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-02-05 02:20:13 +0000
committerGitHub <[email protected]>2022-02-05 02:20:13 +0000
commita1d60774464dfb8d401706fc8a5ea682c246d97b (patch)
tree665ace9908eb1539ec8fde8bc62ade39616c6715 /examples/nrf/src/bin/pwm_double_sequence.rs
parentdef3283e44cfc61d6ed95a0d6c3d8b9f418b4d3f (diff)
parentdf5ba727f2c8bd3f2a67f51a3f43d7f47b011b1c (diff)
Merge #591
591: PWM WS2812B example and flexible sequence config r=Dirbaio a=huntc I've permitted the PWM sequences to be mutated on stopping the PWM by associating them with a new `SingleSequencer` structure. This is so that we can perform effects on the LEDs (and other use-cases, I'm sure!). The example has been updated to illustrate the use of this by flashing a WS2812B LED. There's also a `Sequencer` structure for more sophisticated PWM interactions, along with a `pwm_double_sequence` example to illustrate. These changes should make it possible to attain all of the nRF PWM functionality available. Co-authored-by: huntc <[email protected]>
Diffstat (limited to 'examples/nrf/src/bin/pwm_double_sequence.rs')
-rw-r--r--examples/nrf/src/bin/pwm_double_sequence.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/pwm_double_sequence.rs b/examples/nrf/src/bin/pwm_double_sequence.rs
new file mode 100644
index 000000000..269015f4a
--- /dev/null
+++ b/examples/nrf/src/bin/pwm_double_sequence.rs
@@ -0,0 +1,46 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use defmt::*;
8use embassy::executor::Spawner;
9use embassy::time::{Duration, Timer};
10use embassy_nrf::gpio::NoPin;
11use embassy_nrf::pwm::{
12 Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequencer,
13 StartSequence,
14};
15use embassy_nrf::Peripherals;
16
17#[embassy::main]
18async fn main(_spawner: Spawner, p: Peripherals) {
19 let seq_words_0: [u16; 5] = [1000, 250, 100, 50, 0];
20 let seq_words_1: [u16; 4] = [50, 100, 250, 1000];
21
22 let mut config = Config::default();
23 config.prescaler = Prescaler::Div128;
24 // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
25 // but say we want to hold the value for 5000ms
26 // so we want to repeat our value as many times as necessary until 5000ms passes
27 // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember)
28 let mut seq_config = SequenceConfig::default();
29 seq_config.refresh = 624;
30 // thus our sequence takes 5 * 5000ms or 25 seconds
31
32 let mut pwm = unwrap!(SequencePwm::new(
33 p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
34 ));
35
36 let sequence_0 = Sequence::new(&seq_words_0, seq_config.clone());
37 let sequence_1 = Sequence::new(&seq_words_1, seq_config);
38 let sequencer = Sequencer::new(&mut pwm, sequence_0, Some(sequence_1));
39 unwrap!(sequencer.start(StartSequence::Zero, SequenceMode::Loop(1)));
40
41 // we can abort a sequence if we need to before its complete with pwm.stop()
42 // or stop is also implicitly called when the pwm peripheral is dropped
43 // when it goes out of scope
44 Timer::after(Duration::from_millis(40000)).await;
45 info!("pwm stopped early!");
46}