]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Adapt to config key capabilities.
[pulseview.git] / pv / toolbars / samplingbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
09f5d123 21#include <extdef.h>
dde1a563 22
09f5d123 23#include <assert.h>
215f9499 24
dde1a563 25#include <QAction>
48888313 26#include <QDebug>
40065ab6
JS
27#include <QHelpEvent>
28#include <QToolTip>
dde1a563 29
d4984fe7
JH
30#include "samplingbar.h"
31
dd63af74 32#include <pv/devicemanager.h>
94574501 33#include <pv/device/devinst.h>
51d4a9ab 34#include <pv/popups/deviceoptions.h>
6ac6242b 35#include <pv/popups/channels.h>
62974f45 36#include <pv/util.h>
cdb50f67 37
19adbc2c 38using std::map;
b50ef520
JH
39using std::max;
40using std::min;
f9abf97e 41using std::shared_ptr;
819f4c25 42using std::string;
dd63af74 43
51e77110 44namespace pv {
f4c92e1c 45namespace toolbars {
51e77110 46
b50ef520
JH
47const uint64_t SamplingBar::MinSampleCount = 100ULL;
48const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
49const uint64_t SamplingBar::DefaultSampleCount = 1000000;
09f5d123 50
aca00b1e 51SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
6fb67b27 52 QToolBar("Sampling Bar", parent),
aca00b1e 53 _session(session),
dde1a563 54 _device_selector(this),
95237c18 55 _updating_device_selector(false),
cdb50f67 56 _configure_button(this),
b3e8a5d8 57 _configure_button_action(NULL),
6ac6242b 58 _channels_button(this),
124d97de 59 _sample_count(" samples", this),
1198b887
JH
60 _sample_rate("Hz", this),
61 _updating_sample_rate(false),
85756012 62 _updating_sample_count(false),
6546a6a7 63 _sample_count_supported(false),
2b49eeb0 64 _icon_red(":/icons/status-red.svg"),
f5798068
JH
65 _icon_green(":/icons/status-green.svg"),
66 _icon_grey(":/icons/status-grey.svg"),
274d4f13 67 _run_stop_button(this)
6fb67b27 68{
0c7cdea2
SA
69 setObjectName(QString::fromUtf8("SamplingBar"));
70
cdb50f67 71 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 72 this, SLOT(on_run_stop()));
dde1a563
JH
73 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
74 this, SLOT(on_device_selected()));
124d97de
ML
75 connect(&_sample_count, SIGNAL(value_changed()),
76 this, SLOT(on_sample_count_changed()));
1198b887
JH
77 connect(&_sample_rate, SIGNAL(value_changed()),
78 this, SLOT(on_sample_rate_changed()));
dde1a563 79
124d97de 80 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
215f9499 81
2b49eeb0 82 set_capture_state(pv::SigSession::Stopped);
274d4f13 83
688ef645
JH
84 _configure_button.setIcon(QIcon::fromTheme("configure",
85 QIcon(":/icons/configure.png")));
b7b659aa 86
6ac6242b
ML
87 _channels_button.setIcon(QIcon::fromTheme("channels",
88 QIcon(":/icons/channels.svg")));
cdb50f67 89
f5798068
JH
90 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
91
6fb67b27 92 addWidget(&_device_selector);
b3e8a5d8 93 _configure_button_action = addWidget(&_configure_button);
6ac6242b 94 addWidget(&_channels_button);
124d97de 95 addWidget(&_sample_count);
1198b887 96 addWidget(&_sample_rate);
6fb67b27 97
1198b887 98 addWidget(&_run_stop_button);
40065ab6
JS
99
100 _sample_count.installEventFilter(this);
101 _sample_rate.installEventFilter(this);
6fb67b27
JH
102}
103
18203d86 104void SamplingBar::set_device_list(
e95e8563
JH
105 const std::list< shared_ptr<pv::device::DevInst> > &devices,
106 shared_ptr<pv::device::DevInst> selected)
18203d86 107{
e95e8563
JH
108 int selected_index = -1;
109
110 assert(selected);
111
95237c18
JH
112 _updating_device_selector = true;
113
18203d86 114 _device_selector.clear();
19adbc2c 115 _device_selector_map.clear();
18203d86 116
d9aecf1f 117 for (shared_ptr<pv::device::DevInst> dev_inst : devices) {
19adbc2c
JH
118 assert(dev_inst);
119 const string title = dev_inst->format_device_title();
120 const sr_dev_inst *sdi = dev_inst->dev_inst();
121 assert(sdi);
122
e95e8563
JH
123 if (selected == dev_inst)
124 selected_index = _device_selector.count();
125
19adbc2c 126 _device_selector_map[sdi] = dev_inst;
dd63af74
JH
127 _device_selector.addItem(title.c_str(),
128 qVariantFromValue((void*)sdi));
18203d86
JH
129 }
130
e95e8563
JH
131 // The selected device should have been in the list
132 assert(selected_index != -1);
133 _device_selector.setCurrentIndex(selected_index);
134
135 update_device_config_widgets();
136
95237c18 137 _updating_device_selector = false;
18203d86
JH
138}
139
94574501 140shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
d4984fe7 141{
6fb67b27 142 const int index = _device_selector.currentIndex();
333d5bbc 143 if (index < 0)
94574501 144 return shared_ptr<pv::device::DevInst>();
19adbc2c
JH
145
146 const sr_dev_inst *const sdi =
147 (const sr_dev_inst*)_device_selector.itemData(
148 index).value<void*>();
149 assert(sdi);
6fb67b27 150
f46e495e 151 const auto iter = _device_selector_map.find(sdi);
19adbc2c 152 if (iter == _device_selector_map.end())
94574501 153 return shared_ptr<pv::device::DevInst>();
19adbc2c 154
94574501 155 return shared_ptr<pv::device::DevInst>((*iter).second);
6fb67b27
JH
156}
157
2b49eeb0 158void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 159{
2b49eeb0
JH
160 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
161 _run_stop_button.setIcon(*icons[state]);
162 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
163 tr("Run") : tr("Stop"));
6ac96c2e
JH
164}
165
dde1a563
JH
166void SamplingBar::update_sample_rate_selector()
167{
488f5d3f
BV
168 GVariant *gvar_dict, *gvar_list;
169 const uint64_t *elements = NULL;
170 gsize num_elements;
dde1a563 171
3d4f16af
JH
172 if (_updating_sample_rate)
173 return;
174
94574501 175 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c 176 if (!dev_inst)
ef4d0201
JH
177 return;
178
3d4f16af 179 assert(!_updating_sample_rate);
1198b887
JH
180 _updating_sample_rate = true;
181
8dd44190 182 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
1198b887
JH
183 {
184 _sample_rate.show_none();
185 _updating_sample_rate = false;
dde1a563 186 return;
1198b887 187 }
dde1a563 188
488f5d3f 189 if ((gvar_list = g_variant_lookup_value(gvar_dict,
1198b887
JH
190 "samplerate-steps", G_VARIANT_TYPE("at"))))
191 {
488f5d3f
BV
192 elements = (const uint64_t *)g_variant_get_fixed_array(
193 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
194
195 const uint64_t min = elements[0];
196 const uint64_t max = elements[1];
197 const uint64_t step = elements[2];
198
488f5d3f 199 g_variant_unref(gvar_list);
aafe53af
JH
200
201 assert(min > 0);
202 assert(max > 0);
203 assert(max > min);
204 assert(step > 0);
205
206 if (step == 1)
207 _sample_rate.show_125_list(min, max);
208 else
209 {
210 // When the step is not 1, we cam't make a 1-2-5-10
211 // list of sample rates, because we may not be able to
212 // make round numbers. Therefore in this case, show a
213 // spin box.
214 _sample_rate.show_min_max_step(min, max, step);
215 }
dde1a563 216 }
488f5d3f
BV
217 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
218 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 219 {
488f5d3f
BV
220 elements = (const uint64_t *)g_variant_get_fixed_array(
221 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 222 _sample_rate.show_list(elements, num_elements);
488f5d3f 223 g_variant_unref(gvar_list);
dde1a563 224 }
1198b887 225 _updating_sample_rate = false;
48888313 226
488f5d3f 227 g_variant_unref(gvar_dict);
48888313
JH
228 update_sample_rate_selector_value();
229}
230
231void SamplingBar::update_sample_rate_selector_value()
232{
488f5d3f
BV
233 GVariant *gvar;
234 uint64_t samplerate;
235
3d4f16af
JH
236 if (_updating_sample_rate)
237 return;
238
94574501 239 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
240 if (!dev_inst)
241 return;
242
8dd44190
JH
243 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
244 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
245 return;
246 }
488f5d3f
BV
247 samplerate = g_variant_get_uint64(gvar);
248 g_variant_unref(gvar);
48888313 249
3d4f16af 250 assert(!_updating_sample_rate);
1198b887
JH
251 _updating_sample_rate = true;
252 _sample_rate.set_value(samplerate);
253 _updating_sample_rate = false;
48888313
JH
254}
255
85756012
ML
256void SamplingBar::update_sample_count_selector()
257{
85756012 258 GVariant *gvar;
85756012 259
3d4f16af
JH
260 if (_updating_sample_count)
261 return;
262
94574501 263 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
264 if (!dev_inst)
265 return;
266
3d4f16af 267 assert(!_updating_sample_count);
b50ef520 268 _updating_sample_count = true;
85756012 269
b50ef520 270 if (_sample_count_supported)
df3c1aa3 271 {
3cbffdcf 272 uint64_t sample_count = _sample_count.value();
1d04852f 273 uint64_t min_sample_count = 0;
b50ef520
JH
274 uint64_t max_sample_count = MaxSampleCount;
275
3cbffdcf
JH
276 if (sample_count == 0)
277 sample_count = DefaultSampleCount;
278
8dd44190
JH
279 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
280 {
1d04852f
JH
281 g_variant_get(gvar, "(tt)",
282 &min_sample_count, &max_sample_count);
b50ef520
JH
283 g_variant_unref(gvar);
284 }
285
1d04852f
JH
286 min_sample_count = min(max(min_sample_count, MinSampleCount),
287 max_sample_count);
288
289 _sample_count.show_125_list(
290 min_sample_count, max_sample_count);
b50ef520 291
8dd44190 292 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
b50ef520
JH
293 {
294 sample_count = g_variant_get_uint64(gvar);
295 if (sample_count == 0)
296 sample_count = DefaultSampleCount;
297 sample_count = min(max(sample_count, MinSampleCount),
298 max_sample_count);
299
300 g_variant_unref(gvar);
301 }
302
303 _sample_count.set_value(sample_count);
85756012 304 }
b50ef520
JH
305 else
306 _sample_count.show_none();
df3c1aa3 307
df3c1aa3 308 _updating_sample_count = false;
85756012
ML
309}
310
e95e8563
JH
311void SamplingBar::update_device_config_widgets()
312{
313 GVariant *gvar;
314
315 using namespace pv::popups;
316
317 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
318 if (!dev_inst)
319 return;
320
321 // Update the configure popup
322 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
323 _configure_button_action->setVisible(
324 !opts->binding().properties().empty());
325 _configure_button.set_popup(opts);
326
6ac6242b
ML
327 // Update the channels popup
328 Channels *const channels = new Channels(_session, this);
329 _channels_button.set_popup(channels);
e95e8563
JH
330
331 // Update supported options.
332 _sample_count_supported = false;
333
334 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
335 {
336 gsize num_opts;
337 const int *const options =
338 (const int32_t *)g_variant_get_fixed_array(
339 gvar, &num_opts, sizeof(int32_t));
340 for (unsigned int i = 0; i < num_opts; i++)
341 {
f27ee56d 342 switch (options[i] & SR_CONF_MASK) {
e95e8563 343 case SR_CONF_LIMIT_SAMPLES:
f27ee56d
BV
344 if (options[i] & SR_CONF_SET)
345 _sample_count_supported = true;
e95e8563
JH
346 break;
347 case SR_CONF_LIMIT_FRAMES:
f27ee56d
BV
348 if (options[i] & SR_CONF_SET)
349 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
350 g_variant_new_uint64(1));
e95e8563
JH
351 break;
352 }
353 }
354 }
355
356 // Add notification of reconfigure events
357 disconnect(this, SLOT(on_config_changed()));
358 connect(dev_inst.get(), SIGNAL(config_changed()),
359 this, SLOT(on_config_changed()));
360
361 // Update sweep timing widgets.
362 update_sample_count_selector();
363 update_sample_rate_selector();
364}
365
124d97de
ML
366void SamplingBar::commit_sample_count()
367{
368 uint64_t sample_count = 0;
369
3d4f16af
JH
370 if (_updating_sample_count)
371 return;
372
94574501 373 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
374 if (!dev_inst)
375 return;
376
124d97de
ML
377 sample_count = _sample_count.value();
378
379 // Set the sample count
3d4f16af
JH
380 assert(!_updating_sample_count);
381 _updating_sample_count = true;
3668e2fe
JH
382 if (_sample_count_supported &&
383 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
8dd44190 384 g_variant_new_uint64(sample_count))) {
124d97de
ML
385 qDebug() << "Failed to configure sample count.";
386 return;
387 }
3d4f16af 388 _updating_sample_count = false;
124d97de
ML
389}
390
48888313
JH
391void SamplingBar::commit_sample_rate()
392{
393 uint64_t sample_rate = 0;
394
3d4f16af
JH
395 if (_updating_sample_rate)
396 return;
397
94574501 398 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
399 if (!dev_inst)
400 return;
401
1198b887 402 sample_rate = _sample_rate.value();
f9541bde
JH
403 if (sample_rate == 0)
404 return;
405
48888313 406 // Set the samplerate
3d4f16af
JH
407 assert(!_updating_sample_rate);
408 _updating_sample_rate = true;
8dd44190
JH
409 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
410 g_variant_new_uint64(sample_rate))) {
48888313
JH
411 qDebug() << "Failed to configure samplerate.";
412 return;
413 }
3d4f16af 414 _updating_sample_rate = false;
dde1a563
JH
415}
416
417void SamplingBar::on_device_selected()
418{
95237c18
JH
419 if (_updating_device_selector)
420 return;
421
94574501 422 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
423 if (!dev_inst)
424 return;
425
19adbc2c 426 _session.set_device(dev_inst);
51d4a9ab 427
e95e8563 428 update_device_config_widgets();
dde1a563 429}
51e77110 430
124d97de
ML
431void SamplingBar::on_sample_count_changed()
432{
3d4f16af 433 commit_sample_count();
124d97de
ML
434}
435
48888313
JH
436void SamplingBar::on_sample_rate_changed()
437{
3d4f16af 438 commit_sample_rate();
48888313
JH
439}
440
9f3d12f3
JH
441void SamplingBar::on_run_stop()
442{
b50ef520 443 commit_sample_count();
9f3d12f3
JH
444 commit_sample_rate();
445 run_stop();
446}
447
82afd5e3
JH
448void SamplingBar::on_config_changed()
449{
450 commit_sample_count();
451 update_sample_count_selector();
452 commit_sample_rate();
453 update_sample_rate_selector();
454}
455
40065ab6
JS
456bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
457{
458 if ((watched == &_sample_count || watched == &_sample_rate) &&
459 (event->type() == QEvent::ToolTip)) {
460 double sec = (double)_sample_count.value() / _sample_rate.value();
40065ab6 461 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45
JS
462
463 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
40065ab6
JS
464 QToolTip::showText(help_event->globalPos(), str);
465
466 return true;
467 }
468
469 return false;
470}
471
f4c92e1c 472} // namespace toolbars
51e77110 473} // namespace pv