a runtime generated sin table example

This commit is contained in:
Jacob Rosenthal 2021-10-29 17:09:34 -07:00
parent 1d1d8a848e
commit ef95441442
2 changed files with 42 additions and 0 deletions

View File

@ -31,3 +31,4 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
rand = { version = "0.8.4", default-features = false } rand = { version = "0.8.4", default-features = false }
embedded-storage = "0.2.0" embedded-storage = "0.2.0"
micromath = "2.0.0"

View File

@ -0,0 +1,41 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(array_from_fn)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::*;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::gpio::NoPin;
use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad};
use embassy_nrf::Peripherals;
use micromath::F32Ext;
const W1: f32 = core::f32::consts::PI / 128.0;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
// probably not best use of resources to create the table at runtime, but makes testing fast
let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16);
let config = LoopingConfig {
counter_mode: CounterMode::UpAndDown,
top: 12000,
prescaler: Prescaler::Div16,
sequence: &seq_values,
sequence_load: SequenceLoad::Common,
repeats: 0,
enddelay: 0,
};
let _pwm = unwrap!(Pwm::simple_playback(
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config
));
info!("pwm started!");
loop {
Timer::after(Duration::from_millis(1000)).await;
}
}