diff options
| author | huntc <[email protected]> | 2021-10-15 17:44:23 +1100 |
|---|---|---|
| committer | huntc <[email protected]> | 2021-10-18 10:26:11 +1100 |
| commit | 3be274dc2a471bb837e53f06065a20ec48d445d5 (patch) | |
| tree | 59b481a688d5c5536f47925144189584019a9c36 | |
| parent | 34e9e8581914af6f91ac21de9f027ddb0b67e9c4 (diff) | |
We must allow the run handler to mutate state
The handler may well need to close over and mutate state
| -rw-r--r-- | embassy-nrf/src/saadc.rs | 4 | ||||
| -rw-r--r-- | examples/nrf/src/bin/saadc_continuous.rs | 19 |
2 files changed, 19 insertions, 4 deletions
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> { | |||
| 262 | &mut self, | 262 | &mut self, |
| 263 | bufs: &mut [[i16; N0]; 2], | 263 | bufs: &mut [[i16; N0]; 2], |
| 264 | mode: Mode, | 264 | mode: Mode, |
| 265 | sampler: S, | 265 | mut sampler: S, |
| 266 | ) where | 266 | ) where |
| 267 | S: Fn(&[i16]) -> SamplerState, | 267 | S: FnMut(&[i16]) -> SamplerState, |
| 268 | { | 268 | { |
| 269 | let r = Self::regs(); | 269 | let r = Self::regs(); |
| 270 | 270 | ||
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) { | |||
| 40 | timer.start(); | 40 | timer.start(); |
| 41 | 41 | ||
| 42 | let mut bufs = [[0; 3 * 500]; 2]; // Each buffer of the double buffer has to be large enough for all channels. | 42 | let mut bufs = [[0; 3 * 500]; 2]; // Each buffer of the double buffer has to be large enough for all channels. |
| 43 | |||
| 44 | let mut c = 0; | ||
| 45 | let mut a: i32 = 0; | ||
| 46 | |||
| 43 | saadc | 47 | saadc |
| 44 | .run_sampler(&mut bufs, Mode::Task, |buf| { | 48 | .run_sampler(&mut bufs, Mode::Task, move |buf| { |
| 45 | info!("sample len={}", buf.len()); | 49 | for (i, b) in buf.iter().enumerate() { |
| 50 | if i % 3 == 0 { | ||
| 51 | a += *b as i32; | ||
| 52 | c += 1; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | if c > 10000 { | ||
| 56 | a = a / c as i32; | ||
| 57 | info!("sample: {=i32}", a); | ||
| 58 | c = 0; | ||
| 59 | a = 0; | ||
| 60 | } | ||
| 46 | SamplerState::Sampled | 61 | SamplerState::Sampled |
| 47 | }) | 62 | }) |
| 48 | .await; | 63 | .await; |
