forked from ngscopeclient/scopehal-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelPropertiesDialog.cpp
More file actions
626 lines (532 loc) · 20.4 KB
/
ChannelPropertiesDialog.cpp
File metadata and controls
626 lines (532 loc) · 20.4 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/***********************************************************************************************************************
* *
* ngscopeclient *
* *
* Copyright (c) 2012-2026 Andrew D. Zonenberg and contributors *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/
/**
@file
@author Andrew D. Zonenberg
@brief Implementation of ChannelPropertiesDialog
*/
#include "ngscopeclient.h"
#include "MainWindow.h"
#include "ChannelPropertiesDialog.h"
#include <imgui_node_editor.h>
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction
ChannelPropertiesDialog::ChannelPropertiesDialog(InstrumentChannel* chan, MainWindow* parent, bool graphEditorMode)
: BaseChannelPropertiesDialog(chan, parent, graphEditorMode)
{
// Get oscilloscope state, for that we need to make a shared_ptr out of the base pointer returned by chan->GetInstrument()
m_session = &m_parent->GetSession();
auto instrument = chan->GetInstrument();
if(instrument)
{
std::shared_ptr<Instrument> scopeSharedPointer = instrument->shared_from_this();
shared_ptr<Oscilloscope> sharedScope = dynamic_pointer_cast<Oscilloscope>(scopeSharedPointer);
if(sharedScope)
m_state = m_session->GetOscilloscopeState(sharedScope);
}
auto ochan = dynamic_cast<OscilloscopeChannel*>(chan);
if(!ochan)
LogFatal("ChannelPropertiesDialog expects an OscilloscopeChannel\n");
ochan->AddRef();
m_committedDisplayName = m_channel->GetDisplayName();
m_displayName = m_committedDisplayName;
//Color
auto color = ColorFromString(m_channel->m_displaycolor);
m_color[0] = ((color >> IM_COL32_R_SHIFT) & 0xff) / 255.0f;
m_color[1] = ((color >> IM_COL32_G_SHIFT) & 0xff) / 255.0f;
m_color[2] = ((color >> IM_COL32_B_SHIFT) & 0xff) / 255.0f;
//Vertical settings are per stream
size_t nstreams = m_channel->GetStreamCount();
m_committedOffset.resize(nstreams);
m_offset.resize(nstreams);
m_committedRange.resize(nstreams);
m_range.resize(nstreams);
for(size_t i = 0; i<nstreams; i++)
{
auto unit = m_channel->GetYAxisUnits(i);
m_committedOffset[i] = ochan->GetOffset(i);
m_offset[i] = unit.PrettyPrint(m_committedOffset[i]);
m_committedRange[i] = ochan->GetVoltageRange(i);
m_range[i] = unit.PrettyPrint(m_committedRange[i]);
}
//Digital channel settings
auto scope = ochan->GetScope();
if(scope)
{
m_committedHysteresis = scope->GetDigitalHysteresis(m_channel->GetIndex());
m_committedThreshold = scope->GetDigitalThreshold(m_channel->GetIndex());
}
else
{
m_committedHysteresis = 0;
m_committedThreshold = 0;
}
if(nstreams > 0)
{
auto yunit = m_channel->GetYAxisUnits(0);
m_hysteresis = yunit.PrettyPrint(m_committedHysteresis);
m_threshold = yunit.PrettyPrint(m_committedThreshold);
}
//Hardware acquisition settings if this is a scope channel
if(scope)
RefreshInputSettings(scope, m_channel->GetIndex());
else
{
m_committedAttenuation = 1;
m_navg = 1;
m_attenuation = "";
}
}
/**
@brief Update input configuration values
This is typically used with instruments that have a hardware input mux, since the set of available couplings and
bandwidth limiters etc may change for one input vs another.
*/
void ChannelPropertiesDialog::RefreshInputSettings(Oscilloscope* scope, size_t nchan)
{
auto ochan = dynamic_cast<OscilloscopeChannel*>(m_channel);
m_couplings.clear();
m_couplingNames.clear();
m_bwlValues.clear();
m_bwlNames.clear();
m_imuxNames.clear();
m_modeNames.clear();
//Attenuation
m_committedAttenuation = scope->GetChannelAttenuation(nchan);
m_attenuation = to_string(m_committedAttenuation);
//Coupling
m_coupling = 0;
auto curCoup = ochan->GetCoupling();
m_couplings = scope->GetAvailableCouplings(nchan);
for(size_t i=0; i<m_couplings.size(); i++)
{
auto c = m_couplings[i];
switch(c)
{
case OscilloscopeChannel::COUPLE_DC_50:
m_couplingNames.push_back("DC 50Ω");
break;
case OscilloscopeChannel::COUPLE_AC_50:
m_couplingNames.push_back("AC 50Ω");
break;
case OscilloscopeChannel::COUPLE_DC_1M:
m_couplingNames.push_back("DC 1MΩ");
break;
case OscilloscopeChannel::COUPLE_AC_1M:
m_couplingNames.push_back("AC 1MΩ");
break;
case OscilloscopeChannel::COUPLE_GND:
m_couplingNames.push_back("Ground");
break;
default:
m_couplingNames.push_back("Invalid");
break;
}
if(c == curCoup)
m_coupling = i;
}
//Bandwidth limiters
m_bwlValues = scope->GetChannelBandwidthLimiters(nchan);
m_bwl = 0;
auto curBwl = scope->GetChannelBandwidthLimit(nchan);
Unit hz(Unit::UNIT_HZ);
for(size_t i=0; i<m_bwlValues.size(); i++)
{
auto b = m_bwlValues[i];
if(b == 0)
m_bwlNames.push_back("Full");
else
m_bwlNames.push_back(hz.PrettyPrint(b*1e6));
if(b == curBwl)
m_bwl = i;
}
//Input mux settings
m_imuxNames = scope->GetInputMuxNames(nchan);
m_imux = scope->GetInputMuxSetting(nchan);
//Inversion
m_inverted = scope->IsInverted(nchan);
//ADC modes
m_mode = 0;
if(scope->IsADCModeConfigurable())
{
m_mode = scope->GetADCMode(nchan);
m_modeNames = scope->GetADCModeNames(nchan);
}
//Probe type
m_probe = scope->GetProbeName(nchan);
m_canAutoZero = scope->CanAutoZero(nchan);
m_canDegauss = scope->CanDegauss(nchan);
m_shouldDegauss = scope->ShouldDegauss(nchan);
m_canAverage = scope->CanAverage(nchan);
//Averaging
if(m_canAverage)
m_navg = scope->GetNumAverages(nchan);
}
ChannelPropertiesDialog::~ChannelPropertiesDialog()
{
auto ochan = dynamic_cast<OscilloscopeChannel*>(m_channel);
ochan->Release();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rendering
/**
@brief Renders the dialog and handles UI events
@return True if we should continue showing the dialog
False if it's been closed
*/
bool ChannelPropertiesDialog::DoRender()
{
//Call the base class to draw common stuff
if(!BaseChannelPropertiesDialog::DoRender())
return false;
auto ochan = dynamic_cast<OscilloscopeChannel*>(m_channel);
//Flags for a header that should be open by default EXCEPT in the graph editor
ImGuiTreeNodeFlags defaultOpenFlags = m_graphEditorMode ? 0 : ImGuiTreeNodeFlags_DefaultOpen;
//TODO: handle stream count changing dynamically on some filters
float width = 10 * ImGui::GetFontSize();
auto scope = ochan->GetScope();
auto f = dynamic_cast<Filter*>(m_channel);
//All channels have display settings
if(ImGui::CollapsingHeader("Display", defaultOpenFlags))
{
//If it's a filter, using the default name, check for changes made outside of this properties window
//(e.g. via filter graph editor)
if(f)
{
if(f->IsUsingDefaultName() && (f->GetDisplayName() != m_committedDisplayName))
{
m_committedDisplayName = f->GetDisplayName();
m_displayName = m_committedDisplayName;
}
}
ImGui::SetNextItemWidth(width);
if(TextInputWithImplicitApply("Nickname", m_displayName, m_committedDisplayName))
{
//If it's a filter, we're not using a default name anymore (unless the provided name is blank)
if(f != nullptr)
{
if(m_committedDisplayName == "")
{
f->UseDefaultName(true);
m_committedDisplayName = f->GetDisplayName();
m_displayName = m_committedDisplayName;
}
else
{
f->UseDefaultName(false);
m_channel->SetDisplayName(m_committedDisplayName);
}
}
//No, just set the name
else
m_channel->SetDisplayName(m_committedDisplayName);
}
if(f)
HelpMarker("Display name for the filter.\n\nSet blank to use an auto-generated default name.");
else
HelpMarker("Display name for the channel");
if(ImGui::ColorEdit3(
"Color",
m_color,
ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_Uint8))
{
char tmp[32];
snprintf(tmp, sizeof(tmp), "#%02x%02x%02x",
static_cast<int>(round(m_color[0] * 255)),
static_cast<int>(round(m_color[1] * 255)),
static_cast<int>(round(m_color[2] * 255)));
m_channel->m_displaycolor = tmp;
//If color is changed, request a re-render
m_parent->SetNeedRender();
}
}
//Input settings only make sense if we have an attached scope
auto nstreams = m_channel->GetStreamCount();
auto index = m_channel->GetIndex();
if(scope)
{
if(ImGui::CollapsingHeader("Input", defaultOpenFlags))
{
//Type of probe connected
string ptype = m_probe;
if(ptype == "")
ptype = "(not detected)";
ImGui::BeginDisabled();
ImGui::SetNextItemWidth(width);
ImGui::InputText("Probe Type", &ptype);
ImGui::EndDisabled();
HelpMarker("Type of probe connected to the instrument input");
//See if the channel is digital (first stream digital)
bool isDigital = (m_channel->GetType(0) == Stream::STREAM_TYPE_DIGITAL);
//Digital
if(isDigital)
{
auto yunit = m_channel->GetYAxisUnits(0);
if(scope->IsDigitalThresholdConfigurable())
{
ImGui::SetNextItemWidth(width);
if(UnitInputWithImplicitApply("Threshold", m_threshold, m_committedThreshold, yunit))
{
scope->SetDigitalThreshold(index, m_committedThreshold);
//refresh in case scope driver changed the value
m_committedThreshold = scope->GetDigitalThreshold(index);
m_threshold = yunit.PrettyPrint(m_committedThreshold);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
HelpMarker("Switching threshold for the digital input buffer");
}
if(scope->IsDigitalHysteresisConfigurable())
{
ImGui::SetNextItemWidth(width);
if(UnitInputWithImplicitApply("Hysteresis", m_hysteresis, m_committedHysteresis, yunit))
{
scope->SetDigitalHysteresis(index, m_committedHysteresis);
//refresh in case scope driver changed the value
m_committedHysteresis = scope->GetDigitalHysteresis(index);
m_hysteresis = yunit.PrettyPrint(m_committedHysteresis);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
HelpMarker("Hysteresis for the digital input buffer");
}
//TODO: when value is changed, refresh any dialogs that might be open showing other channels
//from the same digital bank
auto bank = scope->GetDigitalBank(index);
if(bank.size() > 1)
{
ImGui::Text("Changing input buffer settings will also affect the following channels:");
for(auto c : bank)
{
if(c == m_channel)
continue;
ImGui::BulletText("%s", c->GetDisplayName().c_str());
}
}
}
//Analog
else
{
//Attenuation
Unit counts(Unit::UNIT_COUNTS);
ImGui::SetNextItemWidth(width);
if(m_probe != "") //cannot change attenuation on active probes
ImGui::BeginDisabled();
if(UnitInputWithImplicitApply("Attenuation", m_attenuation, m_committedAttenuation, counts))
{
scope->SetChannelAttenuation(index, m_committedAttenuation);
//Update offset and range when attenuation is changed
for(size_t i = 0; i<nstreams; i++)
{
auto unit = m_channel->GetYAxisUnits(i);
m_committedOffset[i] = ochan->GetOffset(i);
m_offset[i] = unit.PrettyPrint(m_committedOffset[i]);
m_committedRange[i] = ochan->GetVoltageRange(i);
m_range[i] = unit.PrettyPrint(m_committedRange[i]);
}
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
if(m_probe != "")
ImGui::EndDisabled();
HelpMarker("Attenuation setting for the probe (for example, 10 for a 10:1 probe)");
//Only show coupling box if the instrument has configurable coupling
if( (m_couplings.size() > 1) && (m_probe == "") )
{
ImGui::SetNextItemWidth(width);
if(Combo("Coupling", m_couplingNames, m_coupling))
{
ochan->SetCoupling(m_couplings[m_coupling]);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
HelpMarker("Coupling configuration for the input");
}
//Bandwidth limiters (only show if more than one value available)
if(m_bwlNames.size() > 1)
{
ImGui::SetNextItemWidth(width);
if(Combo("Bandwidth", m_bwlNames, m_bwl))
{
ochan->SetBandwidthLimit(m_bwlValues[m_bwl]);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
HelpMarker("Hardware bandwidth limiter setting");
}
}
//If there's an input mux, show a combo box for it
if(scope->HasInputMux(index))
{
ImGui::SetNextItemWidth(width);
if(Combo("Input mux", m_imuxNames, m_imux))
{
scope->SetInputMux(index, m_imux);
//When input mux changes, we need to redo all of the other settings since
//the set of valid values can change
RefreshInputSettings(scope, index);
}
HelpMarker("Hardware input multiplexer setting");
}
//If the scope has configurable ADC modes, show dropdown for that
if(!isDigital && scope->IsADCModeConfigurable())
{
bool nomodes = m_modeNames.size() <= 1;
if(nomodes)
ImGui::BeginDisabled();
ImGui::SetNextItemWidth(width);
if(Combo("ADC mode", m_modeNames, m_mode))
{
scope->SetADCMode(index, m_mode);
RefreshInputSettings(scope, index);
}
if(nomodes)
ImGui::EndDisabled();
HelpMarker(
"Operating mode for the analog-to-digital converter.\n\n"
"Some instruments allow the ADC to operate in several modes, typically trading bit depth "
"against sample rate. Available modes may vary depending on the current sample rate and "
"which channels are in use."
);
}
//If the probe supports inversion, show a checkbox for it
if(scope->CanInvert(index))
{
if(ImGui::Checkbox("Invert", &m_inverted))
{
ochan->Invert(m_inverted);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
HelpMarker(
"When checked, input value is multiplied by -1.\n\n"
"For a differential probe, this is equivalent to swapping the positive and negative inputs."
);
}
//If the channel supports averaging, show a spin button for it
if(!isDigital && scope->CanAverage(index))
{
if(ImGui::InputInt("Averaging", &m_navg))
scope->SetNumAverages(index, m_navg);
HelpMarker(
"Reduce noise for repetitive signals by averaging\n"
"multiple consecutive acquisitions");
}
//If the probe supports auto zeroing, show a button for it
if(m_canAutoZero)
{
if(ImGui::Button("Auto Zero"))
ochan->AutoZero();
HelpMarker(
"Click to automatically zero offset of active probe.\n\n"
"Check probe documentation to see whether input signal must be removed before zeroing."
);
}
//If the probe supports degaussing, show a button for it
if(!isDigital && m_canDegauss)
{
string caption = "Degauss";
if(m_shouldDegauss)
caption += "*";
if(ImGui::Button(caption.c_str()))
ochan->Degauss();
HelpMarker(
"Click to automatically degauss current probe.\n\n"
"Check probe documentation to see whether input signal must be removed before degaussing."
);
}
}
}
//If we have more streams than we did when the dialog was created, update them
size_t noldstreams = m_committedOffset.size();
if(noldstreams != nstreams)
{
m_committedOffset.resize(nstreams);
m_offset.resize(nstreams);
m_committedRange.resize(nstreams);
m_range.resize(nstreams);
for(size_t i = noldstreams; i<nstreams; i++)
{
auto unit = m_channel->GetYAxisUnits(i);
m_committedOffset[i] = ochan->GetOffset(i);
m_offset[i] = unit.PrettyPrint(m_committedOffset[i]);
m_committedRange[i] = ochan->GetVoltageRange(i);
m_range[i] = unit.PrettyPrint(m_committedRange[i]);
}
}
//Vertical settings are per stream
for(size_t i = 0; i<nstreams; i++)
{
string streamname = "Vertical";
if(nstreams > 1)
streamname = m_channel->GetStreamName(i);
//Only show if analog channel
if(m_channel->GetType(i) == Stream::STREAM_TYPE_ANALOG)
{
if(ImGui::CollapsingHeader(streamname.c_str()))
{
ImGui::PushID(i);
auto unit = m_channel->GetYAxisUnits(i);
//If no change to offset in dialog, update our input value when we change offset outside the dialog
auto off = ochan->GetOffset(i);
auto soff = unit.PrettyPrint(m_committedOffset[i]);
if( (m_committedOffset[i] != off) && (soff == m_offset[i]) )
{
m_offset[i] = unit.PrettyPrint(off);
m_committedOffset[i] = off;
}
ImGui::SetNextItemWidth(width);
if(UnitInputWithExplicitApply("Offset", m_offset[i], m_committedOffset[i], unit))
{
ochan->SetOffset(m_committedOffset[i], i);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
//Same for range
auto range = ochan->GetVoltageRange(i);
auto srange = unit.PrettyPrint(m_committedRange[i]);
if( (m_committedRange[i] != range) && (srange == m_range[i]) )
{
m_range[i] = unit.PrettyPrint(range);
m_committedRange[i] = range;
}
ImGui::SetNextItemWidth(width);
if(UnitInputWithExplicitApply("Range", m_range[i], m_committedRange[i], unit))
{
ochan->SetVoltageRange(m_committedRange[i], i);
// Tell intrument thread that the scope state has to be updated
if(m_state) m_state->m_needsUpdate[index] = true;
}
ImGui::PopID();
}
}
}
return true;
}