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>
27 #include <libsigrok/libsigrok.h>
32 #include "samplingbar.h"
34 #include <pv/devicemanager.h>
35 #include <pv/devinst.h>
36 #include <pv/popups/deviceoptions.h>
37 #include <pv/popups/probes.h>
39 using boost::shared_ptr;
48 const uint64_t SamplingBar::MinSampleCount = 100ULL;
49 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
50 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
52 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
53 QToolBar("Sampling Bar", parent),
55 _device_selector(this),
56 _updating_device_selector(false),
57 _configure_button(this),
58 _configure_button_action(NULL),
60 _sample_count(" samples", this),
61 _sample_rate("Hz", this),
62 _updating_sample_rate(false),
63 _updating_sample_count(false),
64 _sample_count_supported(false),
65 _icon_red(":/icons/status-red.svg"),
66 _icon_green(":/icons/status-green.svg"),
67 _icon_grey(":/icons/status-grey.svg"),
68 _run_stop_button(this)
70 connect(&_run_stop_button, SIGNAL(clicked()),
71 this, SLOT(on_run_stop()));
72 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
73 this, SLOT(on_device_selected()));
74 connect(&_sample_count, SIGNAL(value_changed()),
75 this, SLOT(on_sample_count_changed()));
76 connect(&_sample_rate, SIGNAL(value_changed()),
77 this, SLOT(on_sample_rate_changed()));
79 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
81 set_capture_state(pv::SigSession::Stopped);
83 _configure_button.setIcon(QIcon::fromTheme("configure",
84 QIcon(":/icons/configure.png")));
86 _probes_button.setIcon(QIcon::fromTheme("probes",
87 QIcon(":/icons/probes.svg")));
89 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
91 addWidget(&_device_selector);
92 _configure_button_action = addWidget(&_configure_button);
93 addWidget(&_probes_button);
94 addWidget(&_sample_count);
95 addWidget(&_sample_rate);
97 addWidget(&_run_stop_button);
100 void SamplingBar::set_device_list(
101 const std::list< shared_ptr<pv::DevInst> > &devices)
103 _updating_device_selector = true;
105 _device_selector.clear();
106 _device_selector_map.clear();
108 BOOST_FOREACH (shared_ptr<pv::DevInst> dev_inst, devices) {
110 const string title = dev_inst->format_device_title();
111 const sr_dev_inst *sdi = dev_inst->dev_inst();
114 _device_selector_map[sdi] = dev_inst;
115 _device_selector.addItem(title.c_str(),
116 qVariantFromValue((void*)sdi));
119 _updating_device_selector = false;
121 on_device_selected();
124 shared_ptr<pv::DevInst> SamplingBar::get_selected_device() const
126 const int index = _device_selector.currentIndex();
128 return shared_ptr<pv::DevInst>();
130 const sr_dev_inst *const sdi =
131 (const sr_dev_inst*)_device_selector.itemData(
132 index).value<void*>();
135 map<const sr_dev_inst*, boost::weak_ptr<DevInst> >::
136 const_iterator iter = _device_selector_map.find(sdi);
137 if (iter == _device_selector_map.end())
138 return shared_ptr<pv::DevInst>();
140 return shared_ptr<pv::DevInst>((*iter).second);
143 void SamplingBar::set_selected_device(boost::shared_ptr<pv::DevInst> dev_inst)
147 for (int i = 0; i < _device_selector.count(); i++)
148 if (dev_inst->dev_inst() ==
149 _device_selector.itemData(i).value<void*>()) {
150 _device_selector.setCurrentIndex(i);
155 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
157 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
158 _run_stop_button.setIcon(*icons[state]);
159 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
160 tr("Run") : tr("Stop"));
163 void SamplingBar::update_sample_rate_selector()
165 GVariant *gvar_dict, *gvar_list;
166 const uint64_t *elements = NULL;
169 const shared_ptr<DevInst> dev_inst = get_selected_device();
173 const sr_dev_inst *const sdi = dev_inst->dev_inst();
176 _updating_sample_rate = true;
178 if (sr_config_list(sdi->driver, sdi, NULL,
179 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
181 _sample_rate.show_none();
182 _updating_sample_rate = false;
186 if ((gvar_list = g_variant_lookup_value(gvar_dict,
187 "samplerate-steps", G_VARIANT_TYPE("at"))))
189 elements = (const uint64_t *)g_variant_get_fixed_array(
190 gvar_list, &num_elements, sizeof(uint64_t));
192 const uint64_t min = elements[0];
193 const uint64_t max = elements[1];
194 const uint64_t step = elements[2];
196 g_variant_unref(gvar_list);
204 _sample_rate.show_125_list(min, max);
207 // When the step is not 1, we cam't make a 1-2-5-10
208 // list of sample rates, because we may not be able to
209 // make round numbers. Therefore in this case, show a
211 _sample_rate.show_min_max_step(min, max, step);
214 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
215 "samplerates", G_VARIANT_TYPE("at"))))
217 elements = (const uint64_t *)g_variant_get_fixed_array(
218 gvar_list, &num_elements, sizeof(uint64_t));
219 _sample_rate.show_list(elements, num_elements);
220 g_variant_unref(gvar_list);
222 _updating_sample_rate = false;
224 g_variant_unref(gvar_dict);
225 update_sample_rate_selector_value();
228 void SamplingBar::update_sample_rate_selector_value()
233 const shared_ptr<DevInst> dev_inst = get_selected_device();
237 const sr_dev_inst *const sdi = dev_inst->dev_inst();
240 if (sr_config_get(sdi->driver, sdi, NULL,
241 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
243 "WARNING: Failed to get value of sample rate";
246 samplerate = g_variant_get_uint64(gvar);
247 g_variant_unref(gvar);
249 _updating_sample_rate = true;
250 _sample_rate.set_value(samplerate);
251 _updating_sample_rate = false;
254 void SamplingBar::update_sample_count_selector()
258 const shared_ptr<DevInst> dev_inst = get_selected_device();
262 const sr_dev_inst *const sdi = dev_inst->dev_inst();
265 _updating_sample_count = true;
267 if (_sample_count_supported)
269 uint64_t sample_count = DefaultSampleCount;
270 uint64_t min_sample_count = 0;
271 uint64_t max_sample_count = MaxSampleCount;
273 if (sr_config_list(sdi->driver, sdi, NULL,
274 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
275 g_variant_get(gvar, "(tt)",
276 &min_sample_count, &max_sample_count);
277 g_variant_unref(gvar);
280 min_sample_count = min(max(min_sample_count, MinSampleCount),
283 _sample_count.show_125_list(
284 min_sample_count, max_sample_count);
286 if (sr_config_get(sdi->driver, sdi, NULL,
287 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK)
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 const shared_ptr<DevInst> dev_inst = get_selected_device();
314 const sr_dev_inst *const sdi = dev_inst->dev_inst();
317 sample_count = _sample_count.value();
319 // Set the sample count
320 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
321 g_variant_new_uint64(sample_count)) != SR_OK) {
322 qDebug() << "Failed to configure sample count.";
327 void SamplingBar::commit_sample_rate()
329 uint64_t sample_rate = 0;
331 const shared_ptr<DevInst> dev_inst = get_selected_device();
335 const sr_dev_inst *const sdi = dev_inst->dev_inst();
338 sample_rate = _sample_rate.value();
339 if (sample_rate == 0)
342 // Set the samplerate
343 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
344 g_variant_new_uint64(sample_rate)) != SR_OK) {
345 qDebug() << "Failed to configure samplerate.";
350 void SamplingBar::on_device_selected()
354 using namespace pv::popups;
356 if (_updating_device_selector)
359 const shared_ptr<DevInst> dev_inst = get_selected_device();
363 const sr_dev_inst *const sdi = dev_inst->dev_inst();
366 _session.set_device(dev_inst);
368 // Update the configure popup
369 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
370 _configure_button_action->setVisible(
371 !opts->binding().properties().empty());
372 _configure_button.set_popup(opts);
374 // Update the probes popup
375 Probes *const probes = new Probes(_session, this);
376 _probes_button.set_popup(probes);
378 // Update supported options.
379 _sample_count_supported = false;
381 if (sr_config_list(sdi->driver, sdi, NULL,
382 SR_CONF_DEVICE_OPTIONS, &gvar) == SR_OK)
385 const int *const options = (const int32_t *)g_variant_get_fixed_array(
386 gvar, &num_opts, sizeof(int32_t));
387 for (unsigned int i = 0; i < num_opts; i++)
389 switch (options[i]) {
390 case SR_CONF_LIMIT_SAMPLES:
391 _sample_count_supported = true;
393 case SR_CONF_LIMIT_FRAMES:
394 sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES,
395 g_variant_new_uint64(1));
401 // Update sweep timing widgets.
402 update_sample_count_selector();
403 update_sample_rate_selector();
406 void SamplingBar::on_sample_count_changed()
408 if(!_updating_sample_count)
409 commit_sample_count();
412 void SamplingBar::on_sample_rate_changed()
414 if (!_updating_sample_rate)
415 commit_sample_rate();
418 void SamplingBar::on_run_stop()
420 commit_sample_count();
421 commit_sample_rate();
425 } // namespace toolbars