diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 2a1059836..56aa8d48c 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs @@ -262,9 +262,9 @@ impl<'d, const N: usize> Saadc<'d, N> { &mut self, bufs: &mut [[i16; N0]; 2], mode: Mode, - sampler: S, + mut sampler: S, ) where - S: Fn(&[i16]) -> SamplerState, + S: FnMut(&[i16]) -> SamplerState, { let r = Self::regs(); diff --git a/examples/nrf/src/bin/saadc_continuous.rs b/examples/nrf/src/bin/saadc_continuous.rs index fba00ebdf..8bbdca665 100644 --- a/examples/nrf/src/bin/saadc_continuous.rs +++ b/examples/nrf/src/bin/saadc_continuous.rs @@ -40,9 +40,24 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { timer.start(); let mut bufs = [[0; 3 * 500]; 2]; // Each buffer of the double buffer has to be large enough for all channels. + + let mut c = 0; + let mut a: i32 = 0; + saadc - .run_sampler(&mut bufs, Mode::Task, |buf| { - info!("sample len={}", buf.len()); + .run_sampler(&mut bufs, Mode::Task, move |buf| { + for (i, b) in buf.iter().enumerate() { + if i % 3 == 0 { + a += *b as i32; + c += 1; + } + } + if c > 10000 { + a = a / c as i32; + info!("sample: {=i32}", a); + c = 0; + a = 0; + } SamplerState::Sampled }) .await;