From 4982aaa59f7bde467f12ab54efd9159ca0484b91 Mon Sep 17 00:00:00 2001 From: Misha Vicha Date: Wed, 1 Oct 2025 10:06:10 +0200 Subject: [PATCH] Implements all the other signal parsing blocks. --- src/headproc.rs | 173 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 155 insertions(+), 18 deletions(-) diff --git a/src/headproc.rs b/src/headproc.rs index 3e4b23e..ece9c16 100644 --- a/src/headproc.rs +++ b/src/headproc.rs @@ -1,4 +1,4 @@ -use std::{u64, vec}; +use std::{collections::HashMap, u64, vec}; use crate::SignalFormat; @@ -134,6 +134,14 @@ impl Record { } } +#[derive(Debug, Eq, Hash, PartialEq)] +enum AdcBlockKeys { + Gain, + Baseline, + Units +} + + #[derive(Debug, Clone)] struct SignalSpec { filename: String, @@ -142,12 +150,12 @@ struct SignalSpec { skew: Option, offset: Option, adc_gain: Option, - baseline: Option, + baseline: Option, units: Option, adc_resolution: Option, - adc_zero: Option, - initial_val: Option, - checksum: Option, + adc_zero: i64, + initial_val: Option, + checksum: Option, blocksize: Option, desc: Option } @@ -170,17 +178,17 @@ impl SignalSpec { let samples_frame: Option = None; let skew: Option = None; let offset: Option = None; - let adc_gain: Option = None; - let baseline: Option = None; - let units: Option = None; - let adc_resolution: Option = None; - let adc_zero: Option = None; - let initial_val: Option = None; - let checksum: Option = None; - let blocksize: Option = None; - let desc: Option = None; + let mut adc_gain: Option = None; + let mut baseline: Option = None; + let mut units: Option = None; + let mut adc_resolution: Option = None; + let mut adc_zero:i64 = 0; + let mut initial_val: Option = None; + let mut checksum: Option = None; + let mut blocksize: Option = None; + let mut desc: Option = None; - // TODO: implement samples per frame, skew, and offset + // TODO: implement samplesperframe, skew, and offset match args[1].parse::() { Ok(value) => { sigformat = SignalSpec::parse_format(value); @@ -196,13 +204,142 @@ impl SignalSpec { } { - let bracket_split: Vec<&str> = args[2].split("(").collect(); - } + let mut results: HashMap = HashMap::new(); + let mut buffer: String = String::new(); + let mut found_baseline = false; + let mut found_units = false; + // Parse through all the characters and get the individual parts + for character in args[2].chars() { + loop { + if !found_baseline && !found_units && character == '(' { + found_baseline = true; + results.insert(AdcBlockKeys::Gain, buffer); + buffer = String::new(); + break; + } + else if found_baseline && character == ')' { + results.insert(AdcBlockKeys::Baseline, buffer); + buffer = String::new(); + break; + } + else if !found_units && character == '/' { + found_units = true; + if !found_baseline { + results.insert(AdcBlockKeys::Gain, buffer); + } + buffer = String::new(); + break; + } + buffer.push(character); + break; + } + } + if found_units { + results.insert(AdcBlockKeys::Units, buffer); + } + + // Try and get the actual values out of the results + match results.get(&AdcBlockKeys::Gain) { + Some(value ) => { + match value.parse::() { + Ok(parsed) => { + adc_gain = Some(parsed); + }, + Err(_) => { + return Err("error: Signal contains adc gain block with invalid entry"); + } + } + }, + None => { + return Err("error: Signal contains adc gain block but the block is empty."); + } + } + + match results.get(&AdcBlockKeys::Baseline) { + Some(value) => { + match value.parse::() { + Ok(parsed) => { + baseline = Some(parsed); + }, + Err(_) => { + return Err("error: Unable to parse baseline from signal"); + } + } + }, + None => {} + } + match results.get(&AdcBlockKeys::Units) { + Some(value) => { + units = Some(value.clone()); + }, + None => {} + } + } if args.len() <= 3 { - + break; } + match args[3].parse::() { + Ok(value) => { + adc_resolution = Some(value); + }, + Err(_) => { + return Err("error: unable to parse resolution from signal"); + } + } + + if args.len() <= 4 { + break; + } + match args[4].parse::() { + Ok(value) => { + adc_zero = value; + }, + Err(_) => { + adc_zero = 0; // Standard: If this field is missing, a value of zero is assumed. + } + } + + if args.len() <= 5 { + break; + } + match args[5].parse::() { + Ok(value) => { + initial_val = Some(value); + }, + Err(_) => {} // Standard: If this field is missing, adc_zero value is presumed. + // We do this on the data parsing part. + } + + if args.len() <= 6 { + break; + } + match args[6].parse::() { + Ok(value) => { + checksum = Some(value); + }, + Err(_) => { + return Err("error: unable to parse checksum from signal"); + } + } + + if args.len() <= 7 { + break; + } + match args[7].parse::() { + Ok(value) => { + blocksize = Some(value); + }, + Err(_) => { + return Err("error: unable to parse block size from signal"); + } + } + + if args.len() <= 8 { + break; + } + desc = Some(args[8].to_string()); break; }