2022-07-13 15:07:56 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
2023-01-31 22:12:43 +00:00
using System.Text.RegularExpressions ;
using System.Threading.Channels ;
2022-07-13 15:07:56 +00:00
using System.Threading.Tasks ;
using CommandLine ;
namespace CLCapture
{
2023-02-04 14:49:15 +00:00
[Verb(name: "capture", HelpText = "Start a signal capture.")]
2023-01-31 22:12:43 +00:00
public class CLCaptureOptions
2022-07-13 15:07:56 +00:00
{
2023-01-31 22:12:43 +00:00
[Value(0, Required = true, HelpText = "Device's serial port or IP address and port.")]
public string? AddressPort { get ; set ; }
2023-07-20 15:07:27 +00:00
2022-07-13 15:07:56 +00:00
[Value(1, Required = true, HelpText = "Desired sampling frequency.")]
public int SamplingFrequency { get ; set ; }
2023-07-20 15:07:27 +00:00
2023-06-28 11:32:35 +00:00
[Value(2, Required = true, HelpText = "List of channels to capture (channels sepparated by comma, can contain a name adding a semicolon after the channel number, no spaces allowed).")]
2022-07-13 15:07:56 +00:00
public string? Channels { get ; set ; }
2023-07-20 15:07:27 +00:00
2022-07-13 15:07:56 +00:00
[Value(3, Required = true, HelpText = "Number of samples to capture before the trigger.")]
public int PreSamples { get ; set ; }
2023-07-20 15:07:27 +00:00
2022-07-13 15:07:56 +00:00
[Value(4, Required = true, HelpText = "Number of samples to capture after the trigger.")]
public int PostSamples { get ; set ; }
2023-07-20 15:07:27 +00:00
[Value(5, Required = true, HelpText = "Number of bursts to capture (0 or 1 to disable burst mode).")]
public int LoopCount { get ; set ; }
[Value(6, Required = true, HelpText = "Trigger definition in the form of \"TriggerType:(Edge, Fast or Complex),Channel:(base trigger channel),Value:(string containing 1's and 0's indicating each trigger chanel state)\".")]
2022-07-13 15:07:56 +00:00
public CLTrigger ? Trigger { get ; set ; }
2023-07-20 15:07:27 +00:00
[Value(7, Required = true, HelpText = "Name of the output file.")]
2022-07-13 15:07:56 +00:00
public string? OutputFile { get ; set ; }
}
public class CLTrigger
{
public CLTrigger ( string Data )
{
string [ ] parts = Data . Split ( "," , StringSplitOptions . RemoveEmptyEntries ) ;
if ( parts = = null | | parts . Length ! = 3 )
throw new ArgumentException ( "Invalid trigger parameters." ) ;
foreach ( var part in parts )
{
string [ ] components = part . Split ( ":" , StringSplitOptions . RemoveEmptyEntries ) ;
if ( components = = null | | components . Length ! = 2 )
throw new ArgumentException ( "Invalid trigger parameters." ) ;
switch ( components [ 0 ] . ToLower ( ) )
{
case "triggertype" :
CLTriggerType type ;
var typeParsed = Enum . TryParse < CLTriggerType > ( components [ 1 ] , true , out type ) ;
if ( ! typeParsed )
throw new ArgumentException ( $"Unknown trigger type: {type}." ) ;
TriggerType = type ;
break ;
case "channel" :
int channel ;
if ( ! int . TryParse ( components [ 1 ] , out channel ) )
throw new ArgumentException ( $"Invalid value for trigger channel." ) ;
Channel = channel ;
break ;
case "value" :
if ( components [ 1 ] . Any ( v = > v ! = '0' & & v ! = '1' ) )
throw new ArgumentException ( $"Trigger values can only be composed of '0's or '1's." ) ;
Value = components [ 1 ] ;
break ;
default :
throw new ArgumentException ( $"Unknown trigger parameter: {components[0]}" ) ;
}
}
}
public CLTriggerType TriggerType { get ; set ; }
public int Channel { get ; set ; }
public string Value { get ; set ; }
}
public enum CLTriggerType
{
Edge ,
Fast ,
Complex
}
}