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
25 #include <boost/foreach.hpp>
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/probes.h>
37 using boost::shared_ptr;
46 const uint64_t SamplingBar::MinSampleCount = 100ULL;
47 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
48 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
50 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
51 QToolBar("Sampling Bar", parent),
53 _device_selector(this),
54 _updating_device_selector(false),
55 _configure_button(this),
56 _configure_button_action(NULL),
58 _sample_count(" samples", this),
59 _sample_rate("Hz", this),
60 _updating_sample_rate(false),
61 _updating_sample_count(false),
62 _sample_count_supported(false),
63 _icon_red(":/icons/status-red.svg"),
64 _icon_green(":/icons/status-green.svg"),
65 _icon_grey(":/icons/status-grey.svg"),
66 _run_stop_button(this)
68 connect(&_run_stop_button, SIGNAL(clicked()),
69 this, SLOT(on_run_stop()));
70 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
71 this, SLOT(on_device_selected()));
72 connect(&_sample_count, SIGNAL(value_changed()),
73 this, SLOT(on_sample_count_changed()));
74 connect(&_sample_rate, SIGNAL(value_changed()),
75 this, SLOT(on_sample_rate_changed()));
77 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
79 set_capture_state(pv::SigSession::Stopped);
81 _configure_button.setIcon(QIcon::fromTheme("configure",
82 QIcon(":/icons/configure.png")));
84 _probes_button.setIcon(QIcon::fromTheme("probes",
85 QIcon(":/icons/probes.svg")));
87 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
89 addWidget(&_device_selector);
90 _configure_button_action = addWidget(&_configure_button);
91 addWidget(&_probes_button);
92 addWidget(&_sample_count);
93 addWidget(&_sample_rate);
95 addWidget(&_run_stop_button);
98 void SamplingBar::set_device_list(
99 const std::list< shared_ptr<pv::device::DevInst> > &devices)
101 _updating_device_selector = true;
103 _device_selector.clear();
104 _device_selector_map.clear();
106 BOOST_FOREACH (shared_ptr<pv::device::DevInst> dev_inst, devices) {
108 const string title = dev_inst->format_device_title();
109 const sr_dev_inst *sdi = dev_inst->dev_inst();
112 _device_selector_map[sdi] = dev_inst;
113 _device_selector.addItem(title.c_str(),
114 qVariantFromValue((void*)sdi));
117 _updating_device_selector = false;
120 shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
122 const int index = _device_selector.currentIndex();
124 return shared_ptr<pv::device::DevInst>();
126 const sr_dev_inst *const sdi =
127 (const sr_dev_inst*)_device_selector.itemData(
128 index).value<void*>();
131 map<const sr_dev_inst*, boost::weak_ptr<device::DevInst> >::
132 const_iterator iter = _device_selector_map.find(sdi);
133 if (iter == _device_selector_map.end())
134 return shared_ptr<pv::device::DevInst>();
136 return shared_ptr<pv::device::DevInst>((*iter).second);
139 void SamplingBar::set_selected_device(shared_ptr<pv::device::DevInst> dev_inst)
143 for (int i = 0; i < _device_selector.count(); i++)
144 if (dev_inst->dev_inst() ==
145 _device_selector.itemData(i).value<void*>()) {
146 // Calling this leads to on_device_selected being
147 // invoked, which updates the sampling bar widgets.
148 _device_selector.setCurrentIndex(i);
153 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
155 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
156 _run_stop_button.setIcon(*icons[state]);
157 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
158 tr("Run") : tr("Stop"));
161 void SamplingBar::update_sample_rate_selector()
163 GVariant *gvar_dict, *gvar_list;
164 const uint64_t *elements = NULL;
167 if (_updating_sample_rate)
170 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
174 assert(!_updating_sample_rate);
175 _updating_sample_rate = true;
177 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
179 _sample_rate.show_none();
180 _updating_sample_rate = false;
184 if ((gvar_list = g_variant_lookup_value(gvar_dict,
185 "samplerate-steps", G_VARIANT_TYPE("at"))))
187 elements = (const uint64_t *)g_variant_get_fixed_array(
188 gvar_list, &num_elements, sizeof(uint64_t));
190 const uint64_t min = elements[0];
191 const uint64_t max = elements[1];
192 const uint64_t step = elements[2];
194 g_variant_unref(gvar_list);
202 _sample_rate.show_125_list(min, max);
205 // When the step is not 1, we cam't make a 1-2-5-10
206 // list of sample rates, because we may not be able to
207 // make round numbers. Therefore in this case, show a
209 _sample_rate.show_min_max_step(min, max, step);
212 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
213 "samplerates", G_VARIANT_TYPE("at"))))
215 elements = (const uint64_t *)g_variant_get_fixed_array(
216 gvar_list, &num_elements, sizeof(uint64_t));
217 _sample_rate.show_list(elements, num_elements);
218 g_variant_unref(gvar_list);
220 _updating_sample_rate = false;
222 g_variant_unref(gvar_dict);
223 update_sample_rate_selector_value();
226 void SamplingBar::update_sample_rate_selector_value()
231 if (_updating_sample_rate)
234 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
238 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
239 qDebug() << "WARNING: Failed to get value of sample rate";
242 samplerate = g_variant_get_uint64(gvar);
243 g_variant_unref(gvar);
245 assert(!_updating_sample_rate);
246 _updating_sample_rate = true;
247 _sample_rate.set_value(samplerate);
248 _updating_sample_rate = false;
251 void SamplingBar::update_sample_count_selector()
255 if (_updating_sample_count)
258 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
262 assert(!_updating_sample_count);
263 _updating_sample_count = true;
265 if (_sample_count_supported)
267 uint64_t sample_count = _sample_count.value();
268 uint64_t min_sample_count = 0;
269 uint64_t max_sample_count = MaxSampleCount;
271 if (sample_count == 0)
272 sample_count = DefaultSampleCount;
274 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
276 g_variant_get(gvar, "(tt)",
277 &min_sample_count, &max_sample_count);
278 g_variant_unref(gvar);
281 min_sample_count = min(max(min_sample_count, MinSampleCount),
284 _sample_count.show_125_list(
285 min_sample_count, max_sample_count);
287 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
289 sample_count = g_variant_get_uint64(gvar);
290 if (sample_count == 0)
291 sample_count = DefaultSampleCount;
292 sample_count = min(max(sample_count, MinSampleCount),
295 g_variant_unref(gvar);
298 _sample_count.set_value(sample_count);
301 _sample_count.show_none();
303 _updating_sample_count = false;
306 void SamplingBar::commit_sample_count()
308 uint64_t sample_count = 0;
310 if (_updating_sample_count)
313 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
317 sample_count = _sample_count.value();
319 // Set the sample count
320 assert(!_updating_sample_count);
321 _updating_sample_count = true;
322 if (_sample_count_supported &&
323 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
324 g_variant_new_uint64(sample_count))) {
325 qDebug() << "Failed to configure sample count.";
328 _updating_sample_count = false;
331 void SamplingBar::commit_sample_rate()
333 uint64_t sample_rate = 0;
335 if (_updating_sample_rate)
338 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
342 sample_rate = _sample_rate.value();
343 if (sample_rate == 0)
346 // Set the samplerate
347 assert(!_updating_sample_rate);
348 _updating_sample_rate = true;
349 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
350 g_variant_new_uint64(sample_rate))) {
351 qDebug() << "Failed to configure samplerate.";
354 _updating_sample_rate = false;
357 void SamplingBar::on_device_selected()
361 using namespace pv::popups;
363 if (_updating_device_selector)
366 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
370 _session.set_device(dev_inst);
372 // Update the configure popup
373 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
374 _configure_button_action->setVisible(
375 !opts->binding().properties().empty());
376 _configure_button.set_popup(opts);
378 // Update the probes popup
379 Probes *const probes = new Probes(_session, this);
380 _probes_button.set_popup(probes);
382 // Update supported options.
383 _sample_count_supported = false;
385 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
388 const int *const options =
389 (const int32_t *)g_variant_get_fixed_array(
390 gvar, &num_opts, sizeof(int32_t));
391 for (unsigned int i = 0; i < num_opts; i++)
393 switch (options[i]) {
394 case SR_CONF_LIMIT_SAMPLES:
395 _sample_count_supported = true;
397 case SR_CONF_LIMIT_FRAMES:
398 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
399 g_variant_new_uint64(1));
405 // Add notification of reconfigure events
406 disconnect(this, SLOT(on_config_changed()));
407 connect(dev_inst.get(), SIGNAL(config_changed()),
408 this, SLOT(on_config_changed()));
410 // Update sweep timing widgets.
411 update_sample_count_selector();
412 update_sample_rate_selector();
415 void SamplingBar::on_sample_count_changed()
417 commit_sample_count();
420 void SamplingBar::on_sample_rate_changed()
422 commit_sample_rate();
425 void SamplingBar::on_run_stop()
427 commit_sample_count();
428 commit_sample_rate();
432 void SamplingBar::on_config_changed()
434 commit_sample_count();
435 update_sample_count_selector();
436 commit_sample_rate();
437 update_sample_rate_selector();
440 } // namespace toolbars