diff --git a/src/blocks/fft.rs b/src/blocks/fft.rs index f27dbbeb..afb0e2f5 100644 --- a/src/blocks/fft.rs +++ b/src/blocks/fft.rs @@ -10,6 +10,7 @@ use crate::runtime::BlockMetaBuilder; use crate::runtime::Kernel; use crate::runtime::MessageIo; use crate::runtime::MessageIoBuilder; +use crate::runtime::Pmt; use crate::runtime::StreamIo; use crate::runtime::StreamIoBuilder; use crate::runtime::WorkIo; @@ -26,6 +27,10 @@ use crate::runtime::WorkIo; /// /// `out`: FFT results (Complex32) /// +/// # Messages +/// +/// `fft_size`: Change the FFT size (Pmt::Usize) +/// /// # Usage /// ``` /// use futuresdr::blocks::Fft; @@ -80,7 +85,9 @@ impl Fft { .add_input::("in") .add_output::("out") .build(), - MessageIoBuilder::::new().build(), + MessageIoBuilder::::new() + .add_input("fft_size", Self::fft_size_handler) + .build(), Fft { len, plan, @@ -91,6 +98,37 @@ impl Fft { }, ) } + + /// Handle incoming messages to change FFT size + #[message_handler] + fn fft_size_handler( + &mut self, + _io: &mut WorkIo, + _mio: &mut MessageIo, + _meta: &mut BlockMeta, + p: Pmt, + ) -> Result { + match p { + Pmt::Usize(new_len) => self.set_fft_size(new_len), + Pmt::Null => Ok(Pmt::Usize(self.len)), + _ => Ok(Pmt::InvalidValue), + } + } + + /// Set a new FFT size + fn set_fft_size(&mut self, new_len: usize) -> Result { + let mut planner = FftPlanner::::new(); + let new_plan = match self.direction { + FftDirection::Forward => planner.plan_fft_forward(new_len), + FftDirection::Inverse => planner.plan_fft_inverse(new_len), + }; + + self.len = new_len; + self.plan = new_plan; + self.scratch = vec![Complex32::new(0.0, 0.0); new_len * 10].into_boxed_slice(); + + Ok(Pmt::Ok) + } } #[doc(hidden)]