-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUIHandlerHorizontal.cs
498 lines (425 loc) · 22.4 KB
/
UIHandlerHorizontal.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
#if ANDROID
using Android.Content;
#endif
using ESuite.Drawables;
using LabNation.DeviceInterface.Hardware;
using LabNation.DeviceInterface.Devices;
namespace ESuite
{
partial class UIHandler
{
const double MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING = 0.3; //in seconds
double TriggerHoldoff
{
get { return Settings.Current.TriggerHoldoff.Value; }
set
{
if (double.IsNaN(value))
Settings.Current.TriggerHoldoff = 0; //WARNING: if a breakpoint was hit here, examine it as you've hit a very rare bug!
else
Settings.Current.TriggerHoldoff = value;
}
}
double TriggerHoldoffForIndicator
{
get
{
if (gridAnchor == GridAnchor.AcquisitionBuffer)
return TriggerHoldoff;
else if (gridAnchor == GridAnchor.Viewport)
return TriggerHoldoff - ViewportCenter;
else
throw new Exception("Dunno what to do");
}
}
double ViewportOffset
{
get { return Settings.Current.ViewportOffset.Value; }
set
{
if (double.IsNaN(value))
Settings.Current.ViewportOffset = 0; //WARNING: if a breakpoint was hit here, examine it as you've hit a very rare bug!
else
Settings.Current.ViewportOffset = value;
}
}
double ViewportCenter
{
get { return ViewportOffset + ViewportTimespan / 2.0 - AcquisitionLength / 2.0; }
}
/// <summary>
/// This constant defines when we tell the scope to attempt at making
/// partial dumps rather than completing the acquisition and only dumping
/// then. We do this in order to get a better UI experience where the
/// graph is updated while data is coming in.
/// </summary>
const double VIEWPORT_TIMESPAN_PREFER_PARTIAL = 500e-3;
double minimalViewportTimespan
{
get { return scope.SamplePeriod * 10; }
}
double ViewportTimespan
{
get { return Settings.Current.ViewportTimespan.Value; }
set { Settings.Current.ViewportTimespan = value; }
}
bool PanoramaVisible { get { return panoramaSplitter.PanoramaShown; } }
bool PanoramaEnabledPreference
{
get { return Settings.Current.PanoramaVisible.Value; }
set { Settings.Current.PanoramaVisible = value; }
}
double AcquisitionLength { get { return Settings.Current.acquisitionLength.Value; } set { Settings.Current.acquisitionLength = value; } }
public bool MinimizeAcquisitionLengthPending { get; private set; }
public const float MIN_TIME_PER_DIVISION_SMARTSCOPE = 20e-9f;
public const float MIN_TIME_PER_DIVISION_AUDIOJACK = 20e-6f;
public const float MAX_TIME_PER_DIVISION = 200e-3f;
internal void ShowPanorama(bool show, bool minimizeAcquisition = true)
{
PanoramaEnabledPreference = show;
panoramaSplitter.ShowPanorama(show);
if (show && scope.Rolling && scope.Running)
return;
scope.SendOverviewBuffer = show;
if (PanoramaVisible != show)
{
if (!show && minimizeAcquisition)
{
if (scope.Running)
MinimizeAcquisitionLengthToFitViewport(ViewportTimespan);
else
MinimizeAcquisitionLengthPending = true;
}
else
MinimizeAcquisitionLengthPending = false;
}
}
internal void MinimizeAcquisitionLengthToFitViewport(double viewportTimespanTarget)
{
MinimizeAcquisitionLengthPending = false;
if (scope.Rolling)
{
//put viewport at far right of acqBuffer, in attempt to make rolling mode respond instantaneously
//FIXME: rolling mode still respond instantaneously, while viewport is on right side of acqBuffer (hit space to verify). seems like data isnt' coming in?
double viewportCenterToTriggerHoldoff = ViewportCenter - 0;
double newViewportCenter = scope.AcquisitionLength / 2 - viewportTimespanTarget / 2;
double newTriggerHoldoff = newViewportCenter - viewportCenterToTriggerHoldoff;
SetViewportCenterAndTimespan(newViewportCenter, viewportTimespanTarget);
SetTriggerHoldoff(newTriggerHoldoff);
}
else if (!PanoramaVisible) //When panorama is hidden and not rolling, adjust acquisitionlength to make acquisition as fast as possible
{
double viewportCenterToTriggerHoldoff = ViewportCenter - TriggerHoldoff;
double newViewportCenter = 0;
double newTriggerHoldoff = newViewportCenter - viewportCenterToTriggerHoldoff;
double acquisitionLengthTarget = Math.Max(viewportTimespanTarget * (scope.Rolling ? 1.0 : 2.0), scope.Rolling ? 0 : newTriggerHoldoff * 2.0);
SetAcquisitionLength(acquisitionLengthTarget, scope.Rolling); //Maintain rolling state
SetViewportCenterAndTimespan(newViewportCenter, viewportTimespanTarget);
SetTriggerHoldoff(newTriggerHoldoff);
}
}
internal void SetTimeScale(LinLog scale)
{
throw new NotImplementedException();
/*
if (scopeMode != ScopeMode.ScopeFrequency)
return;
foreach (Waveform w in Waveform.EnabledWaveforms.Values)
{
(w as WaveformAnalog).scale = scale;
}
frequencyModeTimeScale = scale;
UpdateUiRanges();*/
}
internal void PanZoomGridHorizontal(float ratio, float offset, float center, bool invertBehaviour = false)
{
//in case of loading from file, zooming can only be done on the Viewport
if (scope is DummyScope && (scope as DummyScope).isFile)
{
PanZoomViewportFromGridPinch(ratio, offset, center);
}
else
{
if ((!invertBehaviour && !scope.Running) || (invertBehaviour && scope.Running))
PanZoomViewportFromGridPinch(ratio, offset, center);
else
PanZoomPanoramaFromGridPinch(1 / ratio, offset, center);
}
}
internal void PanZoomViewportFromPanorama(float zoom, float pan, float center, bool wasMouseScroll)
{
if (!PanoramaVisible)
{
ShowSimpleToast("Can't zoom the viewport when panorama is hidden", 2000, null);
return;
}
if (wasMouseScroll) zoom = 1f / zoom;
PanoramaEnabledPreference = PanoramaVisible;
double newAcquisitionLength = AcquisitionLength;
double newViewportTimespan = ViewportTimespan * zoom;
double newTriggerHoldoff = TriggerHoldoff;
//when the scope is stopped, you still want to be able to zoom out the panorama. But when stopped, you want to limit zooming in to the level where the Acquisition buffer covers the entire panorama.
if (!scope.Running && (newViewportTimespan < newAcquisitionLength))
if (lastScopeData != null)
newAcquisitionLength = Math.Max(lastScopeData.AcquisitionLength, newViewportTimespan);
if (float.IsNaN(center))
center = (float)(ViewportCenter / AcquisitionLength);
//<Distance pinchCenter to holdOff> = center * AcquisitionLength - TriggerHoldoff
double pinchCenterToViewportCenter = center * AcquisitionLength - ViewportCenter;
if (wasMouseScroll) pinchCenterToViewportCenter = 0;
double newViewportCenter = (center + pan) * newAcquisitionLength - pinchCenterToViewportCenter;
SetAcquisitionLength(newAcquisitionLength, false); //don't roll since we're using panorama to zoom
SetTriggerHoldoff(newTriggerHoldoff);
SetViewportCenterAndTimespan(newViewportCenter, newViewportTimespan);
}
internal void PanZoomViewportFromGridPinch(float zoom, float pan, float center)
{
/*
* Keep trigger holdoff where it is, and ensure the pinch-center to holdoff
* distance is constant
*/
PanoramaEnabledPreference = PanoramaVisible;
double newAcquisitionLength = AcquisitionLength;
double newViewportTimespan = ViewportTimespan * zoom;
if (newViewportTimespan > newAcquisitionLength)
newAcquisitionLength = newViewportTimespan;
//when the scope is stopped, you still want to be able to zoom out the panorama. But when stopped, you want to limit zooming in to the level where the Acquisition buffer covers the entire panorama.
if (!scope.Running && (newViewportTimespan < newAcquisitionLength))
if (lastScopeData != null)
newAcquisitionLength = Math.Max(lastScopeData.AcquisitionLength, newViewportTimespan);
if (float.IsNaN(center))
center = (float)(ViewportCenter / AcquisitionLength);
double pinchCenterToTriggerAbsolute = ViewportCenter + center * ViewportTimespan - TriggerHoldoff;
bool shouldRollInAutomaticSetting = (scope.Rolling && newViewportTimespan > MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING) || (ViewportTimespan <= MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING && newViewportTimespan > MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING);
SetAcquisitionLength(newAcquisitionLength, Settings.Current.SwitchAutomaticallyToRollingMode.Value ? shouldRollInAutomaticSetting : scope.Rolling);
SetTriggerHoldoff(TriggerHoldoff);
double newViewportCenter = pinchCenterToTriggerAbsolute + TriggerHoldoff - (center + pan) * newViewportTimespan;
SetViewportCenterAndTimespan(newViewportCenter, newViewportTimespan);
MinimizeAcquisitionLengthToFitViewport(ViewportTimespan);
}
internal void SetTDivAbsolute(double newTDiv)
{
double newViewportTimespan = newTDiv * Grid.DivisionsHorizontalMax;
double zoom = ViewportTimespan / newViewportTimespan;
double newAcquisitionLength = AcquisitionLength / zoom;
double newViewportCenter = ViewportCenter / AcquisitionLength * newAcquisitionLength;
float center = (float)(ViewportCenter / AcquisitionLength);
double pinchCenterToTriggerAbsolute = ViewportCenter + center * ViewportTimespan - TriggerHoldoff;
double newTriggerHoldoff = newViewportCenter + (center + 0) * newViewportTimespan - pinchCenterToTriggerAbsolute;
if (newTriggerHoldoff > newAcquisitionLength / 2.0)
{
if (PanoramaVisible)
{
ShowTriggerClippedToast();
return;
}
else
{
newAcquisitionLength = newTriggerHoldoff * 2.0;
}
}
bool shouldRollInAutomaticSetting = (scope.Rolling && newViewportTimespan > MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING) || (ViewportTimespan <= MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING && newViewportTimespan > MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING);
SetAcquisitionLength(newAcquisitionLength, Settings.Current.SwitchAutomaticallyToRollingMode.Value ? shouldRollInAutomaticSetting : scope.Rolling);
SetTriggerHoldoff(newTriggerHoldoff);
SetViewportCenterAndTimespan(newViewportCenter, newViewportTimespan);
MinimizeAcquisitionLengthToFitViewport(ViewportTimespan);
}
internal void PanZoomPanoramaFromGridPinch(float zoom, float pan, float center)
{
/*
* Pan Zoom Hell - Explained
*
* Key to computing the new view is
* - Understanding what is the boundary condition
* - First compute locally, then call methods
*
* In the case of a horizontal (time) pinch on the grid, you expect
* the center of the pinch gesture to stick to the shap of the wave
* beneath. This translates to keeping the time between that center
* point and the trigger holdoff (the shape-time-origin of the waveform)
* constant.
*
* So what we do here, is start by computing the knowns:
* - AcquisitionLength and ViewportTimespan just scale
* - We want the viewport to retain it's relative position on the panorama
*
* From here on we compute the new TriggerHoldoff, from the boundary condition
*
* <Distance pinchCenter to holdOff> = center * ViewportTimespan + ViewportCenter - TriggerHoldoff
*
*/
double newAcquisitionLength = AcquisitionLength / zoom;
double newViewportTimespan = ViewportTimespan / zoom;
double newViewportCenter = ViewportCenter / AcquisitionLength * newAcquisitionLength;
if (float.IsNaN(center))
center = (float)(ViewportCenter / AcquisitionLength);
double pinchCenterToTriggerAbsolute = ViewportCenter + center * ViewportTimespan - TriggerHoldoff;
double newTriggerHoldoff = newViewportCenter + (center + pan) * newViewportTimespan - pinchCenterToTriggerAbsolute;
if (newTriggerHoldoff > newAcquisitionLength / 2.0)
{
if (PanoramaVisible)
{
ShowTriggerClippedToast();
return;
}
else
{
newAcquisitionLength = newTriggerHoldoff * 2.0;
}
}
bool shouldRollInAutomaticSetting = (scope.Rolling && newViewportTimespan > MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING) || (ViewportTimespan <= MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING && newViewportTimespan > MIN_VIEWPORT_TIMESPAN_BEFORE_ROLLING);
SetAcquisitionLength(newAcquisitionLength, Settings.Current.SwitchAutomaticallyToRollingMode.Value ? shouldRollInAutomaticSetting : scope.Rolling);
SetTriggerHoldoff(newTriggerHoldoff);
SetViewportCenterAndTimespan(newViewportCenter, newViewportTimespan);
MinimizeAcquisitionLengthToFitViewport(ViewportTimespan);
}
internal void PanZoomPanoramaFromPanorama(float zoom, float pan, float center, bool wasMouseScroll)
{
/*
* When zooming the panorama, keep the viewfinder's center
* and the trigger holdoff fixed on the panorama. This is
* done by storing their relative positions.
* Of course, when there's a pan offset, we add this to the
* relative positions
*/
if (!PanoramaVisible)
{
ShowSimpleToast("Can't zoom the panorama when it's hidden", 2000, null);
return;
}
double newAcquisitionLength = AcquisitionLength / zoom;
double newViewportTimespan = ViewportTimespan / zoom;
if (float.IsNaN(center))
center = (float)(TriggerHoldoff / AcquisitionLength);
double triggerHoldoffToPinchCenter = center * AcquisitionLength - TriggerHoldoff;
double newTriggerHoldoff = (center + pan) * newAcquisitionLength - triggerHoldoffToPinchCenter;
if (newTriggerHoldoff > newAcquisitionLength / 2.0)
{
ShowTriggerClippedToast();
newTriggerHoldoff = newAcquisitionLength / 2.0;
}
double newViewportCenter = ViewportCenter / AcquisitionLength * newAcquisitionLength;
SetAcquisitionLength(newAcquisitionLength, false);
SetTriggerHoldoff(newTriggerHoldoff);
SetViewportCenterAndTimespan(newViewportCenter, newViewportTimespan);
}
internal void EnableRollingByUser()
{
EnableRolling(true);
}
internal void EnableRolling(bool enable)
{
SetAcquisitionLength(AcquisitionLength, enable);
}
internal void ChangeTimebaseLimits(float min, float max)
{
//update picker wheels
gm.Graphs[GraphType.Analog].RepopulateTDivWheelItems(min, max);
gm.Graphs[GraphType.Digital].RepopulateTDivWheelItems(min, max);
//in case current setting is outside of new bounds: correct
if (ViewportTimespan / Grid.DivisionsHorizontalMax > max)
ViewportTimespan = max * Grid.DivisionsHorizontalMax;
if (ViewportTimespan / Grid.DivisionsHorizontalMax < min)
ViewportTimespan = min * Grid.DivisionsHorizontalMax;
}
internal void SetAcquisitionLength(double time, bool requestRoll)
{
if (time > scope.AcquisitionLength && ViewportTimespan == AcquisitionLength && scope.SubSampleRate == scope.InputDecimationMax && scope.AcquisitionDepth == scope.AcquisitionDepthUserMaximum && scope.AcquisitionDepthUserMaximum < scope.AcquisitionDepthMax)
ShowSimpleToast("Max limit reached. Enlarge the RAM depth through Menu -> System -> Acquisition Depth", 2000);
if (scope is LabNation.DeviceInterface.Devices.DummyScope && time > scope.AcquisitionLengthMax)
{
time = scope.AcquisitionLengthMax;
if (ViewportTimespan == AcquisitionLength)
{
LabNation.DeviceInterface.Devices.DummyScope dummy = scope as LabNation.DeviceInterface.Devices.DummyScope;
if (dummy.isAudio)
ShowSimpleToast("A SmartScope is needed to zoom out till 2s/div", 3000);
}
}
if (time < minimalViewportTimespan)
{
time = minimalViewportTimespan;
}
bool couldRoll = scope.CanRoll;
bool wasRolling = scope.Rolling;
scope.PreferPartial = time >= VIEWPORT_TIMESPAN_PREFER_PARTIAL;
scope.AcquisitionLength = time;
AcquisitionLength = Math.Min(time, scope.AcquisitionLength);
if (scope.CanRoll && requestRoll)
{
//do only when changing from false to true
if (!scope.Rolling)
EnableFFT(false);
scope.Rolling = true;
triggerDropDown.SelectItemByTag("roll");
if (scope.Running)
ShowPanorama(false, false);
}
else
{
//do only when changing from true to false
if (scope.Rolling)
{
if (Settings.Current.analogGraphCombo == AnalogGraphCombo.AnalogFFT) EnableFFT(true);
if (Settings.Current.analogGraphCombo == AnalogGraphCombo.AnalogXY) EnableXY(true);
}
scope.Rolling = false;
scope.AcquisitionMode = AcquisitionMode;
triggerDropDown.SelectItemByTag(AcquisitionMode);
triggerDropDownRollingModeItem.Disabled = !scope.CanRoll;
ShowPanorama(PanoramaEnabledPreference);
}
measurementManager.SystemMeasurements[Measurements.SystemMeasurementType.AcquisitionLength].UpdateValueInternal(AcquisitionLength);
measurementManager.SystemMeasurements[Measurements.SystemMeasurementType.AcquisitionDepth].UpdateValueInternal(scope.AcquisitionDepth);
}
public void SetViewportCenterAndTimespan(double center, double timespan)
{
double offset = (center + AcquisitionLength / 2.0) - timespan / 2;
if (timespan < minimalViewportTimespan)
{
ShowSimpleToast("Viewfinder reached maximum zoom", 2000);
if (scope is LabNation.DeviceInterface.Devices.DummyScope)
{
LabNation.DeviceInterface.Devices.DummyScope dummy = scope as LabNation.DeviceInterface.Devices.DummyScope;
if (dummy.isAudio)
{
ShowSimpleToast("A SmartScope is needed to zoom till 10ns/div", 3000);
}
}
double timespanDifference = scope.SamplePeriod * 10 - timespan;
timespan += timespanDifference;
offset -= timespanDifference / 2;
}
if (offset < 0)
offset = 0;
if (timespan > AcquisitionLength)
{
timespan = AcquisitionLength;
offset = 0;
}
if (offset + timespan > AcquisitionLength)
offset = AcquisitionLength - timespan;
double offsetConvertor = 0;
if (!scope.Running && currentDataCollection != null)
{
offsetConvertor = currentDataCollection.HoldoffCenter - TriggerHoldoff;
offsetConvertor += (currentDataCollection.AcquisitionLength - AcquisitionLength) / 2.0;
}
else
{
offsetConvertor = (scope.AcquisitionLength - AcquisitionLength) / 2.0;
}
scope.SetViewPort(offset + offsetConvertor, timespan);
double scopeTimeRange = scope.ViewPortTimeSpan;
if (ViewportTimespan != timespan)
etsProcessor.RequestETSReset();
ViewportTimespan = timespan;
ViewportOffset = offset;
measurementManager.SystemMeasurements[Measurements.SystemMeasurementType.ViewportLength].UpdateValueInternal(ViewportTimespan);
measurementManager.SystemMeasurements[Measurements.SystemMeasurementType.ViewportOffset].UpdateValueInternal(ViewportOffset);
SetTriggerHoldoff(TriggerHoldoff);
UpdateUiRanges(graphManager.Graphs[GraphType.Analog].Grid);
}
}
}