From 3c4e83bf0ffecf0ae93dcc21987280f76ab8ff0b Mon Sep 17 00:00:00 2001 From: Matt Huszagh Date: Tue, 24 Nov 2020 23:30:57 -0800 Subject: [PATCH] lockin: move fifo trait before use This clarifies what it means to "push" to an array. --- dsp/src/lockin.rs | 52 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/dsp/src/lockin.rs b/dsp/src/lockin.rs index 3645754..8b7e51d 100644 --- a/dsp/src/lockin.rs +++ b/dsp/src/lockin.rs @@ -74,6 +74,32 @@ pub const TIMESTAMP_BUFFER_SIZE: usize = ADC_SAMPLE_BUFFER_SIZE / 2; /// The number of outputs sent to the DAC for each ADC batch. pub const DECIMATED_BUFFER_SIZE: usize = 1; +/// Treat the 2-element array as a FIFO. This allows new elements to +/// be pushed into the array, existing elements to shift back in the +/// array, and the last element to fall off the array. +trait Fifo2 { + fn push(&mut self, new_element: Option); +} + +impl Fifo2 for [Option; 2] { + /// Push a new element into the array. The existing elements move + /// backward in the array by one location, and the current last + /// element is discarded. + /// + /// # Arguments + /// + /// * `new_element` - New element pushed into the front of the + /// array. + fn push(&mut self, new_element: Option) { + // For array sizes greater than 2 it would be preferable to + // use a rotating index to avoid unnecessary data + // copying. However, this would somewhat complicate the use of + // iterators and for 2 elements, shifting is inexpensive. + self[1] = self[0]; + self[0] = new_element; + } +} + /// Performs lock-in amplifier processing of a signal. pub struct Lockin { phase_offset: f32, @@ -274,32 +300,6 @@ pub fn magnitude_phase(in_phase: &mut [f32], quadrature: &mut [f32]) { }); } -/// Treat the 2-element array as a FIFO. This allows new elements to -/// be pushed into the array, existing elements to shift back in the -/// array, and the last element to fall off the array. -trait Fifo2 { - fn push(&mut self, new_element: Option); -} - -impl Fifo2 for [Option; 2] { - /// Push a new element into the array. The existing elements move - /// backward in the array by one location, and the current last - /// element is discarded. - /// - /// # Arguments - /// - /// * `new_element` - New element pushed into the front of the - /// array. - fn push(&mut self, new_element: Option) { - // For array sizes greater than 2 it would be preferable to - // use a rotating index to avoid unnecessary data - // copying. However, this would somewhat complicate the use of - // iterators and for 2 elements, shifting is inexpensive. - self[1] = self[0]; - self[0] = new_element; - } -} - #[cfg(test)] mod tests { use super::*;