-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (118 loc) · 4.73 KB
/
Program.cs
File metadata and controls
142 lines (118 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using NDesk.Options;
using Suisse00.Hardware.Quantities;
using System;
using System.Linq;
namespace Suisse00.CSV2OLS
{
internal class Program
{
private Frequency _frequency = new Frequency(100, FrequencyUnit.MHz);
private int[] _channelsToUse = new int[] { 1 };
private double _highSignalVoltage = 0.1;
private string[] _delimiters = new string[] { "," };
private int _linesToSkip = 2;
private const char CHANNEL_SEPARATOR = ',';
static void Main(string[] args)
{
new Program().Run(args);
}
private void Run(string[] args)
{
OptionSet options = GetOptions();
try
{
options.Parse(args);
Console.WriteLine(";Rate: " + _frequency.Hertz.ToString());
Console.WriteLine(";Channels: " + _channelsToUse.Length.ToString());
string OLSOutputFormat = "{0:x" + Math.Ceiling((decimal)_channelsToUse.Length / 8).ToString() + "}@{1}";
int indexLine = 0;
for (string line = Console.ReadLine(); line != null; line = Console.ReadLine())
{
if (_linesToSkip > 0)
{
_linesToSkip--;
continue;
}
Console.WriteLine(String.Format(OLSOutputFormat,
GetOLSValue(line.Split(_delimiters, StringSplitOptions.None), _channelsToUse),
indexLine));
indexLine++;
}
}
catch (OptionException e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine("Try --help");
}
}
private OptionSet GetOptions()
{
OptionSet options = new OptionSet()
{
{
"f|frequency=",
String.Format("The frequency of of simpling (default: {0})",
_frequency.ToString()),
v => _frequency = Frequency.Parse(v)
},
{
"c|channels=",
String.Format("Channels to use [1, X] divized by a comma (default: {0})",
String.Join(CHANNEL_SEPARATOR.ToString(), _channelsToUse)),
v => _channelsToUse = v.Split(CHANNEL_SEPARATOR)
.Select(channel => int.Parse(channel))
.ToArray()
},
{
"hs|highsignal=",
String.Format("Threshold which set the signal to high (default: {0})",
_highSignalVoltage),
v => _highSignalVoltage = double.Parse(v)
},
{
"d|delimiter=",
String.Format("Column delimiter (default: {0})",
_delimiters.First()),
v => _delimiters = new string[] { v }
},
{
"s|skipline=",
String.Format("Number of line to skip from the input (default: {0})",
_linesToSkip),
v => _linesToSkip = int.Parse(v)
}
};
options.Add("help",
"Display available commands",
v =>
{
options.WriteOptionDescriptions(Console.Out);
Environment.Exit(0);
});
return options;
}
public static int GetOLSValue(string[] rawValues, int[] channelsToUse, double highSignalVoltage)
{
int result = 0;
foreach (int channelIndex in channelsToUse)
{
int booleanValue = 0;
if (rawValues.Length > channelIndex)
{
booleanValue = (String.IsNullOrEmpty(rawValues[channelIndex])
? 0
: double.Parse(rawValues[channelIndex]))
> highSignalVoltage
? 1
: 0;
}
result |= booleanValue << channelIndex - 1;
}
return result;
}
private int GetOLSValue(string[] rawValues, int[] channelsToUse)
{
return GetOLSValue(rawValues, channelsToUse, _highSignalVoltage);
}
}
}