2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
30 #include "samplingbar.h"
32 #include <pv/devicemanager.h>
33 #include <pv/device/devinst.h>
34 #include <pv/popups/deviceoptions.h>
35 #include <pv/popups/channels.h>
41 using std::shared_ptr;
47 const uint64_t SamplingBar::MinSampleCount = 100ULL;
48 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
49 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
51 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
52 QToolBar("Sampling Bar", parent),
54 _device_selector(this),
55 _updating_device_selector(false),
56 _configure_button(this),
57 _configure_button_action(NULL),
58 _channels_button(this),
59 _sample_count(" samples", this),
60 _sample_rate("Hz", this),
61 _updating_sample_rate(false),
62 _updating_sample_count(false),
63 _sample_count_supported(false),
64 _icon_red(":/icons/status-red.svg"),
65 _icon_green(":/icons/status-green.svg"),
66 _icon_grey(":/icons/status-grey.svg"),
67 _run_stop_button(this)
69 setObjectName(QString::fromUtf8("SamplingBar"));
71 connect(&_run_stop_button, SIGNAL(clicked()),
72 this, SLOT(on_run_stop()));
73 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
74 this, SLOT(on_device_selected()));
75 connect(&_sample_count, SIGNAL(value_changed()),
76 this, SLOT(on_sample_count_changed()));
77 connect(&_sample_rate, SIGNAL(value_changed()),
78 this, SLOT(on_sample_rate_changed()));
80 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
82 set_capture_state(pv::SigSession::Stopped);
84 _configure_button.setIcon(QIcon::fromTheme("configure",
85 QIcon(":/icons/configure.png")));
87 _channels_button.setIcon(QIcon::fromTheme("channels",
88 QIcon(":/icons/channels.svg")));
90 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
92 addWidget(&_device_selector);
93 _configure_button_action = addWidget(&_configure_button);
94 addWidget(&_channels_button);
95 addWidget(&_sample_count);
96 addWidget(&_sample_rate);
98 addWidget(&_run_stop_button);
100 _sample_count.installEventFilter(this);
101 _sample_rate.installEventFilter(this);
104 void SamplingBar::set_device_list(
105 const std::list< shared_ptr<pv::device::DevInst> > &devices,
106 shared_ptr<pv::device::DevInst> selected)
108 int selected_index = -1;
112 _updating_device_selector = true;
114 _device_selector.clear();
115 _device_selector_map.clear();
117 for (shared_ptr<pv::device::DevInst> dev_inst : devices) {
119 const string title = dev_inst->format_device_title();
120 const sr_dev_inst *sdi = dev_inst->dev_inst();
123 if (selected == dev_inst)
124 selected_index = _device_selector.count();
126 _device_selector_map[sdi] = dev_inst;
127 _device_selector.addItem(title.c_str(),
128 qVariantFromValue((void*)sdi));
131 // The selected device should have been in the list
132 assert(selected_index != -1);
133 _device_selector.setCurrentIndex(selected_index);
135 update_device_config_widgets();
137 _updating_device_selector = false;
140 shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
142 const int index = _device_selector.currentIndex();
144 return shared_ptr<pv::device::DevInst>();
146 const sr_dev_inst *const sdi =
147 (const sr_dev_inst*)_device_selector.itemData(
148 index).value<void*>();
151 const auto iter = _device_selector_map.find(sdi);
152 if (iter == _device_selector_map.end())
153 return shared_ptr<pv::device::DevInst>();
155 return shared_ptr<pv::device::DevInst>((*iter).second);
158 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
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"));
166 void SamplingBar::update_sample_rate_selector()
168 GVariant *gvar_dict, *gvar_list;
169 const uint64_t *elements = NULL;
172 if (_updating_sample_rate)
175 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
179 assert(!_updating_sample_rate);
180 _updating_sample_rate = true;
182 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
184 _sample_rate.show_none();
185 _updating_sample_rate = false;
189 if ((gvar_list = g_variant_lookup_value(gvar_dict,
190 "samplerate-steps", G_VARIANT_TYPE("at"))))
192 elements = (const uint64_t *)g_variant_get_fixed_array(
193 gvar_list, &num_elements, sizeof(uint64_t));
195 const uint64_t min = elements[0];
196 const uint64_t max = elements[1];
197 const uint64_t step = elements[2];
199 g_variant_unref(gvar_list);
207 _sample_rate.show_125_list(min, max);
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
214 _sample_rate.show_min_max_step(min, max, step);
217 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
218 "samplerates", G_VARIANT_TYPE("at"))))
220 elements = (const uint64_t *)g_variant_get_fixed_array(
221 gvar_list, &num_elements, sizeof(uint64_t));
222 _sample_rate.show_list(elements, num_elements);
223 g_variant_unref(gvar_list);
225 _updating_sample_rate = false;
227 g_variant_unref(gvar_dict);
228 update_sample_rate_selector_value();
231 void SamplingBar::update_sample_rate_selector_value()
236 if (_updating_sample_rate)
239 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
243 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
244 qDebug() << "WARNING: Failed to get value of sample rate";
247 samplerate = g_variant_get_uint64(gvar);
248 g_variant_unref(gvar);
250 assert(!_updating_sample_rate);
251 _updating_sample_rate = true;
252 _sample_rate.set_value(samplerate);
253 _updating_sample_rate = false;
256 void SamplingBar::update_sample_count_selector()
260 if (_updating_sample_count)
263 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
267 assert(!_updating_sample_count);
268 _updating_sample_count = true;
270 if (_sample_count_supported)
272 uint64_t sample_count = _sample_count.value();
273 uint64_t min_sample_count = 0;
274 uint64_t max_sample_count = MaxSampleCount;
276 if (sample_count == 0)
277 sample_count = DefaultSampleCount;
279 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
281 g_variant_get(gvar, "(tt)",
282 &min_sample_count, &max_sample_count);
283 g_variant_unref(gvar);
286 min_sample_count = min(max(min_sample_count, MinSampleCount),
289 _sample_count.show_125_list(
290 min_sample_count, max_sample_count);
292 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
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),
300 g_variant_unref(gvar);
303 _sample_count.set_value(sample_count);
306 _sample_count.show_none();
308 _updating_sample_count = false;
311 void SamplingBar::update_device_config_widgets()
315 using namespace pv::popups;
317 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
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);
327 // Update the channels popup
328 Channels *const channels = new Channels(_session, this);
329 _channels_button.set_popup(channels);
331 // Update supported options.
332 _sample_count_supported = false;
334 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
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++)
342 switch (options[i] & SR_CONF_MASK) {
343 case SR_CONF_LIMIT_SAMPLES:
344 if (options[i] & SR_CONF_SET)
345 _sample_count_supported = true;
347 case SR_CONF_LIMIT_FRAMES:
348 if (options[i] & SR_CONF_SET)
349 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
350 g_variant_new_uint64(1));
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()));
361 // Update sweep timing widgets.
362 update_sample_count_selector();
363 update_sample_rate_selector();
366 void SamplingBar::commit_sample_count()
368 uint64_t sample_count = 0;
370 if (_updating_sample_count)
373 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
377 sample_count = _sample_count.value();
379 // Set the sample count
380 assert(!_updating_sample_count);
381 _updating_sample_count = true;
382 if (_sample_count_supported &&
383 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
384 g_variant_new_uint64(sample_count))) {
385 qDebug() << "Failed to configure sample count.";
388 _updating_sample_count = false;
391 void SamplingBar::commit_sample_rate()
393 uint64_t sample_rate = 0;
395 if (_updating_sample_rate)
398 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
402 sample_rate = _sample_rate.value();
403 if (sample_rate == 0)
406 // Set the samplerate
407 assert(!_updating_sample_rate);
408 _updating_sample_rate = true;
409 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
410 g_variant_new_uint64(sample_rate))) {
411 qDebug() << "Failed to configure samplerate.";
414 _updating_sample_rate = false;
417 void SamplingBar::on_device_selected()
419 if (_updating_device_selector)
422 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
426 _session.set_device(dev_inst);
428 update_device_config_widgets();
431 void SamplingBar::on_sample_count_changed()
433 commit_sample_count();
436 void SamplingBar::on_sample_rate_changed()
438 commit_sample_rate();
441 void SamplingBar::on_run_stop()
443 commit_sample_count();
444 commit_sample_rate();
448 void SamplingBar::on_config_changed()
450 commit_sample_count();
451 update_sample_count_selector();
452 commit_sample_rate();
453 update_sample_rate_selector();
456 bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
458 if ((watched == &_sample_count || watched == &_sample_rate) &&
459 (event->type() == QEvent::ToolTip)) {
460 double sec = (double)_sample_count.value() / _sample_rate.value();
461 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
463 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
464 QToolTip::showText(help_event->globalPos(), str);
472 } // namespace toolbars