We must allow the run handler to mutate state

The handler may well need to close over and mutate state
This commit is contained in:
huntc 2021-10-15 17:44:23 +11:00
parent 34e9e85819
commit 3be274dc2a
2 changed files with 19 additions and 4 deletions

View File

@ -262,9 +262,9 @@ impl<'d, const N: usize> Saadc<'d, N> {
&mut self, &mut self,
bufs: &mut [[i16; N0]; 2], bufs: &mut [[i16; N0]; 2],
mode: Mode, mode: Mode,
sampler: S, mut sampler: S,
) where ) where
S: Fn(&[i16]) -> SamplerState, S: FnMut(&[i16]) -> SamplerState,
{ {
let r = Self::regs(); let r = Self::regs();

View File

@ -40,9 +40,24 @@ async fn main(_spawner: Spawner, mut p: Peripherals) {
timer.start(); 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 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 saadc
.run_sampler(&mut bufs, Mode::Task, |buf| { .run_sampler(&mut bufs, Mode::Task, move |buf| {
info!("sample len={}", buf.len()); 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 SamplerState::Sampled
}) })
.await; .await;