aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Rosenthal <[email protected]>2021-10-29 17:09:34 -0700
committerJacob Rosenthal <[email protected]>2021-10-29 17:10:37 -0700
commitef95441442e0ccab0ac6d62264dedb7254d4cb84 (patch)
treee5fd996270346f3d76c6451dd30150e8dd3be513
parent1d1d8a848e165e2754720fa442571782616cb822 (diff)
a runtime generated sin table example
-rw-r--r--examples/nrf/Cargo.toml1
-rw-r--r--examples/nrf/src/bin/pwm_simple_sin.rs41
2 files changed, 42 insertions, 0 deletions
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml
index b89aa513f..e0025b737 100644
--- a/examples/nrf/Cargo.toml
+++ b/examples/nrf/Cargo.toml
@@ -31,3 +31,4 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] }
31futures = { version = "0.3.17", default-features = false, features = ["async-await"] } 31futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
32rand = { version = "0.8.4", default-features = false } 32rand = { version = "0.8.4", default-features = false }
33embedded-storage = "0.2.0" 33embedded-storage = "0.2.0"
34micromath = "2.0.0" \ No newline at end of file
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"]
7mod example_common;
8use defmt::*;
9use embassy::executor::Spawner;
10use embassy::time::{Duration, Timer};
11use embassy_nrf::gpio::NoPin;
12use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad};
13use embassy_nrf::Peripherals;
14use micromath::F32Ext;
15
16const W1: f32 = core::f32::consts::PI / 128.0;
17
18#[embassy::main]
19async 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}