lockin: change demodulate to return result instead of option

This commit is contained in:
Matt Huszagh 2020-11-24 23:44:39 -08:00
parent da4430e912
commit 4edda09d86
2 changed files with 12 additions and 10 deletions

View File

@ -175,8 +175,10 @@ impl Lockin {
adc_samples: [i16; ADC_SAMPLE_BUFFER_SIZE], adc_samples: [i16; ADC_SAMPLE_BUFFER_SIZE],
timestamps: [u16; TIMESTAMP_BUFFER_SIZE], timestamps: [u16; TIMESTAMP_BUFFER_SIZE],
valid_timestamps: u16, valid_timestamps: u16,
) -> Option<([f32; ADC_SAMPLE_BUFFER_SIZE], [f32; ADC_SAMPLE_BUFFER_SIZE])> ) -> Result<
{ ([f32; ADC_SAMPLE_BUFFER_SIZE], [f32; ADC_SAMPLE_BUFFER_SIZE]),
&str,
> {
// update old timestamps for new ADC batch // update old timestamps for new ADC batch
let sample_period = self.sample_period as i32; let sample_period = self.sample_period as i32;
self.timestamps.iter_mut().for_each(|t| match *t { self.timestamps.iter_mut().for_each(|t| match *t {
@ -202,7 +204,7 @@ impl Lockin {
// return prematurely if there aren't enough timestamps for // return prematurely if there aren't enough timestamps for
// processing // processing
if self.timestamps.iter().filter(|t| t.is_some()).count() < 2 { if self.timestamps.iter().filter(|t| t.is_some()).count() < 2 {
return None; return Err("insufficient timestamps");
} }
// compute ADC sample phases, sines/cosines and demodulate // compute ADC sample phases, sines/cosines and demodulate
@ -227,7 +229,7 @@ impl Lockin {
*q = cosine * sample; *q = cosine * sample;
}); });
Some((in_phase, quadrature)) Ok((in_phase, quadrature))
} }
/// Filter the in-phase and quadrature signals using the supplied /// Filter the in-phase and quadrature signals using the supplied
@ -448,7 +450,7 @@ mod tests {
[0; TIMESTAMP_BUFFER_SIZE], [0; TIMESTAMP_BUFFER_SIZE],
0 0
), ),
None Err("insufficient timestamps")
); );
} }
@ -471,7 +473,7 @@ mod tests {
[0; TIMESTAMP_BUFFER_SIZE], [0; TIMESTAMP_BUFFER_SIZE],
1 1
), ),
None Err("insufficient timestamps")
); );
} }

View File

@ -601,11 +601,11 @@ fn lowpass_test(
let lockin_demodulate = let lockin_demodulate =
lockin.demodulate(signal, timestamps, valid_timestamps as u16); lockin.demodulate(signal, timestamps, valid_timestamps as u16);
match lockin_demodulate { match lockin_demodulate {
Some(i) => { Ok((i, q)) => {
in_phase = i.0; in_phase = i;
quadrature = i.1; quadrature = q;
} }
None => { Err(_) => {
continue; continue;
} }
} }