2022-07-10 20:12:25 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::info;
|
|
|
|
use embassy_executor::Spawner;
|
2023-03-05 20:50:45 +01:00
|
|
|
use embassy_nrf::pdm::{self, Config, Pdm};
|
|
|
|
use embassy_nrf::{bind_interrupts, peripherals};
|
2023-10-15 01:57:25 +02:00
|
|
|
use embassy_time::Timer;
|
2023-07-18 03:31:43 +02:00
|
|
|
use fixed::types::I7F1;
|
|
|
|
use num_integer::Roots;
|
2022-07-10 20:12:25 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2023-03-05 20:50:45 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
PDM => pdm::InterruptHandler<peripherals::PDM>;
|
|
|
|
});
|
|
|
|
|
2022-07-10 20:12:25 +02:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_p: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
|
|
|
let config = Config::default();
|
2023-03-05 20:50:45 +01:00
|
|
|
let mut pdm = Pdm::new(p.PDM, Irqs, p.P0_01, p.P0_00, config);
|
2022-07-10 20:12:25 +02:00
|
|
|
|
|
|
|
loop {
|
2023-07-18 03:31:43 +02:00
|
|
|
for gain in [I7F1::from_num(-20), I7F1::from_num(0), I7F1::from_num(20)] {
|
|
|
|
pdm.set_gain(gain, gain);
|
|
|
|
info!("Gain = {} dB", defmt::Debug2Format(&gain));
|
|
|
|
pdm.start().await;
|
|
|
|
|
|
|
|
// wait some time till the microphon settled
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(1000).await;
|
2023-07-18 03:31:43 +02:00
|
|
|
|
|
|
|
const SAMPLES: usize = 2048;
|
|
|
|
let mut buf = [0i16; SAMPLES];
|
|
|
|
pdm.sample(&mut buf).await.unwrap();
|
|
|
|
|
|
|
|
let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16;
|
|
|
|
info!(
|
|
|
|
"{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}",
|
|
|
|
buf.len(),
|
|
|
|
buf.iter().min().unwrap(),
|
|
|
|
buf.iter().max().unwrap(),
|
|
|
|
mean,
|
2023-07-18 23:17:04 +02:00
|
|
|
(buf.iter()
|
|
|
|
.map(|v| i32::from(*v - mean).pow(2))
|
|
|
|
.fold(0i32, |a, b| a.saturating_add(b))
|
|
|
|
/ buf.len() as i32)
|
|
|
|
.sqrt() as i16,
|
2023-07-18 03:31:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
info!("samples: {:?}", &buf);
|
|
|
|
|
|
|
|
pdm.stop().await;
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(100).await;
|
2023-07-18 03:31:43 +02:00
|
|
|
}
|
2022-07-10 20:12:25 +02:00
|
|
|
}
|
|
|
|
}
|