diff options
| author | Jacob Rosenthal <[email protected]> | 2021-10-29 17:09:34 -0700 |
|---|---|---|
| committer | Jacob Rosenthal <[email protected]> | 2021-10-29 17:10:37 -0700 |
| commit | ef95441442e0ccab0ac6d62264dedb7254d4cb84 (patch) | |
| tree | e5fd996270346f3d76c6451dd30150e8dd3be513 /examples/nrf/src/bin | |
| parent | 1d1d8a848e165e2754720fa442571782616cb822 (diff) | |
a runtime generated sin table example
Diffstat (limited to 'examples/nrf/src/bin')
| -rw-r--r-- | examples/nrf/src/bin/pwm_simple_sin.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs new file mode 100644 index 000000000..866202a4c --- /dev/null +++ b/examples/nrf/src/bin/pwm_simple_sin.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | #![feature(array_from_fn)] | ||
| 5 | |||
| 6 | #[path = "../example_common.rs"] | ||
| 7 | mod example_common; | ||
| 8 | use defmt::*; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy::time::{Duration, Timer}; | ||
| 11 | use embassy_nrf::gpio::NoPin; | ||
| 12 | use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; | ||
| 13 | use embassy_nrf::Peripherals; | ||
| 14 | use micromath::F32Ext; | ||
| 15 | |||
| 16 | const W1: f32 = core::f32::consts::PI / 128.0; | ||
| 17 | |||
| 18 | #[embassy::main] | ||
| 19 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 20 | // probably not best use of resources to create the table at runtime, but makes testing fast | ||
| 21 | let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16); | ||
| 22 | |||
| 23 | let config = LoopingConfig { | ||
| 24 | counter_mode: CounterMode::UpAndDown, | ||
| 25 | top: 12000, | ||
| 26 | prescaler: Prescaler::Div16, | ||
| 27 | sequence: &seq_values, | ||
| 28 | sequence_load: SequenceLoad::Common, | ||
| 29 | repeats: 0, | ||
| 30 | enddelay: 0, | ||
| 31 | }; | ||
| 32 | |||
| 33 | let _pwm = unwrap!(Pwm::simple_playback( | ||
| 34 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config | ||
| 35 | )); | ||
| 36 | info!("pwm started!"); | ||
| 37 | |||
| 38 | loop { | ||
| 39 | Timer::after(Duration::from_millis(1000)).await; | ||
| 40 | } | ||
| 41 | } | ||
