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 connect(&_run_stop_button, SIGNAL(clicked()),
70 this, SLOT(on_run_stop()));
71 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
72 this, SLOT(on_device_selected()));
73 connect(&_sample_count, SIGNAL(value_changed()),
74 this, SLOT(on_sample_count_changed()));
75 connect(&_sample_rate, SIGNAL(value_changed()),
76 this, SLOT(on_sample_rate_changed()));
78 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
80 set_capture_state(pv::SigSession::Stopped);
82 _configure_button.setIcon(QIcon::fromTheme("configure",
83 QIcon(":/icons/configure.png")));
85 _channels_button.setIcon(QIcon::fromTheme("channels",
86 QIcon(":/icons/channels.svg")));
88 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
90 addWidget(&_device_selector);
91 _configure_button_action = addWidget(&_configure_button);
92 addWidget(&_channels_button);
93 addWidget(&_sample_count);
94 addWidget(&_sample_rate);
96 addWidget(&_run_stop_button);
98 _sample_count.installEventFilter(this);
99 _sample_rate.installEventFilter(this);
102 void SamplingBar::set_device_list(
103 const std::list< shared_ptr<pv::device::DevInst> > &devices,
104 shared_ptr<pv::device::DevInst> selected)
106 int selected_index = -1;
110 _updating_device_selector = true;
112 _device_selector.clear();
113 _device_selector_map.clear();
115 for (shared_ptr<pv::device::DevInst> dev_inst : devices) {
117 const string title = dev_inst->format_device_title();
118 const sr_dev_inst *sdi = dev_inst->dev_inst();
121 if (selected == dev_inst)
122 selected_index = _device_selector.count();
124 _device_selector_map[sdi] = dev_inst;
125 _device_selector.addItem(title.c_str(),
126 qVariantFromValue((void*)sdi));
129 // The selected device should have been in the list
130 assert(selected_index != -1);
131 _device_selector.setCurrentIndex(selected_index);
133 update_device_config_widgets();
135 _updating_device_selector = false;
138 shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
140 const int index = _device_selector.currentIndex();
142 return shared_ptr<pv::device::DevInst>();
144 const sr_dev_inst *const sdi =
145 (const sr_dev_inst*)_device_selector.itemData(
146 index).value<void*>();
149 const auto iter = _device_selector_map.find(sdi);
150 if (iter == _device_selector_map.end())
151 return shared_ptr<pv::device::DevInst>();
153 return shared_ptr<pv::device::DevInst>((*iter).second);
156 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
158 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
159 _run_stop_button.setIcon(*icons[state]);
160 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
161 tr("Run") : tr("Stop"));
164 void SamplingBar::update_sample_rate_selector()
166 GVariant *gvar_dict, *gvar_list;
167 const uint64_t *elements = NULL;
170 if (_updating_sample_rate)
173 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
177 assert(!_updating_sample_rate);
178 _updating_sample_rate = true;
180 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
182 _sample_rate.show_none();
183 _updating_sample_rate = false;
187 if ((gvar_list = g_variant_lookup_value(gvar_dict,
188 "samplerate-steps", G_VARIANT_TYPE("at"))))
190 elements = (const uint64_t *)g_variant_get_fixed_array(
191 gvar_list, &num_elements, sizeof(uint64_t));
193 const uint64_t min = elements[0];
194 const uint64_t max = elements[1];
195 const uint64_t step = elements[2];
197 g_variant_unref(gvar_list);
205 _sample_rate.show_125_list(min, max);
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
212 _sample_rate.show_min_max_step(min, max, step);
215 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
216 "samplerates", G_VARIANT_TYPE("at"))))
218 elements = (const uint64_t *)g_variant_get_fixed_array(
219 gvar_list, &num_elements, sizeof(uint64_t));
220 _sample_rate.show_list(elements, num_elements);
221 g_variant_unref(gvar_list);
223 _updating_sample_rate = false;
225 g_variant_unref(gvar_dict);
226 update_sample_rate_selector_value();
229 void SamplingBar::update_sample_rate_selector_value()
234 if (_updating_sample_rate)
237 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
241 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
242 qDebug() << "WARNING: Failed to get value of sample rate";
245 samplerate = g_variant_get_uint64(gvar);
246 g_variant_unref(gvar);
248 assert(!_updating_sample_rate);
249 _updating_sample_rate = true;
250 _sample_rate.set_value(samplerate);
251 _updating_sample_rate = false;
254 void SamplingBar::update_sample_count_selector()
258 if (_updating_sample_count)
261 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
265 assert(!_updating_sample_count);
266 _updating_sample_count = true;
268 if (_sample_count_supported)
270 uint64_t sample_count = _sample_count.value();
271 uint64_t min_sample_count = 0;
272 uint64_t max_sample_count = MaxSampleCount;
274 if (sample_count == 0)
275 sample_count = DefaultSampleCount;
277 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
279 g_variant_get(gvar, "(tt)",
280 &min_sample_count, &max_sample_count);
281 g_variant_unref(gvar);
284 min_sample_count = min(max(min_sample_count, MinSampleCount),
287 _sample_count.show_125_list(
288 min_sample_count, max_sample_count);
290 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
292 sample_count = g_variant_get_uint64(gvar);
293 if (sample_count == 0)
294 sample_count = DefaultSampleCount;
295 sample_count = min(max(sample_count, MinSampleCount),
298 g_variant_unref(gvar);
301 _sample_count.set_value(sample_count);
304 _sample_count.show_none();
306 _updating_sample_count = false;
309 void SamplingBar::update_device_config_widgets()
313 using namespace pv::popups;
315 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
319 // Update the configure popup
320 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
321 _configure_button_action->setVisible(
322 !opts->binding().properties().empty());
323 _configure_button.set_popup(opts);
325 // Update the channels popup
326 Channels *const channels = new Channels(_session, this);
327 _channels_button.set_popup(channels);
329 // Update supported options.
330 _sample_count_supported = false;
332 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
335 const int *const options =
336 (const int32_t *)g_variant_get_fixed_array(
337 gvar, &num_opts, sizeof(int32_t));
338 for (unsigned int i = 0; i < num_opts; i++)
340 switch (options[i]) {
341 case SR_CONF_LIMIT_SAMPLES:
342 _sample_count_supported = true;
344 case SR_CONF_LIMIT_FRAMES:
345 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
346 g_variant_new_uint64(1));
352 // Add notification of reconfigure events
353 disconnect(this, SLOT(on_config_changed()));
354 connect(dev_inst.get(), SIGNAL(config_changed()),
355 this, SLOT(on_config_changed()));
357 // Update sweep timing widgets.
358 update_sample_count_selector();
359 update_sample_rate_selector();
362 void SamplingBar::commit_sample_count()
364 uint64_t sample_count = 0;
366 if (_updating_sample_count)
369 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
373 sample_count = _sample_count.value();
375 // Set the sample count
376 assert(!_updating_sample_count);
377 _updating_sample_count = true;
378 if (_sample_count_supported &&
379 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
380 g_variant_new_uint64(sample_count))) {
381 qDebug() << "Failed to configure sample count.";
384 _updating_sample_count = false;
387 void SamplingBar::commit_sample_rate()
389 uint64_t sample_rate = 0;
391 if (_updating_sample_rate)
394 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
398 sample_rate = _sample_rate.value();
399 if (sample_rate == 0)
402 // Set the samplerate
403 assert(!_updating_sample_rate);
404 _updating_sample_rate = true;
405 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
406 g_variant_new_uint64(sample_rate))) {
407 qDebug() << "Failed to configure samplerate.";
410 _updating_sample_rate = false;
413 void SamplingBar::on_device_selected()
415 if (_updating_device_selector)
418 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
422 _session.set_device(dev_inst);
424 update_device_config_widgets();
427 void SamplingBar::on_sample_count_changed()
429 commit_sample_count();
432 void SamplingBar::on_sample_rate_changed()
434 commit_sample_rate();
437 void SamplingBar::on_run_stop()
439 commit_sample_count();
440 commit_sample_rate();
444 void SamplingBar::on_config_changed()
446 commit_sample_count();
447 update_sample_count_selector();
448 commit_sample_rate();
449 update_sample_rate_selector();
452 bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
454 if ((watched == &_sample_count || watched == &_sample_rate) &&
455 (event->type() == QEvent::ToolTip)) {
456 double sec = (double)_sample_count.value() / _sample_rate.value();
457 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
459 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
460 QToolTip::showText(help_event->globalPos(), str);
468 } // namespace toolbars