dual-iir: add closure nesting helper macro

This commit is contained in:
Robert Jördens 2021-05-25 12:05:19 +02:00
parent 2a9657f98c
commit c13859d486
2 changed files with 43 additions and 49 deletions

View File

@ -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,12 +150,9 @@ 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);
@ -163,11 +169,9 @@ const APP: () = {
.fold(x, |yi, (ch, state)| {
ch.update(state, yi, hold)
});
// Note(unsafe): The filter limits must ensure that
// the value is in range.
// 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() };
let y: i16 = unsafe { y.to_int_unchecked() };
// Convert to DAC code
*di = DacCode::from(y).0;
})
@ -175,21 +179,14 @@ const APP: () = {
}
// Update telemetry measurements.
telemetry.adcs = [
AdcCode(adc_samples[0][0]),
AdcCode(adc_samples[1][0]),
];
telemetry.adcs =
[AdcCode(adc_samples[0][0]), AdcCode(adc_samples[1][0])];
telemetry.dacs = [
DacCode(dac_samples[0][0]),
DacCode(dac_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);
})
})
})
});
}

View File

@ -1,8 +1,5 @@
#![no_std]
#![cfg_attr(feature = "nightly", feature(core_intrinsics))]
#[macro_use]
extern crate log;
pub mod hardware;
pub mod net;