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