forked from ngscopeclient/scopehal-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamBrowserDialog.cpp
More file actions
2169 lines (1953 loc) · 69.5 KB
/
StreamBrowserDialog.cpp
File metadata and controls
2169 lines (1953 loc) · 69.5 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************************************************
* *
* 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 StreamBrowserDialog
*/
#include "ngscopeclient.h"
#include "StreamBrowserDialog.h"
#include "MainWindow.h"
using namespace std;
#define ELLIPSIS_CHAR "…"
#define PLUS_CHAR "+"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// StreamBrowserTimebaseInfo
StreamBrowserTimebaseInfo::StreamBrowserTimebaseInfo(shared_ptr<Oscilloscope> scope)
{
//Interleaving flag
m_interleaving = scope->IsInterleaving();
//Sample rate
Unit srate(Unit::UNIT_SAMPLERATE);
auto rate = scope->GetSampleRate();
if(m_interleaving)
m_rates = scope->GetSampleRatesInterleaved();
else
m_rates = scope->GetSampleRatesNonInterleaved();
m_rate = 0;
for(size_t i=0; i<m_rates.size(); i++)
{
m_rateNames.push_back(srate.PrettyPrint(m_rates[i]));
if(m_rates[i] == rate)
m_rate = i;
}
//Sample depth
Unit sdepth(Unit::UNIT_SAMPLEDEPTH);
auto depth = scope->GetSampleDepth();
if(m_interleaving)
m_depths = scope->GetSampleDepthsInterleaved();
else
m_depths = scope->GetSampleDepthsNonInterleaved();
m_depth = 0;
for(size_t i=0; i<m_depths.size(); i++)
{
m_depthNames.push_back(sdepth.PrettyPrint(m_depths[i]));
if(m_depths[i] == depth)
m_depth = i;
}
Unit hz(Unit::UNIT_HZ);
m_rbw = scope->GetResolutionBandwidth();
m_rbwText = hz.PrettyPrintInt64(m_rbw);
m_span = scope->GetSpan();
m_spanText = hz.PrettyPrint(m_span);
//TODO: some instruments have per channel center freq, how to handle this?
m_center = scope->GetCenterFrequency(0);
m_centerText = hz.PrettyPrint(m_center);
m_start = m_center - m_span/2;
m_startText = hz.PrettyPrint(m_start);
m_end = m_center + m_span/2;
m_endText = hz.PrettyPrint(m_end);
m_samplingMode = scope->GetSamplingMode();
auto spec = dynamic_pointer_cast<SCPISpectrometer>(scope);
if(spec)
{
Unit fs(Unit::UNIT_FS);
m_integrationTime = spec->GetIntegrationTime();
m_integrationText = fs.PrettyPrint(m_integrationTime);
}
else
m_integrationTime = 0;
m_adcmode = scope->GetADCMode(0);
m_adcmodeNames = scope->GetADCModeNames(0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction
StreamBrowserDialog::StreamBrowserDialog(Session& session, MainWindow* parent)
: Dialog("Stream Browser", "Stream Browser", ImVec2(550, 400), &session, parent)
{
}
StreamBrowserDialog::~StreamBrowserDialog()
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rendering
//Helper methods for rendering widgets that appear in the StreamBrowserDialog.
/**
@brief prepare rendering context to display a badge at the end of current line
*/
void StreamBrowserDialog::startBadgeLine()
{
ImGuiWindow *window = ImGui::GetCurrentWindowRead();
// roughly, what ImGui::GetCursorPosPrevLineX would be, if it existed; convert from absolute-space to window-space
m_badgeXMin = (window->DC.CursorPosPrevLine - window->Pos + window->Scroll).x;
m_badgeXCur = ImGui::GetWindowContentRegionMax().x;
}
/**
@brief render a badge for an instrument node
@param inst the instrument to render the badge for
@param latched true if the redering of this batch should be latched (i.e. only render if previous badge has been here for more than a given time)
@param badge the badge type
*/
void StreamBrowserDialog::renderInstrumentBadge(std::shared_ptr<Instrument> inst, bool latched, InstrumentBadge badge)
{
auto& prefs = m_session->GetPreferences();
double now = GetTime();
if(latched)
{
std::pair<double,InstrumentBadge> old = m_instrumentLastBadge[inst];
double elapsed = now - old.first;
if(elapsed < prefs.GetReal("Appearance.Stream Browser.instrument_badge_latch_duration"))
{ // Keep previous badge
badge = old.second;
}
}
else
m_instrumentLastBadge[inst] = std::pair<double,InstrumentBadge>(now,badge);
switch (badge)
{
case StreamBrowserDialog::BADGE_ARMED:
/* prefer language "ARMED" to "RUN":
* "RUN" could mean either "waiting
* for trigger" or "currently
* capturing samples post-trigger",
* "ARMED" is unambiguous */
renderBadge(ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.trigger_armed_badge_color")), "ARMED", "A", NULL);
break;
case StreamBrowserDialog::BADGE_STOPPED:
renderBadge(ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.trigger_stopped_badge_color")), "STOPPED", "STOP", "S", NULL);
break;
case StreamBrowserDialog::BADGE_TRIGGERED:
renderBadge(ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.trigger_triggered_badge_color")), "TRIGGERED", "TRIG'D", "T'D", "T", NULL);
break;
case StreamBrowserDialog::BADGE_BUSY:
/* prefer language "BUSY" to "WAIT":
* "WAIT" could mean "waiting for
* trigger", "BUSY" means "I am
* doing something internally and am
* not ready for some reason" */
renderBadge(ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.trigger_busy_badge_color")), "BUSY", "B", NULL);
break;
case StreamBrowserDialog::BADGE_AUTO:
renderBadge(ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.trigger_auto_badge_color")), "AUTO", "A", NULL);
break;
default:
break;
}
}
/**
@brief render a badge at the end of current line with provided color and text
@param color the color of the badge
@param ... a null terminated list of labels form the largest to the smallest to use as a badge label according to the available space
*/
void StreamBrowserDialog::renderBadge(ImVec4 color, ... /* labels, ending in NULL */)
{
va_list ap;
va_start(ap, color);
while (const char *label = va_arg(ap, const char *))
{
float xsz = ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x + ImGui::GetStyle().FramePadding.x * 2;
if ((m_badgeXCur - xsz) < m_badgeXMin)
continue;
// ok, we have enough space -- commit to it!
m_badgeXCur -= xsz - ImGui::GetStyle().ItemSpacing.x;
ImGui::SameLine(m_badgeXCur);
ImGui::PushStyleColor(ImGuiCol_Button, color);
SmallDisabledButton(label);
ImGui::PopStyleColor();
break;
}
va_end(ap);
}
/**
@brief Render a combo box with provided color and values
@param label Label for the combo box
@param alignRight if true
@param color the color of the combo box
@param selected the selected value index (in/out)
@param values the combo box values
@param useColorForText if true, use the provided color for text (and a darker version of it for background color)
@param cropTextTo if >0 crop the combo text up to this number of characters to have it fit the available space
@param hideArrow True to hide the dropdown arrow (defaults to true)
@param paddingRight the padding to leave at the right of the combo when alighRight is true (defaults to 0)
@return true if the selected value of the combo has been changed
*/
bool StreamBrowserDialog::renderCombo(
const char* label,
bool alignRight,
ImVec4 color,
int &selected,
const vector<string> &values,
bool useColorForText,
uint8_t cropTextTo,
bool hideArrow,
float paddingRight)
{
if(selected >= (int)values.size() || selected < 0)
{
selected = 0;
}
const char* selectedLabel = "";
if(selected < (int)values.size())
selectedLabel = values[selected].c_str();
if(alignRight)
{
int padding = ImGui::GetStyle().ItemSpacing.x + ImGui::GetStyle().FramePadding.x * 2 + paddingRight;
float xsz = ImGui::CalcTextSize(selectedLabel).x + padding;
string resizedLabel;
if ((m_badgeXCur - xsz) < m_badgeXMin)
{
if(cropTextTo == 0)
return false; // No room and we don't want to crop text
resizedLabel = selectedLabel;
while((m_badgeXCur - xsz) < m_badgeXMin)
{
// Try and crop text
resizedLabel = resizedLabel.substr(0,resizedLabel.size()-1);
if(resizedLabel.size() < cropTextTo)
break; // We don't want to make the text that short
xsz = ImGui::CalcTextSize((resizedLabel + ELLIPSIS_CHAR).c_str()).x + padding;
}
if((m_badgeXCur - xsz) < m_badgeXMin)
return false; // Still no room
// We found an acceptable size
resizedLabel = resizedLabel + ELLIPSIS_CHAR;
selectedLabel = resizedLabel.c_str();
}
m_badgeXCur -= xsz - ImGui::GetStyle().ItemSpacing.x;
ImGui::SameLine(m_badgeXCur);
}
if(useColorForText)
{
// Use channel color for shape combo, but darken it to make text readable
float bgmul = 0.4;
auto bcolor = ImGui::ColorConvertFloat4ToU32(ImVec4(color.x*bgmul, color.y*bgmul, color.z*bgmul, color.w));
ImGui::PushStyleColor(ImGuiCol_FrameBg, bcolor);
ImGui::PushStyleColor(ImGuiCol_Text, color);
}
else
{
ImGui::PushStyleColor(ImGuiCol_FrameBg, color);
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(4, 0));
int flags = 0;
if(hideArrow)
flags |= ImGuiComboFlags_NoArrowButton;
if(alignRight)
flags |= ImGuiComboFlags_WidthFitPreview;
bool changed = false;
if(ImGui::BeginCombo(label, selectedLabel, flags))
{
for(int i = 0 ; i < (int)values.size() ; i++)
{
const bool is_selected = (i == selected);
if(ImGui::Selectable(values[i].c_str(), is_selected))
{
selected = i;
changed = true;
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::PopStyleVar();
ImGui::PopStyleColor();
if(useColorForText)
ImGui::PopStyleColor();
return changed;
}
/**
@brief Render a combo box with provded color and values
@param label Label for the combo box
@param alignRight true if the combo should be aligned to the right
@param color the color of the combo box
@param selected the selected value index (in/out)
@param ... the combo box values
@return true if the selected value of the combo has been changed
*/
bool StreamBrowserDialog::renderCombo(
const char* label,
bool alignRight,
ImVec4 color,
int *selected, ... /* values, ending in NULL */)
{
if(!selected)
{
LogError("Invalid call to renderCombo() method, 'selected' parameter must not be null.");
return false;
}
va_list ap;
va_start(ap, selected);
std::vector<string> values;
while (const char *item = va_arg(ap, const char *))
values.push_back(string(item));
va_end(ap);
return renderCombo(label, alignRight, color, (*selected), values);
}
/**
@brief Render a toggle button combo
@param label Label for the combo box
@param alignRight true if the combo should be aligned to the right
@param color the color of the toggle button
@param curValue the value of the toggle button
@param valueOff label for value off (optional, defaults to "OFF")
@param valueOn label for value on (optional, defaults to "ON")
@param cropTextTo if >0 crop the combo text up to this number of characters to have it fit the available space (optional, defaults to 0)
@param paddingRight the padding to leave at the right of the combo when alighRight is true (defaults to 0)
@return true if selection has changed
*/
bool StreamBrowserDialog::renderToggle(const char* label, bool alignRight, ImVec4 color, bool& curValue, const char* valueOff, const char* valueOn, uint8_t cropTextTo, float paddingRight)
{
int selection = (int)curValue;
std::vector<string> values;
values.push_back(string(valueOff));
values.push_back(string(valueOn));
bool ret = renderCombo(label, alignRight, color, selection, values, false, cropTextTo, true, paddingRight);
curValue = (selection == 1);
return ret;
}
/**
@brief Render an on/off toggle button combo
@param label Label for the combo box
@param alignRight true if the combo should be aligned to the right
@param curValue the value of the toggle button
@param valueOff label for value off (optional, defaults to "OFF")
@param valueOn label for value on (optional, defaults to "ON")
@param cropTextTo if >0 crop the combo text up to this number of characters to have it fit the available space (optional, defaults to 0)
@param paddingRight the padding to leave at the right of the combo when alighRight is true (defaults to 0)
@return true if value has changed
*/
bool StreamBrowserDialog::renderOnOffToggle(const char* label, bool alignRight, bool& curValue, const char* valueOff, const char* valueOn, uint8_t cropTextTo, float paddingRight)
{
auto& prefs = m_session->GetPreferences();
ImVec4 color = ImGui::ColorConvertU32ToFloat4(
(curValue ?
prefs.GetColor("Appearance.Stream Browser.instrument_on_badge_color") :
prefs.GetColor("Appearance.Stream Browser.instrument_off_badge_color")));
return renderToggle(label, alignRight, color, curValue, valueOff, valueOn, cropTextTo,paddingRight);
}
/**
@brief Render a download progress bar for a given instrument channel
@param inst the instrument to render the progress channel for
@param chan the channel to render the progress for
@param isLast true if it is the last channel of the instrument
@return Returns true if the progress bar has been rendered
*/
bool StreamBrowserDialog::renderDownloadProgress(std::shared_ptr<Instrument> inst, InstrumentChannel *chan, bool isLast)
{
static const char* const download[] = {"DOWNLOADING", "DOWNLOAD" ,"DL","D", NULL};
/* prefer language "PENDING" to "WAITING": "PENDING" implies
* that we are going to do it when we get through a list of
* other things, "WAITING" could mean that the channel is
* waiting for something else (trigger?)
*/
static const char* const pend[] = {"PENDING" , "PEND" ,"PE","P", NULL};
/* prefer language "COMPLETE" to "READY": "READY" implies
* that the channel might be ready to capture or something,
* but "COMPLETE" at least is not to be confused with that.
* ("DOWNLOADED" is more specific but is easy to confuse
* with "DOWNLOADING". If you can come up with a better
* mid-length abbreviation for "COMPLETE" than "DL OK" /
* "OK", give it a go, I guess.)
*/
static const char* const ready[] = {"COMPLETE" , "DL OK" ,"OK","C", NULL};
/* Let's use active for fast download channels to display when data is available
*/
static const char* const active[] = {"ACTIVE" , "ACTV" ,"ACT","A", NULL};
static const char* const* labels;
ImVec4 color;
bool shouldRender = true;
bool hasProgress = false;
double elapsed = GetTime() - chan->GetDownloadStartTime();
auto& prefs = m_session->GetPreferences();
// determine what label we should apply, and while we are at
// it, determine if this channel appears to be slow enough
// to need a progress bar
/// @brief hysteresis threshold for a channel finishing a download faster than this to be declared fast
#define CHANNEL_DOWNLOAD_THRESHOLD_FAST_SECONDS ((double)0.2)
/// @brief hysteresis threshold for a channel finishing a still being in progress for longer than this to be declared slow
#define CHANNEL_DOWNLOAD_THRESHOLD_SLOW_SECONDS ((double)0.4)
switch(chan->GetDownloadState())
{
case InstrumentChannel::DownloadState::DOWNLOAD_NONE:
if (isLast && (elapsed < CHANNEL_DOWNLOAD_THRESHOLD_FAST_SECONDS))
m_instrumentDownloadIsSlow[inst] = false;
/* FALLTHRU */
case InstrumentChannel::DownloadState::DOWNLOAD_UNKNOWN:
/* There is nothing to say about this --
* either there is nothing pending at all on
* the system, or this scope doesn't know
* how to report it, and in either case, we
* don't need to render a badge about it.
*/
shouldRender = false;
break;
case InstrumentChannel::DownloadState::DOWNLOAD_WAITING:
labels = pend;
if (elapsed > CHANNEL_DOWNLOAD_THRESHOLD_SLOW_SECONDS)
m_instrumentDownloadIsSlow[inst] = true;
hasProgress = m_instrumentDownloadIsSlow[inst];
color = ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.download_wait_badge_color"));
break;
case InstrumentChannel::DownloadState::DOWNLOAD_IN_PROGRESS:
labels = download;
if (elapsed > CHANNEL_DOWNLOAD_THRESHOLD_SLOW_SECONDS)
m_instrumentDownloadIsSlow[inst] = true;
hasProgress = m_instrumentDownloadIsSlow[inst];
color = ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.download_progress_badge_color"));
break;
case InstrumentChannel::DownloadState::DOWNLOAD_FINISHED:
labels = ready;
if (isLast && (elapsed < CHANNEL_DOWNLOAD_THRESHOLD_FAST_SECONDS))
m_instrumentDownloadIsSlow[inst] = false;
color = ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.download_finished_badge_color"));
break;
default:
shouldRender = false;
break;
}
// For fast channels, show a constant green badge when a
// download has started "recently" -- even if we're not
// downloading at this moment. This could be slightly
// misleading (i.e., after a channel goes into STOP mode, we
// will remain ACTIVE for up to THRESHOLD_SLOW time) but the
// period of time for which it is misleading is short!
if(!m_instrumentDownloadIsSlow[inst] && elapsed < CHANNEL_DOWNLOAD_THRESHOLD_SLOW_SECONDS)
{
labels = active;
color = ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.download_active_badge_color"));
shouldRender = true;
hasProgress = false;
}
if (!shouldRender)
return false;
/// @brief Width used to display progress bars (e.g. download progress bar)
#define PROGRESS_BAR_WIDTH 80
// try first adding a bar, and if there isn't enough room
// for a bar, skip it and try just putting a label
for (int withoutBar = 0; withoutBar < 2; withoutBar++)
{
if (withoutBar)
hasProgress = false;
for (int i = 0; labels[i]; i++)
{
const char *label = labels[i];
float xsz = ImGui::CalcTextSize(label).x + (hasProgress ? (ImGui::GetStyle().ItemSpacing.x + PROGRESS_BAR_WIDTH) : 0) + ImGui::GetStyle().FramePadding.x * 2 + ImGui::GetStyle().ItemSpacing.x;
if ((m_badgeXCur - xsz) < m_badgeXMin)
continue;
// ok, we have enough space -- commit to it!
m_badgeXCur -= xsz - ImGui::GetStyle().ItemSpacing.x;
ImGui::SameLine(m_badgeXCur);
ImGui::PushStyleColor(ImGuiCol_Button, color);
SmallDisabledButton(label);
ImGui::PopStyleColor();
if(hasProgress)
{
ImGui::SameLine();
ImGui::ProgressBar(chan->GetDownloadProgress(), ImVec2(PROGRESS_BAR_WIDTH, ImGui::GetFontSize()));
}
return true;
}
}
// well, shoot -- I guess there wasn't enough room to do *anything* useful!
return true;
}
/**
@brief Render a PSU properties row
@param isVoltage true for voltage rows, false for current rows
@param cc true if the PSU channel is in constant current mode, false for constant voltage mode
@param chan the PSU channel to render properties for
@param setValue the set value text
@param committedValue the last commited value
@param measuredValue the measured value text
@param clicked output param for clicked state
@param hovered output param for hovered state
@return true if the value has been modified
*/
bool StreamBrowserDialog::renderPsuRows(
bool isVoltage,
bool cc,
PowerSupplyChannel* chan,
std::string& currentValue,
float& committedValue,
std::string& measuredValue,
bool &clicked,
bool &hovered)
{
bool changed = false;
auto& prefs = m_session->GetPreferences();
// Row 1
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text(isVoltage ? "Voltage:" : "Current:");
ImGui::TableSetColumnIndex(1);
StreamDescriptor sv(chan, isVoltage ? 1 : 3);
ImGui::PushID(isVoltage ? "sV" : "sC");
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.psu_set_label_color")));
ImGui::Selectable("- Set");
ImGui::PopStyleColor();
if(ImGui::BeginDragDropSource())
{
ImGui::SetDragDropPayload("Scalar", &sv, sizeof(sv));
string dragText = chan->GetDisplayName() + (isVoltage ? " voltage" : " current") + " set value";
ImGui::TextUnformatted(dragText.c_str());
ImGui::EndDragDropSource();
}
else
DoItemHelp();
ImGui::PopID();
ImGui::TableSetColumnIndex(2);
ImGui::PushID(isVoltage ? "sV" : "sC");
ImVec4 color = ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.psu_7_segment_color"));
Unit unit(isVoltage ? Unit::UNIT_VOLTS : Unit::UNIT_AMPS);
if(renderEditablePropertyWithExplicitApply(0,"##psuSetValue",currentValue,committedValue,unit,nullptr,color,true))
{
changed = true;
}
ImGui::PopID();
// Row 2
ImGui::TableNextRow();
if((isVoltage && !cc) || (!isVoltage && cc))
{
ImGui::TableSetColumnIndex(0);
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::ColorConvertU32ToFloat4(prefs.GetColor(isVoltage ? "Appearance.Stream Browser.psu_cv_badge_color" : "Appearance.Stream Browser.psu_cc_badge_color")));
SmallDisabledButton(isVoltage ? "CV" : "CC");
ImGui::PopStyleColor();
}
ImGui::TableSetColumnIndex(1);
StreamDescriptor mv(chan, isVoltage ? 0 : 2);
ImGui::PushID(isVoltage ? "mV" : "mC");
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::ColorConvertU32ToFloat4(prefs.GetColor("Appearance.Stream Browser.psu_meas_label_color")));
ImGui::Selectable("- Meas.");
ImGui::PopStyleColor();
if(ImGui::BeginDragDropSource())
{
ImGui::SetDragDropPayload("Scalar", &mv, sizeof(mv));
string dragText = chan->GetDisplayName() + (isVoltage ? " voltage" : " current") + " measured value";
ImGui::TextUnformatted(dragText.c_str());
ImGui::EndDragDropSource();
}
else
DoItemHelp();
ImGui::PopID();
ImGui::TableSetColumnIndex(2);
ImGui::PushID(isVoltage ? "mV" : "mC");
renderNumericValue(measuredValue,clicked,hovered,color,true,0,false);
ImGui::PopID();
return changed;
}
/**
@brief Render DMM channel properties
@param dmm the DMM to render channel properties for
@param dmmchan the DMM channel to render properties for
@param clicked output param for clicked state
@param hovered output param for hovered state
*/
void StreamBrowserDialog::renderDmmProperties(std::shared_ptr<Multimeter> dmm, MultimeterChannel* dmmchan, bool isMain, bool &clicked, bool &hovered)
{
size_t streamIndex = isMain ? 0 : 1;
Unit unit = dmmchan->GetYAxisUnits(streamIndex);
float mainValue = dmmchan->GetScalarValue(streamIndex);
string valueText = unit.PrettyPrint(mainValue,dmm->GetMeterDigits());
ImVec4 color = ImGui::ColorConvertU32ToFloat4(ColorFromString(dmmchan->m_displaycolor));
string streamName = isMain ? "Main" : "Secondary";
ImGui::PushID(streamName.c_str());
// Get available operating and current modes
auto modemask = isMain ? dmm->GetMeasurementTypes() : dmm->GetSecondaryMeasurementTypes();
auto curMode = isMain ? dmm->GetMeterMode() : dmm->GetSecondaryMeterMode();
// Stream name
bool open = ImGui::TreeNodeEx(streamName.c_str(), (curMode > 0) ? ImGuiTreeNodeFlags_DefaultOpen : 0);
// Mode combo
startBadgeLine();
ImGui::PushID(streamName.c_str());
vector<std::string> modeNames;
vector<Multimeter::MeasurementTypes> modes;
if(!isMain)
{
// Add None for secondary measurement to be able to disable it
modeNames.push_back("None");
modes.push_back(Multimeter::MeasurementTypes::NONE);
}
int modeSelector = 0;
for(unsigned int i=0; i<32; i++)
{
auto mode = static_cast<Multimeter::MeasurementTypes>(1 << i);
if(modemask & mode)
{
modes.push_back(mode);
modeNames.push_back(dmm->ModeToText(mode));
if(curMode == mode)
modeSelector = modes.size() - 1;
}
}
if(renderCombo("##mode", true, color, modeSelector, modeNames,true,3,true))
{
curMode = modes[modeSelector];
if(isMain)
dmm->SetMeterMode(curMode);
else
{
dmm->SetSecondaryMeterMode(curMode);
// Open or close tree node according the secondary measure mode
ImGuiContext& g = *GImGui;
ImGui::TreeNodeSetOpen(g.LastItemData.ID,(curMode > 0));
}
}
ImGui::PopID();
StreamDescriptor s(dmmchan, streamIndex);
if(ImGui::BeginDragDropSource())
{
if(s.GetType() == Stream::STREAM_TYPE_ANALOG_SCALAR)
ImGui::SetDragDropPayload("Scalar", &s, sizeof(s));
else
ImGui::SetDragDropPayload("Stream", &s, sizeof(s));
ImGui::TextUnformatted(s.GetName().c_str());
ImGui::EndDragDropSource();
}
else
DoItemHelp();
if(open)
ImGui::TreePop();
if(open)
{
renderNumericValue(valueText,clicked,hovered,color,true,ImGui::GetFontSize()*2,false);
if(isMain)
{
auto dmmState = m_session->GetDmmState(dmm);
if(dmmState)
{
ImGui::PushID("autorange");
// For main, also show the autorange combo
startBadgeLine();
bool autorange = dmmState->m_autoRange.load();
if(renderOnOffToggle("##autorange",true,autorange,"Manual Range","Autorange",3))
{
dmm->SetMeterAutoRange(autorange);
dmmState->m_needsUpdate = true;
}
ImGui::PopID();
}
}
}
ImGui::PopID();
}
/**
@brief Render AWG channel properties
@param awg the AWG to render channel properties for
@param awgchan the AWG channel to render properties for
*/
void StreamBrowserDialog::renderAwgProperties(std::shared_ptr<FunctionGenerator> awg, FunctionGeneratorChannel* awgchan)
{
Unit volts(Unit::UNIT_VOLTS);
Unit hz(Unit::UNIT_HZ);
Unit percent(Unit::UNIT_PERCENT);
Unit fs(Unit::UNIT_FS);
size_t channelIndex = awgchan->GetIndex();
auto awgState = m_session->GetFunctionGeneratorState(awg);
if(!awgState)
return;
auto dwidth = ImGui::GetFontSize() * 6;
//Check if anything changed hardware side
float amp = awgState->m_channelAmplitude[channelIndex];
if(amp != awgState->m_committedAmplitude[channelIndex])
{
awgState->m_committedAmplitude[channelIndex] = amp;
awgState->m_strAmplitude[channelIndex] = volts.PrettyPrint(amp);
}
float off = awgState->m_channelOffset[channelIndex];
if(off != awgState->m_committedOffset[channelIndex])
{
awgState->m_committedOffset[channelIndex] = off;
awgState->m_strOffset[channelIndex] = volts.PrettyPrint(off);
}
float freq = awgState->m_channelFrequency[channelIndex];
if(freq != awgState->m_committedFrequency[channelIndex])
{
awgState->m_committedFrequency[channelIndex] = freq;
awgState->m_strFrequency[channelIndex] = hz.PrettyPrint(freq);
}
float dutyCycle = awgState->m_channelDutyCycle[channelIndex];
if(dutyCycle != awgState->m_committedDutyCycle[channelIndex])
{
awgState->m_committedDutyCycle[channelIndex] = dutyCycle;
awgState->m_strDutyCycle[channelIndex] = percent.PrettyPrint(dutyCycle);
}
float riseTime = awgState->m_channelRiseTime[channelIndex];
if(riseTime != awgState->m_committedRiseTime[channelIndex])
{
awgState->m_committedRiseTime[channelIndex] = riseTime;
awgState->m_strRiseTime[channelIndex] = fs.PrettyPrint(riseTime);
}
float fallTime = awgState->m_channelFallTime[channelIndex];
if(fallTime != awgState->m_committedFallTime[channelIndex])
{
awgState->m_committedFallTime[channelIndex] = fallTime;
awgState->m_strFallTime[channelIndex] = fs.PrettyPrint(fallTime);
}
auto& prefs = m_session->GetPreferences();
// Row 1
ImGui::Text("Waveform:");
startBadgeLine(); // Needed for shape combo
// Shape combo
// Get current shape and shape index
FunctionGenerator::WaveShape shape = awgState->m_channelShape[channelIndex];
int shapeIndex = awgState->m_channelShapeIndexes[channelIndex][shape];
if(renderCombo(
"##waveform",
true,
ImGui::ColorConvertU32ToFloat4(ColorFromString(awgchan->m_displaycolor)),
shapeIndex, awgState->m_channelShapeNames[channelIndex],
true,
3,true))
{
shape = awgState->m_channelShapes[channelIndex][shapeIndex];
awg->SetFunctionChannelShape(channelIndex, shape);
// Update state right now to cover from slow intruments
awgState->m_channelShape[channelIndex]=shape;
// Tell intrument thread that the FunctionGenerator state has to be updated
awgState->m_needsUpdate[channelIndex] = true;
}
// Store current Y position for shape preview
float shapePreviewY = ImGui::GetCursorPosY();
// Row 2
// Frequency label
if(renderEditableProperty(dwidth,
"Frequency",
awgState->m_strFrequency[channelIndex],
awgState->m_committedFrequency[channelIndex],
hz/*,"Frequency of the generated waveform"*/))
{
awg->SetFunctionChannelFrequency(channelIndex, awgState->m_committedFrequency[channelIndex]);
awgState->m_needsUpdate[channelIndex] = true;
}
/*StreamDescriptor sfreq(awgchan, 0);
if(ImGui::BeginDragDropSource())
{
ImGui::SetDragDropPayload("Scalar", &sfreq, sizeof(sfreq));
string dragText = awgchan->GetDisplayName() + " frequency";
ImGui::TextUnformatted(dragText.c_str());
ImGui::EndDragDropSource();
}
else
DoItemHelp();
*/
//Row 2
//Duty cycle
if(renderEditableProperty(dwidth,
"Duty cycle",
awgState->m_strDutyCycle[channelIndex],
awgState->m_committedDutyCycle[channelIndex],
percent/*,"Duty cycle of the waveform, in percent. Not applicable to all waveform types."*/))
{
awg->SetFunctionChannelDutyCycle(channelIndex, awgState->m_committedDutyCycle[channelIndex]);
awgState->m_needsUpdate[channelIndex] = true;
}
// Shape preview
startBadgeLine();
auto height = ImGui::GetFontSize() * 2;
auto width = height * 2;
if ((m_badgeXCur - width) >= m_badgeXMin)
{
// ok, we have enough space draw preview
m_badgeXCur -= width;
// save current y position to restore it after drawing the preview
float currentY = ImGui::GetCursorPosY();
// Continue layout on current line (row 3)
ImGui::SameLine(m_badgeXCur);
// But use y position of row 2
ImGui::SetCursorPosY(shapePreviewY);
ImGui::Image(
m_parent->GetTextureManager()->GetTexture(m_parent->GetIconForWaveformShape(shape)),
ImVec2(width,height));
// Now that we're done with shape preview, restore y position of row 3
ImGui::SetCursorPosY(currentY);
}
if(awg->HasFunctionRiseFallTimeControls(channelIndex))
{ //Row 3
//Fall Time
if(renderEditableProperty(dwidth,
"Rise Time",
awgState->m_strRiseTime[channelIndex],
awgState->m_committedRiseTime[channelIndex],
fs))
{
awg->SetFunctionChannelRiseTime(channelIndex, awgState->m_committedRiseTime[channelIndex]);
awgState->m_needsUpdate[channelIndex] = true;
}
//Row 4
//Fall Time
if(renderEditableProperty(dwidth,
"Fall Time",
awgState->m_strFallTime[channelIndex],
awgState->m_committedFallTime[channelIndex],
fs))
{
awg->SetFunctionChannelFallTime(channelIndex, awgState->m_committedFallTime[channelIndex]);
awgState->m_needsUpdate[channelIndex] = true;
}
}
// Row 5
if(renderEditablePropertyWithExplicitApply(dwidth,
"Amplitude",
awgState->m_strAmplitude[channelIndex],
awgState->m_committedAmplitude[channelIndex],
volts,"Peak-to-peak amplitude of the generated waveform"))
{
awg->SetFunctionChannelAmplitude(channelIndex, awgState->m_committedAmplitude[channelIndex]);
awgState->m_needsUpdate[channelIndex] = true;
}
//Row 6
//Offset
if(renderEditablePropertyWithExplicitApply(dwidth,
"Offset",
awgState->m_strOffset[channelIndex],
awgState->m_committedOffset[channelIndex],
volts,"DC offset for the waveform above (positive) or below (negative) ground"))
{
awg->SetFunctionChannelOffset(channelIndex, awgState->m_committedOffset[channelIndex]);
awgState->m_needsUpdate[channelIndex] = true;
}
//Row 7
//Impedance
ImGui::SetNextItemWidth(dwidth);
FunctionGenerator::OutputImpedance impedance = awgState->m_channelOutputImpedance[channelIndex];
bool isHiZ = (impedance == FunctionGenerator::OutputImpedance::IMPEDANCE_HIGH_Z);
int comboValue = isHiZ ? 0 : 1;
bool changed = renderCombo(
"Impedance",
false,
ImGui::ColorConvertU32ToFloat4(prefs.GetColor(
isHiZ ? "Appearance.Stream Browser.awg_hiz_badge_color" :
"Appearance.Stream Browser.awg_50ohms_badge_color")),
&comboValue,
"Hi-Z",
"50 Ω",
nullptr);
HelpMarker(
"Select the expected load impedance.\n\n"
"If set incorrectly, amplitude and offset will be inaccurate due to reflections.");
if(changed)
{
impedance = ((comboValue == 0) ? FunctionGenerator::OutputImpedance::IMPEDANCE_HIGH_Z : FunctionGenerator::OutputImpedance::IMPEDANCE_50_OHM);
awg->SetFunctionChannelOutputImpedance(channelIndex,impedance);
// Update state right now to cover from slow intruments
awgState->m_channelOutputImpedance[channelIndex]=impedance;
// Tell intrument thread that the FunctionGenerator state has to be updated
awgState->m_needsUpdate[channelIndex] = true;
}
}
/**
@brief Rendering of an instrument node
@param instrument the instrument to render
*/
void StreamBrowserDialog::renderInstrumentNode(shared_ptr<Instrument> instrument)
{
// Get preferences for colors
auto& prefs = m_session->GetPreferences();
ImGui::PushID(instrument.get());
bool instIsOpen = ImGui::TreeNodeEx(instrument->m_nickname.c_str(), ImGuiTreeNodeFlags_DefaultOpen);
startBadgeLine();
auto state = m_session->GetInstrumentConnectionState(instrument);