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:
parent
34e9e85819
commit
3be274dc2a
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user