aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorQuentin Smith <[email protected]>2023-07-18 17:17:04 -0400
committerQuentin Smith <[email protected]>2023-07-18 17:17:04 -0400
commit2c01f277c27bc6ca4f3d41ac57aa1ea24868cfc1 (patch)
treeb5a407f457472fc70dafc3ced5e0744f25443f35 /examples
parentc333d855fca0743de1d58ef14e3c2f6389f73145 (diff)
cargo fmt
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf52840/src/bin/pdm.rs8
-rw-r--r--examples/nrf52840/src/bin/pdm_continuous.rs65
2 files changed, 38 insertions, 35 deletions
diff --git a/examples/nrf52840/src/bin/pdm.rs b/examples/nrf52840/src/bin/pdm.rs
index 47fe67733..444b9137f 100644
--- a/examples/nrf52840/src/bin/pdm.rs
+++ b/examples/nrf52840/src/bin/pdm.rs
@@ -41,9 +41,11 @@ async fn main(_p: Spawner) {
41 buf.iter().min().unwrap(), 41 buf.iter().min().unwrap(),
42 buf.iter().max().unwrap(), 42 buf.iter().max().unwrap(),
43 mean, 43 mean,
44 ( 44 (buf.iter()
45 buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) 45 .map(|v| i32::from(*v - mean).pow(2))
46 / buf.len() as i32).sqrt() as i16, 46 .fold(0i32, |a, b| a.saturating_add(b))
47 / buf.len() as i32)
48 .sqrt() as i16,
47 ); 49 );
48 50
49 info!("samples: {:?}", &buf); 51 info!("samples: {:?}", &buf);
diff --git a/examples/nrf52840/src/bin/pdm_continuous.rs b/examples/nrf52840/src/bin/pdm_continuous.rs
index 9eaf30717..7d8531475 100644
--- a/examples/nrf52840/src/bin/pdm_continuous.rs
+++ b/examples/nrf52840/src/bin/pdm_continuous.rs
@@ -2,14 +2,15 @@
2#![no_main] 2#![no_main]
3#![feature(type_alias_impl_trait)] 3#![feature(type_alias_impl_trait)]
4 4
5use defmt::info;
6use core::cmp::Ordering; 5use core::cmp::Ordering;
6
7use defmt::info;
7use embassy_executor::Spawner; 8use embassy_executor::Spawner;
9use embassy_nrf::pdm::{self, Config, Frequency, OperationMode, Pdm, Ratio, SamplerState};
8use embassy_nrf::{bind_interrupts, peripherals}; 10use embassy_nrf::{bind_interrupts, peripherals};
9use embassy_nrf::pdm::{self, Config, OperationMode, Pdm, SamplerState, Frequency, Ratio};
10use fixed::types::I7F1; 11use fixed::types::I7F1;
11use num_integer::Roots;
12use microfft::real::rfft_1024; 12use microfft::real::rfft_1024;
13use num_integer::Roots;
13use {defmt_rtt as _, panic_probe as _}; 14use {defmt_rtt as _, panic_probe as _};
14 15
15// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer 16// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer
@@ -31,34 +32,34 @@ async fn main(_p: Spawner) {
31 32
32 let mut bufs = [[0; 1024]; 2]; 33 let mut bufs = [[0; 1024]; 2];
33 34
34 pdm 35 pdm.run_task_sampler(&mut bufs, move |buf| {
35 .run_task_sampler( 36 // NOTE: It is important that the time spent within this callback
36 &mut bufs, 37 // does not exceed the time taken to acquire the 1500 samples we
37 move |buf| { 38 // have in this example, which would be 10us + 2us per
38 // NOTE: It is important that the time spent within this callback 39 // sample * 1500 = 18ms. You need to measure the time taken here
39 // does not exceed the time taken to acquire the 1500 samples we 40 // and set the sample buffer size accordingly. Exceeding this
40 // have in this example, which would be 10us + 2us per 41 // time can lead to the peripheral re-writing the other buffer.
41 // sample * 1500 = 18ms. You need to measure the time taken here 42 let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16;
42 // and set the sample buffer size accordingly. Exceeding this 43 let (peak_freq_index, peak_mag) = fft_peak_freq(&buf);
43 // time can lead to the peripheral re-writing the other buffer. 44 let peak_freq = peak_freq_index * 16000 / buf.len();
44 let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16; 45 info!(
45 let (peak_freq_index, peak_mag) = fft_peak_freq(&buf); 46 "{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}, peak {} @ {} Hz",
46 let peak_freq = peak_freq_index * 16000 / buf.len(); 47 buf.len(),
47 info!( 48 buf.iter().min().unwrap(),
48 "{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}, peak {} @ {} Hz", 49 buf.iter().max().unwrap(),
49 buf.len(), 50 mean,
50 buf.iter().min().unwrap(), 51 (buf.iter()
51 buf.iter().max().unwrap(), 52 .map(|v| i32::from(*v - mean).pow(2))
52 mean, 53 .fold(0i32, |a, b| a.saturating_add(b))
53 ( 54 / buf.len() as i32)
54 buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) 55 .sqrt() as i16,
55 / buf.len() as i32).sqrt() as i16, 56 peak_mag,
56 peak_mag, peak_freq, 57 peak_freq,
57 ); 58 );
58 SamplerState::Sampled 59 SamplerState::Sampled
59 }, 60 })
60 ) 61 .await
61 .await.unwrap(); 62 .unwrap();
62} 63}
63 64
64fn fft_peak_freq(input: &[i16; 1024]) -> (usize, u32) { 65fn fft_peak_freq(input: &[i16; 1024]) -> (usize, u32) {
@@ -75,6 +76,6 @@ fn fft_peak_freq(input: &[i16; 1024]) -> (usize, u32) {
75 .map(|c| c.norm_sqr()) 76 .map(|c| c.norm_sqr())
76 .enumerate() 77 .enumerate()
77 .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal)) 78 .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
78 .map(|(i, v)| (i, ((v*32768.0) as u32).sqrt())) 79 .map(|(i, v)| (i, ((v * 32768.0) as u32).sqrt()))
79 .unwrap() 80 .unwrap()
80} 81}