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