From 3be274dc2a471bb837e53f06065a20ec48d445d5 Mon Sep 17 00:00:00 2001 From: huntc Date: Fri, 15 Oct 2021 17:44:23 +1100 Subject: We must allow the run handler to mutate state The handler may well need to close over and mutate state --- examples/nrf/src/bin/saadc_continuous.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'examples') 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; -- cgit