This repository was archived by the owner on Nov 7, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathThreadDraw.cs
More file actions
484 lines (397 loc) · 18.2 KB
/
Copy pathThreadDraw.cs
File metadata and controls
484 lines (397 loc) · 18.2 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
using System;
using System.Threading;
using System.Windows.Forms;
namespace PasiveRadar
{
public unsafe partial class Form1
{
Thread thread_calculate_draw_data = null;
volatile bool calculate_draw_data_exit = false;
volatile bool calculate_draw_data_exited = false;
Thread thread_calculate_radar = null;
volatile bool calculate_radar_exit = false;
volatile bool calculate_radar_exited = false;
private readonly Object LockRadar = new Object();
public AutoResetEvent DrawEvent2 = new AutoResetEvent(false);
public AutoResetEvent DrawEvent = new AutoResetEvent(false);
private bool resizing;
WindowWave[] window_wave;
WindowFlow[] window_flow;
double[][] dataRadio;
float[] dataRadar;
double[] dataDifference;
float[] PostProc;
Complex[][] DataIn;
Complex[][] DataOut;
bool[] MouseDownPanel;
int[] oldX_position;
double[] MHz_perPixel;
bool FilterMove = false;
bool RightBandMove = false;
bool LeftBandMove = false;
public void StartDraw()
{
//Draw radar only the scale and crosses
if (flags.showRadar0)
windowRadar.RenderRadar(PostProc, true);
calculate_draw_data_exit = false;
calculate_draw_data_exited = false;
if (thread_calculate_draw_data == null)
{
thread_calculate_draw_data = new Thread(new ThreadStart(CalculateDataForScenes));
thread_calculate_draw_data.Priority = System.Threading.ThreadPriority.Normal;
thread_calculate_draw_data.Start();
}
calculate_radar_exit = false;
calculate_radar_exited = false;
if (thread_calculate_radar == null)
{
thread_calculate_radar = new Thread(new ThreadStart(CalculateRadarScene));
thread_calculate_radar.Priority = System.Threading.ThreadPriority.AboveNormal;
thread_calculate_radar.Start();
}
}
public void StopDraw()
{
//Stop thread calculate data for scenes
if (thread_calculate_draw_data != null)
{
calculate_draw_data_exit = true;
while (!calculate_draw_data_exited) ;
thread_calculate_draw_data = null;
}
////Stop thread calculate data for radar
if (thread_calculate_radar != null)
{
calculate_radar_exit = true;
while (!calculate_radar_exited) ;
thread_calculate_radar = null;
}
}
private void CalculateDataForScenes()
{
DataIn = new Complex[Flags.MAX_DONGLES][];
for (int i = 0; i < Flags.MAX_DONGLES; i++)
DataIn[i] = new Complex[flags.BufferSizeRadio[i]];//+ flags.Negative + flags.Positive
DataOut = new Complex[Flags.MAX_DONGLES][];
for (int i = 0; i < Flags.MAX_DONGLES; i++)
DataOut[i] = new Complex[flags.BufferSizeRadio[i]];
while (!calculate_draw_data_exit)
{
DrawAll();
}
calculate_draw_data_exited = true;
}
//Calculate the scenes and draw them
void DrawAll()
{
int i;
//Slow it down a bit
if (flags.refresh_delay > 0)
System.Threading.Thread.Sleep(flags.refresh_delay);
//Start FFT calculations
for (i = 0; i < Flags.MAX_DONGLES; i++)
{
if (rd[i].Visible & (flags.showRadioWave[i] | flags.showRadioFlow[i]))
{
calculate[i].CopyToComplex(radio[i].dataIQ, ref DataIn[i], false);
calculate[i].FFT(DataIn[i], DataOut[i]);
}
}
//Now the trick to calculate both FFT in the same time
//Wait until completed FFT calculations
for (i = 0; i < Flags.MAX_DONGLES; i++)
if (i == 0 || i == 1)
{
if (rd[i].Visible & (flags.showRadioWave[i] || flags.showRadioFlow[i]))
calculate[i].FFTWaitToComplete();
}
//Start calculations FFT data stream
for (i = 0; i < Flags.MAX_DONGLES; i++)
if (i == 0 || i == 1)
{
if (rd[i].Visible & (flags.showRadioWave[i] || flags.showRadioFlow[i]))
calculate[i].Start(DataOut[i], dataRadio[i], flags.Cumulation[i], flags.Amplification[i]);
}
//Wait until completed calculations for streams
for (i = 0; i < Flags.MAX_DONGLES; i++)
{
if (rd[i].Visible & (flags.showRadioWave[i] || flags.showRadioFlow[i]))
calculate[i].WaitToFinischCalc();
}
//Draw scenes waves
for (i = 0; i < Flags.MAX_DONGLES; i++)
if (rd[i].Visible & flags.showRadioWave[i])
window_wave[i].RenderWave(dataRadio[i]);
//Draw scenes flows
for (i = 0; i < Flags.MAX_DONGLES; i++)
if (rd[i].Visible & flags.showRadioFlow[i])
window_flow[i].RenderFlow(dataRadio[i]);
}
private void CalculateRadarScene()
{
uint CorrelationShift = 0;
float[] DataCorrelate = new float[flags.BufferSize + flags.Negative + flags.Positive];
correlate.Init(flags.BufferSize);
while (!calculate_radar_exit)
{
if (runing)
{
//Copy
lock (LockRadar)
{
if (flags.BufferSize > dataOutRadio0.Length || flags.BufferSize > dataOutRadio1.Length)
return;
if (radio[0] != null)
for (int i = 0; i < flags.BufferSize; i++)
dataOutRadio0[i] = radio[0].dataIQ[i] - 128;
else if (radio[1] != null)
for (int i = 0; i < flags.BufferSize; i++)
dataOutRadio0[i] = radio[1].dataIQ[i] - 128;
if (radio[1] != null & flags.TwoDonglesMode)
for (int i = 0; i < flags.BufferSize; i++)
dataOutRadio1[i] = radio[1].dataIQ[i] - 128;
}
//Correlate data streams
if (flags.TwoDonglesMode)
correlate.Begin(dataOutRadio0, dataOutRadio1, DataCorrelate, ref CorrelationShift, flags);
//Calculate ambiguity map extremally slow 10x
if (flags.showRadar0 & runing)
try
{
ambiguity.StartGPU(dataOutRadio0, dataOutRadio1, dataRadar, flags);
ambiguity.StopGPU();
}
catch (Exception ex)
{
String str = "Error ambiguity function. " + ex.ToString();
MessageBox.Show(str);
break;
}
//Average maps; ups it was slow
if (flags.showRadar0)
radar_cumulate.Run(dataRadar, PostProc, flags.average);
if (flags.CorrectBackground)
{
if (!flags.FreezeBackground)
Regresion.Add(PostProc, flags.ColectEvery); //Add frames for regresion fit to find the best background correction, the second parameter describe how othen to add the element
Regresion.CorrectBackground(PostProc, flags.CorectionWeight);
}
//Draw radar
if (flags.showRadar0)
windowRadar.RenderRadar(PostProc, true);
//Correlate
if (flags.showCorrelateWave0)
{
windowCorrelateWave.RenderCorrelate(DataCorrelate, flags);
flags.Resynchronisation = false; //reset flag
}
if (flags.showCorrelateFlow0)
{
windowCorrelateFlow.RenderCorrelateFlow(DataCorrelate, CorrelationShift);
}
//Draw scene radar background
if (flags.showBackground)
{
Regresion.Background(PostProc, flags);
windowBackground.RenderRadar(PostProc, false);
}
}
else
Thread.Sleep(100);
}
calculate_radar_exited = true; //Thread exit normally
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach (WindowWave x in window_wave)
x.OnLoadWindow();
foreach (WindowFlow x in window_flow)
x.OnLoadWindow();
windowRadar.OnLoadWindow();
windowBackground.OnLoadWindow();
windowCorrelateWave.OnLoadWindow();
windowCorrelateFlow.OnLoadWindow();
resizing = false;
UpdateAllScenesWhenRunning();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
resizing = true;
}
private void splitContainer_SplitterMoved(object sender, SplitterEventArgs e)
{
// resizing = false;
UpdateAllScenesWhenRunning();
}
//Size correction only for radio windows
private void WindowsSizeCorection(int Nr)
{
StopDraw();//Thread stop
if (rd[Nr].radio_resize == false)
{
window_wave[Nr].SizeChange();
window_flow[Nr].SizeChange();
UpdateAllScenes();
}
if (runing)
StartDraw();//Thread start
}
//Size correction for all
public void UpdateAllScenesWhenRunning()
{
StopDraw();//Thread stop
if (resizing == false)
{
if (windowRadar != null) windowRadar.SizeChange();
if (windowBackground != null) windowBackground.SizeChange();
if (windowCorrelateWave != null) windowCorrelateWave.SizeChange();
if (windowCorrelateFlow != null) windowCorrelateFlow.SizeChange();
if (window_wave != null)
foreach (WindowWave x in window_wave)
if (x != null) x.SizeChange();
if (window_flow != null)
foreach (WindowFlow x in window_flow)
if (x != null) x.SizeChange();
//For scenes update when is not running
UpdateAllScenes();
}
if (runing)
StartDraw();//Thread start
}
//Scenes update when is not running
void UpdateAllScenes()
{
if (resizing == false)
if (FormsReady)
{
StartDraw();
System.Threading.Thread.Sleep(20);
StopDraw();
}
}
private void panelViewport3_MouseMove(object sender, MouseEventArgs e)
{
CalculateMouseMove(0, e.Location.X);
}
#region Mouse
private void CalculateMouseUp(int Nr, int x)
{
MouseDownPanel[Nr] = false;
UpdateFrequencies(Nr);
FilterMove = false;
LeftBandMove = false;
RightBandMove = false;
window_wave[Nr].panelViewport.Cursor = Cursors.Cross;
UpdateFiltersAndScale(Nr);
}
private void CalculateMouseDown(int Nr, int x)
{
MouseDownPanel[Nr] = true;
oldX_position[Nr] = x;
//Find how many MHz is one pixel
MHz_perPixel[Nr] = 1.0 / ((rd[Nr].panelRadioWave.Width - DrawWave.LeftMargin - DrawWave.RightMargin) / flags.rate[Nr]);
float pos = (float)((x - DrawWave.LeftMargin) * MHz_perPixel[Nr] + flags.rate[Nr] / 2);
float leftBand = (float)(flags.rate[Nr] - flags.FilterCentralFreq[Nr] - flags.RadioHalfBandwith);
float rightBand = (float)(flags.rate[Nr] - flags.FilterCentralFreq[Nr] + flags.RadioHalfBandwith);
if (pos > leftBand && pos < rightBand)
FilterMove = true;
if (pos > leftBand - 20000 && pos < leftBand)
LeftBandMove = true;
if (pos < rightBand + 20000 && pos > rightBand)
RightBandMove = true;
}
private void CalculateMouseMove(int Nr, int x)
{
float half_rate = (float)flags.rate[Nr] / 2;
MHz_perPixel[Nr] = 1.0 / ((rd[Nr].panelRadioWave.Width - DrawWave.LeftMargin - DrawWave.RightMargin) / flags.rate[Nr]);
double PointedFrequency = (flags.frequency[Nr] - half_rate);
float FrequencyAtCursorPosition = (float)(PointedFrequency + (x - DrawWave.LeftMargin) * MHz_perPixel[Nr]) / 1000000;
if (MouseDownPanel[Nr] == false)
{
WindowsLocation(FrequencyAtCursorPosition);
float pos = (float)((x - DrawWave.LeftMargin) * MHz_perPixel[Nr] + half_rate);
float leftBand = (float)(flags.rate[Nr] - flags.FilterCentralFreq[Nr] - flags.RadioHalfBandwith);
float rightBand = (float)(flags.rate[Nr] - flags.FilterCentralFreq[Nr] + flags.RadioHalfBandwith);
if (pos > leftBand - 20000 && pos < leftBand)
window_wave[Nr].panelViewport.Cursor = Cursors.VSplit;
else
if (pos < rightBand + 20000 && pos > rightBand)
window_wave[Nr].panelViewport.Cursor = Cursors.VSplit;
else
window_wave[Nr].panelViewport.Cursor = Cursors.Cross;
}
else
{
int delta = x - oldX_position[Nr];
double s = delta * MHz_perPixel[Nr];
if (FilterMove)
{
flags.FilterCentralFreq[Nr] -= (float)s;
float band = (float)(flags.FilterCentralFreq[Nr] + half_rate);
if (band < 1) flags.FilterCentralFreq[Nr] = 1 - half_rate;
else if (band > flags.rate[Nr]) flags.FilterCentralFreq[Nr] = half_rate;
if (band >= 1 && band <= flags.rate[Nr])
oldX_position[Nr] = x;
UpdateFiltersAndScale(Nr);
}
if (LeftBandMove)
{
flags.RadioHalfBandwith -= (float)s;
if (flags.RadioHalfBandwith < 10) flags.RadioHalfBandwith = 10;
else if (flags.RadioHalfBandwith > 250000 / 2) flags.RadioHalfBandwith = 250000 / 2;
if (flags.RadioHalfBandwith >= 10 && flags.RadioHalfBandwith < 250000 / 2)
{
oldX_position[Nr] = x;
UpdateFiltersAndScale(Nr);
//Cursor
window_wave[Nr].panelViewport.Cursor = Cursors.VSplit;
}
}
if (RightBandMove)
{
flags.RadioHalfBandwith += (float)s;
if (flags.RadioHalfBandwith < 10) flags.RadioHalfBandwith = 10;
else if (flags.RadioHalfBandwith > 250000 / 2) flags.RadioHalfBandwith = 250000 / 2;
if (flags.RadioHalfBandwith >= 10 / 2 && flags.RadioHalfBandwith < 250000 / 2)
{
oldX_position[Nr] = x;
UpdateFiltersAndScale(Nr);
//Cursor
window_wave[Nr].panelViewport.Cursor = Cursors.VSplit;
}
}
if (!FilterMove & !LeftBandMove & !RightBandMove)
{
flags.frequency[Nr] -= s;
if (flags.FREQUENCY_EQUAL)
for (int i = 0; i < Flags.MAX_DONGLES; i++)
flags.frequency[i] = flags.frequency[Nr];
UpdateFrequencies(Nr);
oldX_position[Nr] = x;
}
}
}
void UpdateFiltersAndScale(int Nr)//Nr-active radio at the moment
{
for (int i = 0; i < Flags.MAX_DONGLES; i++)
{
if (Nr != -1 && flags.FREQUENCY_EQUAL) flags.FilterCentralFreq[i] = flags.FilterCentralFreq[Nr];
window_wave[i].Location(-1, (float)(flags.rate[i] / 2 - (flags.FilterCentralFreq[i] + flags.RadioHalfBandwith)) / 1000000, (float)(flags.rate[i] / 2 - (flags.FilterCentralFreq[i] - flags.RadioHalfBandwith)) / 1000000);
window_flow[i].Location(-1);
}
}
void WindowsLocation(float FrequencyAtCursorPosition)
{
windowBackground.Location(FrequencyAtCursorPosition);
for (int i = 0; i < Flags.MAX_DONGLES; i++)
{
window_wave[i].Location(FrequencyAtCursorPosition, (float)(flags.rate[i] / 2 - (flags.FilterCentralFreq[i] + flags.RadioHalfBandwith)) / 1000000, (float)(flags.rate[i] / 2 - (flags.FilterCentralFreq[i] - flags.RadioHalfBandwith)) / 1000000);
}
foreach (WindowFlow x in window_flow)
x.Location(FrequencyAtCursorPosition);
}
#endregion
}
}