From c13859d486479872415d7a57315fd616aeabc099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 25 May 2021 12:05:19 +0200 Subject: [PATCH] dual-iir: add closure nesting helper macro --- src/bin/dual-iir.rs | 89 ++++++++++++++++++++++----------------------- src/lib.rs | 3 -- 2 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index 855e578..3ce9817 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -52,6 +52,15 @@ impl Default for Settings { } } +macro_rules! flatten_closures { + ($fn:ident, $e:ident, $fun:block) => { + $e.$fn(|$e| $fun ) + }; + ($fn:ident, $e:ident, $($es:ident),+, $fun:block) => { + $e.$fn(|$e| flatten_closures!($fn, $($es),*, $fun)) + }; +} + #[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, monotonic = stabilizer::hardware::SystemTimer)] const APP: () = { struct Resources { @@ -141,55 +150,43 @@ const APP: () = { let hold = settings.force_hold || (digital_inputs[1] && settings.allow_hold); - adc0.with_buffer(|a0| { - adc1.with_buffer(|a1| { - dac0.with_buffer(|d0| { - dac1.with_buffer(|d1| { - let adc_samples = [a0, a1]; - let dac_samples = [d0, d1]; + flatten_closures!(with_buffer, adc0, adc1, dac0, dac1, { + let adc_samples = [adc0, adc1]; + let dac_samples = [dac0, dac1]; - // Preserve instruction and data ordering w.r.t. DMA flag access. - fence(Ordering::SeqCst); + // Preserve instruction and data ordering w.r.t. DMA flag access. + fence(Ordering::SeqCst); - for channel in 0..adc_samples.len() { - adc_samples[channel] - .iter() - .zip(dac_samples[channel].iter_mut()) - .map(|(ai, di)| { - let x = f32::from(*ai as i16); - let y = settings.iir_ch[channel] - .iter() - .zip(iir_state[channel].iter_mut()) - .fold(x, |yi, (ch, state)| { - ch.update(state, yi, hold) - }); - // Note(unsafe): The filter limits must ensure that - // the value is in range. - // The truncation introduces 1/2 LSB distortion. - let y: i16 = - unsafe { y.to_int_unchecked() }; - // Convert to DAC code - *di = DacCode::from(y).0; - }) - .last(); - } - - // Update telemetry measurements. - telemetry.adcs = [ - AdcCode(adc_samples[0][0]), - AdcCode(adc_samples[1][0]), - ]; - - telemetry.dacs = [ - DacCode(dac_samples[0][0]), - DacCode(dac_samples[1][0]), - ]; - - // Preserve instruction and data ordering w.r.t. DMA flag access. - fence(Ordering::SeqCst); + for channel in 0..adc_samples.len() { + adc_samples[channel] + .iter() + .zip(dac_samples[channel].iter_mut()) + .map(|(ai, di)| { + let x = f32::from(*ai as i16); + let y = settings.iir_ch[channel] + .iter() + .zip(iir_state[channel].iter_mut()) + .fold(x, |yi, (ch, state)| { + ch.update(state, yi, hold) + }); + // Note(unsafe): The filter limits must ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y: i16 = unsafe { y.to_int_unchecked() }; + // Convert to DAC code + *di = DacCode::from(y).0; }) - }) - }) + .last(); + } + + // Update telemetry measurements. + telemetry.adcs = + [AdcCode(adc_samples[0][0]), AdcCode(adc_samples[1][0])]; + + telemetry.dacs = + [DacCode(dac_samples[0][0]), DacCode(dac_samples[1][0])]; + + // Preserve instruction and data ordering w.r.t. DMA flag access. + fence(Ordering::SeqCst); }); } diff --git a/src/lib.rs b/src/lib.rs index 975b08f..85964a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,5 @@ #![no_std] #![cfg_attr(feature = "nightly", feature(core_intrinsics))] -#[macro_use] -extern crate log; - pub mod hardware; pub mod net;