-
Notifications
You must be signed in to change notification settings - Fork 6
/
RcvrForm.cs
461 lines (413 loc) · 16.8 KB
/
RcvrForm.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
namespace DigiRite
{
public partial class RcvrForm : Form
{
/* Show:
** a) incoming decoded messages. all of them.
** b) a spectrum (or waterfall if WriteLog is available)
** c) decoder parameter controls.
*/
public RcvrForm(MainForm mf, int inst)
{
mainForm = mf;
instanceNumber = inst;
InitializeComponent();
}
MainForm mainForm;
int instanceNumber;
LogFile logfile;
public uint CYCLE { set {
ClockLabel cl = labelClockAnimation as ClockLabel;
if (null != cl)
cl.CYCLE = value;
} }
public LogFile logFile { set { logfile = value; } }
private WaterfallManager waterfallManager;
private WriteLog.IWaterfallV3 iwaterfall
{
get
{
if (null != waterfallManager)
return waterfallManager.iwaterfall;
return null;
}
}
private WriteLog.IAnnotations annotations
{
get
{
if (null != waterfallManager)
return waterfallManager.annotations;
return null;
}
}
private WriteLog.IPeakRx peak { get {
if (null != waterfallManager) return waterfallManager.peak;
return null;
} }
private static uint AUDIO_BUFFER_SAMPLE_COUNT = 4096;
private void XcvrForm_Load(object sender, EventArgs e)
{
this.Text = String.Format("{0}-{1}", this.Text, instanceNumber);
labelPower.Text = "";
ClockLabel cl = new ClockLabel();
cl.Location = labelClockAnimation.Location;
cl.Size = labelClockAnimation.Size;
panelRightGain.Controls.Remove(labelClockAnimation);
panelRightGain.Controls.Add(cl);
labelClockAnimation.Dispose();
labelClockAnimation = cl;
var verticalBarLabel = new VerticalBarLabel();
verticalBarLabel.Location = labelVUmeter.Location;
verticalBarLabel.Size = labelVUmeter.Size;
panelRightGain.Controls.Remove(labelVUmeter);
panelRightGain.Controls.Add(verticalBarLabel);
labelVUmeter.Dispose();
labelVUmeter = verticalBarLabel;
comboBoxnDepth.SelectedIndex = Properties.Settings.Default.Decode_ndepth - 1;
checkBoxEnableAP.Checked = Properties.Settings.Default.Decode_lft8apon;
panelRightGain.BackColor =
panel1.BackColor = CustomColors.CommonBackgroundColor;
waterfallManager = new WaterfallManager();
if (waterfallManager.OnLoad(logfile, chartSpectrum, labelWaterfall, splitContainerMain.Panel2.Controls, mainForm))
{
iwaterfall.MaxHz = MaxDecodeFrequency;
iwaterfall.MinHz = MinDecodeFrequency;
myDemod.SetAudioSamplesCallback(null, AUDIO_BUFFER_SAMPLE_COUNT,
AUDIO_BUFFER_SAMPLE_COUNT, iwaterfall.GetAudioProcessor());
buttonOptions.Enabled = true;
chartSpectrum.Dispose();
chartSpectrum = null;
}
if (null != annotations)
{
checkBoxAnnotate.Checked = annotations.Visible;
checkBoxAnnotate.Enabled = true;
}
if (null != peak)
timerVU.Enabled = true;
prevSplitter = splitContainerMain.SplitterDistance;
locationToSave = Location;
sizeToSave = Size;
}
public void SetFixedFont(System.Drawing.Font font)
{
listBoxReceived.Font = font;
}
MultiDemodulatorWrapper myDemod = null;
public MultiDemodulatorWrapper demodParams { set {
myDemod = value;
if (null != value)
{
if (null != iwaterfall)
{ //plumb the demodulator's audio stream to the waterfall
myDemod.SetAudioSamplesCallback(null,
4096,
4096,
iwaterfall.GetAudioProcessor());
}
DecodeFrequencyRangeChanged(null,null);
}
} }
public void StoreProperties()
{
if (null != waterfallManager)
waterfallManager.StoreProperties();
}
XD.WaveDevicePlayer waveDevicePlayer;
public XD.WaveDevicePlayer Player { set {
waveDevicePlayer = value;
float gain = waveDevicePlayer.Gain;
bool gainOK = gain >= 0;
if (gainOK)
trackBarRxGain.Value = (int)(gain * 100);
trackBarRxGain.Enabled = gainOK;
} }
public int MinDecodeFrequency { get {
return (int)numericUpDownMinFreq.Value;}
set { numericUpDownMinFreq.Value = value; }
}
public int MaxDecodeFrequency { get {
return (int)numericUpDownMaxFreq.Value; }
set { numericUpDownMaxFreq.Value = value; }
}
#region chart spectrum
const int NUM_TO_AVERAGE = 5;
// this magic number comes from deep inside the wsjtx sources.
// Hint: stop at the bin for 5000Hz when doing a 2**14 bin FFT on 12000KHz data.
const int NUM_FREQUENCY_BINS = 6827; // each bin is 12000/(2**14) Hz
private float[] spectrum = new float[NUM_FREQUENCY_BINS];
private float[][] averagedSpectrum = new float[NUM_TO_AVERAGE][];
private int numAveragedSpectrum = 0;
private int movingAverageIdx = 0;
public void DisplaySpectrum(XDft.Demodulator demodulator)
{
if (IsDisposed)
return;
if (null == chartSpectrum)
return;
for (int j = 0; j < NUM_TO_AVERAGE; j++)
{
if (null == averagedSpectrum[j])
averagedSpectrum[j] = new float[NUM_FREQUENCY_BINS];
}
float power = 0;
/* wsjtx-2.0.1 implements its spectrum calculation in a non-reentrant
** way. The result is that ONLY one instance of XDft.Decoder
** can call GetSignalSpectrum else you won't get results that are
** of any use. You may switch from one instance to another only
** if you're willing to put up with an undefined length of time
** it takes for the new one to properly synchronize (a full 15sec
** cycle is enough, but before that, you don't know what you're getting.) */
uint samples = demodulator.GetSignalSpectrum(spectrum, ref power);
for (int i = 0; i < NUM_FREQUENCY_BINS; i++)
averagedSpectrum[movingAverageIdx][i] = spectrum[i];
movingAverageIdx += 1;
if (movingAverageIdx >= NUM_TO_AVERAGE)
movingAverageIdx = 0;
numAveragedSpectrum += 1;
if (numAveragedSpectrum >= NUM_TO_AVERAGE)
numAveragedSpectrum = NUM_TO_AVERAGE;
var series = chartSpectrum.Series[0];
series.Points.Clear();
for (int i = 0; i < 4096; i += 1)
{
double sample = 0;
for (int j = 0; j < numAveragedSpectrum; j++)
sample += averagedSpectrum[j][i];
series.Points.AddXY((5000 * i) / NUM_FREQUENCY_BINS, sample / numAveragedSpectrum);
}
labelPower.Text = power.ToString();
}
#endregion
private void DecodeFrequencyRangeChanged(object sender, EventArgs e)
{
if (null == myDemod)
return;
if (Object.ReferenceEquals(sender, numericUpDownMinFreq))
{
int max = (int)numericUpDownMaxFreq.Value - 60;
if ((numericUpDownMinFreq.Value > max))
{
if (max < numericUpDownMinFreq.Minimum)
max = (int)numericUpDownMinFreq.Minimum;
numericUpDownMinFreq.Value = max;
return;
}
}
else if (Object.ReferenceEquals(sender, numericUpDownMaxFreq))
{
int min = (int)numericUpDownMinFreq.Value + 60;
if (numericUpDownMaxFreq.Value < min)
{
if (min > numericUpDownMaxFreq.Maximum)
min = (int)numericUpDownMaxFreq.Maximum;
numericUpDownMaxFreq.Value = min;
return;
}
}
myDemod.nfa = (int)numericUpDownMinFreq.Value;
myDemod.nfb = (int)numericUpDownMaxFreq.Value;
if (null != iwaterfall)
{
iwaterfall.MaxHz = (int)numericUpDownMaxFreq.Value;
iwaterfall.MinHz = (int)numericUpDownMinFreq.Value;
}
}
#region waterfall
public int TxHz {
set {
if (null != iwaterfall)
iwaterfall.TxFreqHz = value;
}
}
public int RxHz {
set {
if (null != iwaterfall)
iwaterfall.RxFreqHz = value;
}
}
#endregion
private void comboBoxnDepth_SelectedIndexChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Decode_ndepth = comboBoxnDepth.SelectedIndex + 1;
if (null != myDemod)
myDemod.ndepth = comboBoxnDepth.SelectedIndex + 1;
}
private void checkBoxEnableAP_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Decode_lft8apon = checkBoxEnableAP.Checked;
if (null != myDemod)
myDemod.lft8apon = checkBoxEnableAP.Checked;
}
class DisplayReceivedMessage
{
public DisplayReceivedMessage(XDpack77.Pack77Message.ReceivedMessage rm)
{ m = rm; }
public override string ToString() { return m.ToString(); }
private XDpack77.Pack77Message.ReceivedMessage m;
}
const int MAX_RECEIVED_LOOKBACK = 10000; // number of entries in the history listbox
public void OnReceived(XDpack77.Pack77Message.ReceivedMessage m)
{
while (listBoxReceived.Items.Count > MAX_RECEIVED_LOOKBACK)
listBoxReceived.Items.RemoveAt(0);
listBoxReceived.Items.Add(new DisplayReceivedMessage(m));
int visibleItems = listBoxReceived.ClientSize.Height / listBoxReceived.ItemHeight;
listBoxReceived.TopIndex = Math.Max(listBoxReceived.Items.Count - visibleItems + 1, 0);
if (null != iwaterfall)
{
var call = m.Pack77Message as XDpack77.Pack77Message.ToFromCall;
if (null != call)
{
int tag = 1;
var tgString = m.TimeTag;
if (!String.IsNullOrEmpty(tgString))
Int32.TryParse(tgString, out tag);
if (null != annotations)
annotations.AddAnnotation(tag, (int)m.Hz, call.FromCall, 0);
}
}
}
bool markedWaterfallThisCycle = false;
public void OnClock(uint tick, bool isTransmit)
{
labelClock.Text = (tick/10).ToString();
if (tick < 10)
{
if (!markedWaterfallThisCycle)
{
if (null != iwaterfall)
{
var dt = System.DateTime.UtcNow;
int tag = dt.Second + dt.Minute * 100 + dt.Hour * 10000;
iwaterfall.MarkCurrentRaster(tag);
#if DEBUG
int f = (MinDecodeFrequency + MaxDecodeFrequency) / 2;
annotations.AddAnnotation(tag, f, "TEST " + f.ToString(), 0);
#endif
}
}
markedWaterfallThisCycle = true;
}
else
markedWaterfallThisCycle = false;
ClockLabel cl = labelClockAnimation as ClockLabel;
if (null != cl)
{
cl.Tenths = tick;
cl.AmTransmit = isTransmit;
}
}
private object listBoxReceivedSelectedItem = null;
private void listBoxReceived_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var item = listBoxReceived.IndexFromPoint(e.Location);
contextMenuStripOnReceived.Items[1].Enabled = item != ListBox.NoMatches;
listBoxReceived.SelectedIndex = item;
listBoxReceivedSelectedItem = listBoxReceived.Items[item];
contextMenuStripOnReceived.Show(this, new System.Drawing.Point(e.X, e.Y));//place the menu at the pointer position
}
}
private void XcvrForm_FormClosing(object sender, FormClosingEventArgs e)
{ // don't "close" this form. Minimize it.
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
private System.Drawing.Point locationToSave;
public System.Drawing.Point LocationToSave {
get { return locationToSave; }
}
private System.Drawing.Size sizeToSave;
public System.Drawing.Size SizeToSave {
get { return sizeToSave;} }
public int SplitterDistance {
get { return splitContainerMain.SplitterDistance; }
set {
if (value >= splitContainerMain.Panel1MinSize &&
value <= splitContainerMain.Width - splitContainerMain.Panel2MinSize)
{
splitContainerMain.SplitterDistance = value;
prevSplitter = value;
}
}
}
#region splitter handler kludge
int prevSplitter = -1;
bool armSplitterMoved = false;
private void splitContainerMain_SizeChanged(object sender, EventArgs e)
{
if (prevSplitter > 0)
splitContainerMain.SplitterDistance = prevSplitter;
}
private void splitContainerMain_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
armSplitterMoved = true;
}
private void splitContainerMain_SplitterMoved(object sender, SplitterEventArgs e)
{
if (armSplitterMoved)
{
armSplitterMoved = false;
prevSplitter = splitContainerMain.SplitterDistance;
}
}
#endregion
private void XcvrForm_LocationChanged(object sender, EventArgs e)
{
if (WindowState != FormWindowState.Minimized)
locationToSave = Location;
}
private void XcvrForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState != FormWindowState.Minimized)
sizeToSave = Size;
}
private void trackBarRxGain_Scroll(object sender, EventArgs e)
{
waveDevicePlayer.Gain = (float)(trackBarRxGain.Value / 100.0);
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
listBoxReceived.Items.Clear();
}
private void copyItemToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listBoxReceivedSelectedItem != null)
System.Windows.Forms.Clipboard.SetText(listBoxReceivedSelectedItem.ToString());
}
private void contextMenuStripOnReceived_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
listBoxReceived.SelectedIndex = ListBox.NoMatches;
}
private void timerVU_Tick(object sender, EventArgs e)
{
var p = peak;
if (null != p)
{
var vu = (labelVUmeter as VerticalBarLabel);
vu.Value = p.GetPeakRxAndReset();
}
}
private void checkBoxAnnotate_CheckedChanged(object sender, EventArgs e)
{
if (null != annotations)
annotations.Visible = checkBoxAnnotate.Checked;
}
private void buttonOptions_Click(object sender, EventArgs e)
{
if (waterfallManager != null)
waterfallManager.ShowOptionsDialog(this);
}
}
}